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

Refactor CodePipeline pipeline and webhook resources to use keyvaluetags package and fix webhook resource recreation issues #11387

Merged
merged 5 commits into from
Jan 11, 2020
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
52 changes: 37 additions & 15 deletions aws/resource_aws_codepipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/codepipeline"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func resourceAwsCodePipeline() *schema.Resource {
Expand Down Expand Up @@ -170,7 +170,7 @@ func resourceAwsCodePipelineCreate(d *schema.ResourceData, meta interface{}) err
conn := meta.(*AWSClient).codepipelineconn
params := &codepipeline.CreatePipelineInput{
Pipeline: expandAwsCodePipeline(d),
Tags: tagsFromMapCodePipeline(d.Get("tags").(map[string]interface{})),
Tags: keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().CodepipelineTags(),
}

var resp *codepipeline.CreatePipelineOutput
Expand All @@ -195,7 +195,8 @@ func resourceAwsCodePipelineCreate(d *schema.ResourceData, meta interface{}) err
return fmt.Errorf("Error creating CodePipeline: invalid response from AWS")
}

d.SetId(*resp.Pipeline.Name)
d.SetId(aws.StringValue(resp.Pipeline.Name))

return resourceAwsCodePipelineRead(d, meta)
}

Expand Down Expand Up @@ -429,15 +430,16 @@ func resourceAwsCodePipelineRead(d *schema.ResourceData, meta interface{}) error
Name: aws.String(d.Id()),
})

if isAWSErr(err, codepipeline.ErrCodePipelineNotFoundException, "") {
log.Printf("[WARN] Codepipeline (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
pipelineerr, ok := err.(awserr.Error)
if ok && pipelineerr.Code() == "PipelineNotFoundException" {
log.Printf("[INFO] Codepipeline %q not found", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("Error retreiving Pipeline: %q", err)
return fmt.Errorf("error reading Codepipeline: %s", err)
}

metadata := resp.Metadata
pipeline := resp.Pipeline

Expand All @@ -449,12 +451,19 @@ func resourceAwsCodePipelineRead(d *schema.ResourceData, meta interface{}) error
return err
}

d.Set("arn", metadata.PipelineArn)
arn := aws.StringValue(metadata.PipelineArn)
d.Set("arn", arn)
d.Set("name", pipeline.Name)
d.Set("role_arn", pipeline.RoleArn)

if err := saveTagsCodePipeline(conn, d); err != nil {
return err
tags, err := keyvaluetags.CodepipelineListTags(conn, arn)

if err != nil {
return fmt.Errorf("error listing tags for Codepipeline (%s): %s", arn, err)
}

if err := d.Set("tags", tags.IgnoreAws().Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

return nil
Expand All @@ -475,8 +484,13 @@ func resourceAwsCodePipelineUpdate(d *schema.ResourceData, meta interface{}) err
d.Id(), err)
}

if err := setTagsCodePipeline(conn, d); err != nil {
return fmt.Errorf("Error updating CodePipeline tags: %s", d.Id())
arn := d.Get("arn").(string)
if d.HasChange("tags") {
o, n := d.GetChange("tags")

if err := keyvaluetags.CodepipelineUpdateTags(conn, arn, o, n); err != nil {
return fmt.Errorf("error updating Codepipeline (%s) tags: %s", arn, err)
}
}

return resourceAwsCodePipelineRead(d, meta)
Expand All @@ -489,5 +503,13 @@ func resourceAwsCodePipelineDelete(d *schema.ResourceData, meta interface{}) err
Name: aws.String(d.Id()),
})

if isAWSErr(err, codepipeline.ErrCodePipelineNotFoundException, "") {
return nil
}

if err != nil {
return fmt.Errorf("error deleting Codepipeline (%s): %s", d.Id(), err)
}

return err
}
29 changes: 21 additions & 8 deletions aws/resource_aws_codepipeline_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func resourceAwsCodePipelineWebhook() *schema.Resource {
return &schema.Resource{
Create: resourceAwsCodePipelineWebhookCreate,
Read: resourceAwsCodePipelineWebhookRead,
Update: nil,
Update: resourceAwsCodePipelineWebhookUpdate,
Delete: resourceAwsCodePipelineWebhookDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
Expand Down Expand Up @@ -43,11 +44,13 @@ func resourceAwsCodePipelineWebhook() *schema.Resource {
"secret_token": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recreate resource when secret_token changes.

Sensitive: true,
},
"allowed_ip_range": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recreate resource when allowed_ip_range changes.

ValidateFunc: validation.CIDRNetwork(0, 32),
},
},
Expand Down Expand Up @@ -93,11 +96,7 @@ func resourceAwsCodePipelineWebhook() *schema.Resource {
ForceNew: true,
Required: true,
},
"tags": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to recreate resource when tags change.

},
"tags": tagsSchema(),
},
}
}
Expand Down Expand Up @@ -149,7 +148,7 @@ func resourceAwsCodePipelineWebhookCreate(d *schema.ResourceData, meta interface
TargetPipeline: aws.String(d.Get("target_pipeline").(string)),
AuthenticationConfiguration: extractCodePipelineWebhookAuthConfig(authType, authConfig),
},
Tags: tagsFromMapCodePipeline(d.Get("tags").(map[string]interface{})),
Tags: keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().CodepipelineTags(),
}

webhook, err := conn.PutWebhook(request)
Expand Down Expand Up @@ -271,13 +270,27 @@ func resourceAwsCodePipelineWebhookRead(d *schema.ResourceData, meta interface{}
return fmt.Errorf("error setting filter: %s", err)
}

if err := d.Set("tags", tagsToMapCodePipeline(webhook.Tags)); err != nil {
if err := d.Set("tags", keyvaluetags.CodepipelineKeyValueTags(webhook.Tags).IgnoreAws().Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

return nil
}

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

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

if err := keyvaluetags.CodepipelineUpdateTags(conn, d.Id(), o, n); err != nil {
return fmt.Errorf("error updating CodePipeline Webhook (%s) tags: %s", d.Id(), err)
}
}

return resourceAwsCodePipelineWebhookRead(d, meta)
}

func resourceAwsCodePipelineWebhookDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).codepipelineconn
name := d.Get("name").(string)
Expand Down
Loading