Skip to content

Commit

Permalink
Merge pull request #68 from spring-media/fix/stop-channel
Browse files Browse the repository at this point in the history
Fix/stop channel
  • Loading branch information
thatsddr authored Jan 19, 2023
2 parents 9267d2e + 29d2b91 commit 23e759e
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 47 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ $(BUILD_DIR):
%/coverage.html: %/coverage.profile
go tool cover -html=$< -o $@

test: $(clean) $(BUILD_DIR)/coverage.html $(BUILD_DIR)/coverage_func.txt $(BUILD_DIR)/coverage.profile
test: clean $(BUILD_DIR)/coverage.html $(BUILD_DIR)/coverage_func.txt $(BUILD_DIR)/coverage.profile
@echo "finished test"

sweep:
Expand Down
31 changes: 31 additions & 0 deletions awsmt/helpers_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,34 @@ func getOutputs(d *schema.ResourceData) []*mediatailor.RequestOutputItem {
}
return nil
}

func startChannel(client *mediatailor.MediaTailor, channelName string) error {
_, err := client.StartChannel(&mediatailor.StartChannelInput{
ChannelName: aws.String(channelName),
})
if err != nil {
return fmt.Errorf("error while starting the channel: %v", err)
}
return nil
}

func stopChannel(client *mediatailor.MediaTailor, channelName string) error {
_, err := client.StopChannel(&mediatailor.StopChannelInput{
ChannelName: aws.String(channelName),
})
if err != nil {
return fmt.Errorf("error while stopping the channel: %v", err)
}
return nil
}

func checkStatusAndStartChannel(client *mediatailor.MediaTailor, d *schema.ResourceData) error {
if v, ok := d.GetOk("channel_state"); ok && v != nil && v.(string) != "" {
if v.(string) == "RUNNING" {
if err := startChannel(client, d.Get("name").(string)); err != nil {
return err
}
}
}
return nil
}
54 changes: 50 additions & 4 deletions awsmt/resource_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,23 @@ func resourceChannel() *schema.Resource {
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"arn": &computedString,
"name": &requiredString,
"channel_state": &computedString,
"arn": &computedString,
"name": &requiredString,
// @ADR
// Context: We cannot test the deletion of a running channel if we cannot set the channel_state property
// through the provider
// Decision: We decided to turn the channel_state property into an optional string and call the SDK to
//start/stop the channel accordingly.
// Consequences: The schema of the object differs from that of the SDK and we need to make additional
// SDK calls.
"channel_state": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"RUNNING", "STOPPED"}, false),
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return len(new) == 0
},
},
"creation_time": &computedString,
"filler_slate": createOptionalList(map[string]*schema.Schema{
"source_location_name": &optionalString,
Expand Down Expand Up @@ -117,6 +131,10 @@ func resourceChannelCreate(ctx context.Context, d *schema.ResourceData, meta int
return diag.FromErr(fmt.Errorf("error while creating the channel: %v", err))
}

if err := checkStatusAndStartChannel(client, d); err != nil {
return diag.FromErr(fmt.Errorf("error while starting the channel: %v", err))
}

if err := createChannelPolicy(client, d); err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -170,6 +188,22 @@ func resourceChannelUpdate(ctx context.Context, d *schema.ResourceData, meta int
}
}

res, err := client.DescribeChannel(&mediatailor.DescribeChannelInput{ChannelName: &resourceName})
if err != nil {
return diag.FromErr(err)
}
previousStatus := res.ChannelState
newStatusFromSchema := ""
if *previousStatus == "RUNNING" {
if err := stopChannel(client, resourceName); err != nil {
return diag.FromErr(err)
}
}
if _, ok := d.GetOk("channel_state"); ok {
_, newValue := d.GetChange("channel_state")
newStatusFromSchema = newValue.(string)
}

if err := updatePolicy(client, d, &resourceName); err != nil {
return diag.FromErr(err)
}
Expand All @@ -179,6 +213,13 @@ func resourceChannelUpdate(ctx context.Context, d *schema.ResourceData, meta int
if err != nil {
return diag.FromErr(fmt.Errorf("error while updating the channel: %v", err))
}

if (*previousStatus == "RUNNING" || newStatusFromSchema == "RUNNING") && newStatusFromSchema != "STOPPED" {
if err := startChannel(client, resourceName); err != nil {
return diag.FromErr(err)
}
}

d.SetId(aws.StringValue(channel.Arn))

return resourceChannelRead(ctx, d, meta)
Expand All @@ -187,7 +228,12 @@ func resourceChannelUpdate(ctx context.Context, d *schema.ResourceData, meta int
func resourceChannelDelete(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*mediatailor.MediaTailor)

_, err := client.DeleteChannelPolicy(&mediatailor.DeleteChannelPolicyInput{ChannelName: aws.String(d.Get("name").(string))})
_, err := client.StopChannel(&mediatailor.StopChannelInput{ChannelName: aws.String(d.Get("name").(string))})
if err != nil {
return diag.FromErr(fmt.Errorf("error while stopping the channel: %v", err))
}

_, err = client.DeleteChannelPolicy(&mediatailor.DeleteChannelPolicyInput{ChannelName: aws.String(d.Get("name").(string))})
if err != nil {
return diag.FromErr(fmt.Errorf("error while deleting the channel policy: %v", err))
}
Expand Down
61 changes: 52 additions & 9 deletions awsmt/resource_channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package awsmt

import (
"fmt"
"os"
"regexp"
"strings"
"testing"

"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/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"os"
"regexp"
"strings"
"testing"
)

func init() {
Expand Down Expand Up @@ -277,14 +276,42 @@ func TestAccChannelResource_policy(t *testing.T) {
})
}

func TestAccChannelResource_stopAndDelete(t *testing.T) {
rName := "channel_stop"
resourceName := "awsmt_channel.test"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: ProviderFactories,
CheckDestroy: testAccCheckChannelDestroy,
Steps: []resource.TestStep{
{
Config: testAccChannelConfig_stopAndDelete(rName, "RUNNING"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "name", rName),
),
},
{
Config: testAccChannelConfig_stopAndDelete(rName, "STOPPED"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "channel_state", "STOPPED"),
),
},
{
Config: testAccChannelConfig_stopAndDelete(rName, "RUNNING"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "channel_state", "RUNNING"),
),
},
},
})
}

func testAccCheckChannelDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*mediatailor.MediaTailor)

for _, rs := range s.RootModule().Resources {
if rs.Type != "awsmt_channel" {
continue
}

var resourceName string

if arn.IsARN(rs.Primary.ID) {
Expand Down Expand Up @@ -327,6 +354,22 @@ resource "awsmt_channel" "test" {
`, rName)
}

func testAccChannelConfig_stopAndDelete(rName string, status string) string {
return fmt.Sprintf(`
resource "awsmt_channel" "test" {
name = "%[1]s"
channel_state = "%[2]s"
outputs {
manifest_name = "default"
source_group = "default"
hls_manifest_windows_seconds = 30
}
playback_mode = "LOOP"
tier = "BASIC"
}
`, rName, status)
}

func testAccChannelConfig_Conflict(rName string) string {
return fmt.Sprintf(`
resource "awsmt_channel" "test" {
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/awsmt_channel.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ resource "awsmt_channel" "example" {
The following arguments are supported:

- `name` - (Required) The name of the channel.
- `channel_state` - (Optional) The state of the channel. Can be either `RUNNING` or `STOPPED`.
- `filler_slate` – (Optional) The slate used to fill gaps between programs in the schedule. You must configure filler slate if your channel uses the LINEAR PlaybackMode.
- `source_location_name` - (Optional) The name of the source location where the slate VOD source is stored.
- `vod_source_name` - (Optional) The slate VOD source name. The VOD source must already exist in a source location before it can be used for slate.
Expand All @@ -43,7 +44,6 @@ The following arguments are supported:
In addition to all arguments above, the following attributes are exported:

- `arn` - The ARN of the channel.
- `channel_state` - Returns whether the channel is running or not.
- `creation_time` - The timestamp of when the channel was created.
- `last_modified_time` - The timestamp of when the channel was last modified.
- `outputs` – The channel's output properties.
Expand Down
Loading

0 comments on commit 23e759e

Please sign in to comment.