Skip to content
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

r/ssm_parameter - tag on create + validations #17830

Merged
merged 2 commits into from
Mar 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changelog/17830.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:enhancement
resource/aws_ssm_parameter: Add plan time validation to `name`, `description` and `allowed_pattern`
```

```release-note:enhancement
resource/aws_ssm_parameter: Tag on create
```
51 changes: 27 additions & 24 deletions aws/resource_aws_ssm_parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ func resourceAwsSsmParameter() *schema.Resource {

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(1, 2048),
},
"description": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(1, 1024),
},
"tier": {
Type: schema.TypeString,
Expand All @@ -50,13 +52,9 @@ func resourceAwsSsmParameter() *schema.Resource {
}, false),
},
"type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
ssm.ParameterTypeString,
ssm.ParameterTypeStringList,
ssm.ParameterTypeSecureString,
}, false),
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(ssm.ParameterType_Values(), false),
},
"value": {
Type: schema.TypeString,
Expand Down Expand Up @@ -87,8 +85,9 @@ func resourceAwsSsmParameter() *schema.Resource {
Optional: true,
},
"allowed_pattern": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(1, 1024),
},
"version": {
Type: schema.TypeInt,
Expand Down Expand Up @@ -166,7 +165,7 @@ func resourceAwsSsmParameterRead(d *schema.ResourceData, meta interface{}) error
}
describeResp, err := ssmconn.DescribeParameters(describeParamsInput)
if err != nil {
return fmt.Errorf("error describing SSM parameter: %s", err)
return fmt.Errorf("error describing SSM parameter: %w", err)
}

if describeResp == nil || len(describeResp.Parameters) == 0 || describeResp.Parameters[0] == nil {
Expand All @@ -188,11 +187,11 @@ func resourceAwsSsmParameterRead(d *schema.ResourceData, meta interface{}) error
tags, err := keyvaluetags.SsmListTags(ssmconn, name, ssm.ResourceTypeForTaggingParameter)

if err != nil {
return fmt.Errorf("error listing tags for SSM Parameter (%s): %s", name, err)
return fmt.Errorf("error listing tags for SSM Parameter (%s): %w", name, err)
}

if err := d.Set("tags", tags.IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
return fmt.Errorf("error setting tags: %w", err)
}

d.Set("arn", param.ARN)
Expand All @@ -218,10 +217,11 @@ func resourceAwsSsmParameterDelete(d *schema.ResourceData, meta interface{}) err
func resourceAwsSsmParameterPut(d *schema.ResourceData, meta interface{}) error {
ssmconn := meta.(*AWSClient).ssmconn

log.Printf("[INFO] Creating SSM Parameter: %s", d.Get("name").(string))
name := d.Get("name").(string)
log.Printf("[INFO] Creating SSM Parameter: %s", name)

paramInput := &ssm.PutParameterInput{
Name: aws.String(d.Get("name").(string)),
Name: aws.String(name),
Type: aws.String(d.Get("type").(string)),
Tier: aws.String(d.Get("tier").(string)),
Value: aws.String(d.Get("value").(string)),
Expand All @@ -242,6 +242,10 @@ func resourceAwsSsmParameterPut(d *schema.ResourceData, meta interface{}) error
paramInput.SetKeyId(keyID.(string))
}

if v, ok := d.GetOk("tags"); ok && d.IsNewResource() {
paramInput.Tags = keyvaluetags.New(v.(map[string]interface{})).IgnoreAws().SsmTags()
}

log.Printf("[DEBUG] Waiting for SSM Parameter %v to be updated", d.Get("name"))
_, err := ssmconn.PutParameter(paramInput)

Expand All @@ -251,19 +255,18 @@ func resourceAwsSsmParameterPut(d *schema.ResourceData, meta interface{}) error
}

if err != nil {
return fmt.Errorf("error creating SSM parameter: %s", err)
return fmt.Errorf("error creating SSM parameter: %w", err)
}

name := d.Get("name").(string)
if d.HasChange("tags") {
if !d.IsNewResource() && d.HasChange("tags") {
o, n := d.GetChange("tags")

if err := keyvaluetags.SsmUpdateTags(ssmconn, name, ssm.ResourceTypeForTaggingParameter, o, n); err != nil {
return fmt.Errorf("error updating SSM Parameter (%s) tags: %s", name, err)
return fmt.Errorf("error updating SSM Parameter (%s) tags: %w", name, err)
}
}

d.SetId(d.Get("name").(string))
d.SetId(name)

return resourceAwsSsmParameterRead(d, meta)
}
Expand Down