From 23a781c0c93b327f52f773984f1934daaa82dd5d Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Sun, 4 Apr 2021 21:43:17 +0300 Subject: [PATCH 01/10] add tags + arn --- aws/resource_aws_codedeploy_app.go | 98 +++++++++++++++---------- aws/resource_aws_codedeploy_app_test.go | 97 ++++++++++++++++++++++++ 2 files changed, 156 insertions(+), 39 deletions(-) diff --git a/aws/resource_aws_codedeploy_app.go b/aws/resource_aws_codedeploy_app.go index 251ab724852..73fe9b81f43 100644 --- a/aws/resource_aws_codedeploy_app.go +++ b/aws/resource_aws_codedeploy_app.go @@ -6,10 +6,11 @@ import ( "strings" "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/aws/awserr" + "github.com/aws/aws-sdk-go/aws/arn" "github.com/aws/aws-sdk-go/service/codedeploy" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags" ) func resourceAwsCodeDeployApp() *schema.Resource { @@ -52,30 +53,37 @@ func resourceAwsCodeDeployApp() *schema.Resource { }, Schema: map[string]*schema.Schema{ - "name": { + "arn": { Type: schema.TypeString, - Required: true, - ForceNew: true, + Computed: true, }, - - "compute_platform": { + "application_id": { Type: schema.TypeString, - Optional: true, - ForceNew: true, - ValidateFunc: validation.StringInSlice([]string{ - codedeploy.ComputePlatformEcs, - codedeploy.ComputePlatformLambda, - codedeploy.ComputePlatformServer, - }, false), - Default: codedeploy.ComputePlatformServer, + Computed: true, + }, + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringLenBetween(1, 100), }, - // The unique ID is set by AWS on create. - "unique_id": { + "compute_platform": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + ValidateFunc: validation.StringInSlice(codedeploy.ComputePlatform_Values(), false), + Default: codedeploy.ComputePlatformServer, + }, + "github_account_name": { Type: schema.TypeString, - Optional: true, Computed: true, }, + "linked_to_github": { + Type: schema.TypeBool, + Computed: true, + }, + "tags": tagsSchema(), }, } } @@ -90,6 +98,7 @@ func resourceAwsCodeDeployAppCreate(d *schema.ResourceData, meta interface{}) er resp, err := conn.CreateApplication(&codedeploy.CreateApplicationInput{ ApplicationName: aws.String(application), ComputePlatform: aws.String(computePlatform), + Tags: keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().CodedeployTags(), }) if err != nil { return err @@ -101,7 +110,7 @@ func resourceAwsCodeDeployAppCreate(d *schema.ResourceData, meta interface{}) er // the state file. This allows us to reliably detect both when the TF // config file changes and when the user deletes the app without removing // it first from the TF config. - d.SetId(fmt.Sprintf("%s:%s", *resp.ApplicationId, application)) + d.SetId(fmt.Sprintf("%s:%s", aws.StringValue(resp.ApplicationId), application)) return resourceAwsCodeDeployAppRead(d, meta) } @@ -115,17 +124,33 @@ func resourceAwsCodeDeployAppRead(d *schema.ResourceData, meta interface{}) erro ApplicationName: aws.String(application), }) if err != nil { - if codedeployerr, ok := err.(awserr.Error); ok && codedeployerr.Code() == "ApplicationDoesNotExistException" { + if isAWSErr(err, codedeploy.ErrCodeApplicationDoesNotExistException, "") { d.SetId("") + log.Printf("[WARN] CodeDeploy Application (%s) not found, removing from state", d.Id()) return nil - } else { - log.Printf("[ERROR] Error finding CodeDeploy application: %s", err) - return err } + + log.Printf("[ERROR] Error finding CodeDeploy application: %s", err) + return err } - d.Set("compute_platform", resp.Application.ComputePlatform) - d.Set("name", resp.Application.ApplicationName) + app := resp.Application + appName := aws.StringValue(app.ApplicationName) + + appArn := arn.ARN{ + Partition: meta.(*AWSClient).partition, + Service: "codedeploy", + Region: meta.(*AWSClient).region, + AccountID: meta.(*AWSClient).accountid, + Resource: fmt.Sprintf("application:%s", appName), + }.String() + + d.Set("arn", appArn) + d.Set("application_id", app.ApplicationId) + d.Set("compute_platform", app.ComputePlatform) + d.Set("name", appName) + d.Set("github_account_name", app.GitHubAccountName) + d.Set("linked_to_github", app.LinkedToGitHub) return nil } @@ -133,20 +158,15 @@ func resourceAwsCodeDeployAppRead(d *schema.ResourceData, meta interface{}) erro func resourceAwsCodeDeployUpdate(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).codedeployconn - o, n := d.GetChange("name") + if d.HasChange("tags") { + o, n := d.GetChange("tags") - _, err := conn.UpdateApplication(&codedeploy.UpdateApplicationInput{ - ApplicationName: aws.String(o.(string)), - NewApplicationName: aws.String(n.(string)), - }) - if err != nil { - return err + if err := keyvaluetags.CodedeployUpdateTags(conn, d.Get("arn").(string), o, n); err != nil { + return fmt.Errorf("error updating CodeDeploy Application (%s) tags: %w", d.Get("arn").(string), err) + } } - log.Printf("[DEBUG] CodeDeploy application %s updated", n) - - d.Set("name", n) - return nil + return resourceAwsCodeDeployAppRead(d, meta) } func resourceAwsCodeDeployAppDelete(d *schema.ResourceData, meta interface{}) error { @@ -156,12 +176,12 @@ func resourceAwsCodeDeployAppDelete(d *schema.ResourceData, meta interface{}) er ApplicationName: aws.String(d.Get("name").(string)), }) if err != nil { - if cderr, ok := err.(awserr.Error); ok && cderr.Code() == "InvalidApplicationNameException" { + if isAWSErr(err, codedeploy.ErrCodeApplicationDoesNotExistException, "") { return nil - } else { - log.Printf("[ERROR] Error deleting CodeDeploy application: %s", err) - return err } + + log.Printf("[ERROR] Error deleting CodeDeploy application: %s", err) + return err } return nil diff --git a/aws/resource_aws_codedeploy_app_test.go b/aws/resource_aws_codedeploy_app_test.go index 61db3b4c141..19f05286a69 100644 --- a/aws/resource_aws_codedeploy_app_test.go +++ b/aws/resource_aws_codedeploy_app_test.go @@ -27,8 +27,12 @@ func TestAccAWSCodeDeployApp_basic(t *testing.T) { Config: testAccAWSCodeDeployAppConfigName(rName), Check: resource.ComposeTestCheckFunc( testAccCheckAWSCodeDeployAppExists(resourceName, &application1), + testAccCheckResourceAttrRegionalARN(resourceName, "arn", "codedeploy", fmt.Sprintf(`application:%s`, rName)), resource.TestCheckResourceAttr(resourceName, "compute_platform", "Server"), resource.TestCheckResourceAttr(resourceName, "name", rName), + resource.TestCheckResourceAttr(resourceName, "linked_to_github", "false"), + resource.TestCheckResourceAttr(resourceName, "tags.%", "0"), + resource.TestCheckResourceAttrSet(resourceName, "application_id"), ), }, // Import by ID @@ -168,6 +172,74 @@ func TestAccAWSCodeDeployApp_name(t *testing.T) { }) } +func TestAccAWSCodeDeployApp_tags(t *testing.T) { + var application codedeploy.ApplicationInfo + rName := acctest.RandomWithPrefix("tf-acc-test") + resourceName := "aws_codedeploy_app.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ErrorCheck: testAccErrorCheck(t, codedeploy.EndpointsID), + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSCodeDeployAppDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSCodeDeployAppConfigTags1(rName, "key1", "value1"), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSCodeDeployAppExists(resourceName, &application), + resource.TestCheckResourceAttr(resourceName, "tags.%", "1"), + resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccAWSCodeDeployAppConfigTags2(rName, "key1", "value1updated", "key2", "value2"), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSCodeDeployAppExists(resourceName, &application), + resource.TestCheckResourceAttr(resourceName, "tags.%", "2"), + resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1updated"), + resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"), + ), + }, + { + Config: testAccAWSCodeDeployAppConfigTags1(rName, "key2", "value2"), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSCodeDeployAppExists(resourceName, &application), + resource.TestCheckResourceAttr(resourceName, "tags.%", "1"), + resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"), + ), + }, + }, + }) +} + +func TestAccAWSCodeDeployApp_disappears(t *testing.T) { + var application1 codedeploy.ApplicationInfo + rName := acctest.RandomWithPrefix("tf-acc-test") + resourceName := "aws_codedeploy_app.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ErrorCheck: testAccErrorCheck(t, codedeploy.EndpointsID), + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSCodeDeployAppDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSCodeDeployAppConfigName(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSCodeDeployAppExists(resourceName, &application1), + testAccCheckResourceDisappears(testAccProvider, resourceAwsAmi(), resourceName), + ), + ExpectNonEmptyPlan: true, + }, + }, + }) +} + func testAccCheckAWSCodeDeployAppDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).codedeployconn @@ -249,3 +321,28 @@ resource "aws_codedeploy_app" "test" { } `, rName) } + +func testAccAWSCodeDeployAppConfigTags1(rName, tagKey1, tagValue1 string) string { + return fmt.Sprintf(` +resource "aws_codedeploy_app" "test" { + name = %[1]q + + tags = { + %[2]q = %[3]q + } +} +`, rName, tagKey1, tagValue1) +} + +func testAccAWSCodeDeployAppConfigTags2(rName, tagKey1, tagValue1, tagKey2, tagValue2 string) string { + return fmt.Sprintf(` +resource "aws_codedeploy_app" "test" { + name = %[1]q + + tags = { + %[2]q = %[3]q + %[4]q = %[5]q + } +} +`, rName, tagKey1, tagValue1, tagKey2, tagValue2) +} From d9cd9952a22657f1b95291ceb5c906bde37b4365 Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Sun, 4 Apr 2021 21:47:42 +0300 Subject: [PATCH 02/10] docs --- website/docs/r/codedeploy_app.html.markdown | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/website/docs/r/codedeploy_app.html.markdown b/website/docs/r/codedeploy_app.html.markdown index 1003a2d9930..54d2dff8190 100644 --- a/website/docs/r/codedeploy_app.html.markdown +++ b/website/docs/r/codedeploy_app.html.markdown @@ -45,13 +45,18 @@ The following arguments are supported: * `name` - (Required) The name of the application. * `compute_platform` - (Optional) The compute platform can either be `ECS`, `Lambda`, or `Server`. Default is `Server`. +* `tags` - (Optional) Key-value map of resource tags ## Attributes Reference In addition to all arguments above, the following attributes are exported: +* `arn` - The ARN of the CodeDeploy application. +* `application_id` - The application ID. * `id` - Amazon's assigned ID for the application. * `name` - The application's name. +* `github_account_name` - The name for a connection to a GitHub account. +* `linked_to_github` - Whether the user has authenticated with GitHub for the specified application. ## Import From b54d80792c492640b751e601918f94bfb5a4f1d5 Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Mon, 5 Apr 2021 01:19:41 +0300 Subject: [PATCH 03/10] fix tests + add tag read --- aws/resource_aws_codedeploy_app.go | 11 +++++++++++ aws/resource_aws_codedeploy_app_test.go | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/aws/resource_aws_codedeploy_app.go b/aws/resource_aws_codedeploy_app.go index 73fe9b81f43..3e6fa8dfcb1 100644 --- a/aws/resource_aws_codedeploy_app.go +++ b/aws/resource_aws_codedeploy_app.go @@ -117,6 +117,7 @@ func resourceAwsCodeDeployAppCreate(d *schema.ResourceData, meta interface{}) er func resourceAwsCodeDeployAppRead(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).codedeployconn + ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig application := resourceAwsCodeDeployAppParseId(d.Id()) log.Printf("[DEBUG] Reading CodeDeploy application %s", application) @@ -152,6 +153,16 @@ func resourceAwsCodeDeployAppRead(d *schema.ResourceData, meta interface{}) erro d.Set("github_account_name", app.GitHubAccountName) d.Set("linked_to_github", app.LinkedToGitHub) + tags, err := keyvaluetags.CodedeployListTags(conn, appArn) + + if err != nil { + return fmt.Errorf("error listing tags for CodeDeploy application (%s): %w", d.Id(), err) + } + + if err := d.Set("tags", tags.IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil { + return fmt.Errorf("error setting tags: %w", err) + } + return nil } diff --git a/aws/resource_aws_codedeploy_app_test.go b/aws/resource_aws_codedeploy_app_test.go index 19f05286a69..dbf79704ee9 100644 --- a/aws/resource_aws_codedeploy_app_test.go +++ b/aws/resource_aws_codedeploy_app_test.go @@ -232,7 +232,7 @@ func TestAccAWSCodeDeployApp_disappears(t *testing.T) { Config: testAccAWSCodeDeployAppConfigName(rName), Check: resource.ComposeTestCheckFunc( testAccCheckAWSCodeDeployAppExists(resourceName, &application1), - testAccCheckResourceDisappears(testAccProvider, resourceAwsAmi(), resourceName), + testAccCheckResourceDisappears(testAccProvider, resourceAwsCodeDeployApp(), resourceName), ), ExpectNonEmptyPlan: true, }, From 00629d442a4ed6c222b0e9f85e8122cedf1c38d8 Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Mon, 5 Apr 2021 01:25:19 +0300 Subject: [PATCH 04/10] changelog --- .changelog/18564.txt | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 .changelog/18564.txt diff --git a/.changelog/18564.txt b/.changelog/18564.txt new file mode 100644 index 00000000000..33d6bc7eec0 --- /dev/null +++ b/.changelog/18564.txt @@ -0,0 +1,11 @@ +```release-note:enhancement +resource/aws_codedeploy_app: Add `arn`, `linked_to_github`, `github_account_name`, `application_id` attributes +``` + +```release-note:enhancement +resource/aws_codedeploy_app: Add `tags` argument +``` + +```release-note:enhancement +resource/aws_codedeploy_app: Add plan time validation for `name` +``` \ No newline at end of file From b65fa587da3e49c2fc6371f9abc48e72b6f80c4d Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Mon, 5 Apr 2021 01:26:36 +0300 Subject: [PATCH 05/10] fmt --- aws/resource_aws_codedeploy_app_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws/resource_aws_codedeploy_app_test.go b/aws/resource_aws_codedeploy_app_test.go index dbf79704ee9..5c9e38d4738 100644 --- a/aws/resource_aws_codedeploy_app_test.go +++ b/aws/resource_aws_codedeploy_app_test.go @@ -341,7 +341,7 @@ resource "aws_codedeploy_app" "test" { tags = { %[2]q = %[3]q - %[4]q = %[5]q + %[4]q = %[5]q } } `, rName, tagKey1, tagValue1, tagKey2, tagValue2) From 3b32eb373b89b295a4b122a70658f97158811a6f Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Thu, 8 Apr 2021 11:35:19 +0300 Subject: [PATCH 06/10] add sweeper --- aws/resource_aws_codedeploy_app_test.go | 52 +++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/aws/resource_aws_codedeploy_app_test.go b/aws/resource_aws_codedeploy_app_test.go index 5c9e38d4738..9878e7eeea0 100644 --- a/aws/resource_aws_codedeploy_app_test.go +++ b/aws/resource_aws_codedeploy_app_test.go @@ -3,15 +3,67 @@ package aws import ( "errors" "fmt" + "log" "testing" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/codedeploy" + "github.com/hashicorp/go-multierror" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" ) +func init() { + resource.AddTestSweepers("aws_codedeploy_app", &resource.Sweeper{ + Name: "aws_codedeploy_app", + F: testSweepCodeDeployApps, + }) +} + +func testSweepCodeDeployApps(region string) error { + client, err := sharedClientForRegion(region) + if err != nil { + return fmt.Errorf("error getting client: %s", err) + } + conn := client.(*AWSClient).codedeployconn + input := &codedeploy.ListApplicationsInput{} + var sweeperErrs *multierror.Error + + err = conn.ListApplicationsPages(input, func(page *codedeploy.ListApplicationsOutput, lastPage bool) bool { + for _, app := range page.Applications { + if app == nil { + continue + } + + appName := aws.StringValue(app) + r := resourceAwsCodeDeployApp() + d := r.Data(nil) + d.SetId(fmt.Sprintf("%s:%s", "xxxx", appName)) + err = r.Delete(d, client) + + if err != nil { + sweeperErr := fmt.Errorf("error deleting CodeDeploy Application (%s): %w", appName, err) + log.Printf("[ERROR] %s", sweeperErr) + sweeperErrs = multierror.Append(sweeperErrs, sweeperErr) + } + } + + return !lastPage + }) + + if testSweepSkipSweepError(err) { + log.Printf("[WARN] Skipping CodeDeploy Application sweep for %s: %s", region, err) + return nil + } + + if err != nil { + return fmt.Errorf("error listing CodeDeploy Applications: %w", err) + } + + return sweeperErrs.ErrorOrNil() +} + func TestAccAWSCodeDeployApp_basic(t *testing.T) { var application1 codedeploy.ApplicationInfo rName := acctest.RandomWithPrefix("tf-acc-test") From 89b0e85c4fb2da8c1e58a6d58d614c2d70c9b931 Mon Sep 17 00:00:00 2001 From: Ilia Lazebnik Date: Wed, 14 Apr 2021 20:13:21 +0300 Subject: [PATCH 07/10] Update aws/resource_aws_codedeploy_app.go Co-authored-by: angie pinilla --- aws/resource_aws_codedeploy_app.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/aws/resource_aws_codedeploy_app.go b/aws/resource_aws_codedeploy_app.go index 3e6fa8dfcb1..b7c17e7c6ee 100644 --- a/aws/resource_aws_codedeploy_app.go +++ b/aws/resource_aws_codedeploy_app.go @@ -120,6 +120,9 @@ func resourceAwsCodeDeployAppRead(d *schema.ResourceData, meta interface{}) erro ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig application := resourceAwsCodeDeployAppParseId(d.Id()) + if application != d.Get("name").(string) { + application = d.Get("name").(string) + } log.Printf("[DEBUG] Reading CodeDeploy application %s", application) resp, err := conn.GetApplication(&codedeploy.GetApplicationInput{ ApplicationName: aws.String(application), From 3228ee7069e1bd7ac3c154b73269ceac8277fed0 Mon Sep 17 00:00:00 2001 From: Ilia Lazebnik Date: Wed, 14 Apr 2021 20:13:27 +0300 Subject: [PATCH 08/10] Update aws/resource_aws_codedeploy_app.go Co-authored-by: angie pinilla --- aws/resource_aws_codedeploy_app.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/aws/resource_aws_codedeploy_app.go b/aws/resource_aws_codedeploy_app.go index b7c17e7c6ee..1548827d0d1 100644 --- a/aws/resource_aws_codedeploy_app.go +++ b/aws/resource_aws_codedeploy_app.go @@ -141,6 +141,10 @@ func resourceAwsCodeDeployAppRead(d *schema.ResourceData, meta interface{}) erro app := resp.Application appName := aws.StringValue(app.ApplicationName) + if !strings.Contains(d.Id(), appName) { + d.SetId(fmt.Sprintf("%s:%s", aws.StringValue(app.ApplicationId), appName)) + } + appArn := arn.ARN{ Partition: meta.(*AWSClient).partition, Service: "codedeploy", From 16bf6817315291c974d054e680d8168cc913a305 Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Wed, 14 Apr 2021 20:22:23 +0300 Subject: [PATCH 09/10] docs --- website/docs/index.html.markdown | 1 + 1 file changed, 1 insertion(+) diff --git a/website/docs/index.html.markdown b/website/docs/index.html.markdown index 44d0fec791d..a81e54a0c20 100644 --- a/website/docs/index.html.markdown +++ b/website/docs/index.html.markdown @@ -254,6 +254,7 @@ This functionality is only supported in the following resources: - [`aws_apigatewayv2_stage` resource](/docs/providers/aws/r/apigatewayv2_stage.html) - [`aws_athena_workgroup` resource](/docs/providers/aws/r/athena_workgroup.html) - [`aws_budgets_budget` resource](/docs/providers/aws/r/budgets_budget.html) + - [`aws_codedeploy_app` resource](/docs/providers/aws/r/codedeploy_app.html) - [`aws_cognito_identity_pool` resource](/docs/providers/aws/r/cognito_identity_pool.html) - [`aws_cognito_user_pools` data source](/docs/providers/aws/d/cognito_user_pools.html) - [`aws_default_vpc_dhcp_options`](/docs/providers/aws/r/default_vpc_dhcp_options.html) From 72d4a8a06ff941edafdccc2f340907a76f39b5a4 Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Wed, 14 Apr 2021 20:33:35 +0300 Subject: [PATCH 10/10] fix name check on read --- aws/resource_aws_codedeploy_app.go | 27 ++++++++++++++++++------- aws/resource_aws_codedeploy_app_test.go | 1 - 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/aws/resource_aws_codedeploy_app.go b/aws/resource_aws_codedeploy_app.go index 1548827d0d1..d57cf3f37e4 100644 --- a/aws/resource_aws_codedeploy_app.go +++ b/aws/resource_aws_codedeploy_app.go @@ -64,7 +64,6 @@ func resourceAwsCodeDeployApp() *schema.Resource { "name": { Type: schema.TypeString, Required: true, - ForceNew: true, ValidateFunc: validation.StringLenBetween(1, 100), }, @@ -120,8 +119,9 @@ func resourceAwsCodeDeployAppRead(d *schema.ResourceData, meta interface{}) erro ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig application := resourceAwsCodeDeployAppParseId(d.Id()) - if application != d.Get("name").(string) { - application = d.Get("name").(string) + name := d.Get("name").(string) + if name != "" && application != name { + application = name } log.Printf("[DEBUG] Reading CodeDeploy application %s", application) resp, err := conn.GetApplication(&codedeploy.GetApplicationInput{ @@ -141,10 +141,10 @@ func resourceAwsCodeDeployAppRead(d *schema.ResourceData, meta interface{}) erro app := resp.Application appName := aws.StringValue(app.ApplicationName) - if !strings.Contains(d.Id(), appName) { - d.SetId(fmt.Sprintf("%s:%s", aws.StringValue(app.ApplicationId), appName)) - } - + if !strings.Contains(d.Id(), appName) { + d.SetId(fmt.Sprintf("%s:%s", aws.StringValue(app.ApplicationId), appName)) + } + appArn := arn.ARN{ Partition: meta.(*AWSClient).partition, Service: "codedeploy", @@ -176,6 +176,19 @@ func resourceAwsCodeDeployAppRead(d *schema.ResourceData, meta interface{}) erro func resourceAwsCodeDeployUpdate(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).codedeployconn + if d.HasChange("name") { + o, n := d.GetChange("name") + + _, err := conn.UpdateApplication(&codedeploy.UpdateApplicationInput{ + ApplicationName: aws.String(o.(string)), + NewApplicationName: aws.String(n.(string)), + }) + + if err != nil { + return fmt.Errorf("error updating CodeDeploy Application (%s) name: %w", d.Id(), err) + } + } + if d.HasChange("tags") { o, n := d.GetChange("tags") diff --git a/aws/resource_aws_codedeploy_app_test.go b/aws/resource_aws_codedeploy_app_test.go index 9878e7eeea0..1c5fcdf7c28 100644 --- a/aws/resource_aws_codedeploy_app_test.go +++ b/aws/resource_aws_codedeploy_app_test.go @@ -211,7 +211,6 @@ func TestAccAWSCodeDeployApp_name(t *testing.T) { Config: testAccAWSCodeDeployAppConfigName(rName2), Check: resource.ComposeTestCheckFunc( testAccCheckAWSCodeDeployAppExists(resourceName, &application2), - testAccCheckAWSCodeDeployAppRecreated(&application1, &application2), resource.TestCheckResourceAttr(resourceName, "name", rName2), ), },