Skip to content

Commit

Permalink
Add SSM parameter importer
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Duchesne committed Nov 10, 2017
1 parent eba16bc commit a80788f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
32 changes: 32 additions & 0 deletions aws/import_aws_ssm_parameter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package aws

import (
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccAWSSSMParameter_importBasic(t *testing.T) {
resourceName := "aws_ssm_parameter.foo"
randName := acctest.RandString(5)
randValue := acctest.RandString(5)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSSMParameterDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSSSMParameterBasicConfig(randName, randValue),
},

{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"overwrite"},
},
},
})
}
26 changes: 17 additions & 9 deletions aws/resource_aws_ssm_parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ func resourceAwsSsmParameter() *schema.Resource {
Update: resourceAwsSsmParameterPut,
Delete: resourceAwsSsmParameterDelete,
Exists: resourceAwsSmmParameterExists,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"name": {
Expand Down Expand Up @@ -61,7 +64,7 @@ func resourceAwsSmmParameterExists(d *schema.ResourceData, meta interface{}) (bo
conn := meta.(*AWSClient).ssmconn

resp, err := conn.GetParameters(&ssm.GetParametersInput{
Names: []*string{aws.String(d.Get("name").(string))},
Names: []*string{aws.String(d.Id())},
WithDecryption: aws.Bool(true),
})

Expand All @@ -72,14 +75,19 @@ func resourceAwsSmmParameterExists(d *schema.ResourceData, meta interface{}) (bo
}

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

log.Printf("[DEBUG] Reading SSM Parameter: %s", d.Id())

resp, err := conn.GetParameters(&ssm.GetParametersInput{
Names: []*string{aws.String(d.Get("name").(string))},
paramInput := &ssm.GetParametersInput{
Names: []*string{
aws.String(d.Id()),
},
WithDecryption: aws.Bool(true),
})
}

resp, err := ssmconn.GetParameters(paramInput)

if err != nil {
return errwrap.Wrapf("[ERROR] Error getting SSM parameter: {{err}}", err)
}
Expand All @@ -89,11 +97,11 @@ func resourceAwsSsmParameterRead(d *schema.ResourceData, meta interface{}) error
d.Set("type", param.Type)
d.Set("value", param.Value)

respDetailed, err := conn.DescribeParameters(&ssm.DescribeParametersInput{
respDetailed, err := ssmconn.DescribeParameters(&ssm.DescribeParametersInput{
Filters: []*ssm.ParametersFilter{
&ssm.ParametersFilter{
Key: aws.String("Name"),
Values: []*string{aws.String(d.Get("name").(string))},
Values: []*string{aws.String(d.Id())},
},
},
})
Expand All @@ -116,8 +124,8 @@ func resourceAwsSsmParameterRead(d *schema.ResourceData, meta interface{}) error
d.Set("description", detail.Description)
d.Set("allowed_pattern", detail.AllowedPattern)

if tagList, err := conn.ListTagsForResource(&ssm.ListTagsForResourceInput{
ResourceId: aws.String(d.Get("name").(string)),
if tagList, err := ssmconn.ListTagsForResource(&ssm.ListTagsForResourceInput{
ResourceId: aws.String(d.Id()),
ResourceType: aws.String("Parameter"),
}); err != nil {
return fmt.Errorf("Failed to get SSM parameter tags for %s: %s", d.Get("name"), err)
Expand Down

0 comments on commit a80788f

Please sign in to comment.