Skip to content

Commit

Permalink
Merge pull request #58 from spring-media/feat/source-location
Browse files Browse the repository at this point in the history
Feat/source location
  • Loading branch information
thatsddr authored May 27, 2022
2 parents c960e08 + 96840ad commit 05dbf3d
Show file tree
Hide file tree
Showing 17 changed files with 896 additions and 99 deletions.
90 changes: 86 additions & 4 deletions awsmt/channel_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ func getCreateChannelInput(d *schema.ResourceData) mediatailor.CreateChannelInpu
params.ChannelName = aws.String(v.(string))
}

params.FillerSlate = getFillerSlate(d)
if f := getFillerSlate(d); f != nil {
params.FillerSlate = f
}

params.Outputs = getOutputs(d)
if o := getOutputs(d); o != nil {
params.Outputs = o
}

if v, ok := d.GetOk("playback_mode"); ok {
params.PlaybackMode = aws.String(v.(string))
Expand Down Expand Up @@ -63,7 +67,9 @@ func getUpdateChannelInput(d *schema.ResourceData) mediatailor.UpdateChannelInpu

params.FillerSlate = getFillerSlate(d)

params.Outputs = getOutputs(d)
if o := getOutputs(d); o != nil {
params.Outputs = o
}

return params
}
Expand Down Expand Up @@ -118,7 +124,7 @@ func setOutputs(values *mediatailor.DescribeChannelOutput, d *schema.ResourceDat
return nil
}

func returnChannel(res *mediatailor.DescribeChannelOutput, d *schema.ResourceData) error {
func setChannel(res *mediatailor.DescribeChannelOutput, d *schema.ResourceData) error {
var errors []error

errors = append(errors, d.Set("arn", res.Arn))
Expand All @@ -139,3 +145,79 @@ func returnChannel(res *mediatailor.DescribeChannelOutput, d *schema.ResourceDat
}
return nil
}

func getOutputs(d *schema.ResourceData) []*mediatailor.RequestOutputItem {
if v, ok := d.GetOk("outputs"); ok && v.([]interface{})[0] != nil {
outputs := v.([]interface{})

var res []*mediatailor.RequestOutputItem

for _, output := range outputs {
current := output.(map[string]interface{})
temp := mediatailor.RequestOutputItem{}

if str, ok := current["manifest_name"]; ok {
temp.ManifestName = aws.String(str.(string))
}
if str, ok := current["source_group"]; ok {
temp.SourceGroup = aws.String(str.(string))
}

if num, ok := current["hls_manifest_windows_seconds"]; ok && num.(int) != 0 {
tempHls := mediatailor.HlsPlaylistSettings{}
tempHls.ManifestWindowSeconds = aws.Int64(int64(num.(int)))
temp.HlsPlaylistSettings = &tempHls
}

tempDash := mediatailor.DashPlaylistSettings{}
if num, ok := current["dash_manifest_windows_seconds"]; ok && num.(int) != 0 {
tempDash.ManifestWindowSeconds = aws.Int64(int64(num.(int)))
}
if num, ok := current["dash_min_buffer_time_seconds"]; ok && num.(int) != 0 {
tempDash.MinBufferTimeSeconds = aws.Int64(int64(num.(int)))
}
if num, ok := current["dash_min_update_period_seconds"]; ok && num.(int) != 0 {
tempDash.MinUpdatePeriodSeconds = aws.Int64(int64(num.(int)))
}
if num, ok := current["dash_suggested_presentation_delay_seconds"]; ok && num.(int) != 0 {
tempDash.SuggestedPresentationDelaySeconds = aws.Int64(int64(num.(int)))
}
if tempDash != (mediatailor.DashPlaylistSettings{}) {
temp.DashPlaylistSettings = &tempDash
}

res = append(res, &temp)
}
return res
}
return nil
}

func updateTags(client *mediatailor.MediaTailor, arn *string, oldTagValue, newTagValue interface{}) error {

var removedTags []string
for k := range oldTagValue.(map[string]interface{}) {
if _, ok := (newTagValue.(map[string]interface{}))[k]; !ok {
removedTags = append(removedTags, k)
}
}

err := deleteTags(client, aws.StringValue(arn), removedTags)
if err != nil {
return fmt.Errorf("%w", err)
}

if newTagValue != nil {
var newTags = make(map[string]*string)
for k, v := range newTagValue.(map[string]interface{}) {
val := v.(string)
newTags[k] = &val
}
tagInput := mediatailor.TagResourceInput{ResourceArn: arn, Tags: newTags}
_, err := client.TagResource(&tagInput)
if err != nil {
return fmt.Errorf("%w", err)
}
}
return nil
}
2 changes: 1 addition & 1 deletion awsmt/data_source_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func dataSourceChannelRead(_ context.Context, d *schema.ResourceData, m interfac

d.SetId(aws.StringValue(res.ChannelName))

err = returnChannel(res, d)
err = setChannel(res, d)
if err != nil {
diag.FromErr(err)
}
Expand Down
2 changes: 1 addition & 1 deletion awsmt/data_source_channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestAccChannelDataSourceBasic(t *testing.T) {
dataSourceName := "awsmt_channel.test"
dataSourceName := "data.awsmt_channel.test"
rName := "basic-channel"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down
75 changes: 75 additions & 0 deletions awsmt/data_source_source_location.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package awsmt

import (
"context"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/mediatailor"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceSourceLocation() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceSourceLocationRead,
Schema: map[string]*schema.Schema{
"access_configuration": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"access_type": &computedString,
// SMATC is short for Secret Manager Access Token Configuration
"smatc_header_name": &computedString,
"smatc_secret_arn": &computedString,
"smatc_secret_string_key": &computedString,
},
},
},
"arn": &computedString,
"creation_time": &computedString,
"default_segment_delivery_configuration_url": &computedString,
"http_configuration_url": &computedString,
"last_modified_time": &computedString,
"segment_delivery_configurations": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"base_url": &computedString,
"name": &computedString,
},
},
},
"source_location_name": &requiredString,
"tags": {
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}

func dataSourceSourceLocationRead(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*mediatailor.MediaTailor)

name := d.Get("source_location_name").(string)
if name == "" {
return diag.Errorf("`source_location_name` parameter required")
}
res, err := client.DescribeSourceLocation(&mediatailor.DescribeSourceLocationInput{SourceLocationName: &name})
if err != nil {
return diag.FromErr(fmt.Errorf("error while retrieving the source location: %w", err))
}

d.SetId(aws.StringValue(res.SourceLocationName))

err = setSourceLocation(res, d)
if err != nil {
diag.FromErr(err)
}
return nil
}
49 changes: 49 additions & 0 deletions awsmt/data_source_source_location_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package awsmt

import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"regexp"
"testing"
)

func TestAccSourceLocationDataSourceBasic(t *testing.T) {
dataSourceName := "data.awsmt_source_location.test"
rName := "basic_source_location"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccSourceLocationDataSourceBasic(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr(dataSourceName, "arn", regexp.MustCompile(`^arn:aws:mediatailor:[\w-]+:\d+:sourceLocation\/.*$`)),
resource.TestMatchResourceAttr(dataSourceName, "creation_time", regexp.MustCompile(`^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} \+\d{4} \w+$`)),
resource.TestMatchResourceAttr(dataSourceName, "last_modified_time", regexp.MustCompile(`^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} \+\d{4} \w+$`)),
resource.TestCheckResourceAttr(dataSourceName, "source_location_name", rName),
),
},
},
})
}

func testAccSourceLocationDataSourceBasic(rName string) string {
return fmt.Sprintf(`
resource "awsmt_source_location" "test_data_source"{
access_configuration {
access_type = "S3_SIGV4"
}
default_segment_delivery_configuration_url = "https://www.example.com"
http_configuration_url = "https://ott-mediatailor-test.s3.eu-central-1.amazonaws.com/test-img.jpeg"
source_location_name = "%[1]s"
segment_delivery_configurations {
base_url = "https://ott-mediatailor-test.s3.eu-central-1.amazonaws.com/test-img.jpeg"
name = "example"
}
}
data "awsmt_source_location" "test" {
source_location_name = awsmt_source_location.test_data_source.source_location_name
}
`, rName)
}
2 changes: 2 additions & 0 deletions awsmt/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ func Provider() *schema.Provider {
ResourcesMap: map[string]*schema.Resource{
"awsmt_playback_configuration": resourcePlaybackConfiguration(),
"awsmt_channel": resourceChannel(),
"awsmt_source_location": resourceSourceLocation(),
},
DataSourcesMap: map[string]*schema.Resource{
"awsmt_playback_configuration": dataSourcePlaybackConfiguration(),
"awsmt_channel": dataSourceChannel(),
"awsmt_source_location": dataSourceSourceLocation(),
},
ConfigureContextFunc: providerConfigure,
}
Expand Down
74 changes: 5 additions & 69 deletions awsmt/resource_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func resourceChannelRead(_ context.Context, d *schema.ResourceData, meta interfa
return diag.FromErr(fmt.Errorf("error while retrieving the channel: %v", err))
}

err = returnChannel(res, d)
err = setChannel(res, d)
if err != nil {
diag.FromErr(err)
}
Expand All @@ -174,39 +174,22 @@ func resourceChannelUpdate(ctx context.Context, d *schema.ResourceData, meta int

if d.HasChange("tags") {
oldValue, newValue := d.GetChange("tags")
var removedTags []string
for k := range oldValue.(map[string]interface{}) {
if _, ok := (newValue.(map[string]interface{}))[k]; !ok {
removedTags = append(removedTags, k)
}
}

resourceName := d.Get("channel_name").(string)
res, err := client.DescribeChannel(&mediatailor.DescribeChannelInput{ChannelName: &resourceName})
if err != nil {
return diag.FromErr(err)
}
err = deleteTags(client, aws.StringValue(res.Arn), removedTags)
if err != nil {

if err := updateTags(client, res.Arn, oldValue, newValue); err != nil {
return diag.FromErr(err)
}
if newValue != nil {
var newTags = make(map[string]*string)
for k, v := range newValue.(map[string]interface{}) {
val := v.(string)
newTags[k] = &val
}
tagInput := mediatailor.TagResourceInput{ResourceArn: res.Arn, Tags: newTags}
_, err := client.TagResource(&tagInput)
if err != nil {
return diag.FromErr(err)
}
}
}

var params = getUpdateChannelInput(d)
channel, err := client.UpdateChannel(&params)
if err != nil {
return diag.FromErr(fmt.Errorf("error while creating the channel: %v", err))
return diag.FromErr(fmt.Errorf("error while updating the channel: %v", err))
}
d.SetId(aws.StringValue(channel.Arn))

Expand All @@ -223,50 +206,3 @@ func resourceChannelDelete(_ context.Context, d *schema.ResourceData, meta inter

return nil
}

func getOutputs(d *schema.ResourceData) []*mediatailor.RequestOutputItem {
if v, ok := d.GetOk("outputs"); ok && v.([]interface{})[0] != nil {
outputs := v.([]interface{})

var res []*mediatailor.RequestOutputItem

for _, output := range outputs {
current := output.(map[string]interface{})
temp := mediatailor.RequestOutputItem{}

if str, ok := current["manifest_name"]; ok {
temp.ManifestName = aws.String(str.(string))
}
if str, ok := current["source_group"]; ok {
temp.SourceGroup = aws.String(str.(string))
}

if num, ok := current["hls_manifest_windows_seconds"]; ok && num.(int) != 0 {
tempHls := mediatailor.HlsPlaylistSettings{}
tempHls.ManifestWindowSeconds = aws.Int64(int64(num.(int)))
temp.HlsPlaylistSettings = &tempHls
}

tempDash := mediatailor.DashPlaylistSettings{}
if num, ok := current["dash_manifest_windows_seconds"]; ok && num.(int) != 0 {
tempDash.ManifestWindowSeconds = aws.Int64(int64(num.(int)))
}
if num, ok := current["dash_min_buffer_time_seconds"]; ok && num.(int) != 0 {
tempDash.MinBufferTimeSeconds = aws.Int64(int64(num.(int)))
}
if num, ok := current["dash_min_update_period_seconds"]; ok && num.(int) != 0 {
tempDash.MinUpdatePeriodSeconds = aws.Int64(int64(num.(int)))
}
if num, ok := current["dash_suggested_presentation_delay_seconds"]; ok && num.(int) != 0 {
tempDash.SuggestedPresentationDelaySeconds = aws.Int64(int64(num.(int)))
}
if tempDash != (mediatailor.DashPlaylistSettings{}) {
temp.DashPlaylistSettings = &tempDash
}

res = append(res, &temp)
}
return res
}
return nil
}
Loading

0 comments on commit 05dbf3d

Please sign in to comment.