Skip to content

Commit

Permalink
move update logic to separate resource func
Browse files Browse the repository at this point in the history
  • Loading branch information
anGie44 committed Apr 8, 2021
1 parent 5c09b09 commit 4548347
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 68 deletions.
163 changes: 105 additions & 58 deletions aws/resource_aws_ssm_parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ const (

func resourceAwsSsmParameter() *schema.Resource {
return &schema.Resource{
Create: resourceAwsSsmParameterPut,
Create: resourceAwsSsmParameterCreate,
Read: resourceAwsSsmParameterRead,
Update: resourceAwsSsmParameterPut,
Update: resourceAwsSsmParameterUpdate,
Delete: resourceAwsSsmParameterDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
Expand Down Expand Up @@ -110,6 +110,66 @@ func resourceAwsSsmParameter() *schema.Resource {
}
}

func resourceAwsSsmParameterCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ssmconn

name := d.Get("name").(string)

paramInput := &ssm.PutParameterInput{
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)),
Overwrite: aws.Bool(shouldUpdateSsmParameter(d)),
AllowedPattern: aws.String(d.Get("allowed_pattern").(string)),
}

if v, ok := d.GetOk("data_type"); ok {
paramInput.DataType = aws.String(v.(string))
}

if v, ok := d.GetOk("description"); ok {
paramInput.Description = aws.String(v.(string))
}

if keyID, ok := d.GetOk("key_id"); ok && d.Get("type").(string) == ssm.ParameterTypeSecureString {
paramInput.SetKeyId(keyID.(string))
}

// AWS SSM Service only supports PutParameter requests with Tags
// iff Overwrite is not provided or is false; in this resource's case,
// the Overwrite value is always set in the paramInput so we check for the value
if v, ok := d.GetOk("tags"); ok && !aws.BoolValue(paramInput.Overwrite) {
paramInput.Tags = keyvaluetags.New(v.(map[string]interface{})).IgnoreAws().SsmTags()
}

_, err := conn.PutParameter(paramInput)

if tfawserr.ErrMessageContains(err, "ValidationException", "Tier is not supported") {
paramInput.Tier = nil
_, err = conn.PutParameter(paramInput)
}

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

// Since the AWS SSM Service does not support PutParameter requests with
// Tags and Overwrite set to true, we make an additional API call
// to Update the resource's tags if necessary
if d.HasChange("tags") && paramInput.Tags == nil {
o, n := d.GetChange("tags")

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

d.SetId(name)

return resourceAwsSsmParameterRead(d, meta)
}

func resourceAwsSsmParameterRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ssmconn
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig
Expand Down Expand Up @@ -201,83 +261,70 @@ func resourceAwsSsmParameterRead(d *schema.ResourceData, meta interface{}) error
return nil
}

func resourceAwsSsmParameterDelete(d *schema.ResourceData, meta interface{}) error {
func resourceAwsSsmParameterUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ssmconn

_, err := conn.DeleteParameter(&ssm.DeleteParameterInput{
Name: aws.String(d.Get("name").(string)),
})
if d.HasChangesExcept("tags") {
paramInput := &ssm.PutParameterInput{
Name: aws.String(d.Get("name").(string)),
Type: aws.String(d.Get("type").(string)),
Tier: aws.String(d.Get("tier").(string)),
Value: aws.String(d.Get("value").(string)),
Overwrite: aws.Bool(shouldUpdateSsmParameter(d)),
AllowedPattern: aws.String(d.Get("allowed_pattern").(string)),
}

if tfawserr.ErrCodeEquals(err, ssm.ErrCodeParameterNotFound) {
return nil
}
if d.HasChange("data_type") {
paramInput.DataType = aws.String(d.Get("data_type").(string))
}

if err != nil {
return fmt.Errorf("error deleting SSM Parameter (%s): %s", d.Id(), err)
}
if d.HasChange("description") {
paramInput.Description = aws.String(d.Get("description").(string))
}

return nil
}
if d.HasChange("key_id") && d.Get("type").(string) == ssm.ParameterTypeSecureString {
paramInput.SetKeyId(d.Get("key_id").(string))
}

func resourceAwsSsmParameterPut(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ssmconn
_, err := conn.PutParameter(paramInput)

name := d.Get("name").(string)
if tfawserr.ErrMessageContains(err, "ValidationException", "Tier is not supported") {
paramInput.Tier = nil
_, err = conn.PutParameter(paramInput)
}

paramInput := &ssm.PutParameterInput{
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)),
Overwrite: aws.Bool(shouldUpdateSsmParameter(d)),
AllowedPattern: aws.String(d.Get("allowed_pattern").(string)),
if err != nil {
return fmt.Errorf("error updating SSM parameter (%s): %w", d.Id(), err)
}
}

if v, ok := d.GetOk("data_type"); ok {
paramInput.DataType = aws.String(v.(string))
}
if d.HasChange("tags") {
o, n := d.GetChange("tags")

if d.HasChange("description") {
_, n := d.GetChange("description")
paramInput.Description = aws.String(n.(string))
if err := keyvaluetags.SsmUpdateTags(conn, d.Id(), ssm.ResourceTypeForTaggingParameter, o, n); err != nil {
return fmt.Errorf("error updating SSM Parameter (%s) tags: %w", d.Id(), err)
}
}

if keyID, ok := d.GetOk("key_id"); ok && d.Get("type").(string) == ssm.ParameterTypeSecureString {
paramInput.SetKeyId(keyID.(string))
}
return resourceAwsSsmParameterRead(d, meta)
}

// AWS SSM Service only supports PutParameter requests with Tags
// iff Overwrite is not provided or is false; in this resource's case,
// the Overwrite value is always set in the paramInput so we check for the value
if v, ok := d.GetOk("tags"); ok && aws.BoolValue(paramInput.Overwrite) == false {
paramInput.Tags = keyvaluetags.New(v.(map[string]interface{})).IgnoreAws().SsmTags()
}
func resourceAwsSsmParameterDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ssmconn

_, err := conn.PutParameter(paramInput)
_, err := conn.DeleteParameter(&ssm.DeleteParameterInput{
Name: aws.String(d.Get("name").(string)),
})

if tfawserr.ErrMessageContains(err, "ValidationException", "Tier is not supported") {
paramInput.Tier = nil
_, err = conn.PutParameter(paramInput)
if tfawserr.ErrCodeEquals(err, ssm.ErrCodeParameterNotFound) {
return nil
}

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

// Since the AWS SSM Service does not support PutParameter requests with
// Tags and Overwrite set to true, we make an additional API call
// to Update the resource's tags if necessary
if d.HasChange("tags") && paramInput.Tags == nil {
o, n := d.GetChange("tags")

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

d.SetId(name)

return resourceAwsSsmParameterRead(d, meta)
return nil
}

func shouldUpdateSsmParameter(d *schema.ResourceData) bool {
Expand Down
36 changes: 26 additions & 10 deletions aws/resource_aws_ssm_parameter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ func TestAccAWSSSMParameter_overwrite(t *testing.T) {
})
}

// Reference: https://github.com/hashicorp/terraform-provider-aws/issues/18550
func TestAccAWSSSMParameter_overwriteWithTags(t *testing.T) {
var param ssm.Parameter
rName := fmt.Sprintf("%s_%s", t.Name(), acctest.RandString(10))
Expand All @@ -264,18 +265,41 @@ func TestAccAWSSSMParameter_overwriteWithTags(t *testing.T) {
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"overwrite"},
},
},
})
}

// Reference: https://github.com/hashicorp/terraform-provider-aws/issues/18550
func TestAccAWSSSMParameter_noOverwriteWithTags(t *testing.T) {
var param ssm.Parameter
rName := fmt.Sprintf("%s_%s", t.Name(), acctest.RandString(10))
resourceName := "aws_ssm_parameter.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, ssm.EndpointsID),
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSSMParameterDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSSSMParameterConfigOverwriteWithTags1(rName, true, "key1", "value2"),
Config: testAccAWSSSMParameterConfigOverwriteWithTags1(rName, false, "key1", "value1"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSSMParameterExists(resourceName, &param),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value2"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"overwrite"},
},
},
})
}

// Reference: https://github.com/hashicorp/terraform-provider-aws/issues/18550
func TestAccAWSSSMParameter_updateToOverwriteWithTags(t *testing.T) {
var param ssm.Parameter
rName := fmt.Sprintf("%s_%s", t.Name(), acctest.RandString(10))
Expand Down Expand Up @@ -308,14 +332,6 @@ func TestAccAWSSSMParameter_updateToOverwriteWithTags(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value2"),
),
},
{
Config: testAccAWSSSMParameterConfigOverwriteWithTags1(rName, false, "key1", "value1"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSSMParameterExists(resourceName, &param),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"),
),
},
},
})
}
Expand Down

0 comments on commit 4548347

Please sign in to comment.