Skip to content

Commit

Permalink
feat: support for vod source ad break opportunities offset milliseconds
Browse files Browse the repository at this point in the history
  • Loading branch information
isadoral committed Nov 14, 2023
1 parent b71e6f5 commit b70dc7c
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 17 deletions.
34 changes: 31 additions & 3 deletions awsmt/data_source_vod_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package awsmt

import (
"context"

"github.com/aws/aws-sdk-go/service/mediatailor"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)

var (
Expand All @@ -25,7 +27,33 @@ func (d *dataSourceVodSource) Metadata(_ context.Context, req datasource.Metadat
}

func (d *dataSourceVodSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = buildDatasourceSchema()
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": computedString,
"arn": computedString,
"creation_time": computedString,
"http_package_configurations": schema.ListNestedAttribute{
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"path": computedString,
"source_group": computedString,
"type": schema.StringAttribute{
Computed: true,
Validators: []validator.String{
stringvalidator.OneOf("HLS", "DASH"),
},
},
},
},
},
"last_modified_time": computedString,
"name": requiredString,
"source_location_name": requiredString,
"tags": computedMap,
"ad_break_opportunities_offset_millis": computedMap,
},
}
}

func (d *dataSourceVodSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) {
Expand Down Expand Up @@ -53,7 +81,7 @@ func (d *dataSourceVodSource) Read(ctx context.Context, req datasource.ReadReque
return
}

data = readVodSourceToPlan(data, mediatailor.CreateVodSourceOutput(*vodSource))
data = readVodSourceToState(ctx, data, *vodSource)

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
52 changes: 52 additions & 0 deletions awsmt/helpers_vod_source.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package awsmt

import (
"context"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/mediatailor"
"github.com/hashicorp/terraform-plugin-framework/types"
Expand Down Expand Up @@ -63,6 +64,57 @@ func readVodSourceToPlan(plan vodSourceModel, vodSource mediatailor.CreateVodSou
return plan
}

func readVodSourceToState(ctx context.Context, plan vodSourceModel, vodSource mediatailor.DescribeVodSourceOutput) vodSourceModel {
vodSourceName := *vodSource.VodSourceName
sourceLocationName := *vodSource.SourceLocationName
idNames := sourceLocationName + "," + vodSourceName

plan.ID = types.StringValue(idNames)

if vodSource.AdBreakOpportunities != nil {
for _, value := range vodSource.AdBreakOpportunities {
plan.AdBreakOpportunitiesOffsetMillis = append(plan.AdBreakOpportunitiesOffsetMillis, value.OffsetMillis)
}
}

if vodSource.Arn != nil {
plan.Arn = types.StringValue(*vodSource.Arn)
}

if vodSource.CreationTime != nil {
plan.CreationTime = types.StringValue((aws.TimeValue(vodSource.CreationTime)).String())
}

if vodSource.HttpPackageConfigurations != nil && len(vodSource.HttpPackageConfigurations) > 0 {
plan.HttpPackageConfigurations = []httpPackageConfigurationsModel{}
for _, httpPackageConfiguration := range vodSource.HttpPackageConfigurations {
httpPackageConfigurations := httpPackageConfigurationsModel{}
httpPackageConfigurations.Path = httpPackageConfiguration.Path
httpPackageConfigurations.SourceGroup = httpPackageConfiguration.SourceGroup
httpPackageConfigurations.Type = httpPackageConfiguration.Type
plan.HttpPackageConfigurations = append(plan.HttpPackageConfigurations, httpPackageConfigurations)
}
}

if vodSource.LastModifiedTime != nil {
plan.LastModifiedTime = types.StringValue((aws.TimeValue(vodSource.LastModifiedTime)).String())
}

if vodSource.VodSourceName != nil {
plan.Name = vodSource.VodSourceName
}

if vodSource.SourceLocationName != nil {
plan.SourceLocationName = vodSource.SourceLocationName
}

if len(vodSource.Tags) > 0 {
plan.Tags = vodSource.Tags
}

return plan
}

func vodSourceUpdateInput(plan vodSourceModel) mediatailor.UpdateVodSourceInput {
var input mediatailor.UpdateVodSourceInput

Expand Down
42 changes: 37 additions & 5 deletions awsmt/resource_vod_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ package awsmt
import (
"context"
"fmt"
"reflect"
"strings"

"github.com/aws/aws-sdk-go/service/mediatailor"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"reflect"
"strings"
)

var (
Expand All @@ -30,7 +33,36 @@ func (r *resourceVodSource) Metadata(_ context.Context, req resource.MetadataReq
}

func (r *resourceVodSource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = buildResourceSchema()
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": computedString,
"arn": computedString,
"creation_time": computedString,
"http_package_configurations": schema.ListNestedAttribute{
Required: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"path": requiredString,
"source_group": requiredString,
"type": schema.StringAttribute{
Required: true,
Validators: []validator.String{
stringvalidator.OneOf("HLS", "DASH"),
},
},
},
},
},
"last_modified_time": computedString,
"source_location_name": requiredString,
"tags": optionalMap,
"name": requiredString,
"ad_break_opportunities_offset_millis": schema.ListAttribute{
Optional: true,
ElementType: types.Int64Type,
},
},
}
}

func (r *resourceVodSource) Configure(_ context.Context, req resource.ConfigureRequest, _ *resource.ConfigureResponse) {
Expand Down Expand Up @@ -94,7 +126,7 @@ func (r *resourceVodSource) Read(ctx context.Context, req resource.ReadRequest,
return
}

state = readVodSourceToPlan(state, mediatailor.CreateVodSourceOutput(*vodSource))
state = readVodSourceToState(ctx, state, *vodSource)

diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
Expand Down
21 changes: 12 additions & 9 deletions awsmt/vod_source_structs.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package awsmt

import "github.com/hashicorp/terraform-plugin-framework/types"
import (
"github.com/hashicorp/terraform-plugin-framework/types"
)

type vodSourceModel struct {
ID types.String `tfsdk:"id"`
Arn types.String `tfsdk:"arn"`
CreationTime types.String `tfsdk:"creation_time"`
HttpPackageConfigurations []httpPackageConfigurationsModel `tfsdk:"http_package_configurations"`
LastModifiedTime types.String `tfsdk:"last_modified_time"`
SourceLocationName *string `tfsdk:"source_location_name"`
Tags map[string]*string `tfsdk:"tags"`
Name *string `tfsdk:"name"`
ID types.String `tfsdk:"id"`
Arn types.String `tfsdk:"arn"`
CreationTime types.String `tfsdk:"creation_time"`
HttpPackageConfigurations []httpPackageConfigurationsModel `tfsdk:"http_package_configurations"`
LastModifiedTime types.String `tfsdk:"last_modified_time"`
SourceLocationName *string `tfsdk:"source_location_name"`
Tags map[string]*string `tfsdk:"tags"`
Name *string `tfsdk:"name"`
AdBreakOpportunitiesOffsetMillis []*int64 `tfsdk:"ad_break_opportunities_offset_millis"`
}

0 comments on commit b70dc7c

Please sign in to comment.