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

service/codedeploy: Support resource import #6025

Merged
merged 3 commits into from
Oct 3, 2018
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
32 changes: 32 additions & 0 deletions aws/resource_aws_codedeploy_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,38 @@ func resourceAwsCodeDeployApp() *schema.Resource {
Read: resourceAwsCodeDeployAppRead,
Update: resourceAwsCodeDeployUpdate,
Delete: resourceAwsCodeDeployAppDelete,
Importer: &schema.ResourceImporter{
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
idParts := strings.Split(d.Id(), ":")

if len(idParts) == 2 {
return []*schema.ResourceData{d}, nil
}

applicationName := d.Id()
conn := meta.(*AWSClient).codedeployconn

input := &codedeploy.GetApplicationInput{
ApplicationName: aws.String(applicationName),
}

log.Printf("[DEBUG] Reading CodeDeploy Application: %s", input)
output, err := conn.GetApplication(input)

if err != nil {
return []*schema.ResourceData{}, err
}

if output == nil || output.Application == nil {
return []*schema.ResourceData{}, fmt.Errorf("error reading CodeDeploy Application (%s): empty response", applicationName)
}

d.SetId(fmt.Sprintf("%s:%s", aws.StringValue(output.Application.ApplicationId), applicationName))
d.Set("name", applicationName)

return []*schema.ResourceData{d}, nil
},
},

Schema: map[string]*schema.Schema{
"name": {
Expand Down
152 changes: 116 additions & 36 deletions aws/resource_aws_codedeploy_app_test.go
Original file line number Diff line number Diff line change
@@ -1,64 +1,117 @@
package aws

import (
"errors"
"fmt"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/codedeploy"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccAWSCodeDeployApp_basic(t *testing.T) {
var application1 codedeploy.ApplicationInfo
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_codedeploy_app.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCodeDeployAppDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSCodeDeployApp,
Config: testAccAWSCodeDeployAppConfigName(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("aws_codedeploy_app.foo", "compute_platform", "Server"),
testAccCheckAWSCodeDeployAppExists("aws_codedeploy_app.foo"),
testAccCheckAWSCodeDeployAppExists(resourceName, &application1),
resource.TestCheckResourceAttr(resourceName, "compute_platform", "Server"),
resource.TestCheckResourceAttr(resourceName, "name", rName),
),
},
// Import by ID
{
Config: testAccAWSCodeDeployAppModified,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeDeployAppExists("aws_codedeploy_app.foo"),
),
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
// Import by name
{
ResourceName: resourceName,
ImportState: true,
ImportStateId: rName,
ImportStateVerify: true,
},
},
})
}

func TestAccAWSCodeDeployApp_computePlatform(t *testing.T) {
rName := acctest.RandString(5)
var application1, application2 codedeploy.ApplicationInfo
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_codedeploy_app.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCodeDeployAppDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSCodeDeployAppConfigComputePlatform(rName, "Lambda"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeDeployAppExists(resourceName, &application1),
resource.TestCheckResourceAttr(resourceName, "compute_platform", "Lambda"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAWSCodeDeployAppConfigComputePlatform(rName, "Server"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeDeployAppExists("aws_codedeploy_app.foo"),
resource.TestCheckResourceAttr(
"aws_codedeploy_app.foo", "compute_platform", "Server"),
testAccCheckAWSCodeDeployAppExists(resourceName, &application2),
testAccCheckAWSCodeDeployAppRecreated(&application1, &application2),
resource.TestCheckResourceAttr(resourceName, "compute_platform", "Server"),
),
},
},
})
}

func TestAccAWSCodeDeployApp_name(t *testing.T) {
var application1, application2 codedeploy.ApplicationInfo
rName1 := acctest.RandomWithPrefix("tf-acc-test")
rName2 := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_codedeploy_app.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCodeDeployAppDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSCodeDeployAppConfigName(rName1),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeDeployAppExists(resourceName, &application1),
resource.TestCheckResourceAttr(resourceName, "name", rName1),
),
},
{
Config: testAccAWSCodeDeployAppConfigComputePlatform(rName, "Lambda"),
Config: testAccAWSCodeDeployAppConfigName(rName2),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeDeployAppExists("aws_codedeploy_app.foo"),
resource.TestCheckResourceAttr(
"aws_codedeploy_app.foo", "compute_platform", "Lambda"),
testAccCheckAWSCodeDeployAppExists(resourceName, &application2),
testAccCheckAWSCodeDeployAppRecreated(&application1, &application2),
resource.TestCheckResourceAttr(resourceName, "name", rName2),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand All @@ -75,11 +128,11 @@ func testAccCheckAWSCodeDeployAppDestroy(s *terraform.State) error {
ApplicationName: aws.String(rs.Primary.Attributes["name"]),
})

if isAWSErr(err, codedeploy.ErrCodeApplicationDoesNotExistException, "") {
continue
}

if err != nil {
// Verify the error is what we want
if ae, ok := err.(awserr.Error); ok && ae.Code() == "ApplicationDoesNotExistException" {
continue
}
return err
}

Expand All @@ -89,31 +142,58 @@ func testAccCheckAWSCodeDeployAppDestroy(s *terraform.State) error {
return nil
}

func testAccCheckAWSCodeDeployAppExists(name string) resource.TestCheckFunc {
func testAccCheckAWSCodeDeployAppExists(name string, application *codedeploy.ApplicationInfo) resource.TestCheckFunc {
return func(s *terraform.State) error {
_, ok := s.RootModule().Resources[name]
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}

conn := testAccProvider.Meta().(*AWSClient).codedeployconn

input := &codedeploy.GetApplicationInput{
ApplicationName: aws.String(rs.Primary.Attributes["name"]),
}

output, err := conn.GetApplication(input)

if err != nil {
return err
}

if output == nil || output.Application == nil {
return fmt.Errorf("error reading CodeDeploy Application (%s): empty response", rs.Primary.ID)
}

*application = *output.Application

return nil
}
}

func testAccAWSCodeDeployAppConfigComputePlatform(rName string, value string) string {
return fmt.Sprintf(`
resource "aws_codedeploy_app" "foo" {
name = "test-codedeploy-app-%s"
compute_platform = "%s"
}`, rName, value)
func testAccCheckAWSCodeDeployAppRecreated(i, j *codedeploy.ApplicationInfo) resource.TestCheckFunc {
return func(s *terraform.State) error {
if aws.TimeValue(i.CreateTime) == aws.TimeValue(j.CreateTime) {
return errors.New("CodeDeploy Application was not recreated")
}

return nil
}
}

var testAccAWSCodeDeployApp = `
resource "aws_codedeploy_app" "foo" {
name = "foo"
}`
func testAccAWSCodeDeployAppConfigComputePlatform(rName string, computePlatform string) string {
return fmt.Sprintf(`
resource "aws_codedeploy_app" "test" {
compute_platform = %q
name = %q
}
`, computePlatform, rName)
}

var testAccAWSCodeDeployAppModified = `
resource "aws_codedeploy_app" "foo" {
name = "bar"
}`
func testAccAWSCodeDeployAppConfigName(rName string) string {
return fmt.Sprintf(`
resource "aws_codedeploy_app" "test" {
name = %q
}
`, rName)
}
3 changes: 3 additions & 0 deletions aws/resource_aws_codedeploy_deployment_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ func resourceAwsCodeDeployDeploymentConfig() *schema.Resource {
Create: resourceAwsCodeDeployDeploymentConfigCreate,
Read: resourceAwsCodeDeployDeploymentConfigRead,
Delete: resourceAwsCodeDeployDeploymentConfigDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"deployment_config_name": {
Expand Down
Loading