Skip to content

Commit

Permalink
Made change to the override mechanism to simplify it. Already existin…
Browse files Browse the repository at this point in the history
…g resource should set overwrite to true by default and let the terraform lifecycle rule handle the update of the resource. Otherwise, overwrite is false by default unless explicitly specified through override parameter.
  • Loading branch information
jocgir committed Sep 26, 2017
1 parent d7738c4 commit c3047e8
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 45 deletions.
15 changes: 3 additions & 12 deletions aws/resource_aws_ssm_parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,13 @@ func resourceAwsSsmParameterPut(d *schema.ResourceData, meta interface{}) error

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

// Overwrite is set to true if the ressource already exists in the state or
// if it has been explicitly specified
paramInput := &ssm.PutParameterInput{
Name: aws.String(d.Get("name").(string)),
Type: aws.String(d.Get("type").(string)),
Value: aws.String(d.Get("value").(string)),
Overwrite: aws.Bool(shouldUpdateSsmParameter(d)),
Overwrite: aws.Bool(!d.IsNewResource() || d.Get("overwrite").(bool)),
AllowedPattern: aws.String(d.Get("allowed_pattern").(string)),
}

Expand Down Expand Up @@ -184,14 +186,3 @@ func resourceAwsSsmParameterPut(d *schema.ResourceData, meta interface{}) error

return resourceAwsSsmParameterRead(d, meta)
}

func shouldUpdateSsmParameter(d *schema.ResourceData) bool {
// If the user has specified a preference, return their preference
if value, ok := d.GetOkExists("overwrite"); ok {
return value.(bool)
}

// Since the user has not specified a preference, obey lifecycle rules
// if it is not a new resource, otherwise overwrite should be set to false.
return !d.IsNewResource()
}
32 changes: 0 additions & 32 deletions aws/resource_aws_ssm_parameter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,35 +277,3 @@ resource "aws_kms_key" "test_key" {
}
`, rName, rName, value)
}

func TestAWSSSMParameterShouldUpdate(t *testing.T) {
data := resourceAwsSsmParameter().TestResourceData()
failure := false

if !shouldUpdateSsmParameter(data) {
t.Logf("Existing resources should be overwritten if the values don't match!")
failure = true
}

data.MarkNewResource()
if shouldUpdateSsmParameter(data) {
t.Logf("New resources must never be overwritten, this will overwrite parameters created outside of the system")
failure = true
}

data = resourceAwsSsmParameter().TestResourceData()
data.Set("overwrite", true)
if !shouldUpdateSsmParameter(data) {
t.Logf("Resources should always be overwritten if the user requests it")
failure = true
}

data.Set("overwrite", false)
if shouldUpdateSsmParameter(data) {
t.Logf("Resources should never be overwritten if the user requests it")
failure = true
}
if failure {
t.Fail()
}
}
2 changes: 1 addition & 1 deletion website/docs/r/ssm_parameter.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ The following arguments are supported:
* `value` - (Required) The value of the parameter.
* `description` - (Optional) The description of the parameter.
* `key_id` - (Optional) The KMS key id or arn for encrypting a SecureString.
* `overwrite` - (Optional) Overwrite an existing parameter. If not specified, will default to `false` if the resource has not been created by terraform to avoid overwrite of existing resource and will default to `true` otherwise (terraform lifecycle rules should then be used to manage the update behavior).
* `overwrite` - (Optional) Default `false`. Force overwrite of an existing parameter that is not yet handled by terraform. Terraform lifecycle rules apply on resources that are already in the state file.
* `allowed_pattern` - (Optional) A regular expression used to validate the parameter value.
* `tags` - (Optional) A mapping of tags to assign to the object.

Expand Down

0 comments on commit c3047e8

Please sign in to comment.