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

r/codedeploy_app - add arn attribute & tagging support & sweeper #18564

Merged
merged 10 commits into from
Apr 14, 2021
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
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())
DrFaust92 marked this conversation as resolved.
Show resolved Hide resolved
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)

DrFaust92 marked this conversation as resolved.
Show resolved Hide resolved
if !strings.Contains(d.Id(), appName) {
d.SetId(fmt.Sprintf("%s:%s", aws.StringValue(app.ApplicationId), appName))
}

appArn := arn.ARN{
DrFaust92 marked this conversation as resolved.
Show resolved Hide resolved
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{
DrFaust92 marked this conversation as resolved.
Show resolved Hide resolved
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