-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
thatsddr
committed
Jun 1, 2022
1 parent
5aaf929
commit 648dadb
Showing
3 changed files
with
220 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package awsmt | ||
|
||
import ( | ||
"fmt" | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/mediatailor" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func setLiveSource(values *mediatailor.DescribeLiveSourceOutput, d *schema.ResourceData) error { | ||
var errors []error | ||
|
||
if values.Arn != nil { | ||
errors = append(errors, d.Set("arn", values.Arn)) | ||
} | ||
if values.CreationTime != nil { | ||
errors = append(errors, d.Set("creation_time", values.CreationTime.String())) | ||
} | ||
errors = append(errors, setHttpPackageConfigurations(values.HttpPackageConfigurations, d)) | ||
if values.LastModifiedTime != nil { | ||
errors = append(errors, d.Set("last_modified_time", values.LastModifiedTime.String())) | ||
} | ||
if values.LiveSourceName != nil { | ||
errors = append(errors, d.Set("live_source_name", values.LiveSourceName)) | ||
} | ||
if values.SourceLocationName != nil { | ||
errors = append(errors, d.Set("source_location_name", values.SourceLocationName)) | ||
} | ||
errors = append(errors, d.Set("tags", values.Tags)) | ||
for _, e := range errors { | ||
if e != nil { | ||
return fmt.Errorf("the following error occured while setting the values: %w", e) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func getCreateLiveSourceInput(d *schema.ResourceData) mediatailor.CreateLiveSourceInput { | ||
var inputParams mediatailor.CreateLiveSourceInput | ||
|
||
if c := getHttpPackageConfigurations(d); c != nil { | ||
inputParams.HttpPackageConfigurations = c | ||
} | ||
|
||
if s, ok := d.GetOk("source_location_name"); ok { | ||
inputParams.SourceLocationName = aws.String(s.(string)) | ||
} | ||
|
||
outputMap := make(map[string]*string) | ||
if v, ok := d.GetOk("tags"); ok { | ||
val := v.(map[string]interface{}) | ||
for k, value := range val { | ||
temp := value.(string) | ||
outputMap[k] = &temp | ||
} | ||
} | ||
inputParams.Tags = outputMap | ||
|
||
if s, ok := d.GetOk("live_source_name"); ok { | ||
inputParams.LiveSourceName = aws.String(s.(string)) | ||
} | ||
|
||
return inputParams | ||
} | ||
|
||
func getUpdateLiveSourceInput(d *schema.ResourceData) mediatailor.UpdateLiveSourceInput { | ||
var updateParams mediatailor.UpdateLiveSourceInput | ||
|
||
if c := getHttpPackageConfigurations(d); c != nil { | ||
updateParams.HttpPackageConfigurations = c | ||
} | ||
if s, ok := d.GetOk("source_location_name"); ok { | ||
updateParams.SourceLocationName = aws.String(s.(string)) | ||
} | ||
if s, ok := d.GetOk("vod_source_name"); ok { | ||
updateParams.LiveSourceName = aws.String(s.(string)) | ||
} | ||
return updateParams | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package awsmt | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/arn" | ||
"github.com/aws/aws-sdk-go/service/mediatailor" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
"strings" | ||
) | ||
|
||
func resourceLiveSource() *schema.Resource { | ||
return &schema.Resource{ | ||
CreateContext: resourceLiveSourceCreate, | ||
ReadContext: resourceLiveSourceRead, | ||
UpdateContext: resourceLiveSourceUpdate, | ||
DeleteContext: resourceLiveSourceDelete, | ||
Importer: &schema.ResourceImporter{ | ||
StateContext: schema.ImportStatePassthroughContext, | ||
}, | ||
Schema: map[string]*schema.Schema{ | ||
"arn": &computedString, | ||
"creation_time": &computedString, | ||
"http_package_configurations": { | ||
Type: schema.TypeList, | ||
Required: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"path": &requiredString, | ||
"source_group": &requiredString, | ||
"type": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringInSlice([]string{"DASH", "HLS"}, false), | ||
}, | ||
}, | ||
}, | ||
}, | ||
"last_modified_time": &computedString, | ||
"live_source_name": &requiredString, | ||
"source_location_name": &requiredString, | ||
"tags": { | ||
Type: schema.TypeMap, | ||
Optional: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
}, | ||
CustomizeDiff: customdiff.Sequence( | ||
customdiff.ForceNewIfChange("live_source_name", func(ctx context.Context, old, new, meta interface{}) bool { return old.(string) != new.(string) }), | ||
customdiff.ForceNewIfChange("source_location_name", func(ctx context.Context, old, new, meta interface{}) bool { return old.(string) != new.(string) }), | ||
), | ||
} | ||
} | ||
|
||
func resourceLiveSourceCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
client := meta.(*mediatailor.MediaTailor) | ||
|
||
params := getCreateLiveSourceInput(d) | ||
liveSource, err := client.CreateLiveSource(¶ms) | ||
if err != nil { | ||
return diag.FromErr(fmt.Errorf("error while creating the live source: %v", err)) | ||
} | ||
d.SetId(aws.StringValue(liveSource.Arn)) | ||
|
||
return resourceLiveSourceRead(ctx, d, meta) | ||
} | ||
|
||
func resourceLiveSourceRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
client := meta.(*mediatailor.MediaTailor) | ||
resourceName := d.Get("live_source_name").(string) | ||
sourceLocationName := d.Get("source_location_name").(string) | ||
|
||
if len(resourceName) == 0 && len(d.Id()) > 0 { | ||
resourceArn, err := arn.Parse(d.Id()) | ||
if err != nil { | ||
return diag.FromErr(fmt.Errorf("error parsing the name from resource arn: %v", err)) | ||
} | ||
arnSections := strings.Split(resourceArn.Resource, "/") | ||
resourceName = arnSections[len(arnSections)-1] | ||
sourceLocationName = arnSections[len(arnSections)-2] | ||
} | ||
|
||
input := &mediatailor.DescribeLiveSourceInput{SourceLocationName: &(sourceLocationName), LiveSourceName: aws.String(resourceName)} | ||
|
||
res, err := client.DescribeLiveSource(input) | ||
if err != nil { | ||
return diag.FromErr(fmt.Errorf("error while reading the live source: %v", err)) | ||
} | ||
|
||
if err = setLiveSource(res, d); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceLiveSourceUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
client := meta.(*mediatailor.MediaTailor) | ||
|
||
if d.HasChange("tags") { | ||
oldValue, newValue := d.GetChange("tags") | ||
|
||
resourceName := d.Get("live_source_name").(string) | ||
sourceLocationName := d.Get("source_location_name").(string) | ||
res, err := client.DescribeLiveSource(&mediatailor.DescribeLiveSourceInput{SourceLocationName: &sourceLocationName, LiveSourceName: &resourceName}) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
if err := updateTags(client, res.Arn, oldValue, newValue); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
} | ||
|
||
var params = getUpdateLiveSourceInput(d) | ||
liveSource, err := client.UpdateLiveSource(¶ms) | ||
if err != nil { | ||
return diag.FromErr(fmt.Errorf("error while updating the live source: %v", err)) | ||
} | ||
d.SetId(aws.StringValue(liveSource.Arn)) | ||
|
||
return resourceLiveSourceRead(ctx, d, meta) | ||
} | ||
|
||
func resourceLiveSourceDelete(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
client := meta.(*mediatailor.MediaTailor) | ||
|
||
_, err := client.DeleteLiveSource(&mediatailor.DeleteLiveSourceInput{LiveSourceName: aws.String(d.Get("live_source_name").(string)), SourceLocationName: aws.String(d.Get("source_location_name").(string))}) | ||
if err != nil { | ||
return diag.FromErr(fmt.Errorf("error while deleting the resource: %v", err)) | ||
} | ||
|
||
return nil | ||
} |