Skip to content

Commit

Permalink
refactor: put models in separate package
Browse files Browse the repository at this point in the history
OTT-6870
  • Loading branch information
thatsddr committed Dec 19, 2024
1 parent 817ee59 commit 750972d
Show file tree
Hide file tree
Showing 27 changed files with 433 additions and 355 deletions.
3 changes: 2 additions & 1 deletion awsmt/data_source_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"strings"
"terraform-provider-mediatailor/awsmt/models"
)

var (
Expand Down Expand Up @@ -90,7 +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
var data models.ChannelModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
Expand Down
3 changes: 2 additions & 1 deletion awsmt/data_source_live_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/aws/aws-sdk-go-v2/service/mediatailor"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"terraform-provider-mediatailor/awsmt/models"
)

var (
Expand Down Expand Up @@ -48,7 +49,7 @@ func (d *dataSourceLiveSource) Configure(_ context.Context, req datasource.Confi
}

func (d *dataSourceLiveSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data liveSourceModel
var data models.LiveSourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
Expand Down
3 changes: 2 additions & 1 deletion awsmt/data_source_playback_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"terraform-provider-mediatailor/awsmt/models"
)

var (
Expand Down Expand Up @@ -109,7 +110,7 @@ func (d *dataSourcePlaybackConfiguration) Configure(_ context.Context, req datas
}

func (d *dataSourcePlaybackConfiguration) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data playbackConfigurationModel
var data models.PlaybackConfigurationModel

resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
Expand Down
3 changes: 2 additions & 1 deletion awsmt/data_source_source_location.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"terraform-provider-mediatailor/awsmt/models"
)

var (
Expand Down Expand Up @@ -88,7 +89,7 @@ func (d *dataSourceSourceLocation) Configure(_ context.Context, req datasource.C
}

func (d *dataSourceSourceLocation) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data sourceLocationModel
var data models.SourceLocationModel
diags := req.Config.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand Down
3 changes: 2 additions & 1 deletion awsmt/data_source_vod_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/aws/aws-sdk-go-v2/service/mediatailor"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"terraform-provider-mediatailor/awsmt/models"
)

var (
Expand Down Expand Up @@ -49,7 +50,7 @@ func (d *dataSourceVodSource) Configure(_ context.Context, req datasource.Config
}

func (d *dataSourceVodSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data vodSourceModel
var data models.VodSourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
Expand Down
51 changes: 26 additions & 25 deletions awsmt/helpers_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import (
"github.com/hashicorp/terraform-plugin-framework/types"
"reflect"
"strings"
"terraform-provider-mediatailor/awsmt/models"
"time"
)

// functions to create MediaTailor inputs

func getCreateChannelInput(model channelModel) *mediatailor.CreateChannelInput {
func getCreateChannelInput(model models.ChannelModel) *mediatailor.CreateChannelInput {
var input mediatailor.CreateChannelInput

input.ChannelName, input.FillerSlate, input.Outputs = getSharedChannelInput(&model)
Expand Down Expand Up @@ -48,19 +49,19 @@ func getCreateChannelInput(model channelModel) *mediatailor.CreateChannelInput {
return &input
}

func getUpdateChannelInput(model channelModel) *mediatailor.UpdateChannelInput {
func getUpdateChannelInput(model models.ChannelModel) *mediatailor.UpdateChannelInput {
var input mediatailor.UpdateChannelInput

input.ChannelName, input.FillerSlate, input.Outputs = getSharedChannelInput(&model)

return &input
}

func getSharedChannelInput(model *channelModel) (name *string, source *awsTypes.SlateSource, outputItem []awsTypes.RequestOutputItem) {
func getSharedChannelInput(model *models.ChannelModel) (name *string, source *awsTypes.SlateSource, outputItem []awsTypes.RequestOutputItem) {
return model.Name, buildSlateSource(model), buildRequestOutputs(model)
}

func buildSlateSource(model *channelModel) *awsTypes.SlateSource {
func buildSlateSource(model *models.ChannelModel) *awsTypes.SlateSource {
if model.FillerSlate == nil {
return nil
}
Expand All @@ -74,7 +75,7 @@ func buildSlateSource(model *channelModel) *awsTypes.SlateSource {
return temp
}

func buildRequestOutputs(model *channelModel) []awsTypes.RequestOutputItem {
func buildRequestOutputs(model *models.ChannelModel) []awsTypes.RequestOutputItem {
var temp []awsTypes.RequestOutputItem

for _, o := range model.Outputs {
Expand Down Expand Up @@ -102,7 +103,7 @@ func buildRequestOutputs(model *channelModel) []awsTypes.RequestOutputItem {
return temp
}

func buildDashPlaylistSettings(settings *dashPlaylistSettingsModel) *awsTypes.DashPlaylistSettings {
func buildDashPlaylistSettings(settings *models.DashPlaylistSettingsModel) *awsTypes.DashPlaylistSettings {
dashSettings := &awsTypes.DashPlaylistSettings{}
if settings.ManifestWindowSeconds != nil {
manifestWindowSeconds := int32(*settings.ManifestWindowSeconds)
Expand All @@ -124,7 +125,7 @@ func buildDashPlaylistSettings(settings *dashPlaylistSettingsModel) *awsTypes.Da
return dashSettings
}

func buildHLSPlaylistSettings(settings *hlsPlaylistSettingsModel) *awsTypes.HlsPlaylistSettings {
func buildHLSPlaylistSettings(settings *models.HlsPlaylistSettingsModel) *awsTypes.HlsPlaylistSettings {
hlsSettings := &awsTypes.HlsPlaylistSettings{}

if len(settings.AdMarkupType) > 0 {
Expand Down Expand Up @@ -174,7 +175,7 @@ func stopChannel(state awsTypes.ChannelState, channelName *string, client *media
return nil
}

func handlePolicyUpdate(context context.Context, client *mediatailor.Client, plan channelModel) error {
func handlePolicyUpdate(context context.Context, client *mediatailor.Client, plan models.ChannelModel) error {
var normalizedOldPolicy jsontypes.Normalized

oldPolicy, err := client.GetChannelPolicy(context, &mediatailor.GetChannelPolicyInput{ChannelName: plan.Name})
Expand All @@ -195,7 +196,7 @@ func handlePolicyUpdate(context context.Context, client *mediatailor.Client, pla
return nil
}

func updatePolicy(model *channelModel, channelName *string, oldPolicy jsontypes.Normalized, newPolicy jsontypes.Normalized, client *mediatailor.Client) (channelModel, error) {
func updatePolicy(model *models.ChannelModel, channelName *string, oldPolicy jsontypes.Normalized, newPolicy jsontypes.Normalized, client *mediatailor.Client) (models.ChannelModel, error) {
if !reflect.DeepEqual(oldPolicy, newPolicy) {
if !newPolicy.IsNull() {
model.Policy = newPolicy
Expand All @@ -219,7 +220,7 @@ func updatePolicy(model *channelModel, channelName *string, oldPolicy jsontypes.

// Functions used to read MediaTailor resources to plan and state

func readChannelComputedValues(model channelModel, arn *string, channelName *string, creationTime *time.Time, lastModifiedTime *time.Time) channelModel {
func readChannelComputedValues(model models.ChannelModel, arn *string, channelName *string, creationTime *time.Time, lastModifiedTime *time.Time) models.ChannelModel {
model.ID = types.StringValue(*channelName)

if arn != nil {
Expand All @@ -239,9 +240,9 @@ func readChannelComputedValues(model channelModel, arn *string, channelName *str
return model
}

func readFillerSlate(plan channelModel, fillerSlate *awsTypes.SlateSource) channelModel {
func readFillerSlate(plan models.ChannelModel, fillerSlate *awsTypes.SlateSource) models.ChannelModel {
if fillerSlate != nil {
plan.FillerSlate = &fillerSlateModel{}
plan.FillerSlate = &models.FillerSlateModel{}
if fillerSlate.SourceLocationName != nil {
plan.FillerSlate.SourceLocationName = fillerSlate.SourceLocationName
}
Expand All @@ -252,15 +253,15 @@ func readFillerSlate(plan channelModel, fillerSlate *awsTypes.SlateSource) chann
return plan
}

func readOutputs(plan channelModel, responseOutputItems []awsTypes.ResponseOutputItem) channelModel {
func readOutputs(plan models.ChannelModel, responseOutputItems []awsTypes.ResponseOutputItem) models.ChannelModel {

if responseOutputItems == nil {
return plan
}

var tempOutputs []outputsModel
var tempOutputs []models.OutputsModel
for i, output := range responseOutputItems {
outputs := outputsModel{}
outputs := models.OutputsModel{}
if output.DashPlaylistSettings != nil {
outputs.DashPlaylistSettings = readDashPlaylistConfigurationsToPlan(&output)
}
Expand All @@ -281,7 +282,7 @@ func readOutputs(plan channelModel, responseOutputItems []awsTypes.ResponseOutpu
}

func readRMPS(manifestName *string, playbackUrl *string, sourceGroup *string) (*string, types.String, *string) {
outputs := outputsModel{}
outputs := models.OutputsModel{}
if manifestName != nil {
outputs.ManifestName = manifestName
}
Expand All @@ -294,8 +295,8 @@ func readRMPS(manifestName *string, playbackUrl *string, sourceGroup *string) (*
return outputs.ManifestName, outputs.PlaybackUrl, outputs.SourceGroup
}

func readDashPlaylistConfigurationsToPlan(output *awsTypes.ResponseOutputItem) *dashPlaylistSettingsModel {
outputs := &dashPlaylistSettingsModel{}
func readDashPlaylistConfigurationsToPlan(output *awsTypes.ResponseOutputItem) *models.DashPlaylistSettingsModel {
outputs := &models.DashPlaylistSettingsModel{}
if output.DashPlaylistSettings.ManifestWindowSeconds != nil {
manifestWindowSeconds := int64(*output.DashPlaylistSettings.ManifestWindowSeconds)
outputs.ManifestWindowSeconds = &manifestWindowSeconds
Expand All @@ -315,8 +316,8 @@ func readDashPlaylistConfigurationsToPlan(output *awsTypes.ResponseOutputItem) *
return outputs
}

func readHlsPlaylistConfigurationsToPlan(output *awsTypes.ResponseOutputItem, stateOutput outputsModel) *hlsPlaylistSettingsModel {
outputs := &hlsPlaylistSettingsModel{}
func readHlsPlaylistConfigurationsToPlan(output *awsTypes.ResponseOutputItem, stateOutput models.OutputsModel) *models.HlsPlaylistSettingsModel {
outputs := &models.HlsPlaylistSettingsModel{}
if stateOutput.HlsPlaylistSettings.AdMarkupType != nil && output.HlsPlaylistSettings.AdMarkupType != nil && len(output.HlsPlaylistSettings.AdMarkupType) > 0 {
var adMarkupTypes []*string
for _, a := range output.HlsPlaylistSettings.AdMarkupType {
Expand All @@ -332,8 +333,8 @@ func readHlsPlaylistConfigurationsToPlan(output *awsTypes.ResponseOutputItem, st
return outputs
}

func readHlsPlaylistConfigurationsToPlanDS(output *awsTypes.ResponseOutputItem) *hlsPlaylistSettingsModel {
outputs := &hlsPlaylistSettingsModel{}
func readHlsPlaylistConfigurationsToPlanDS(output *awsTypes.ResponseOutputItem) *models.HlsPlaylistSettingsModel {
outputs := &models.HlsPlaylistSettingsModel{}
if len(output.HlsPlaylistSettings.AdMarkupType) > 0 {
var adMarkupTypes []*string
for _, a := range output.HlsPlaylistSettings.AdMarkupType {
Expand All @@ -349,7 +350,7 @@ func readHlsPlaylistConfigurationsToPlanDS(output *awsTypes.ResponseOutputItem)
return outputs
}

func readOptionalValues(plan channelModel, playbackMode *string, tags map[string]string, tier *string) channelModel {
func readOptionalValues(plan models.ChannelModel, playbackMode *string, tags map[string]string, tier *string) models.ChannelModel {
if playbackMode != nil {
plan.PlaybackMode = playbackMode
}
Expand All @@ -364,7 +365,7 @@ func readOptionalValues(plan channelModel, playbackMode *string, tags map[string
return plan
}

func writeChannelToPlan(model channelModel, channel mediatailor.CreateChannelOutput) channelModel {
func writeChannelToPlan(model models.ChannelModel, channel mediatailor.CreateChannelOutput) models.ChannelModel {

model = readChannelComputedValues(model, channel.Arn, channel.ChannelName, channel.CreationTime, channel.LastModifiedTime)

Expand All @@ -377,7 +378,7 @@ func writeChannelToPlan(model channelModel, channel mediatailor.CreateChannelOut
return model
}

func writeChannelToState(model channelModel, channel mediatailor.DescribeChannelOutput) channelModel {
func writeChannelToState(model models.ChannelModel, channel mediatailor.DescribeChannelOutput) models.ChannelModel {

model = readChannelComputedValues(model, channel.Arn, channel.ChannelName, channel.CreationTime, channel.LastModifiedTime)

Expand Down
9 changes: 5 additions & 4 deletions awsmt/helpers_live_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"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/types"
"terraform-provider-mediatailor/awsmt/models"
)

func getCreateLiveSourceInput(model liveSourceModel) *mediatailor.CreateLiveSourceInput {
func getCreateLiveSourceInput(model models.LiveSourceModel) *mediatailor.CreateLiveSourceInput {
var input mediatailor.CreateLiveSourceInput

input.HttpPackageConfigurations, input.LiveSourceName, input.SourceLocationName = getSharedLiveSourceInput(&model)
Expand All @@ -18,15 +19,15 @@ func getCreateLiveSourceInput(model liveSourceModel) *mediatailor.CreateLiveSour
return &input
}

func getUpdateLiveSourceInput(model liveSourceModel) mediatailor.UpdateLiveSourceInput {
func getUpdateLiveSourceInput(model models.LiveSourceModel) mediatailor.UpdateLiveSourceInput {
var input mediatailor.UpdateLiveSourceInput

input.HttpPackageConfigurations, input.LiveSourceName, input.SourceLocationName = getSharedLiveSourceInput(&model)

return input
}

func getSharedLiveSourceInput(model *liveSourceModel) ([]awsTypes.HttpPackageConfiguration, *string, *string) {
func getSharedLiveSourceInput(model *models.LiveSourceModel) ([]awsTypes.HttpPackageConfiguration, *string, *string) {
var httpPackageConfigurations []awsTypes.HttpPackageConfiguration
var liveSourceName *string
var sourceLocationName *string
Expand All @@ -46,7 +47,7 @@ func getSharedLiveSourceInput(model *liveSourceModel) ([]awsTypes.HttpPackageCon
}

// readLiveSource is used for both plan and state since the output from create/update and describe is compatible
func readLiveSource(model liveSourceModel, liveSource mediatailor.CreateLiveSourceOutput) liveSourceModel {
func readLiveSource(model models.LiveSourceModel, liveSource mediatailor.CreateLiveSourceOutput) models.LiveSourceModel {
liveSourceName := *liveSource.LiveSourceName
sourceLocationName := *liveSource.SourceLocationName
idNames := sourceLocationName + "," + liveSourceName
Expand Down
9 changes: 5 additions & 4 deletions awsmt/helpers_live_vod_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package awsmt
import (
"github.com/aws/aws-sdk-go-v2/aws"
awsTypes "github.com/aws/aws-sdk-go-v2/service/mediatailor/types"
"terraform-provider-mediatailor/awsmt/models"
)

func readHttpPackageConfigurations(configurations []awsTypes.HttpPackageConfiguration) []httpPackageConfigurationsModel {
var httpPackageConfiguration []httpPackageConfigurationsModel
func readHttpPackageConfigurations(configurations []awsTypes.HttpPackageConfiguration) []models.HttpPackageConfigurationsModel {
var httpPackageConfiguration []models.HttpPackageConfigurationsModel
if len(configurations) > 0 {
for _, c := range configurations {
httpPackageConfiguration = append(httpPackageConfiguration, httpPackageConfigurationsModel{
httpPackageConfiguration = append(httpPackageConfiguration, models.HttpPackageConfigurationsModel{
Path: c.Path,
SourceGroup: c.SourceGroup,
Type: aws.String(string(c.Type)),
Expand All @@ -19,7 +20,7 @@ func readHttpPackageConfigurations(configurations []awsTypes.HttpPackageConfigur
return httpPackageConfiguration
}

func getHttpPackageConfigurations(plan []httpPackageConfigurationsModel) []awsTypes.HttpPackageConfiguration {
func getHttpPackageConfigurations(plan []models.HttpPackageConfigurationsModel) []awsTypes.HttpPackageConfiguration {
var tmp []awsTypes.HttpPackageConfiguration
if len(plan) > 0 {
for _, c := range plan {
Expand Down
Loading

0 comments on commit 750972d

Please sign in to comment.