-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from spring-media/feat/live-source
Feat/live source
- Loading branch information
Showing
11 changed files
with
643 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
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 dataSourceLiveSource() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: dataSourceLiveSourceRead, | ||
Schema: map[string]*schema.Schema{ | ||
"arn": &computedString, | ||
"creation_time": &computedString, | ||
"http_package_configurations": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"path": &computedString, | ||
"source_group": &computedString, | ||
"type": &computedString, | ||
}, | ||
}, | ||
}, | ||
"last_modified_time": &computedString, | ||
"name": &requiredString, | ||
"source_location_name": &requiredString, | ||
"tags": { | ||
Type: schema.TypeMap, | ||
Computed: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceLiveSourceRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
client := meta.(*mediatailor.MediaTailor) | ||
resourceName := d.Get("name").(string) | ||
sourceLocationName := d.Get("source_location_name").(string) | ||
|
||
input := &mediatailor.DescribeLiveSourceInput{SourceLocationName: &(sourceLocationName), LiveSourceName: aws.String(resourceName)} | ||
|
||
res, err := client.DescribeLiveSource(input) | ||
if err != nil { | ||
return diag.FromErr(fmt.Errorf("error while reading the live source: %v", err)) | ||
} | ||
|
||
d.SetId(fmt.Sprintf("%q/%q", *res.SourceLocationName, *res.LiveSourceName)) | ||
|
||
if err = setLiveSource(res, d); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package awsmt | ||
|
||
import ( | ||
"fmt" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"regexp" | ||
"testing" | ||
) | ||
|
||
func TestAccLiveSourceDataSourceBasic(t *testing.T) { | ||
dataSourceName := "data.awsmt_live_source.test" | ||
sourceLocationName := "basic_source_location" | ||
liveSourceName := "live_source_data_source_test" | ||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ProviderFactories: ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccLiveSourceDataSourceBasic(sourceLocationName, liveSourceName), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestMatchResourceAttr(dataSourceName, "arn", regexp.MustCompile(`^arn:aws:mediatailor:[\w-]+:\d+:liveSource\/.*$`)), | ||
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, "name", liveSourceName), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccLiveSourceDataSourceBasic(sourceLocationName, liveSourceName string) string { | ||
return fmt.Sprintf(` | ||
resource "awsmt_source_location" "example"{ | ||
source_location_name = "%[1]s" | ||
http_configuration_url = "https://ott-mediatailor-test.s3.eu-central-1.amazonaws.com/test-img.jpeg" | ||
} | ||
resource "awsmt_live_source" "test" { | ||
http_package_configurations { | ||
path = "/" | ||
source_group = "default" | ||
type = "HLS" | ||
} | ||
source_location_name = awsmt_source_location.example.source_location_name | ||
name = "%[2]s" | ||
} | ||
data "awsmt_live_source" "test" { | ||
source_location_name = awsmt_source_location.example.source_location_name | ||
name = awsmt_live_source.test.name | ||
} | ||
`, sourceLocationName, liveSourceName) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package awsmt | ||
|
||
import ( | ||
"fmt" | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/mediatailor" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func setLiveSource(values *mediatailor.DescribeLiveSourceOutput, d *schema.ResourceData) error { | ||
var errors []error | ||
|
||
if values.Arn != nil { | ||
errors = append(errors, d.Set("arn", values.Arn)) | ||
} | ||
if values.CreationTime != nil { | ||
errors = append(errors, d.Set("creation_time", values.CreationTime.String())) | ||
} | ||
errors = append(errors, setHttpPackageConfigurations(values.HttpPackageConfigurations, d)) | ||
if values.LastModifiedTime != nil { | ||
errors = append(errors, d.Set("last_modified_time", values.LastModifiedTime.String())) | ||
} | ||
if values.LiveSourceName != nil { | ||
errors = append(errors, d.Set("name", values.LiveSourceName)) | ||
} | ||
if values.SourceLocationName != nil { | ||
errors = append(errors, d.Set("source_location_name", values.SourceLocationName)) | ||
} | ||
errors = append(errors, d.Set("tags", values.Tags)) | ||
for _, e := range errors { | ||
if e != nil { | ||
return fmt.Errorf("the following error occured while setting the values: %w", e) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func getCreateLiveSourceInput(d *schema.ResourceData) mediatailor.CreateLiveSourceInput { | ||
var liveSourceInputParams mediatailor.CreateLiveSourceInput | ||
|
||
if c := getHttpPackageConfigurations(d); c != nil { | ||
liveSourceInputParams.HttpPackageConfigurations = c | ||
} | ||
|
||
if s, ok := d.GetOk("source_location_name"); ok { | ||
liveSourceInputParams.SourceLocationName = aws.String(s.(string)) | ||
} | ||
|
||
outputMap := make(map[string]*string) | ||
if v, ok := d.GetOk("tags"); ok { | ||
val := v.(map[string]interface{}) | ||
for k, value := range val { | ||
temp := value.(string) | ||
outputMap[k] = &temp | ||
} | ||
} | ||
liveSourceInputParams.Tags = outputMap | ||
|
||
if s, ok := d.GetOk("name"); ok { | ||
liveSourceInputParams.LiveSourceName = aws.String(s.(string)) | ||
} | ||
|
||
return liveSourceInputParams | ||
} | ||
|
||
func getUpdateLiveSourceInput(d *schema.ResourceData) mediatailor.UpdateLiveSourceInput { | ||
var updatedLiveSourceParams mediatailor.UpdateLiveSourceInput | ||
|
||
if c := getHttpPackageConfigurations(d); c != nil { | ||
updatedLiveSourceParams.HttpPackageConfigurations = c | ||
} | ||
if s, ok := d.GetOk("source_location_name"); ok { | ||
updatedLiveSourceParams.SourceLocationName = aws.String(s.(string)) | ||
} | ||
if s, ok := d.GetOk("name"); ok { | ||
updatedLiveSourceParams.LiveSourceName = aws.String(s.(string)) | ||
} | ||
return updatedLiveSourceParams | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package awsmt | ||
|
||
import ( | ||
"context" | ||
"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/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
"strings" | ||
) | ||
|
||
func resourceLiveSource() *schema.Resource { | ||
return &schema.Resource{ | ||
CreateContext: resourceLiveSourceCreate, | ||
ReadContext: resourceLiveSourceRead, | ||
UpdateContext: resourceLiveSourceUpdate, | ||
DeleteContext: resourceLiveSourceDelete, | ||
Schema: map[string]*schema.Schema{ | ||
"arn": &computedString, | ||
"creation_time": &computedString, | ||
"http_package_configurations": { | ||
Type: schema.TypeList, | ||
Required: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"path": &requiredString, | ||
"source_group": &requiredString, | ||
"type": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringInSlice([]string{"DASH", "HLS"}, false), | ||
}, | ||
}, | ||
}, | ||
}, | ||
"last_modified_time": &computedString, | ||
"name": &requiredString, | ||
"source_location_name": &requiredString, | ||
"tags": { | ||
Type: schema.TypeMap, | ||
Optional: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
}, | ||
Importer: &schema.ResourceImporter{ | ||
StateContext: schema.ImportStatePassthroughContext, | ||
}, | ||
CustomizeDiff: customdiff.Sequence( | ||
customdiff.ForceNewIfChange("name", func(ctx context.Context, old, new, meta interface{}) bool { return old.(string) != new.(string) }), | ||
customdiff.ForceNewIfChange("source_location_name", func(ctx context.Context, old, new, meta interface{}) bool { return old.(string) != new.(string) }), | ||
), | ||
} | ||
} | ||
|
||
func resourceLiveSourceCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
client := meta.(*mediatailor.MediaTailor) | ||
|
||
params := getCreateLiveSourceInput(d) | ||
liveSource, err := client.CreateLiveSource(¶ms) | ||
if err != nil { | ||
return diag.FromErr(fmt.Errorf("error while creating the live source: %v", err)) | ||
} | ||
d.SetId(aws.StringValue(liveSource.Arn)) | ||
|
||
return resourceLiveSourceRead(ctx, d, meta) | ||
} | ||
|
||
func resourceLiveSourceRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
client := meta.(*mediatailor.MediaTailor) | ||
liveSourceName := d.Get("name").(string) | ||
sourceLocationName := d.Get("source_location_name").(string) | ||
|
||
if len(liveSourceName) == 0 && len(d.Id()) > 0 { | ||
resourceArn, err := arn.Parse(d.Id()) | ||
if err != nil { | ||
return diag.FromErr(fmt.Errorf("error parsing the name from resource arn: %v", err)) | ||
} | ||
arnSections := strings.Split(resourceArn.Resource, "/") | ||
liveSourceName = arnSections[len(arnSections)-1] | ||
sourceLocationName = arnSections[len(arnSections)-2] | ||
} | ||
|
||
input := &mediatailor.DescribeLiveSourceInput{SourceLocationName: &(sourceLocationName), LiveSourceName: aws.String(liveSourceName)} | ||
|
||
res, err := client.DescribeLiveSource(input) | ||
if err != nil { | ||
return diag.FromErr(fmt.Errorf("error while reading the live source: %v", err)) | ||
} | ||
|
||
if err = setLiveSource(res, d); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceLiveSourceUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
client := meta.(*mediatailor.MediaTailor) | ||
|
||
if d.HasChange("tags") { | ||
oldValue, newValue := d.GetChange("tags") | ||
|
||
resourceName := d.Get("name").(string) | ||
sourceLocationName := d.Get("source_location_name").(string) | ||
res, err := client.DescribeLiveSource(&mediatailor.DescribeLiveSourceInput{SourceLocationName: &sourceLocationName, LiveSourceName: &resourceName}) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
if err := updateTags(client, res.Arn, oldValue, newValue); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
} | ||
|
||
var params = getUpdateLiveSourceInput(d) | ||
liveSource, err := client.UpdateLiveSource(¶ms) | ||
if err != nil { | ||
return diag.FromErr(fmt.Errorf("error while updating the live source: %v", err)) | ||
} | ||
d.SetId(aws.StringValue(liveSource.Arn)) | ||
|
||
return resourceLiveSourceRead(ctx, d, meta) | ||
} | ||
|
||
func resourceLiveSourceDelete(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
client := meta.(*mediatailor.MediaTailor) | ||
|
||
_, err := client.DeleteLiveSource(&mediatailor.DeleteLiveSourceInput{LiveSourceName: aws.String(d.Get("name").(string)), SourceLocationName: aws.String(d.Get("source_location_name").(string))}) | ||
if err != nil { | ||
return diag.FromErr(fmt.Errorf("error while deleting the resource: %v", err)) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.