Skip to content

Commit

Permalink
Merge pull request #11939 from k24d/f-amplify-webhook
Browse files Browse the repository at this point in the history
New Resource: aws_amplify_webhook
  • Loading branch information
ewbankkit authored Jun 2, 2021
2 parents 46f9ca6 + 4882b12 commit d6c8885
Show file tree
Hide file tree
Showing 8 changed files with 473 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .changelog/11937.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ aws_amplify_branch
```

```release-note:bug
resource/aws_amplify_app: Mark the `enable_performance_mode` argumnet in the `auto_branch_creation_config` configuration block as `ForceNew`
resource/aws_amplify_app: Mark the `enable_performance_mode` argument in the `auto_branch_creation_config` configuration block as `ForceNew`
```
3 changes: 3 additions & 0 deletions .changelog/11939.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_amplify_webhook
```
28 changes: 28 additions & 0 deletions aws/internal/service/amplify/finder/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,31 @@ func BranchByAppIDAndBranchName(conn *amplify.Amplify, appID, branchName string)

return output.Branch, nil
}

func WebhookByID(conn *amplify.Amplify, id string) (*amplify.Webhook, error) {
input := &amplify.GetWebhookInput{
WebhookId: aws.String(id),
}

output, err := conn.GetWebhook(input)

if tfawserr.ErrCodeEquals(err, amplify.ErrCodeNotFoundException) {
return nil, &resource.NotFoundError{
LastError: err,
LastRequest: input,
}
}

if err != nil {
return nil, err
}

if output == nil || output.Webhook == nil {
return nil, &resource.NotFoundError{
Message: "Empty result",
LastRequest: input,
}
}

return output.Webhook, nil
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ func Provider() *schema.Provider {
"aws_amplify_app": resourceAwsAmplifyApp(),
"aws_amplify_backend_environment": resourceAwsAmplifyBackendEnvironment(),
"aws_amplify_branch": resourceAwsAmplifyBranch(),
"aws_amplify_webhook": resourceAwsAmplifyWebhook(),
"aws_api_gateway_account": resourceAwsApiGatewayAccount(),
"aws_api_gateway_api_key": resourceAwsApiGatewayApiKey(),
"aws_api_gateway_authorizer": resourceAwsApiGatewayAuthorizer(),
Expand Down
5 changes: 5 additions & 0 deletions aws/resource_aws_amplify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ func TestAccAWSAmplify_serial(t *testing.T) {
"EnvironmentVariables": testAccAWSAmplifyBranch_EnvironmentVariables,
"OptionalArguments": testAccAWSAmplifyBranch_OptionalArguments,
},
"Webhook": {
"basic": testAccAWSAmplifyWebhook_basic,
"disappears": testAccAWSAmplifyWebhook_disappears,
"update": testAccAWSAmplifyWebhook_update,
},
}

for group, m := range testCases {
Expand Down
164 changes: 164 additions & 0 deletions aws/resource_aws_amplify_webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package aws

import (
"fmt"
"log"
"regexp"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/amplify"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"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/service/amplify/finder"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/tfresource"
)

func resourceAwsAmplifyWebhook() *schema.Resource {
return &schema.Resource{
Create: resourceAwsAmplifyWebhookCreate,
Read: resourceAwsAmplifyWebhookRead,
Update: resourceAwsAmplifyWebhookUpdate,
Delete: resourceAwsAmplifyWebhookDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},

"app_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"branch_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringMatch(regexp.MustCompile(`^[0-9A-Za-z/_.-]{1,255}$`), "should be not be more than 255 letters, numbers, and the symbols /_.-"),
},

"description": {
Type: schema.TypeString,
Optional: true,
},

"url": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

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

input := &amplify.CreateWebhookInput{
AppId: aws.String(d.Get("app_id").(string)),
BranchName: aws.String(d.Get("branch_name").(string)),
}

if v, ok := d.GetOk("description"); ok {
input.Description = aws.String(v.(string))
}

log.Printf("[DEBUG] Creating Amplify Webhook: %s", input)
output, err := conn.CreateWebhook(input)

if err != nil {
return fmt.Errorf("error creating Amplify Webhook: %w", err)
}

d.SetId(aws.StringValue(output.Webhook.WebhookId))

return resourceAwsAmplifyWebhookRead(d, meta)
}

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

webhook, err := finder.WebhookByID(conn, d.Id())

if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] Amplify Webhook (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
return fmt.Errorf("error reading Amplify Webhook (%s): %w", d.Id(), err)
}

webhookArn := aws.StringValue(webhook.WebhookArn)
arn, err := arn.Parse(webhookArn)

if err != nil {
return fmt.Errorf("error parsing %q: %w", webhookArn, err)
}

// arn:${Partition}:amplify:${Region}:${Account}:apps/${AppId}/webhooks/${WebhookId}
parts := strings.Split(arn.Resource, "/")

if len(parts) != 4 {
return fmt.Errorf("unexpected format for ARN resource (%s)", arn.Resource)
}

d.Set("app_id", parts[1])
d.Set("arn", webhookArn)
d.Set("branch_name", webhook.BranchName)
d.Set("description", webhook.Description)
d.Set("url", webhook.WebhookUrl)

return nil
}

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

input := &amplify.UpdateWebhookInput{
WebhookId: aws.String(d.Id()),
}

if d.HasChange("branch_name") {
input.BranchName = aws.String(d.Get("branch_name").(string))
}

if d.HasChange("description") {
input.Description = aws.String(d.Get("description").(string))
}

log.Printf("[DEBUG] Updating Amplify Webhook: %s", input)
_, err := conn.UpdateWebhook(input)

if err != nil {
return fmt.Errorf("error updating Amplify Webhook (%s): %w", d.Id(), err)
}

return resourceAwsAmplifyWebhookRead(d, meta)
}

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

log.Printf("[DEBUG] Deleting Amplify Webhook: %s", d.Id())
_, err := conn.DeleteWebhook(&amplify.DeleteWebhookInput{
WebhookId: aws.String(d.Id()),
})

if tfawserr.ErrCodeEquals(err, amplify.ErrCodeNotFoundException) {
return nil
}

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

return nil
}
Loading

0 comments on commit d6c8885

Please sign in to comment.