Skip to content

Commit

Permalink
Merge pull request #40177 from hashicorp/f-aws_ecs_service_vpcl
Browse files Browse the repository at this point in the history
 resource/aws_ecs_service: Add 'vpc_lattice_configurations'
  • Loading branch information
gdavison authored Nov 21, 2024
2 parents 36bffba + 8db07d9 commit 5227827
Show file tree
Hide file tree
Showing 4 changed files with 494 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/40177.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_ecs_service: Add vpc_lattice_configurations argument
```
84 changes: 84 additions & 0 deletions internal/service/ecs/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"
"time"

"github.com/YakDriver/regexache"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/arn"
"github.com/aws/aws-sdk-go-v2/service/ecs"
Expand Down Expand Up @@ -1081,6 +1082,33 @@ func resourceService() *schema.Resource {
},
},
},
"vpc_lattice_configurations": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
names.AttrRoleARN: {
Type: schema.TypeString,
Required: true,
ValidateFunc: verify.ValidARN,
},
"target_group_arn": {
Type: schema.TypeString,
Required: true,
ValidateFunc: verify.ValidARN,
},
"port_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.All(
validation.StringLenBetween(1, 64),
validation.StringMatch(regexache.MustCompile(`^[0-9a-z_-]+$`), "must contain only lowercase letters, numbers, underscores and hyphens"),
validation.StringDoesNotMatch(regexache.MustCompile(`^-`), "cannot begin with a hyphen"),
),
},
},
},
},
},

SchemaVersion: 1,
Expand Down Expand Up @@ -1143,6 +1171,7 @@ func resourceServiceCreate(ctx context.Context, d *schema.ResourceData, meta int
SchedulingStrategy: schedulingStrategy,
ServiceName: aws.String(name),
Tags: getTagsIn(ctx),
VpcLatticeConfigurations: expandVPCLatticeConfiguration(d.Get("vpc_lattice_configurations").(*schema.Set)),
}

if v, ok := d.GetOk("alarms"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
Expand Down Expand Up @@ -1342,6 +1371,15 @@ func resourceServiceRead(ctx context.Context, d *schema.ResourceData, meta inter
if err := d.Set(names.AttrNetworkConfiguration, flattenNetworkConfiguration(service.NetworkConfiguration)); err != nil {
return sdkdiag.AppendErrorf(diags, "setting network_configuration: %s", err)
}

for _, deployment := range service.Deployments {
if aws.ToString(deployment.Status) == "PRIMARY" {
if err := d.Set("vpc_lattice_configurations", flattenVPCLatticeConfigurations(deployment.VpcLatticeConfigurations)); err != nil {
return sdkdiag.AppendErrorf(diags, "setting vpc_lattice_configurations: %s", err)
}
}
}

if err := d.Set("ordered_placement_strategy", flattenPlacementStrategy(service.PlacementStrategy)); err != nil {
return sdkdiag.AppendErrorf(diags, "setting ordered_placement_strategy: %s", err)
}
Expand Down Expand Up @@ -1514,6 +1552,10 @@ func resourceServiceUpdate(ctx context.Context, d *schema.ResourceData, meta int
input.VolumeConfigurations = expandVolumeConfigurations(ctx, d.Get("volume_configuration").([]interface{}))
}

if d.HasChange("vpc_lattice_configurations") {
input.VpcLatticeConfigurations = expandVPCLatticeConfiguration(d.Get("vpc_lattice_configurations").(*schema.Set))
}

// Retry due to IAM eventual consistency.
const (
serviceUpdateTimeout = 2 * time.Minute
Expand Down Expand Up @@ -2096,6 +2138,48 @@ func expandNetworkConfiguration(nc []interface{}) *awstypes.NetworkConfiguration
return &awstypes.NetworkConfiguration{AwsvpcConfiguration: awsVpcConfig}
}

func expandVPCLatticeConfiguration(tfSet *schema.Set) []awstypes.VpcLatticeConfiguration {
tfList := tfSet.List()
if len(tfList) == 0 {
return nil
}

apiObjects := make([]awstypes.VpcLatticeConfiguration, 0)

for _, tfMapRaw := range tfSet.List() {
config := tfMapRaw.(map[string]interface{})

apiObject := awstypes.VpcLatticeConfiguration{
RoleArn: aws.String(config[names.AttrRoleARN].(string)),
TargetGroupArn: aws.String(config["target_group_arn"].(string)),
PortName: aws.String(config["port_name"].(string)),
}

apiObjects = append(apiObjects, apiObject)
}

return apiObjects
}

func flattenVPCLatticeConfigurations(apiObjects []awstypes.VpcLatticeConfiguration) []interface{} {
if len(apiObjects) == 0 {
return nil
}

tfList := make([]interface{}, 0, len(apiObjects))

for _, apiObject := range apiObjects {
tfMap := map[string]interface{}{
names.AttrRoleARN: aws.ToString(apiObject.RoleArn),
"target_group_arn": aws.ToString(apiObject.TargetGroupArn),
"port_name": aws.ToString(apiObject.PortName),
}
tfList = append(tfList, tfMap)
}

return tfList
}

func expandPlacementConstraints(tfList []interface{}) ([]awstypes.PlacementConstraint, error) {
if len(tfList) == 0 {
return nil, nil
Expand Down
Loading

0 comments on commit 5227827

Please sign in to comment.