From 1cddf62706c98e1e961d6b303979dff4f37419e0 Mon Sep 17 00:00:00 2001 From: Domenico Di Ruocco Date: Tue, 20 Aug 2024 11:06:46 +0200 Subject: [PATCH 1/9] refactor: use aws sdk v2 for channel OTT-6334 --- awsmt/data_source_channel.go | 15 +- awsmt/helpers_channels.go | 330 ++++++++++++++++++++--------------- awsmt/models_channel.go | 2 +- awsmt/resource_channel.go | 52 +++--- 4 files changed, 223 insertions(+), 176 deletions(-) diff --git a/awsmt/data_source_channel.go b/awsmt/data_source_channel.go index c645668..6e783b1 100644 --- a/awsmt/data_source_channel.go +++ b/awsmt/data_source_channel.go @@ -2,7 +2,7 @@ package awsmt import ( "context" - "github.com/aws/aws-sdk-go/service/mediatailor" + "github.com/aws/aws-sdk-go-v2/service/mediatailor" "github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/datasource/schema" @@ -19,7 +19,7 @@ func DataSourceChannel() datasource.DataSource { } type dataSourceChannel struct { - client *mediatailor.MediaTailor + client *mediatailor.Client } func (d *dataSourceChannel) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { @@ -86,7 +86,7 @@ func (d *dataSourceChannel) Configure(_ context.Context, req datasource.Configur return } - d.client = req.ProviderData.(clients).v1 + d.client = req.ProviderData.(clients).v2 } func (d *dataSourceChannel) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { @@ -99,13 +99,13 @@ func (d *dataSourceChannel) Read(ctx context.Context, req datasource.ReadRequest channelName := data.Name - channel, err := d.client.DescribeChannel(&mediatailor.DescribeChannelInput{ChannelName: channelName}) + channel, err := d.client.DescribeChannel(ctx, &mediatailor.DescribeChannelInput{ChannelName: channelName}) if err != nil { resp.Diagnostics.AddError("Error while describing channel "+*channelName, err.Error()) return } - policy, err := d.client.GetChannelPolicy(&mediatailor.GetChannelPolicyInput{ChannelName: channelName}) + policy, err := d.client.GetChannelPolicy(ctx, &mediatailor.GetChannelPolicyInput{ChannelName: channelName}) if err != nil && !strings.Contains(err.Error(), "NotFound") { resp.Diagnostics.AddError( "Error while getting the channel policy "+err.Error(), @@ -118,8 +118,9 @@ func (d *dataSourceChannel) Read(ctx context.Context, req datasource.ReadRequest data.Policy = jsontypes.NewNormalizedPointerValue(policy.Policy) } - if channel.ChannelState != nil { - data.ChannelState = channel.ChannelState + if channel.ChannelState != "" { + channelState := string(channel.ChannelState) + data.ChannelState = &channelState } data = writeChannelToState(data, *channel) diff --git a/awsmt/helpers_channels.go b/awsmt/helpers_channels.go index 38a148a..0464d87 100644 --- a/awsmt/helpers_channels.go +++ b/awsmt/helpers_channels.go @@ -1,220 +1,252 @@ package awsmt import ( + "context" + "github.com/aws/aws-sdk-go-v2/service/mediatailor" + awsTypes "github.com/aws/aws-sdk-go-v2/service/mediatailor/types" "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/mediatailor" "github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes" "github.com/hashicorp/terraform-plugin-framework/types" "reflect" "time" ) -// Functions used to edit the channel once created +// functions to create MediaTailor inputs -func createChannelPolicy(channelName *string, policy *string, client *mediatailor.MediaTailor) error { - putChannelPolicyParams := mediatailor.PutChannelPolicyInput{ - ChannelName: channelName, - Policy: policy, - } - _, err := client.PutChannelPolicy(&putChannelPolicyParams) - if err != nil { - return err - } - return err -} +func getCreateChannelInput(model channelModel) *mediatailor.CreateChannelInput { + var input mediatailor.CreateChannelInput -func stopChannel(state *string, channelName *string, client *mediatailor.MediaTailor) error { - if *state == "RUNNING" { - _, err := client.StopChannel(&mediatailor.StopChannelInput{ChannelName: channelName}) - if err != nil { - return err + input.ChannelName, input.FillerSlate, input.Outputs = getSharedChannelInput(&model) + + if model.PlaybackMode != nil { + var mode awsTypes.PlaybackMode + switch *model.PlaybackMode { + case "LINEAR": + mode = awsTypes.PlaybackModeLinear + default: + mode = awsTypes.PlaybackModeLoop } + input.PlaybackMode = mode } - return nil -} -func updatePolicy(plan *channelModel, channelName *string, oldPolicy jsontypes.Normalized, newPolicy jsontypes.Normalized, client *mediatailor.MediaTailor) (channelModel, error) { - if !reflect.DeepEqual(oldPolicy, newPolicy) { - if !newPolicy.IsNull() { - plan.Policy = newPolicy - policy := newPolicy.ValueString() - _, err := client.PutChannelPolicy(&mediatailor.PutChannelPolicyInput{ChannelName: channelName, Policy: &policy}) - if err != nil { - return *plan, err - } - } else if newPolicy.IsNull() { - plan.Policy = jsontypes.NewNormalizedNull() - _, err := client.DeleteChannelPolicy(&mediatailor.DeleteChannelPolicyInput{ChannelName: channelName}) - if err != nil { - return *plan, err - } + if model.Tags != nil && len(model.Tags) > 0 { + input.Tags = model.Tags + } + + if model.Tier != nil { + var tier awsTypes.Tier + switch *model.Tier { + case "BASIC": + tier = awsTypes.TierBasic + default: + tier = awsTypes.TierStandard } - } else { - plan.Policy = oldPolicy + input.Tier = tier } - return *plan, nil + + return &input } -// Functions used to create the resource in MediaTailor +func getUpdateChannelInput(model channelModel) *mediatailor.UpdateChannelInput { + var input mediatailor.UpdateChannelInput -func newChannelInputBuilder(channelName *string, outputs []outputsModel, fillerSlate *fillerSlateModel) (*string, []*mediatailor.RequestOutputItem, *mediatailor.SlateSource) { - theChannelName := channelName - output := buildRequestOutput(outputs) - fillerSlates := buildSlateSource(fillerSlate) - return theChannelName, output, fillerSlates -} + input.ChannelName, input.FillerSlate, input.Outputs = getSharedChannelInput(&model) -func buildChannelInput(plan channelModel) mediatailor.CreateChannelInput { - var input mediatailor.CreateChannelInput + return &input +} - input.ChannelName, input.Outputs, input.FillerSlate = newChannelInputBuilder(plan.Name, plan.Outputs, plan.FillerSlate) +func getSharedChannelInput(model *channelModel) (name *string, source *awsTypes.SlateSource, outputItem []awsTypes.RequestOutputItem) { + return model.Name, buildSlateSource(model), buildRequestOutputs(model) +} - if plan.PlaybackMode != nil { - input.PlaybackMode = plan.PlaybackMode +func buildSlateSource(model *channelModel) *awsTypes.SlateSource { + if model.FillerSlate == nil { + return nil } - if plan.Tags != nil { - input.Tags = plan.Tags + temp := &awsTypes.SlateSource{} + if model.FillerSlate.SourceLocationName != nil { + temp.SourceLocationName = model.FillerSlate.SourceLocationName } - - if plan.Tier != nil { - input.Tier = plan.Tier + if model.FillerSlate.VodSourceName != nil { + temp.VodSourceName = model.FillerSlate.VodSourceName } - - return input -} - -func buildUpdateChannelInput(plan channelModel) mediatailor.UpdateChannelInput { - var input mediatailor.UpdateChannelInput - input.ChannelName, input.Outputs, input.FillerSlate = newChannelInputBuilder(plan.Name, plan.Outputs, plan.FillerSlate) - return input + return temp } -func buildRequestOutput(outputsFromPlan []outputsModel) []*mediatailor.RequestOutputItem { - var res []*mediatailor.RequestOutputItem +func buildRequestOutputs(model *channelModel) []awsTypes.RequestOutputItem { + var temp []awsTypes.RequestOutputItem - for _, output := range outputsFromPlan { - outputs := &mediatailor.RequestOutputItem{} + for _, o := range model.Outputs { + output := awsTypes.RequestOutputItem{} - if output.DashPlaylistSettings != nil { - outputs.DashPlaylistSettings = buildDashPlaylistSettings(output.DashPlaylistSettings) + if o.DashPlaylistSettings != nil { + output.DashPlaylistSettings = buildDashPlaylistSettings(o.DashPlaylistSettings) } - if output.HlsPlaylistSettings != nil { - outputs.HlsPlaylistSettings = buildHLSPlaylistSettings(output.HlsPlaylistSettings) + if o.HlsPlaylistSettings != nil { + output.HlsPlaylistSettings = buildHLSPlaylistSettings(o.HlsPlaylistSettings) } - if output.ManifestName != nil { - outputs.ManifestName = output.ManifestName + if o.ManifestName != nil { + output.ManifestName = o.ManifestName } - if output.SourceGroup != nil { - outputs.SourceGroup = output.SourceGroup + if o.SourceGroup != nil { + output.SourceGroup = o.SourceGroup } - res = append(res, outputs) + temp = append(temp, output) } - return res + return temp } -func buildDashPlaylistSettings(settings *dashPlaylistSettingsModel) *mediatailor.DashPlaylistSettings { - dashSettings := &mediatailor.DashPlaylistSettings{} +func buildDashPlaylistSettings(settings *dashPlaylistSettingsModel) *awsTypes.DashPlaylistSettings { + dashSettings := &awsTypes.DashPlaylistSettings{} if settings.ManifestWindowSeconds != nil { - dashSettings.ManifestWindowSeconds = settings.ManifestWindowSeconds + manifestWindowSeconds := int32(*settings.ManifestWindowSeconds) + dashSettings.ManifestWindowSeconds = &manifestWindowSeconds } if settings.MinBufferTimeSeconds != nil { - dashSettings.MinBufferTimeSeconds = settings.MinBufferTimeSeconds + minBufferTimeSeconds := int32(*settings.MinBufferTimeSeconds) + dashSettings.MinBufferTimeSeconds = &minBufferTimeSeconds } if settings.MinUpdatePeriodSeconds != nil { - dashSettings.MinUpdatePeriodSeconds = settings.MinUpdatePeriodSeconds + minUpdatePeriodSeconds := int32(*settings.MinUpdatePeriodSeconds) + dashSettings.MinUpdatePeriodSeconds = &minUpdatePeriodSeconds } if settings.SuggestedPresentationDelaySeconds != nil { - dashSettings.SuggestedPresentationDelaySeconds = settings.SuggestedPresentationDelaySeconds + suggestedPresentationDelaySeconds := int32(*settings.SuggestedPresentationDelaySeconds) + dashSettings.SuggestedPresentationDelaySeconds = &suggestedPresentationDelaySeconds } return dashSettings } -func buildHLSPlaylistSettings(settings *hlsPlaylistSettingsModel) *mediatailor.HlsPlaylistSettings { - hlsSettings := &mediatailor.HlsPlaylistSettings{} +func buildHLSPlaylistSettings(settings *hlsPlaylistSettingsModel) *awsTypes.HlsPlaylistSettings { + hlsSettings := &awsTypes.HlsPlaylistSettings{} + if settings.AdMarkupType != nil && len(settings.AdMarkupType) > 0 { - hlsSettings.AdMarkupType = append(hlsSettings.AdMarkupType, settings.AdMarkupType...) + var adMarkupType []awsTypes.AdMarkupType + for _, a := range settings.AdMarkupType { + switch *a { + case "SCTE35_ENHANCED": + adMarkupType = append(adMarkupType, awsTypes.AdMarkupTypeScte35Enhanced) + default: + adMarkupType = append(adMarkupType, awsTypes.AdMarkupTypeDaterange) + } + } + hlsSettings.AdMarkupType = append(hlsSettings.AdMarkupType, adMarkupType...) } else if settings.AdMarkupType == nil { - temp := "DATERANGE" - hlsSettings.AdMarkupType = append(hlsSettings.AdMarkupType, &temp) + hlsSettings.AdMarkupType = append(hlsSettings.AdMarkupType, awsTypes.AdMarkupTypeDaterange) } + if settings.ManifestWindowSeconds != nil { - hlsSettings.ManifestWindowSeconds = settings.ManifestWindowSeconds + manifestWindowSeconds := int32(*settings.ManifestWindowSeconds) + hlsSettings.ManifestWindowSeconds = &manifestWindowSeconds } + return hlsSettings } -func buildSlateSource(fillerSlate *fillerSlateModel) *mediatailor.SlateSource { - var slateSource *mediatailor.SlateSource - if fillerSlate != nil { - slateSource = &mediatailor.SlateSource{} - if fillerSlate.SourceLocationName != nil { - slateSource.SourceLocationName = fillerSlate.SourceLocationName +// functions to manipulate a channel once it is created + +func createChannelPolicy(channelName *string, policy *string, client *mediatailor.Client) error { + putChannelPolicyParams := mediatailor.PutChannelPolicyInput{ + ChannelName: channelName, + Policy: policy, + } + _, err := client.PutChannelPolicy(context.TODO(), &putChannelPolicyParams) + if err != nil { + return err + } + return err +} + +func stopChannel(state awsTypes.ChannelState, channelName *string, client *mediatailor.Client) error { + if state == awsTypes.ChannelStateRunning { + _, err := client.StopChannel(context.TODO(), &mediatailor.StopChannelInput{ChannelName: channelName}) + if err != nil { + return err } - if fillerSlate.VodSourceName != nil { - slateSource.VodSourceName = fillerSlate.VodSourceName + } + return nil +} + +func updatePolicy(model *channelModel, channelName *string, oldPolicy jsontypes.Normalized, newPolicy jsontypes.Normalized, client *mediatailor.Client) (channelModel, error) { + if !reflect.DeepEqual(oldPolicy, newPolicy) { + if !newPolicy.IsNull() { + model.Policy = newPolicy + policy := newPolicy.ValueString() + _, err := client.PutChannelPolicy(context.TODO(), &mediatailor.PutChannelPolicyInput{ChannelName: channelName, Policy: &policy}) + if err != nil { + return *model, err + } + } else if newPolicy.IsNull() { + model.Policy = jsontypes.NewNormalizedNull() + _, err := client.DeleteChannelPolicy(context.TODO(), &mediatailor.DeleteChannelPolicyInput{ChannelName: channelName}) + if err != nil { + return *model, err + } } + } else { + model.Policy = oldPolicy } - return slateSource + return *model, nil } // Functions used to read MediaTailor resources to plan and state -func readChannelComputedValues(plan channelModel, arn *string, channelName *string, creationTime *time.Time, lastModifiedTime *time.Time) channelModel { - plan.ID = types.StringValue(*channelName) +func readChannelComputedValues(model channelModel, arn *string, channelName *string, creationTime *time.Time, lastModifiedTime *time.Time) channelModel { + model.ID = types.StringValue(*channelName) if arn != nil { - plan.Arn = types.StringValue(*arn) + model.Arn = types.StringValue(*arn) } - plan.Name = channelName + model.Name = channelName if creationTime != nil { - plan.CreationTime = types.StringValue((aws.TimeValue(creationTime)).String()) + model.CreationTime = types.StringValue((aws.TimeValue(creationTime)).String()) } if lastModifiedTime != nil { - plan.LastModifiedTime = types.StringValue((aws.TimeValue(lastModifiedTime)).String()) + model.LastModifiedTime = types.StringValue((aws.TimeValue(lastModifiedTime)).String()) } - return plan + return model } -func readFillerSlate(plan channelModel, channel *mediatailor.SlateSource) channelModel { - if channel != nil { +func readFillerSlate(plan channelModel, fillerSlate *awsTypes.SlateSource) channelModel { + if fillerSlate != nil { plan.FillerSlate = &fillerSlateModel{} - if channel.SourceLocationName != nil { - plan.FillerSlate.SourceLocationName = channel.SourceLocationName + if fillerSlate.SourceLocationName != nil { + plan.FillerSlate.SourceLocationName = fillerSlate.SourceLocationName } - if channel.VodSourceName != nil { - plan.FillerSlate.VodSourceName = channel.VodSourceName + if fillerSlate.VodSourceName != nil { + plan.FillerSlate.VodSourceName = fillerSlate.VodSourceName } } return plan } -func readOutputs(plan channelModel, channel []*mediatailor.ResponseOutputItem) channelModel { +func readOutputs(plan channelModel, responseOutputItems []awsTypes.ResponseOutputItem) channelModel { - if channel == nil { + if responseOutputItems == nil { return plan } var tempOutputs []outputsModel - for i, output := range channel { + for i, output := range responseOutputItems { outputs := outputsModel{} if output.DashPlaylistSettings != nil { - outputs.DashPlaylistSettings = readDashPlaylistConfigurationsToPlan(output) + outputs.DashPlaylistSettings = readDashPlaylistConfigurationsToPlan(&output) } if output.HlsPlaylistSettings != nil { if len(plan.Outputs) > 0 && i <= len(plan.Outputs) { - outputs.HlsPlaylistSettings = readHlsPlaylistConfigurationsToPlan(output, plan.Outputs[i]) + outputs.HlsPlaylistSettings = readHlsPlaylistConfigurationsToPlan(&output, plan.Outputs[i]) } else { - outputs.HlsPlaylistSettings = readHlsPlaylistConfigurationsToPlanDS(output) + outputs.HlsPlaylistSettings = readHlsPlaylistConfigurationsToPlanDS(&output) } } @@ -240,46 +272,62 @@ func readRMPS(manifestName *string, playbackUrl *string, sourceGroup *string) (* return outputs.ManifestName, outputs.PlaybackUrl, outputs.SourceGroup } -func readDashPlaylistConfigurationsToPlan(output *mediatailor.ResponseOutputItem) *dashPlaylistSettingsModel { +func readDashPlaylistConfigurationsToPlan(output *awsTypes.ResponseOutputItem) *dashPlaylistSettingsModel { outputs := &dashPlaylistSettingsModel{} if output.DashPlaylistSettings.ManifestWindowSeconds != nil { - outputs.ManifestWindowSeconds = output.DashPlaylistSettings.ManifestWindowSeconds + manifestWindowSeconds := int64(*output.DashPlaylistSettings.ManifestWindowSeconds) + outputs.ManifestWindowSeconds = &manifestWindowSeconds } if output.DashPlaylistSettings.MinBufferTimeSeconds != nil { - outputs.MinBufferTimeSeconds = output.DashPlaylistSettings.MinBufferTimeSeconds + minBufferTimeSeconds := int64(*output.DashPlaylistSettings.MinBufferTimeSeconds) + outputs.MinBufferTimeSeconds = &minBufferTimeSeconds } if output.DashPlaylistSettings.MinUpdatePeriodSeconds != nil { - outputs.MinUpdatePeriodSeconds = output.DashPlaylistSettings.MinUpdatePeriodSeconds + minUpdatePeriodSeconds := int64(*output.DashPlaylistSettings.MinUpdatePeriodSeconds) + outputs.MinUpdatePeriodSeconds = &minUpdatePeriodSeconds } if output.DashPlaylistSettings.SuggestedPresentationDelaySeconds != nil { - outputs.SuggestedPresentationDelaySeconds = output.DashPlaylistSettings.SuggestedPresentationDelaySeconds + suggestedPresentationDelaySeconds := int64(*output.DashPlaylistSettings.SuggestedPresentationDelaySeconds) + outputs.SuggestedPresentationDelaySeconds = &suggestedPresentationDelaySeconds } return outputs } -func readHlsPlaylistConfigurationsToPlan(output *mediatailor.ResponseOutputItem, stateOutput outputsModel) *hlsPlaylistSettingsModel { +func readHlsPlaylistConfigurationsToPlan(output *awsTypes.ResponseOutputItem, stateOutput outputsModel) *hlsPlaylistSettingsModel { outputs := &hlsPlaylistSettingsModel{} if stateOutput.HlsPlaylistSettings.AdMarkupType != nil && output.HlsPlaylistSettings.AdMarkupType != nil && len(output.HlsPlaylistSettings.AdMarkupType) > 0 { - outputs.AdMarkupType = append(outputs.AdMarkupType, output.HlsPlaylistSettings.AdMarkupType...) + var adMarkupTypes []*string + for _, a := range output.HlsPlaylistSettings.AdMarkupType { + adMarkupType := string(a) + adMarkupTypes = append(adMarkupTypes, &adMarkupType) + } + outputs.AdMarkupType = append(outputs.AdMarkupType, adMarkupTypes...) } if stateOutput.HlsPlaylistSettings.ManifestWindowSeconds != nil && output.HlsPlaylistSettings.ManifestWindowSeconds != nil { - outputs.ManifestWindowSeconds = output.HlsPlaylistSettings.ManifestWindowSeconds + manifestWindowSeconds := int64(*output.HlsPlaylistSettings.ManifestWindowSeconds) + outputs.ManifestWindowSeconds = &manifestWindowSeconds } return outputs } -func readHlsPlaylistConfigurationsToPlanDS(output *mediatailor.ResponseOutputItem) *hlsPlaylistSettingsModel { +func readHlsPlaylistConfigurationsToPlanDS(output *awsTypes.ResponseOutputItem) *hlsPlaylistSettingsModel { outputs := &hlsPlaylistSettingsModel{} if output.HlsPlaylistSettings.AdMarkupType != nil && len(output.HlsPlaylistSettings.AdMarkupType) > 0 { - outputs.AdMarkupType = append(outputs.AdMarkupType, output.HlsPlaylistSettings.AdMarkupType...) + var adMarkupTypes []*string + for _, a := range output.HlsPlaylistSettings.AdMarkupType { + adMarkupType := string(a) + adMarkupTypes = append(adMarkupTypes, &adMarkupType) + } + outputs.AdMarkupType = append(outputs.AdMarkupType, adMarkupTypes...) } if output.HlsPlaylistSettings.ManifestWindowSeconds != nil { - outputs.ManifestWindowSeconds = output.HlsPlaylistSettings.ManifestWindowSeconds + manifestWindowSeconds := int64(*output.HlsPlaylistSettings.ManifestWindowSeconds) + outputs.ManifestWindowSeconds = &manifestWindowSeconds } return outputs } -func readOptionalValues(plan channelModel, playbackMode *string, tags map[string]*string, tier *string) channelModel { +func readOptionalValues(plan channelModel, playbackMode *string, tags map[string]string, tier *string) channelModel { if playbackMode != nil { plan.PlaybackMode = playbackMode } @@ -294,28 +342,28 @@ func readOptionalValues(plan channelModel, playbackMode *string, tags map[string return plan } -func writeChannelToPlan(plan channelModel, channel mediatailor.CreateChannelOutput) channelModel { +func writeChannelToPlan(model channelModel, channel mediatailor.CreateChannelOutput) channelModel { - plan = readChannelComputedValues(plan, channel.Arn, channel.ChannelName, channel.CreationTime, channel.LastModifiedTime) + model = readChannelComputedValues(model, channel.Arn, channel.ChannelName, channel.CreationTime, channel.LastModifiedTime) - plan = readFillerSlate(plan, channel.FillerSlate) + model = readFillerSlate(model, channel.FillerSlate) - plan = readOutputs(plan, channel.Outputs) + model = readOutputs(model, channel.Outputs) - plan = readOptionalValues(plan, channel.PlaybackMode, channel.Tags, channel.Tier) + model = readOptionalValues(model, channel.PlaybackMode, channel.Tags, channel.Tier) - return plan + return model } -func writeChannelToState(state channelModel, channel mediatailor.DescribeChannelOutput) channelModel { +func writeChannelToState(model channelModel, channel mediatailor.DescribeChannelOutput) channelModel { - state = readChannelComputedValues(state, channel.Arn, channel.ChannelName, channel.CreationTime, channel.LastModifiedTime) + model = readChannelComputedValues(model, channel.Arn, channel.ChannelName, channel.CreationTime, channel.LastModifiedTime) - state = readFillerSlate(state, channel.FillerSlate) + model = readFillerSlate(model, channel.FillerSlate) - state = readOutputs(state, channel.Outputs) + model = readOutputs(model, channel.Outputs) - state = readOptionalValues(state, channel.PlaybackMode, channel.Tags, channel.Tier) + model = readOptionalValues(model, channel.PlaybackMode, channel.Tags, channel.Tier) - return state + return model } diff --git a/awsmt/models_channel.go b/awsmt/models_channel.go index d0f70fb..1d91830 100644 --- a/awsmt/models_channel.go +++ b/awsmt/models_channel.go @@ -16,7 +16,7 @@ type channelModel struct { Outputs []outputsModel `tfsdk:"outputs"` PlaybackMode *string `tfsdk:"playback_mode"` Policy jsontypes.Normalized `tfsdk:"policy"` - Tags map[string]*string `tfsdk:"tags"` + Tags map[string]string `tfsdk:"tags"` Tier *string `tfsdk:"tier"` } diff --git a/awsmt/resource_channel.go b/awsmt/resource_channel.go index 943dad7..dfc1af6 100644 --- a/awsmt/resource_channel.go +++ b/awsmt/resource_channel.go @@ -2,7 +2,8 @@ package awsmt import ( "context" - "github.com/aws/aws-sdk-go/service/mediatailor" + "github.com/aws/aws-sdk-go-v2/service/mediatailor" + awsTypes "github.com/aws/aws-sdk-go-v2/service/mediatailor/types" "github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes" "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" "github.com/hashicorp/terraform-plugin-framework/path" @@ -23,7 +24,7 @@ func ResourceChannel() resource.Resource { } type resourceChannel struct { - client *mediatailor.MediaTailor + client *mediatailor.Client } func (r *resourceChannel) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { @@ -118,28 +119,27 @@ func (r *resourceChannel) Configure(_ context.Context, req resource.ConfigureReq return } - r.client = req.ProviderData.(clients).v1 + r.client = req.ProviderData.(clients).v2 } func (r *resourceChannel) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { var plan channelModel - diags := req.Plan.Get(ctx, &plan) - resp.Diagnostics.Append(diags...) + resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...) if resp.Diagnostics.HasError() { return } - input := buildChannelInput(plan) + input := getCreateChannelInput(plan) - channel, err := r.client.CreateChannel(&input) + channel, err := r.client.CreateChannel(ctx, input) if err != nil { resp.Diagnostics.AddError("Error while creating channel "+*input.ChannelName, err.Error()) return } if plan.ChannelState != nil && *plan.ChannelState == "RUNNING" { - _, err := r.client.StartChannel(&mediatailor.StartChannelInput{ChannelName: plan.Name}) + _, err := r.client.StartChannel(ctx, &mediatailor.StartChannelInput{ChannelName: plan.Name}) if err != nil { resp.Diagnostics.AddError("Error while starting the channel "+*channel.ChannelName, err.Error()) return @@ -156,8 +156,7 @@ func (r *resourceChannel) Create(ctx context.Context, req resource.CreateRequest newPlan := writeChannelToPlan(plan, *channel) - diags = resp.State.Set(ctx, newPlan) - resp.Diagnostics.Append(diags...) + resp.Diagnostics.Append(resp.State.Set(ctx, newPlan)...) if resp.Diagnostics.HasError() { return } @@ -173,7 +172,7 @@ func (r *resourceChannel) Read(ctx context.Context, req resource.ReadRequest, re return } - channel, err := r.client.DescribeChannel(&mediatailor.DescribeChannelInput{ChannelName: state.Name}) + channel, err := r.client.DescribeChannel(ctx, &mediatailor.DescribeChannelInput{ChannelName: state.Name}) if err != nil { resp.Diagnostics.AddError( "Error while describing channel "+err.Error(), @@ -181,7 +180,7 @@ func (r *resourceChannel) Read(ctx context.Context, req resource.ReadRequest, re ) } - policy, err := r.client.GetChannelPolicy(&mediatailor.GetChannelPolicyInput{ChannelName: state.Name}) + policy, err := r.client.GetChannelPolicy(ctx, &mediatailor.GetChannelPolicyInput{ChannelName: state.Name}) if err != nil && !strings.Contains(err.Error(), "NotFound") { resp.Diagnostics.AddError( "Error while getting channel policy "+err.Error(), @@ -199,7 +198,8 @@ func (r *resourceChannel) Read(ctx context.Context, req resource.ReadRequest, re state = writeChannelToState(state, *channel) if state.ChannelState != nil { - state.ChannelState = channel.ChannelState + channelState := string(channel.ChannelState) + state.ChannelState = &channelState } diags = resp.State.Set(ctx, &state) @@ -212,15 +212,14 @@ func (r *resourceChannel) Read(ctx context.Context, req resource.ReadRequest, re func (r *resourceChannel) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { var plan channelModel - diags := req.Plan.Get(ctx, &plan) - resp.Diagnostics.Append(diags...) + resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...) if resp.Diagnostics.HasError() { return } channelName := plan.Name - channel, err := r.client.DescribeChannel(&mediatailor.DescribeChannelInput{ChannelName: channelName}) + channel, err := r.client.DescribeChannel(ctx, &mediatailor.DescribeChannelInput{ChannelName: channelName}) if err != nil { resp.Diagnostics.AddError( "Error while describing channel "+err.Error(), @@ -228,7 +227,7 @@ func (r *resourceChannel) Update(ctx context.Context, req resource.UpdateRequest ) } - err = updatesTags(r.client, channel.Tags, plan.Tags, *channel.Arn) + err = V2UpdatesTags(r.client, channel.Tags, plan.Tags, *channel.Arn) if err != nil { resp.Diagnostics.AddError( "Error while updating channel tags"+err.Error(), @@ -247,7 +246,7 @@ func (r *resourceChannel) Update(ctx context.Context, req resource.UpdateRequest ) } - oldPolicy, err := r.client.GetChannelPolicy(&mediatailor.GetChannelPolicyInput{ChannelName: channelName}) + oldPolicy, err := r.client.GetChannelPolicy(ctx, &mediatailor.GetChannelPolicyInput{ChannelName: channelName}) if err != nil && !strings.Contains(err.Error(), "NotFound") { resp.Diagnostics.AddError( "Error while getting channel policy "+err.Error(), @@ -267,8 +266,8 @@ func (r *resourceChannel) Update(ctx context.Context, req resource.UpdateRequest ) } - var params = buildUpdateChannelInput(plan) - updatedChannel, err := r.client.UpdateChannel(¶ms) + var params = getUpdateChannelInput(plan) + updatedChannel, err := r.client.UpdateChannel(ctx, params) if err != nil { resp.Diagnostics.AddError( "Error while updating channel "+*channel.ChannelName+err.Error(), @@ -276,10 +275,10 @@ func (r *resourceChannel) Update(ctx context.Context, req resource.UpdateRequest ) } - wasRunning := previousState != nil && *previousState == "RUNNING" + wasRunning := previousState == awsTypes.ChannelStateRunning shouldRun := newState != nil && *newState == "RUNNING" if (newState == nil && wasRunning) || shouldRun { - _, err := r.client.StartChannel(&mediatailor.StartChannelInput{ChannelName: channelName}) + _, err := r.client.StartChannel(ctx, &mediatailor.StartChannelInput{ChannelName: channelName}) if err != nil { resp.Diagnostics.AddError("Error while starting the channel "+*channelName, err.Error()) return @@ -298,8 +297,7 @@ func (r *resourceChannel) Update(ctx context.Context, req resource.UpdateRequest plan.PlaybackMode = channel.PlaybackMode - diags = resp.State.Set(ctx, plan) - resp.Diagnostics.Append(diags...) + resp.Diagnostics.Append(resp.State.Set(ctx, plan)...) if resp.Diagnostics.HasError() { return } @@ -313,7 +311,7 @@ func (r *resourceChannel) Delete(ctx context.Context, req resource.DeleteRequest return } - if _, err := r.client.StopChannel(&mediatailor.StopChannelInput{ChannelName: state.Name}); err != nil { + if _, err := r.client.StopChannel(ctx, &mediatailor.StopChannelInput{ChannelName: state.Name}); err != nil { resp.Diagnostics.AddError( "error while stopping the channel "+err.Error(), err.Error(), @@ -321,7 +319,7 @@ func (r *resourceChannel) Delete(ctx context.Context, req resource.DeleteRequest return } - if _, err := r.client.DeleteChannelPolicy(&mediatailor.DeleteChannelPolicyInput{ChannelName: state.Name}); err != nil { + if _, err := r.client.DeleteChannelPolicy(ctx, &mediatailor.DeleteChannelPolicyInput{ChannelName: state.Name}); err != nil { resp.Diagnostics.AddError( "error while deleting the channel policy "+err.Error(), err.Error(), @@ -329,7 +327,7 @@ func (r *resourceChannel) Delete(ctx context.Context, req resource.DeleteRequest return } - if _, err := r.client.DeleteChannel(&mediatailor.DeleteChannelInput{ChannelName: state.Name}); err != nil { + if _, err := r.client.DeleteChannel(ctx, &mediatailor.DeleteChannelInput{ChannelName: state.Name}); err != nil { resp.Diagnostics.AddError( "error while deleting the channel "+err.Error(), err.Error(), From 4d0bb96e5c602fc465dbe2f168abed6a2f56c035 Mon Sep 17 00:00:00 2001 From: Domenico Di Ruocco Date: Tue, 20 Aug 2024 13:13:45 +0200 Subject: [PATCH 2/9] fix: check that channel policy exists OTT-6334 --- awsmt/resource_channel.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/awsmt/resource_channel.go b/awsmt/resource_channel.go index dfc1af6..204d9e6 100644 --- a/awsmt/resource_channel.go +++ b/awsmt/resource_channel.go @@ -188,7 +188,7 @@ func (r *resourceChannel) Read(ctx context.Context, req resource.ReadRequest, re ) } - if policy.Policy != nil { + if policy != nil && policy.Policy != nil { state.Policy = jsontypes.NewNormalizedPointerValue(policy.Policy) } else { From 2d88f041d77cefbf679c2bbd293d1e38c01e3cd5 Mon Sep 17 00:00:00 2001 From: Domenico Di Ruocco Date: Tue, 20 Aug 2024 13:58:41 +0200 Subject: [PATCH 3/9] fix: check that channel policy exists OTT-6334 --- awsmt/data_source_channel.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/awsmt/data_source_channel.go b/awsmt/data_source_channel.go index 6e783b1..d8ab863 100644 --- a/awsmt/data_source_channel.go +++ b/awsmt/data_source_channel.go @@ -91,8 +91,7 @@ func (d *dataSourceChannel) Configure(_ context.Context, req datasource.Configur func (d *dataSourceChannel) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { var data channelModel - diags := req.Config.Get(ctx, &data) - resp.Diagnostics.Append(diags...) + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) if resp.Diagnostics.HasError() { return } @@ -114,7 +113,7 @@ func (d *dataSourceChannel) Read(ctx context.Context, req datasource.ReadRequest return } - if policy.Policy != nil { + if policy != nil && policy.Policy != nil { data.Policy = jsontypes.NewNormalizedPointerValue(policy.Policy) } From e9d6e3be161795118ad97ad256043c958d32ccd9 Mon Sep 17 00:00:00 2001 From: Domenico Di Ruocco Date: Tue, 20 Aug 2024 15:37:29 +0200 Subject: [PATCH 4/9] fix: refactor channel policy logic OTT-6334 --- awsmt/resource_channel.go | 43 +++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/awsmt/resource_channel.go b/awsmt/resource_channel.go index 204d9e6..50e6966 100644 --- a/awsmt/resource_channel.go +++ b/awsmt/resource_channel.go @@ -2,6 +2,7 @@ package awsmt import ( "context" + "fmt" "github.com/aws/aws-sdk-go-v2/service/mediatailor" awsTypes "github.com/aws/aws-sdk-go-v2/service/mediatailor/types" "github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes" @@ -166,8 +167,7 @@ func (r *resourceChannel) Create(ctx context.Context, req resource.CreateRequest func (r *resourceChannel) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { var state channelModel - diags := req.State.Get(ctx, &state) - resp.Diagnostics.Append(diags...) + resp.Diagnostics.Append(req.State.Get(ctx, &state)...) if resp.Diagnostics.HasError() { return } @@ -202,8 +202,7 @@ func (r *resourceChannel) Read(ctx context.Context, req resource.ReadRequest, re state.ChannelState = &channelState } - diags = resp.State.Set(ctx, &state) - resp.Diagnostics.Append(diags...) + resp.Diagnostics.Append(resp.State.Set(ctx, &state)...) if resp.Diagnostics.HasError() { return } @@ -246,20 +245,7 @@ func (r *resourceChannel) Update(ctx context.Context, req resource.UpdateRequest ) } - oldPolicy, err := r.client.GetChannelPolicy(ctx, &mediatailor.GetChannelPolicyInput{ChannelName: channelName}) - if err != nil && !strings.Contains(err.Error(), "NotFound") { - resp.Diagnostics.AddError( - "Error while getting channel policy "+err.Error(), - err.Error(), - ) - } - - policy := jsontypes.NewNormalizedPointerValue(oldPolicy.Policy) - - newPolicy := plan.Policy - - plan, err = updatePolicy(&plan, channelName, policy, newPolicy, r.client) - if err != nil { + if err := handlePolicyUpdate(ctx, r.client, plan); err != nil { resp.Diagnostics.AddError( "Error while updating channel policy "+err.Error(), err.Error(), @@ -339,3 +325,24 @@ func (r *resourceChannel) Delete(ctx context.Context, req resource.DeleteRequest func (r *resourceChannel) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { resource.ImportStatePassthroughID(ctx, path.Root("name"), req, resp) } + +func handlePolicyUpdate(context context.Context, client *mediatailor.Client, plan channelModel) error { + var normalizedOldPolicy jsontypes.Normalized + + oldPolicy, err := client.GetChannelPolicy(context, &mediatailor.GetChannelPolicyInput{ChannelName: plan.Name}) + if err != nil && !strings.Contains(err.Error(), "NotFound") { + return fmt.Errorf("error getting policy %v", err) + } + + if oldPolicy != nil && oldPolicy.Policy != nil { + normalizedOldPolicy = jsontypes.NewNormalizedPointerValue(oldPolicy.Policy) + } else { + normalizedOldPolicy = jsontypes.NewNormalizedNull() + } + + plan, err = updatePolicy(&plan, plan.Name, normalizedOldPolicy, plan.Policy, client) + if err != nil { + return fmt.Errorf("error updating policy %v", err) + } + return nil +} From 907f45ab6f0011208a55b7d8850a1c9a925d12ca Mon Sep 17 00:00:00 2001 From: Domenico Di Ruocco Date: Tue, 20 Aug 2024 15:42:33 +0200 Subject: [PATCH 5/9] refactor: move handle policy update to helpers file OTT-6334 --- awsmt/helpers_channels.go | 23 +++++++++++++++++++++++ awsmt/resource_channel.go | 25 +------------------------ 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/awsmt/helpers_channels.go b/awsmt/helpers_channels.go index 0464d87..f6b69b6 100644 --- a/awsmt/helpers_channels.go +++ b/awsmt/helpers_channels.go @@ -2,12 +2,14 @@ package awsmt import ( "context" + "fmt" "github.com/aws/aws-sdk-go-v2/service/mediatailor" awsTypes "github.com/aws/aws-sdk-go-v2/service/mediatailor/types" "github.com/aws/aws-sdk-go/aws" "github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes" "github.com/hashicorp/terraform-plugin-framework/types" "reflect" + "strings" "time" ) @@ -173,6 +175,27 @@ func stopChannel(state awsTypes.ChannelState, channelName *string, client *media return nil } +func handlePolicyUpdate(context context.Context, client *mediatailor.Client, plan channelModel) error { + var normalizedOldPolicy jsontypes.Normalized + + oldPolicy, err := client.GetChannelPolicy(context, &mediatailor.GetChannelPolicyInput{ChannelName: plan.Name}) + if err != nil && !strings.Contains(err.Error(), "NotFound") { + return fmt.Errorf("error getting policy %v", err) + } + + if oldPolicy != nil && oldPolicy.Policy != nil { + normalizedOldPolicy = jsontypes.NewNormalizedPointerValue(oldPolicy.Policy) + } else { + normalizedOldPolicy = jsontypes.NewNormalizedNull() + } + + plan, err = updatePolicy(&plan, plan.Name, normalizedOldPolicy, plan.Policy, client) + if err != nil { + return fmt.Errorf("error updating policy %v", err) + } + return nil +} + func updatePolicy(model *channelModel, channelName *string, oldPolicy jsontypes.Normalized, newPolicy jsontypes.Normalized, client *mediatailor.Client) (channelModel, error) { if !reflect.DeepEqual(oldPolicy, newPolicy) { if !newPolicy.IsNull() { diff --git a/awsmt/resource_channel.go b/awsmt/resource_channel.go index 50e6966..a1cdc48 100644 --- a/awsmt/resource_channel.go +++ b/awsmt/resource_channel.go @@ -2,7 +2,6 @@ package awsmt import ( "context" - "fmt" "github.com/aws/aws-sdk-go-v2/service/mediatailor" awsTypes "github.com/aws/aws-sdk-go-v2/service/mediatailor/types" "github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes" @@ -291,8 +290,7 @@ func (r *resourceChannel) Update(ctx context.Context, req resource.UpdateRequest func (r *resourceChannel) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { var state channelModel - diags := req.State.Get(ctx, &state) - resp.Diagnostics.Append(diags...) + resp.Diagnostics.Append(req.State.Get(ctx, &state)...) if resp.Diagnostics.HasError() { return } @@ -325,24 +323,3 @@ func (r *resourceChannel) Delete(ctx context.Context, req resource.DeleteRequest func (r *resourceChannel) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { resource.ImportStatePassthroughID(ctx, path.Root("name"), req, resp) } - -func handlePolicyUpdate(context context.Context, client *mediatailor.Client, plan channelModel) error { - var normalizedOldPolicy jsontypes.Normalized - - oldPolicy, err := client.GetChannelPolicy(context, &mediatailor.GetChannelPolicyInput{ChannelName: plan.Name}) - if err != nil && !strings.Contains(err.Error(), "NotFound") { - return fmt.Errorf("error getting policy %v", err) - } - - if oldPolicy != nil && oldPolicy.Policy != nil { - normalizedOldPolicy = jsontypes.NewNormalizedPointerValue(oldPolicy.Policy) - } else { - normalizedOldPolicy = jsontypes.NewNormalizedNull() - } - - plan, err = updatePolicy(&plan, plan.Name, normalizedOldPolicy, plan.Policy, client) - if err != nil { - return fmt.Errorf("error updating policy %v", err) - } - return nil -} From 64ae9247b63f40b8dbc76f675ab1ac6e7ef91068 Mon Sep 17 00:00:00 2001 From: Domenico Di Ruocco Date: Tue, 20 Aug 2024 15:53:51 +0200 Subject: [PATCH 6/9] fix: rad playback mode along with other fields OTT-6334 --- awsmt/resource_channel.go | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/awsmt/resource_channel.go b/awsmt/resource_channel.go index a1cdc48..a259231 100644 --- a/awsmt/resource_channel.go +++ b/awsmt/resource_channel.go @@ -189,7 +189,6 @@ func (r *resourceChannel) Read(ctx context.Context, req resource.ReadRequest, re if policy != nil && policy.Policy != nil { state.Policy = jsontypes.NewNormalizedPointerValue(policy.Policy) - } else { state.Policy = jsontypes.NewNormalizedNull() } @@ -251,8 +250,7 @@ func (r *resourceChannel) Update(ctx context.Context, req resource.UpdateRequest ) } - var params = getUpdateChannelInput(plan) - updatedChannel, err := r.client.UpdateChannel(ctx, params) + updatedChannel, err := r.client.UpdateChannel(ctx, getUpdateChannelInput(plan)) if err != nil { resp.Diagnostics.AddError( "Error while updating channel "+*channel.ChannelName+err.Error(), @@ -274,14 +272,6 @@ func (r *resourceChannel) Update(ctx context.Context, req resource.UpdateRequest plan = writeChannelToPlan(plan, mediatailor.CreateChannelOutput(*updatedChannel)) - // @ADR - // Context: The official AWS Mediatailor Go SDK states that the PlaybackMode is part of the UpdateChannelOutput, - // but it is not. As tested, the PlaybackMode is only returned when describing a channel. - // Decision: We decided to use the previous API call to describe the channel and get the PlaybackMode from there. - // Consequences: The PlaybackMode is not updated when updating the channel. - - plan.PlaybackMode = channel.PlaybackMode - resp.Diagnostics.Append(resp.State.Set(ctx, plan)...) if resp.Diagnostics.HasError() { return From e11860e0b9fc882b43a96e882f8183f4af53d6dc Mon Sep 17 00:00:00 2001 From: Domenico Di Ruocco Date: Tue, 20 Aug 2024 16:43:24 +0200 Subject: [PATCH 7/9] fix: configuration aliases type. Closes #85 OTT-6334 --- awsmt/data_source_playback_configuration.go | 6 +- ...data_source_playback_configuration_test.go | 46 ++++++++------ awsmt/resource_playback_configuration_test.go | 62 +++++++++++++++++++ 3 files changed, 91 insertions(+), 23 deletions(-) diff --git a/awsmt/data_source_playback_configuration.go b/awsmt/data_source_playback_configuration.go index 20c4879..0af66d1 100644 --- a/awsmt/data_source_playback_configuration.go +++ b/awsmt/data_source_playback_configuration.go @@ -52,12 +52,10 @@ func (d *dataSourcePlaybackConfiguration) Schema(_ context.Context, _ datasource "content_segment_url_prefix": computedString, }, }, - "configuration_aliases": schema.ListAttribute{ + "configuration_aliases": schema.MapAttribute{ Computed: true, ElementType: types.MapType{ - ElemType: types.MapType{ - ElemType: types.StringType, - }, + ElemType: types.StringType, }, }, "dash_configuration": schema.SingleNestedAttribute{ diff --git a/awsmt/data_source_playback_configuration_test.go b/awsmt/data_source_playback_configuration_test.go index 1fd9be4..43929b1 100644 --- a/awsmt/data_source_playback_configuration_test.go +++ b/awsmt/data_source_playback_configuration_test.go @@ -7,6 +7,7 @@ import ( ) func TestAccPlaybackConfigurationDataSourceBasic(t *testing.T) { + resourceName := "data.awsmt_playback_configuration.test" resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, @@ -14,25 +15,27 @@ func TestAccPlaybackConfigurationDataSourceBasic(t *testing.T) { { Config: playbackConfigDS(), Check: resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttr("data.awsmt_playback_configuration.test", "id", "example-playback-configuration-awsmt"), - resource.TestCheckResourceAttr("data.awsmt_playback_configuration.test", "ad_decision_server_url", "https://exampleurl.com/"), - resource.TestCheckResourceAttr("data.awsmt_playback_configuration.test", "avail_suppression.fill_policy", "FULL_AVAIL_ONLY"), - resource.TestCheckResourceAttr("data.awsmt_playback_configuration.test", "avail_suppression.mode", "BEHIND_LIVE_EDGE"), - resource.TestCheckResourceAttr("data.awsmt_playback_configuration.test", "avail_suppression.value", "00:00:00"), - resource.TestCheckResourceAttr("data.awsmt_playback_configuration.test", "bumper.end_url", "https://wxample.com/endbumper"), - resource.TestCheckResourceAttr("data.awsmt_playback_configuration.test", "bumper.start_url", "https://wxample.com/startbumper"), - resource.TestCheckResourceAttr("data.awsmt_playback_configuration.test", "cdn_configuration.ad_segment_url_prefix", "https://exampleurl.com/"), - resource.TestCheckResourceAttr("data.awsmt_playback_configuration.test", "cdn_configuration.content_segment_url_prefix", "https://exampleurl.com/"), - resource.TestCheckResourceAttr("data.awsmt_playback_configuration.test", "dash_configuration.mpd_location", "DISABLED"), - resource.TestCheckResourceAttr("data.awsmt_playback_configuration.test", "dash_configuration.origin_manifest_type", "SINGLE_PERIOD"), - resource.TestCheckResourceAttr("data.awsmt_playback_configuration.test", "live_pre_roll_configuration.ad_decision_server_url", "https://exampleurl.com/"), - resource.TestCheckResourceAttr("data.awsmt_playback_configuration.test", "live_pre_roll_configuration.max_duration_seconds", "2"), - resource.TestCheckResourceAttr("data.awsmt_playback_configuration.test", "name", "example-playback-configuration-awsmt"), - resource.TestCheckResourceAttr("data.awsmt_playback_configuration.test", "personalization_threshold_seconds", "2"), - resource.TestCheckResourceAttr("data.awsmt_playback_configuration.test", "slate_ad_url", "https://exampleurl.com/"), - resource.TestCheckResourceAttr("data.awsmt_playback_configuration.test", "tags.Environment", "dev"), - resource.TestCheckResourceAttr("data.awsmt_playback_configuration.test", "video_content_source_url", "https://exampleurl.com/"), - resource.TestCheckResourceAttr("data.awsmt_playback_configuration.test", "log_configuration_percent_enabled", "0"), + resource.TestCheckResourceAttr(resourceName, "id", "example-playback-configuration-awsmt"), + resource.TestCheckResourceAttr(resourceName, "ad_decision_server_url", "https://exampleurl.com/"), + resource.TestCheckResourceAttr(resourceName, "avail_suppression.fill_policy", "FULL_AVAIL_ONLY"), + resource.TestCheckResourceAttr(resourceName, "avail_suppression.mode", "BEHIND_LIVE_EDGE"), + resource.TestCheckResourceAttr(resourceName, "avail_suppression.value", "00:00:00"), + resource.TestCheckResourceAttr(resourceName, "bumper.end_url", "https://wxample.com/endbumper"), + resource.TestCheckResourceAttr(resourceName, "bumper.start_url", "https://wxample.com/startbumper"), + resource.TestCheckResourceAttr(resourceName, "cdn_configuration.ad_segment_url_prefix", "https://exampleurl.com/"), + resource.TestCheckResourceAttr(resourceName, "cdn_configuration.content_segment_url_prefix", "https://exampleurl.com/"), + resource.TestCheckResourceAttr(resourceName, "configuration_aliases.%", "1"), + resource.TestCheckResourceAttr(resourceName, "configuration_aliases.player_params.foo.player_params.bar", "player_params.buzz"), + resource.TestCheckResourceAttr(resourceName, "dash_configuration.mpd_location", "DISABLED"), + resource.TestCheckResourceAttr(resourceName, "dash_configuration.origin_manifest_type", "SINGLE_PERIOD"), + resource.TestCheckResourceAttr(resourceName, "live_pre_roll_configuration.ad_decision_server_url", "https://exampleurl.com/"), + resource.TestCheckResourceAttr(resourceName, "live_pre_roll_configuration.max_duration_seconds", "2"), + resource.TestCheckResourceAttr(resourceName, "name", "example-playback-configuration-awsmt"), + resource.TestCheckResourceAttr(resourceName, "personalization_threshold_seconds", "2"), + resource.TestCheckResourceAttr(resourceName, "slate_ad_url", "https://exampleurl.com/"), + resource.TestCheckResourceAttr(resourceName, "tags.Environment", "dev"), + resource.TestCheckResourceAttr(resourceName, "video_content_source_url", "https://exampleurl.com/"), + resource.TestCheckResourceAttr(resourceName, "log_configuration_percent_enabled", "0"), ), }, }, @@ -68,6 +71,11 @@ func playbackConfigDS() string { ad_segment_url_prefix = "https://exampleurl.com/" content_segment_url_prefix = "https://exampleurl.com/" } + configuration_aliases = { + "player_params.foo" = { + "player_params.bar" = "player_params.buzz" + } + } dash_configuration = { mpd_location = "DISABLED", origin_manifest_type = "SINGLE_PERIOD" diff --git a/awsmt/resource_playback_configuration_test.go b/awsmt/resource_playback_configuration_test.go index 5a5c397..495e3e4 100644 --- a/awsmt/resource_playback_configuration_test.go +++ b/awsmt/resource_playback_configuration_test.go @@ -1,6 +1,7 @@ package awsmt import ( + "encoding/json" "fmt" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "regexp" @@ -40,6 +41,55 @@ func TestAccPlaybackConfigurationMinimal(t *testing.T) { }) } +func TestAccPlaybackConfigurationConfigurationAliases(t *testing.T) { + resourceName := "awsmt_playback_configuration.r4" + name := "test-acc-playback-configuration-minimal" + adUrl := "https://www.foo.de/" + videoSourceUrl := "https://www.bar.at" + aliases := map[string]map[string]string{ + "player_params.foo": { + "player_params.bar": "player_params.boo", + }, + } + aliases2 := map[string]map[string]string{ + "player_params.foo": { + "player_params.bar": "player_params.buzz", + }, + } + + aliasesJson, _ := json.Marshal(aliases) + aliases2Json, _ := json.Marshal(aliases2) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + { + Config: configurationAliasesPlaybackConfiguration(name, adUrl, videoSourceUrl, string(aliasesJson)), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr(resourceName, "id", name), + resource.TestCheckResourceAttr(resourceName, "name", name), + resource.TestCheckResourceAttr(resourceName, "ad_decision_server_url", adUrl), + resource.TestCheckResourceAttr(resourceName, "video_content_source_url", videoSourceUrl), + resource.TestCheckResourceAttr(resourceName, "configuration_aliases.%", "1"), + resource.TestCheckResourceAttr(resourceName, "configuration_aliases.player_params.foo.player_params.bar", "player_params.boo"), + ), + }, + { + Config: configurationAliasesPlaybackConfiguration(name, adUrl, videoSourceUrl, string(aliases2Json)), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr(resourceName, "id", name), + resource.TestCheckResourceAttr(resourceName, "name", name), + resource.TestCheckResourceAttr(resourceName, "ad_decision_server_url", adUrl), + resource.TestCheckResourceAttr(resourceName, "video_content_source_url", videoSourceUrl), + resource.TestCheckResourceAttr(resourceName, "configuration_aliases.%", "1"), + resource.TestCheckResourceAttr(resourceName, "configuration_aliases.player_params.foo.player_params.bar", "player_params.buzz"), + ), + }, + }, + }) +} + func TestAccPlaybackConfigurationLogPercentage(t *testing.T) { resourceName := "awsmt_playback_configuration.r3" name := "test-acc-playback-configuration-log-percentage" @@ -197,6 +247,18 @@ func minimalPlaybackConfiguration(name, adUrl, videoSourceUrl string) string { ) } +func configurationAliasesPlaybackConfiguration(name, adUrl, videoSourceUrl, configAliases string) string { + return fmt.Sprintf(` + resource "awsmt_playback_configuration" "r4" { + ad_decision_server_url = "%[2]s" + name = "%[1]s" + video_content_source_url = "%[3]s" + configuration_aliases = %[4]s + } + `, name, adUrl, videoSourceUrl, configAliases, + ) +} + func logPercentagePlaybackConfiguration(name, adUrl, videoSourceUrl, logPercentage string) string { return fmt.Sprintf(` resource "awsmt_playback_configuration" "r3" { From 29716048d180ccd9682b7fc4cd37122a0e3a2ccb Mon Sep 17 00:00:00 2001 From: Domenico Di Ruocco Date: Wed, 21 Aug 2024 11:39:36 +0200 Subject: [PATCH 8/9] test: lint and rename variables OTT-6334 --- awsmt/resource_channel_test.go | 224 +++++++++++++++++---------------- 1 file changed, 116 insertions(+), 108 deletions(-) diff --git a/awsmt/resource_channel_test.go b/awsmt/resource_channel_test.go index 97b7121..a462d35 100644 --- a/awsmt/resource_channel_test.go +++ b/awsmt/resource_channel_test.go @@ -15,17 +15,18 @@ func testAccPreCheck(t *testing.T) { } func TestAccChannelResourceBasic(t *testing.T) { + resourceName := "awsmt_channel.test" name := "test" - state_stopped := "STOPPED" - state_running := "RUNNING" - mw_s := "30" - mw_s2 := "40" - mbt_s := "2" - mbt_s2 := "3" - mup_s := "2" - mup_s2 := "3" - spd_s := "2" - spd_s2 := "3" + stateStopped := "STOPPED" + stateRunning := "RUNNING" + manifestWindowSeconds := "30" + manifestWindowSeconds2 := "40" + minBufferTimeSeconds := "2" + minBufferTimeSeconds2 := "3" + minUpdatePeriodSeconds := "2" + minUpdatePeriodSeconds2 := "3" + PresentationDelaySeconds := "2" + presentationDelaySeconds2 := "3" k1 := "Environment" v1 := "dev" k2 := "Testing" @@ -37,51 +38,51 @@ func TestAccChannelResourceBasic(t *testing.T) { ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, Steps: []resource.TestStep{ { - Config: basicChannel(name, state_stopped, mw_s, mbt_s, mup_s, spd_s, k1, v1, k2, v2), + Config: basicChannel(name, stateStopped, manifestWindowSeconds, minBufferTimeSeconds, minUpdatePeriodSeconds, PresentationDelaySeconds, k1, v1, k2, v2), Check: resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttr("awsmt_channel.test", "id", "test"), - resource.TestMatchResourceAttr("awsmt_channel.test", "arn", regexp.MustCompile(`^arn:aws:mediatailor:[\w-]+:\d+:channel\/.*$`)), - resource.TestCheckResourceAttr("awsmt_channel.test", "name", "test"), - resource.TestMatchResourceAttr("awsmt_channel.test", "creation_time", regexp.MustCompile(`^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(\.\d{1,3})? \+\d{4} \w+$`)), - resource.TestMatchResourceAttr("awsmt_channel.test", "last_modified_time", regexp.MustCompile(`^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(\.\d{1,3})? \+\d{4} \w+$`)), - resource.TestCheckResourceAttr("awsmt_channel.test", "channel_state", "STOPPED"), - resource.TestCheckResourceAttr("awsmt_channel.test", "playback_mode", "LOOP"), - resource.TestMatchResourceAttr("awsmt_channel.test", "policy", regexp.MustCompile(`mediatailor:GetManifest`)), - resource.TestCheckResourceAttr("awsmt_channel.test", "tier", "BASIC"), - resource.TestCheckResourceAttr("awsmt_channel.test", "tags.Environment", "dev"), - resource.TestCheckResourceAttr("awsmt_channel.test", "outputs.0.manifest_name", "default"), - resource.TestCheckResourceAttr("awsmt_channel.test", "outputs.0.source_group", "default"), - resource.TestCheckResourceAttr("awsmt_channel.test", "outputs.0.dash_playlist_settings.manifest_window_seconds", "30"), - resource.TestCheckResourceAttr("awsmt_channel.test", "outputs.0.dash_playlist_settings.min_buffer_time_seconds", "2"), - resource.TestCheckResourceAttr("awsmt_channel.test", "outputs.0.dash_playlist_settings.min_update_period_seconds", "2"), - resource.TestCheckResourceAttr("awsmt_channel.test", "outputs.0.dash_playlist_settings.suggested_presentation_delay_seconds", "2"), + resource.TestCheckResourceAttr(resourceName, "id", "test"), + resource.TestMatchResourceAttr(resourceName, "arn", regexp.MustCompile(`^arn:aws:mediatailor:[\w-]+:\d+:channel\/.*$`)), + resource.TestCheckResourceAttr(resourceName, "name", "test"), + resource.TestMatchResourceAttr(resourceName, "creation_time", regexp.MustCompile(`^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(\.\d{1,3})? \+\d{4} \w+$`)), + resource.TestMatchResourceAttr(resourceName, "last_modified_time", regexp.MustCompile(`^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(\.\d{1,3})? \+\d{4} \w+$`)), + resource.TestCheckResourceAttr(resourceName, "channel_state", "STOPPED"), + resource.TestCheckResourceAttr(resourceName, "playback_mode", "LOOP"), + resource.TestMatchResourceAttr(resourceName, "policy", regexp.MustCompile(`mediatailor:GetManifest`)), + resource.TestCheckResourceAttr(resourceName, "tier", "BASIC"), + resource.TestCheckResourceAttr(resourceName, "tags.Environment", "dev"), + resource.TestCheckResourceAttr(resourceName, "outputs.0.manifest_name", "default"), + resource.TestCheckResourceAttr(resourceName, "outputs.0.source_group", "default"), + resource.TestCheckResourceAttr(resourceName, "outputs.0.dash_playlist_settings.manifest_window_seconds", "30"), + resource.TestCheckResourceAttr(resourceName, "outputs.0.dash_playlist_settings.min_buffer_time_seconds", "2"), + resource.TestCheckResourceAttr(resourceName, "outputs.0.dash_playlist_settings.min_update_period_seconds", "2"), + resource.TestCheckResourceAttr(resourceName, "outputs.0.dash_playlist_settings.suggested_presentation_delay_seconds", "2"), ), }, // ImportState testing { - ResourceName: "awsmt_channel.test", + ResourceName: resourceName, ImportState: true, }, // Update and Read testing { - Config: basicChannel(name, state_running, mw_s2, mbt_s2, mup_s2, spd_s2, k3, v3, k2, v2), + Config: basicChannel(name, stateRunning, manifestWindowSeconds2, minBufferTimeSeconds2, minUpdatePeriodSeconds2, presentationDelaySeconds2, k3, v3, k2, v2), Check: resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttr("awsmt_channel.test", "id", "test"), - resource.TestMatchResourceAttr("awsmt_channel.test", "arn", regexp.MustCompile(`^arn:aws:mediatailor:[\w-]+:\d+:channel\/.*$`)), - resource.TestCheckResourceAttr("awsmt_channel.test", "name", "test"), - resource.TestMatchResourceAttr("awsmt_channel.test", "creation_time", regexp.MustCompile(`^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(\.\d{1,3})? \+\d{4} \w+$`)), - resource.TestMatchResourceAttr("awsmt_channel.test", "last_modified_time", regexp.MustCompile(`^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(\.\d{1,3})? \+\d{4} \w+$`)), - resource.TestCheckResourceAttr("awsmt_channel.test", "channel_state", "RUNNING"), - resource.TestCheckResourceAttr("awsmt_channel.test", "playback_mode", "LOOP"), - resource.TestCheckResourceAttr("awsmt_channel.test", "tier", "BASIC"), - resource.TestCheckResourceAttr("awsmt_channel.test", "tags.Environment", "prod"), - resource.TestCheckResourceAttr("awsmt_channel.test", "tags.Testing", "pass"), - resource.TestCheckResourceAttr("awsmt_channel.test", "outputs.0.manifest_name", "default"), - resource.TestCheckResourceAttr("awsmt_channel.test", "outputs.0.source_group", "default"), - resource.TestCheckResourceAttr("awsmt_channel.test", "outputs.0.dash_playlist_settings.manifest_window_seconds", "40"), - resource.TestCheckResourceAttr("awsmt_channel.test", "outputs.0.dash_playlist_settings.min_buffer_time_seconds", "3"), - resource.TestCheckResourceAttr("awsmt_channel.test", "outputs.0.dash_playlist_settings.min_update_period_seconds", "3"), - resource.TestCheckResourceAttr("awsmt_channel.test", "outputs.0.dash_playlist_settings.suggested_presentation_delay_seconds", "3"), + resource.TestCheckResourceAttr(resourceName, "id", "test"), + resource.TestMatchResourceAttr(resourceName, "arn", regexp.MustCompile(`^arn:aws:mediatailor:[\w-]+:\d+:channel\/.*$`)), + resource.TestCheckResourceAttr(resourceName, "name", "test"), + resource.TestMatchResourceAttr(resourceName, "creation_time", regexp.MustCompile(`^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(\.\d{1,3})? \+\d{4} \w+$`)), + resource.TestMatchResourceAttr(resourceName, "last_modified_time", regexp.MustCompile(`^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(\.\d{1,3})? \+\d{4} \w+$`)), + resource.TestCheckResourceAttr(resourceName, "channel_state", "RUNNING"), + resource.TestCheckResourceAttr(resourceName, "playback_mode", "LOOP"), + resource.TestCheckResourceAttr(resourceName, "tier", "BASIC"), + resource.TestCheckResourceAttr(resourceName, "tags.Environment", "prod"), + resource.TestCheckResourceAttr(resourceName, "tags.Testing", "pass"), + resource.TestCheckResourceAttr(resourceName, "outputs.0.manifest_name", "default"), + resource.TestCheckResourceAttr(resourceName, "outputs.0.source_group", "default"), + resource.TestCheckResourceAttr(resourceName, "outputs.0.dash_playlist_settings.manifest_window_seconds", "40"), + resource.TestCheckResourceAttr(resourceName, "outputs.0.dash_playlist_settings.min_buffer_time_seconds", "3"), + resource.TestCheckResourceAttr(resourceName, "outputs.0.dash_playlist_settings.min_update_period_seconds", "3"), + resource.TestCheckResourceAttr(resourceName, "outputs.0.dash_playlist_settings.suggested_presentation_delay_seconds", "3"), ), }, }, @@ -134,21 +135,21 @@ resource "awsmt_channel" "test" { } func TestAccChannelResourceRunning(t *testing.T) { - mw_s := "30" - mw_s2 := "40" + manifestWindowSeconds := "30" + manifestWindowSeconds2 := "40" resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, Steps: []resource.TestStep{ { - Config: hlsChannel(mw_s), + Config: hlsChannel(manifestWindowSeconds), Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr("awsmt_channel.test", "channel_state", "RUNNING"), resource.TestCheckResourceAttr("data.awsmt_channel.test", "outputs.0.hls_playlist_settings.manifest_window_seconds", "30"), ), }, { - Config: hlsChannel(mw_s2), + Config: hlsChannel(manifestWindowSeconds2), Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr("awsmt_channel.test", "channel_state", "RUNNING"), resource.TestCheckResourceAttr("data.awsmt_channel.test", "outputs.0.hls_playlist_settings.manifest_window_seconds", "40"), @@ -189,7 +190,7 @@ func TestAccChannelResourceSTANDARD(t *testing.T) { }) } -func basicChannel(name, state, mw_s, mbt_s, mup_s, spd_s, k1, v1, k2, v2 string) string { +func basicChannel(name, state, manifestWindowSeconds, minBufferTimeSeconds, minUpdatePeriodSeconds, presentationDelaySeconds, k1, v1, k2, v2 string) string { return fmt.Sprintf( ` resource "awsmt_channel" "test" { @@ -220,7 +221,7 @@ func basicChannel(name, state, mw_s, mbt_s, mup_s, spd_s, k1, v1, k2, v2 string) output "channel_out" { value = data.awsmt_channel.test } - `, name, state, mw_s, mbt_s, mup_s, spd_s, k1, v1, k2, v2, + `, name, state, manifestWindowSeconds, minBufferTimeSeconds, minUpdatePeriodSeconds, presentationDelaySeconds, k1, v1, k2, v2, ) } @@ -304,66 +305,73 @@ func hlsChannel(mw_s string) string { } `, mw_s) } + func standardTierChannel() string { return `resource "awsmt_vod_source" "test" { - http_package_configurations = [{ -path = "/" -source_group = "default" -type = "HLS" -}] -source_location_name = awsmt_source_location.test_source_location.name -name = "vod_source_example" -tags = {"Environment": "dev"} -} -data "awsmt_vod_source" "data_test" { -source_location_name = awsmt_source_location.test_source_location.name -name = awsmt_vod_source.test.name -} + http_package_configurations = [{ + path = "/" + source_group = "default" + type = "HLS" + }] + source_location_name = awsmt_source_location.test_source_location.name + name = "vod_source_example" + tags = {"Environment": "dev"} + } -output "vod_source_out" { -value = data.awsmt_vod_source.data_test -} -resource "awsmt_source_location" "test_source_location"{ -name = "test_source_location" -http_configuration = { -base_url = "https://ott-mediatailor-test.s3.eu-central-1.amazonaws.com/" -} -default_segment_delivery_configuration = { -base_url = "https://ott-mediatailor-test.s3.eu-central-1.amazonaws.com/test-img.jpeg" -} -} -data "awsmt_source_location" "test" { -name = awsmt_source_location.test_source_location.name -} -output "awsmt_source_location" { -value = data.awsmt_source_location.test -} -resource "awsmt_channel" "test" { -name = "test" -channel_state = "STOPPED" -outputs = [{ -manifest_name = "default" -source_group = "default" -hls_playlist_settings = { -ad_markup_type = ["DATERANGE"] -manifest_window_seconds = 30 -} -}] -playback_mode = "LINEAR" -filler_slate = { -source_location_name = awsmt_source_location.test_source_location.name -vod_source_name = awsmt_vod_source.test.name -} -policy = "{\"Version\": \"2012-10-17\", \"Statement\": [{\"Sid\": \"AllowAnonymous\", \"Effect\": \"Allow\", \"Principal\": \"*\", \"Action\": \"mediatailor:GetManifest\", \"Resource\": \"arn:aws:mediatailor:eu-central-1:985600762523:channel/test\"}]}" -tier = "STANDARD" -tags = {"Environment": "dev"} -} + data "awsmt_vod_source" "data_test" { + source_location_name = awsmt_source_location.test_source_location.name + name = awsmt_vod_source.test.name + } -data "awsmt_channel" "test" { -name = awsmt_channel.test.name -} -output "channel_out" { -value = data.awsmt_channel.test -} + output "vod_source_out" { + value = data.awsmt_vod_source.data_test + } + + resource "awsmt_source_location" "test_source_location"{ + name = "test_source_location" + http_configuration = { + base_url = "https://ott-mediatailor-test.s3.eu-central-1.amazonaws.com/" + } + default_segment_delivery_configuration = { + base_url = "https://ott-mediatailor-test.s3.eu-central-1.amazonaws.com/test-img.jpeg" + } + } + + data "awsmt_source_location" "test" { + name = awsmt_source_location.test_source_location.name + } + + output "awsmt_source_location" { + value = data.awsmt_source_location.test + } + + resource "awsmt_channel" "test" { + name = "test" + channel_state = "STOPPED" + outputs = [{ + manifest_name = "default" + source_group = "default" + hls_playlist_settings = { + ad_markup_type = ["DATERANGE"] + manifest_window_seconds = 30 + } + }] + playback_mode = "LINEAR" + filler_slate = { + source_location_name = awsmt_source_location.test_source_location.name + vod_source_name = awsmt_vod_source.test.name + } + policy = "{\"Version\": \"2012-10-17\", \"Statement\": [{\"Sid\": \"AllowAnonymous\", \"Effect\": \"Allow\", \"Principal\": \"*\", \"Action\": \"mediatailor:GetManifest\", \"Resource\": \"arn:aws:mediatailor:eu-central-1:985600762523:channel/test\"}]}" + tier = "STANDARD" + tags = {"Environment": "dev"} + } + + data "awsmt_channel" "test" { + name = awsmt_channel.test.name + } + + output "channel_out" { + value = data.awsmt_channel.test + } ` } From fe19888111f5738f0a44455061c77db795c85fe8 Mon Sep 17 00:00:00 2001 From: Domenico Di Ruocco Date: Wed, 21 Aug 2024 11:40:06 +0200 Subject: [PATCH 9/9] refactor: move 'shouldStart' logic OTT-6334 --- awsmt/helpers_channels.go | 7 +++++++ awsmt/resource_channel.go | 10 +++------- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/awsmt/helpers_channels.go b/awsmt/helpers_channels.go index f6b69b6..8099553 100644 --- a/awsmt/helpers_channels.go +++ b/awsmt/helpers_channels.go @@ -390,3 +390,10 @@ func writeChannelToState(model channelModel, channel mediatailor.DescribeChannel return model } + +// helper functions to simplify update function logic +func shouldStartChannel(previousState awsTypes.ChannelState, newState *string) bool { + wasRunning := previousState == awsTypes.ChannelStateRunning + shouldRun := newState != nil && *newState == "RUNNING" + return (newState == nil && wasRunning) || shouldRun +} diff --git a/awsmt/resource_channel.go b/awsmt/resource_channel.go index a259231..bcf3241 100644 --- a/awsmt/resource_channel.go +++ b/awsmt/resource_channel.go @@ -3,7 +3,6 @@ package awsmt import ( "context" "github.com/aws/aws-sdk-go-v2/service/mediatailor" - awsTypes "github.com/aws/aws-sdk-go-v2/service/mediatailor/types" "github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes" "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" "github.com/hashicorp/terraform-plugin-framework/path" @@ -238,7 +237,7 @@ func (r *resourceChannel) Update(ctx context.Context, req resource.UpdateRequest err = stopChannel(previousState, channelName, r.client) if err != nil { resp.Diagnostics.AddError( - "Error while stopping running channel "+*channelName+err.Error(), + "Error while stopping running channel "+*channelName+" "+err.Error(), err.Error(), ) } @@ -253,14 +252,12 @@ func (r *resourceChannel) Update(ctx context.Context, req resource.UpdateRequest updatedChannel, err := r.client.UpdateChannel(ctx, getUpdateChannelInput(plan)) if err != nil { resp.Diagnostics.AddError( - "Error while updating channel "+*channel.ChannelName+err.Error(), + "Error while updating channel "+*channel.ChannelName+" "+err.Error(), err.Error(), ) } - wasRunning := previousState == awsTypes.ChannelStateRunning - shouldRun := newState != nil && *newState == "RUNNING" - if (newState == nil && wasRunning) || shouldRun { + if shouldStartChannel(previousState, newState) { _, err := r.client.StartChannel(ctx, &mediatailor.StartChannelInput{ChannelName: channelName}) if err != nil { resp.Diagnostics.AddError("Error while starting the channel "+*channelName, err.Error()) @@ -269,7 +266,6 @@ func (r *resourceChannel) Update(ctx context.Context, req resource.UpdateRequest } plan.ChannelState = newState - plan = writeChannelToPlan(plan, mediatailor.CreateChannelOutput(*updatedChannel)) resp.Diagnostics.Append(resp.State.Set(ctx, plan)...)