Skip to content

Commit

Permalink
Merge pull request #18564 from DrFaust92/r/codedeploy_app_arn_tags
Browse files Browse the repository at this point in the history
r/codedeploy_app - add arn attribute & tagging support & sweeper
  • Loading branch information
anGie44 committed Apr 14, 2021
2 parents 3bd7a92 + 72d4a8a commit 511c1d7
Show file tree
Hide file tree
Showing 5 changed files with 255 additions and 39 deletions.
11 changes: 11 additions & 0 deletions .changelog/18564.txt
Original file line number Diff line number Diff line change
@@ -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`
```
127 changes: 89 additions & 38 deletions aws/resource_aws_codedeploy_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -52,30 +53,36 @@ 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,
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(),
},
}
}
Expand All @@ -90,6 +97,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
Expand All @@ -101,52 +109,95 @@ 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)
}

func resourceAwsCodeDeployAppRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).codedeployconn
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

application := resourceAwsCodeDeployAppParseId(d.Id())
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{
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
}

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",
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)

tags, err := keyvaluetags.CodedeployListTags(conn, appArn)

if err != nil {
return fmt.Errorf("error listing tags for CodeDeploy application (%s): %w", d.Id(), err)
}

d.Set("compute_platform", resp.Application.ComputePlatform)
d.Set("name", resp.Application.ApplicationName)
if err := d.Set("tags", tags.IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %w", err)
}

return nil
}

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

o, n := d.GetChange("name")
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 err
_, 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)
}
}
log.Printf("[DEBUG] CodeDeploy application %s updated", n)

d.Set("name", n)
if d.HasChange("tags") {
o, n := d.GetChange("tags")

return nil
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)
}
}

return resourceAwsCodeDeployAppRead(d, meta)
}

func resourceAwsCodeDeployAppDelete(d *schema.ResourceData, meta interface{}) error {
Expand All @@ -156,12 +207,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
Expand Down
Loading

0 comments on commit 511c1d7

Please sign in to comment.