-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
resource/ecs_cluster: Add ability to enable ECS Cluster Insights #9720
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -10,6 +10,7 @@ import ( | |||||
"github.com/aws/aws-sdk-go/service/ecs" | ||||||
"github.com/hashicorp/terraform/helper/resource" | ||||||
"github.com/hashicorp/terraform/helper/schema" | ||||||
"github.com/hashicorp/terraform/helper/validation" | ||||||
) | ||||||
|
||||||
func resourceAwsEcsCluster() *schema.Resource { | ||||||
|
@@ -33,6 +34,26 @@ func resourceAwsEcsCluster() *schema.Resource { | |||||
Type: schema.TypeString, | ||||||
Computed: true, | ||||||
}, | ||||||
"setting": { | ||||||
Type: schema.TypeSet, | ||||||
Optional: true, | ||||||
Computed: true, | ||||||
Elem: &schema.Resource{ | ||||||
Schema: map[string]*schema.Schema{ | ||||||
"name": { | ||||||
Type: schema.TypeString, | ||||||
Required: true, | ||||||
ValidateFunc: validation.StringInSlice([]string{ | ||||||
ecs.ClusterSettingNameContainerInsights, | ||||||
}, false), | ||||||
}, | ||||||
"value": { | ||||||
Type: schema.TypeString, | ||||||
Required: true, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
} | ||||||
} | ||||||
|
@@ -55,10 +76,16 @@ func resourceAwsEcsClusterCreate(d *schema.ResourceData, meta interface{}) error | |||||
clusterName := d.Get("name").(string) | ||||||
log.Printf("[DEBUG] Creating ECS cluster %s", clusterName) | ||||||
|
||||||
out, err := conn.CreateCluster(&ecs.CreateClusterInput{ | ||||||
input := ecs.CreateClusterInput{ | ||||||
ClusterName: aws.String(clusterName), | ||||||
Tags: tagsFromMapECS(d.Get("tags").(map[string]interface{})), | ||||||
}) | ||||||
} | ||||||
|
||||||
if v, ok := d.GetOk("setting"); ok { | ||||||
input.Settings = expandEcsSettings(v.(*schema.Set).List()) | ||||||
} | ||||||
|
||||||
out, err := conn.CreateCluster(&input) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
@@ -134,6 +161,10 @@ func resourceAwsEcsClusterRead(d *schema.ResourceData, meta interface{}) error { | |||||
d.Set("arn", cluster.ClusterArn) | ||||||
d.Set("name", cluster.ClusterName) | ||||||
|
||||||
if err := d.Set("setting", flattenEcsSettings(cluster.Settings)); err != nil { | ||||||
return fmt.Errorf("error setting setting: %s", err) | ||||||
} | ||||||
|
||||||
if err := d.Set("tags", tagsToMapECS(cluster.Tags)); err != nil { | ||||||
return fmt.Errorf("error setting tags: %s", err) | ||||||
} | ||||||
|
@@ -144,6 +175,18 @@ func resourceAwsEcsClusterRead(d *schema.ResourceData, meta interface{}) error { | |||||
func resourceAwsEcsClusterUpdate(d *schema.ResourceData, meta interface{}) error { | ||||||
conn := meta.(*AWSClient).ecsconn | ||||||
|
||||||
if d.HasChange("setting") { | ||||||
input := ecs.UpdateClusterSettingsInput{ | ||||||
Cluster: aws.String(d.Id()), | ||||||
Settings: expandEcsSettings(d.Get("setting").(*schema.Set).List()), | ||||||
} | ||||||
|
||||||
_, err := conn.UpdateClusterSettings(&input) | ||||||
if err != nil { | ||||||
return fmt.Errorf("error changing ECS cluster settings (%s): %s", d.Id(), err) | ||||||
} | ||||||
} | ||||||
|
||||||
if d.HasChange("tags") { | ||||||
oldTagsRaw, newTagsRaw := d.GetChange("tags") | ||||||
oldTagsMap := oldTagsRaw.(map[string]interface{}) | ||||||
|
@@ -260,3 +303,41 @@ func ecsClusterInactive(out *ecs.DescribeClustersOutput, clusterName string) boo | |||||
} | ||||||
return false | ||||||
} | ||||||
|
||||||
func expandEcsSettings(configured []interface{}) []*ecs.ClusterSetting { | ||||||
if len(configured) == 0 { | ||||||
return nil | ||||||
} | ||||||
|
||||||
settings := make([]*ecs.ClusterSetting, 0, len(configured)) | ||||||
|
||||||
for _, raw := range configured { | ||||||
data := raw.(map[string]interface{}) | ||||||
|
||||||
setting := &ecs.ClusterSetting{ | ||||||
Name: aws.String(data["name"].(string)), | ||||||
Value: aws.String(data["value"].(string)), | ||||||
} | ||||||
|
||||||
settings = append(settings, setting) | ||||||
} | ||||||
|
||||||
return settings | ||||||
} | ||||||
|
||||||
func flattenEcsSettings(list []*ecs.ClusterSetting) []map[string]interface{} { | ||||||
if len(list) == 0 { | ||||||
return nil | ||||||
} | ||||||
|
||||||
result := make([]map[string]interface{}, 0, len(list)) | ||||||
for _, setting := range list { | ||||||
l := map[string]interface{}{ | ||||||
"name": *setting.Name, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: To prevent potential panics (unlikely in this case, but a general pattern for consistency), we should use the AWS Go SDK conversion functions (e.g.
Suggested change
|
||||||
"value": *setting.Value, | ||||||
} | ||||||
|
||||||
result = append(result, l) | ||||||
} | ||||||
return result | ||||||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -24,7 +24,15 @@ The following arguments are supported: | |||||
|
||||||
* `name` - (Required) The name of the cluster (up to 255 letters, numbers, hyphens, and underscores) | ||||||
* `tags` - (Optional) Key-value mapping of resource tags | ||||||
* `setting` - (Optional) The setting to use when creating a cluster. This parameter is used to enable CloudWatch Container Insights for a cluster. Defined below. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Prefer the wording of "configuration block" over "parameter" (which incorrectly may signal it can work as an argument with
Suggested change
|
||||||
|
||||||
## setting | ||||||
|
||||||
The `setting` configuration block supports the following: | ||||||
|
||||||
* `name` - (Required) Name of the setting to manage. Valid values: `containerInsights`. | ||||||
* `value` - (Required) The value to assign to the setting. Value values are `enabled` and `disabled`. | ||||||
|
||||||
## Attributes Reference | ||||||
|
||||||
In addition to all arguments above, the following attributes are exported: | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Since the
aws_ecs_task_definition
andaws_ecs_service
resources are not necessary for testing theaws_ecs_cluster
data source, we can omit them.