Skip to content

Commit

Permalink
Merge pull request #62 from spring-media/feat/channel-policy
Browse files Browse the repository at this point in the history
channel policy and renamings
  • Loading branch information
thatsddr authored Jun 13, 2022
2 parents f655f7d + fa0c990 commit 35898e5
Show file tree
Hide file tree
Showing 24 changed files with 281 additions and 127 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ jobs:
go-version: ${{ env.GO_VERSION }}
- name: Unit & Acceptance Test
run: make test
env:
AWS_ACCOUNT_ID: "319158032161"
AWS_REGION: "eu-central-1"
- name: SonarCloud Scan
uses: SonarSource/sonarcloud-github-action@master
env:
Expand Down
65 changes: 62 additions & 3 deletions awsmt/channel_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package awsmt
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/mediatailor"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"strings"
)

func getFillerSlate(d *schema.ResourceData) *mediatailor.SlateSource {
Expand All @@ -25,7 +27,7 @@ func getFillerSlate(d *schema.ResourceData) *mediatailor.SlateSource {
func getCreateChannelInput(d *schema.ResourceData) mediatailor.CreateChannelInput {
var params mediatailor.CreateChannelInput

if v, ok := d.GetOk("channel_name"); ok {
if v, ok := d.GetOk("name"); ok {
params.ChannelName = aws.String(v.(string))
}

Expand Down Expand Up @@ -61,7 +63,7 @@ func getCreateChannelInput(d *schema.ResourceData) mediatailor.CreateChannelInpu
func getUpdateChannelInput(d *schema.ResourceData) mediatailor.UpdateChannelInput {
var params mediatailor.UpdateChannelInput

if v, ok := d.GetOk("channel_name"); ok {
if v, ok := d.GetOk("name"); ok {
params.ChannelName = aws.String(v.(string))
}

Expand All @@ -74,6 +76,54 @@ func getUpdateChannelInput(d *schema.ResourceData) mediatailor.UpdateChannelInpu
return params
}

func createChannelPolicy(client *mediatailor.MediaTailor, d *schema.ResourceData) error {
if v, ok := d.GetOk("policy"); ok {
var putChannelPolicyParams = mediatailor.PutChannelPolicyInput{
ChannelName: aws.String((d.Get("name")).(string)),
Policy: aws.String(v.(string)),
}

_, err := client.PutChannelPolicy(&putChannelPolicyParams)
if err != nil {
return fmt.Errorf("error while creating the policy: %v", err)
}
}
return nil
}

func updateChannelPolicy(client *mediatailor.MediaTailor, d *schema.ResourceData, channelName *string) error {
_, err := client.PutChannelPolicy(&mediatailor.PutChannelPolicyInput{ChannelName: channelName, Policy: aws.String(d.Get("policy").(string))})

if err != nil && !strings.Contains(err.Error(), "NotFound") {
return fmt.Errorf("error while getting the channel policy: %v", err)
}
return nil
}

func deleteChannelPolicy(client *mediatailor.MediaTailor, d *schema.ResourceData, channelName *string) error {
_, err := client.DeleteChannelPolicy(&mediatailor.DeleteChannelPolicyInput{ChannelName: channelName})
if err != nil {
return fmt.Errorf("error while deleting the policy: %v", err)
}
if err := d.Set("policy", ""); err != nil {
return fmt.Errorf("error while unsetting the policy: %v", err)
}
return nil
}

func getResourceName(d *schema.ResourceData, fieldName string) (*string, error) {
resourceName := d.Get(fieldName).(string)
if len(resourceName) == 0 && len(d.Id()) > 0 {
resourceArn, err := arn.Parse(d.Id())
if err != nil {
return nil, fmt.Errorf("error parsing the name from resource arn: %v", err)
}
arnSections := strings.Split(resourceArn.Resource, "/")
resourceName = arnSections[len(arnSections)-1]
}
return &resourceName, nil
}

func setFillerState(values *mediatailor.DescribeChannelOutput, d *schema.ResourceData) error {
if values.FillerSlate != nil && values.FillerSlate != &(mediatailor.SlateSource{}) {
temp := map[string]interface{}{}
Expand Down Expand Up @@ -128,7 +178,7 @@ func setChannel(res *mediatailor.DescribeChannelOutput, d *schema.ResourceData)
var errors []error

errors = append(errors, d.Set("arn", res.Arn))
errors = append(errors, d.Set("channel_name", res.ChannelName))
errors = append(errors, d.Set("name", res.ChannelName))
errors = append(errors, d.Set("channel_state", res.ChannelState))
errors = append(errors, d.Set("creation_time", res.CreationTime.String()))
errors = append(errors, setFillerState(res, d))
Expand All @@ -146,6 +196,15 @@ func setChannel(res *mediatailor.DescribeChannelOutput, d *schema.ResourceData)
return nil
}

func setChannelPolicy(res *mediatailor.GetChannelPolicyOutput, d *schema.ResourceData) error {
if res.Policy != nil {
if err := d.Set("policy", res.Policy); err != nil {
return fmt.Errorf("error while setting the the channel policy: %v", err)
}
}
return nil
}

func getOutputs(d *schema.ResourceData) []*mediatailor.RequestOutputItem {
if v, ok := d.GetOk("outputs"); ok && v.([]interface{})[0] != nil {
outputs := v.([]interface{})
Expand Down
17 changes: 12 additions & 5 deletions awsmt/data_source_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import (
"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"
"strings"
)

func dataSourceChannel() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceChannelRead,
Schema: map[string]*schema.Schema{
"arn": &computedString,
"channel_name": &requiredString,
"name": &requiredString,
"channel_state": &computedString,
"creation_time": &computedString,
"filler_slate": {
Expand Down Expand Up @@ -46,6 +47,7 @@ func dataSourceChannel() *schema.Resource {
},
},
"playback_mode": &computedString,
"policy": &computedString,
"tags": {
Type: schema.TypeMap,
Optional: true,
Expand All @@ -61,15 +63,20 @@ func dataSourceChannel() *schema.Resource {
func dataSourceChannelRead(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*mediatailor.MediaTailor)

name := d.Get("channel_name").(string)
if name == "" {
return diag.Errorf("`channel_name` parameter required")
}
name := d.Get("name").(string)
res, err := client.DescribeChannel(&mediatailor.DescribeChannelInput{ChannelName: &name})
if err != nil {
return diag.FromErr(fmt.Errorf("error while retrieving the channel: %w", err))
}

policy, err := client.GetChannelPolicy(&mediatailor.GetChannelPolicyInput{ChannelName: aws.String(name)})
if err != nil && !strings.Contains(err.Error(), "NotFound") {
return diag.FromErr(fmt.Errorf("error while getting the channel policy: %v", err))
}
if err := setChannelPolicy(policy, d); err != nil {
diag.FromErr(err)
}

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

err = setChannel(res, d)
Expand Down
6 changes: 3 additions & 3 deletions awsmt/data_source_channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestAccChannelDataSourceBasic(t *testing.T) {
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, "outputs.0.hls_manifest_windows_seconds", "30"),
resource.TestCheckResourceAttr(dataSourceName, "channel_name", rName),
resource.TestCheckResourceAttr(dataSourceName, "name", rName),
),
},
},
Expand All @@ -31,7 +31,7 @@ func TestAccChannelDataSourceBasic(t *testing.T) {
func testAccChannelDataSourceBasic(rName string) string {
return fmt.Sprintf(`
resource "awsmt_channel" "test" {
channel_name = "%[1]s"
name = "%[1]s"
outputs {
manifest_name = "default"
source_group = "default"
Expand All @@ -42,7 +42,7 @@ resource "awsmt_channel" "test" {
}
data "awsmt_channel" "test" {
channel_name = awsmt_channel.test.channel_name
name = awsmt_channel.test.name
}
`, rName)
}
6 changes: 3 additions & 3 deletions awsmt/data_source_live_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestAccLiveSourceDataSourceBasic(t *testing.T) {
func testAccLiveSourceDataSourceBasic(sourceLocationName, liveSourceName string) string {
return fmt.Sprintf(`
resource "awsmt_source_location" "example"{
source_location_name = "%[1]s"
name = "%[1]s"
http_configuration_url = "https://ott-mediatailor-test.s3.eu-central-1.amazonaws.com/test-img.jpeg"
}
Expand All @@ -42,12 +42,12 @@ resource "awsmt_live_source" "test" {
source_group = "default"
type = "HLS"
}
source_location_name = awsmt_source_location.example.source_location_name
source_location_name = awsmt_source_location.example.name
name = "%[2]s"
}
data "awsmt_live_source" "test" {
source_location_name = awsmt_source_location.example.source_location_name
source_location_name = awsmt_source_location.example.name
name = awsmt_live_source.test.name
}
`, sourceLocationName, liveSourceName)
Expand Down
6 changes: 3 additions & 3 deletions awsmt/data_source_source_location.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func dataSourceSourceLocation() *schema.Resource {
},
},
},
"source_location_name": &requiredString,
"name": &requiredString,
"tags": {
Type: schema.TypeMap,
Computed: true,
Expand All @@ -43,9 +43,9 @@ func dataSourceSourceLocation() *schema.Resource {
func dataSourceSourceLocationRead(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*mediatailor.MediaTailor)

name := d.Get("source_location_name").(string)
name := d.Get("name").(string)
if name == "" {
return diag.Errorf("`source_location_name` parameter required")
return diag.Errorf("`name` parameter required")
}
res, err := client.DescribeSourceLocation(&mediatailor.DescribeSourceLocationInput{SourceLocationName: &name})
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions awsmt/data_source_source_location_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestAccSourceLocationDataSourceBasic(t *testing.T) {
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),
resource.TestCheckResourceAttr(dataSourceName, "name", rName),
),
},
},
Expand All @@ -32,15 +32,15 @@ func testAccSourceLocationDataSourceBasic(rName string) string {
resource "awsmt_source_location" "test_data_source"{
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"
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
name = awsmt_source_location.test_data_source.name
}
`, rName)
}
4 changes: 2 additions & 2 deletions awsmt/data_source_vod_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ func dataSourceVodSource() *schema.Resource {
Type: schema.TypeString,
},
},
"vod_source_name": &requiredString,
"name": &requiredString,
},
}
}

func dataSourceVodSourceRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*mediatailor.MediaTailor)
resourceName := d.Get("vod_source_name").(string)
resourceName := d.Get("name").(string)
sourceLocationName := d.Get("source_location_name").(string)

input := &mediatailor.DescribeVodSourceInput{SourceLocationName: &(sourceLocationName), VodSourceName: aws.String(resourceName)}
Expand Down
14 changes: 7 additions & 7 deletions awsmt/data_source_vod_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func TestAccVodSourceDataSourceBasic(t *testing.T) {
dataSourceName := "data.awsmt_vod_source.test"
sourceLocationName := "basic_source_location"
sourceLocationName := "vod_basic_sl"
vodSourceName := "vod_source_data_source_test"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -22,7 +22,7 @@ func TestAccVodSourceDataSourceBasic(t *testing.T) {
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", sourceLocationName),
resource.TestCheckResourceAttr(dataSourceName, "vod_source_name", vodSourceName),
resource.TestCheckResourceAttr(dataSourceName, "name", vodSourceName),
),
},
},
Expand All @@ -32,7 +32,7 @@ func TestAccVodSourceDataSourceBasic(t *testing.T) {
func testAccVodSourceDataSourceBasic(sourceLocationName, vodSourceName string) string {
return fmt.Sprintf(`
resource "awsmt_source_location" "example"{
source_location_name = "%[1]s"
name = "%[1]s"
http_configuration_url = "https://ott-mediatailor-test.s3.eu-central-1.amazonaws.com/test-img.jpeg"
}
Expand All @@ -42,13 +42,13 @@ resource "awsmt_vod_source" "test" {
source_group = "default"
type = "HLS"
}
source_location_name = awsmt_source_location.example.source_location_name
vod_source_name = "%[2]s"
source_location_name = awsmt_source_location.example.name
name = "%[2]s"
}
data "awsmt_vod_source" "test" {
source_location_name = awsmt_source_location.example.source_location_name
vod_source_name = awsmt_vod_source.test.vod_source_name
source_location_name = awsmt_source_location.example.name
name = awsmt_vod_source.test.name
}
`, sourceLocationName, vodSourceName)
}
Loading

0 comments on commit 35898e5

Please sign in to comment.