Skip to content

Commit

Permalink
feat: add possibility to configure log percentage in awsmt provider
Browse files Browse the repository at this point in the history
OTT-6159
  • Loading branch information
thatsddr committed Jul 26, 2024
1 parent a66c254 commit d45e8e1
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 13 deletions.
30 changes: 27 additions & 3 deletions awsmt/helpers_playback_configuration.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-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/mediatailor"
awsTypes "github.com/aws/aws-sdk-go-v2/service/mediatailor/types"
Expand All @@ -18,6 +19,8 @@ type putPlaybackConfigurationModelbuilder struct {
isResource bool
}

// Input Builder functions

func (i *putPlaybackConfigurationInputBuilder) getInput() *mediatailor.PutPlaybackConfigurationInput {

i.addAvailSuppressionToInput()
Expand Down Expand Up @@ -173,6 +176,8 @@ func (i *putPlaybackConfigurationInputBuilder) addRequiredFieldsToInput() {
i.input.Name = i.model.Name
}

// Model Builder functions

func (m *putPlaybackConfigurationModelbuilder) getModel() playbackConfigurationModel {

m.addAvailSuppressionToModel()
Expand Down Expand Up @@ -293,9 +298,9 @@ func (m *putPlaybackConfigurationModelbuilder) addOptionalFieldsToModel() {
}

if m.output.LogConfiguration != nil {
m.model.LogConfigurationPercentEnabled = types.Int64Value(int64(m.output.LogConfiguration.PercentEnabled))
} else {
m.model.LogConfigurationPercentEnabled = types.Int64Value(0)
if !m.isResource && m.model.LogConfigurationPercentEnabled == types.Int64Value(0) {
m.model.LogConfigurationPercentEnabled = types.Int64Value(int64(m.output.LogConfiguration.PercentEnabled))
}
}

if m.output.PersonalizationThresholdSeconds != nil {
Expand All @@ -318,3 +323,22 @@ func (m *putPlaybackConfigurationModelbuilder) addOptionalFieldsToModel() {
m.model.Tags = m.output.Tags
}
}

// Log percentage configuration helper
func setLogPercentage(client *mediatailor.Client, model playbackConfigurationModel) (*mediatailor.PutPlaybackConfigurationOutput, error) {
_, err := client.ConfigureLogsForPlaybackConfiguration(context.TODO(), &mediatailor.ConfigureLogsForPlaybackConfigurationInput{
PlaybackConfigurationName: model.Name,
PercentEnabled: int32(model.LogConfigurationPercentEnabled.ValueInt64()),
})

if err != nil {
return nil, err
}

playbackConfiguration, err := client.GetPlaybackConfiguration(context.TODO(), &mediatailor.GetPlaybackConfigurationInput{Name: model.Name})
if err != nil {
return nil, err
}
output := mediatailor.PutPlaybackConfigurationOutput(*playbackConfiguration)
return &output, nil
}
30 changes: 29 additions & 1 deletion awsmt/resource_playback_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package awsmt
import (
"context"
"github.com/aws/aws-sdk-go-v2/service/mediatailor"
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
"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/resource/schema/int64planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/objectplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
Expand Down Expand Up @@ -93,7 +95,15 @@ func (r *resourcePlaybackConfiguration) Schema(_ context.Context, _ resource.Sch
},
},
"hls_configuration_manifest_endpoint_prefix": computedStringWithStateForUnknown,
"log_configuration_percent_enabled": computedInt64WithStateForUnknown,
"log_configuration_percent_enabled": schema.Int64Attribute{
Optional: true,
Validators: []validator.Int64{
int64validator.Between(0, 100),
},
PlanModifiers: []planmodifier.Int64{
int64planmodifier.UseStateForUnknown(),
},
},
"live_pre_roll_configuration": schema.SingleNestedAttribute{
Optional: true,
Attributes: map[string]schema.Attribute{
Expand Down Expand Up @@ -158,6 +168,15 @@ func (r *resourcePlaybackConfiguration) Create(ctx context.Context, req resource
return
}

playbackConfiguration, err = setLogPercentage(r.client, plan)
if err != nil {
resp.Diagnostics.AddError(
"Error while setting the log percentage "+err.Error(),
err.Error(),
)
return
}

m := putPlaybackConfigurationModelbuilder{model: &plan, output: *playbackConfiguration, isResource: true}

resp.Diagnostics.Append(resp.State.Set(ctx, m.getModel())...)
Expand Down Expand Up @@ -242,6 +261,15 @@ func (r *resourcePlaybackConfiguration) Update(ctx context.Context, req resource
return
}

playbackConfigurationUpdate, err = setLogPercentage(r.client, plan)
if err != nil {
resp.Diagnostics.AddError(
"Error while setting the log percentage "+err.Error(),
err.Error(),
)
return
}

m := putPlaybackConfigurationModelbuilder{model: &plan, output: *playbackConfigurationUpdate, isResource: true}

resp.Diagnostics.Append(resp.State.Set(ctx, m.getModel())...)
Expand Down
43 changes: 43 additions & 0 deletions awsmt/resource_playback_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,37 @@ func TestAccPlaybackConfigurationMinimal(t *testing.T) {
})
}

func TestAccPlaybackConfigurationLogPercentage(t *testing.T) {
resourceName := "awsmt_playback_configuration.r3"
name := "test-acc-playback-configuration-log-percentage"
adUrl := "https://www.foo.de/"
videoSourceUrl := "https://www.bar.at"
p1 := "5"
p2 := "8"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: logPercentagePlaybackConfiguration(name, adUrl, videoSourceUrl, p1),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "id", name),
resource.TestCheckResourceAttr(resourceName, "name", name),
resource.TestCheckResourceAttr(resourceName, "log_configuration_percent_enabled", p1),
),
},
{
Config: logPercentagePlaybackConfiguration(name, adUrl, videoSourceUrl, p2),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "id", name),
resource.TestCheckResourceAttr(resourceName, "name", name),
resource.TestCheckResourceAttr(resourceName, "log_configuration_percent_enabled", p2),
),
},
},
})
}

func TestAccPlaybackConfigurationCreationFail(t *testing.T) {
name := "test-acc-playback-configuration-delete"
adUrl := "invalid"
Expand Down Expand Up @@ -167,6 +198,18 @@ func minimalPlaybackConfiguration(name, adUrl, videoSourceUrl string) string {
)
}

func logPercentagePlaybackConfiguration(name, adUrl, videoSourceUrl, logPercentage string) string {
return fmt.Sprintf(`
resource "awsmt_playback_configuration" "r3" {
ad_decision_server_url = "%[2]s"
name = "%[1]s"
video_content_source_url = "%[3]s"
log_configuration_percent_enabled = %[4]s"
}
`, name, adUrl, videoSourceUrl, logPercentage,
)
}

func completePlaybackConfiguration(name, adUrl, bumperE, bumperS, cdnUrl, maxD, pS, k1, v1, k2, v2 string) string {
return fmt.Sprintf(`
resource "awsmt_playback_configuration" "r1" {
Expand Down
7 changes: 0 additions & 7 deletions awsmt/schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@ var computedInt64 = schema.Int64Attribute{
Computed: true,
}

var computedInt64WithStateForUnknown = schema.Int64Attribute{
Computed: true,
PlanModifiers: []planmodifier.Int64{
int64planmodifier.UseStateForUnknown(),
},
}

var computedMap = schema.MapAttribute{
Computed: true,
ElementType: types.StringType,
Expand Down
3 changes: 1 addition & 2 deletions docs/resources/awsmt_playback_configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ The following arguments are supported:
- `live_pre_roll_configuration` - The configuration for pre-roll ad insertion.
- `ad_decision_server_url` - The URL for the ad decision server (ADS) for pre-roll ads.
- `max_duration_seconds` - The maximum allowed duration for the pre-roll ad avail.
- `log_configuration_percent_enabled` - The percentage of session logs that MediaTailor sends to your Cloudwatch Logs account.
- `manifest_processing_rules` – The configuration for manifest processing rules
- `ad_marker_passthrough` – For HLS, when set to true, MediaTailor passes through EXT-X-CUE-IN, EXT-X-CUE-OUT, and EXT-X-SPLICEPOINT-SCTE35 ad markers from the origin manifest to the MediaTailor personalized manifest.
- `enabled` - Enables ad marker passthrough for your configuration.
Expand All @@ -73,8 +74,6 @@ In addition to all arguments above, the following attributes are exported:
- `dash_configuration` - The configuration for DASH content.
- `manifest_endpoint_prefix` - URL generated by MediaTailor to initiate a playback session.
- `hls_configuration_manifest_endpoint_prefix` - URL generated by MediaTailor to initiate a playback session on devices that support Apple HLS.
- `log_configuration` - The Amazon CloudWatch log settings for a playback configuration.
- `percent_enabled` - The percentage of session logs that MediaTailor sends to your Cloudwatch Logs account.
- `playback_configuration_arn` - The Amazon Resource Name (ARN) for the playback configuration.
- `playback_endpoint_prefix` - The URL that the player accesses to get a manifest from AWS Elemental MediaTailor.
- `session_initialization_endpoint_prefix` - The URL that the player uses to initialize a session that uses client-side reporting.
Expand Down
1 change: 1 addition & 0 deletions examples/playback_configuration.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ resource "awsmt_playback_configuration" "r1" {
enabled = "false"
}
}
log_configuration_percent_enabled = 5
name = "example-playback-configuration-awsmt"
personalization_threshold_seconds = 2
tags = { "Environment" : "dev" }
Expand Down

0 comments on commit d45e8e1

Please sign in to comment.