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

New Resource: aws_amplify_backend_environment #11936

Merged
merged 5 commits into from
May 21, 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
3 changes: 3 additions & 0 deletions .changelog/11936.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_amplify_backend_environment
```
29 changes: 29 additions & 0 deletions aws/internal/service/amplify/finder/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,32 @@ func AppByID(conn *amplify.Amplify, id string) (*amplify.App, error) {

return output.App, nil
}

func BackendEnvironmentByAppIDAndEnvironmentName(conn *amplify.Amplify, appID, environmentName string) (*amplify.BackendEnvironment, error) {
input := &amplify.GetBackendEnvironmentInput{
AppId: aws.String(appID),
EnvironmentName: aws.String(environmentName),
}

output, err := conn.GetBackendEnvironment(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.BackendEnvironment == nil {
return nil, &resource.NotFoundError{
Message: "Empty result",
LastRequest: input,
}
}

return output.BackendEnvironment, nil
}
25 changes: 25 additions & 0 deletions aws/internal/service/amplify/id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package amplify

import (
"fmt"
"strings"
)

const backendEnvironmentResourceIDSeparator = "/"

func BackendEnvironmentCreateResourceID(appID, environmentName string) string {
parts := []string{appID, environmentName}
id := strings.Join(parts, backendEnvironmentResourceIDSeparator)

return id
}

func BackendEnvironmentParseResourceID(id string) (string, string, error) {
parts := strings.Split(id, backendEnvironmentResourceIDSeparator)

if len(parts) == 2 && parts[0] != "" && parts[1] != "" {
return parts[0], parts[1], nil
}

return "", "", fmt.Errorf("unexpected format for ID (%[1]s), expected APPID%[2]sENVIRONMENTNAME", id, backendEnvironmentResourceIDSeparator)
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ func Provider() *schema.Provider {
"aws_ami_from_instance": resourceAwsAmiFromInstance(),
"aws_ami_launch_permission": resourceAwsAmiLaunchPermission(),
"aws_amplify_app": resourceAwsAmplifyApp(),
"aws_amplify_backend_environment": resourceAwsAmplifyBackendEnvironment(),
"aws_api_gateway_account": resourceAwsApiGatewayAccount(),
"aws_api_gateway_api_key": resourceAwsApiGatewayApiKey(),
"aws_api_gateway_authorizer": resourceAwsApiGatewayAuthorizer(),
Expand Down
151 changes: 151 additions & 0 deletions aws/resource_aws_amplify_backend_environment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package aws

import (
"fmt"
"log"
"regexp"

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

func resourceAwsAmplifyBackendEnvironment() *schema.Resource {
return &schema.Resource{
Create: resourceAwsAmplifyBackendEnvironmentCreate,
Read: resourceAwsAmplifyBackendEnvironmentRead,
Delete: resourceAwsAmplifyBackendEnvironmentDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"app_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

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

"deployment_artifacts": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validation.StringMatch(regexp.MustCompile(`^[0-9A-Za-z-]{1,100}$`), "should be not be more than 100 alphanumeric or hyphen characters"),
},

"environment_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringMatch(regexp.MustCompile(`^[a-z]{2,10}$`), "should be between 2 and 10 characters (only lowercase alphabetic)"),
},

"stack_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validation.StringMatch(regexp.MustCompile(`^[0-9A-Za-z-]{1,100}$`), "should be not be more than 100 alphanumeric or hyphen characters"),
},
},
}
}

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

appID := d.Get("app_id").(string)
environmentName := d.Get("environment_name").(string)
id := tfamplify.BackendEnvironmentCreateResourceID(appID, environmentName)

input := &amplify.CreateBackendEnvironmentInput{
AppId: aws.String(appID),
EnvironmentName: aws.String(environmentName),
}

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

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

log.Printf("[DEBUG] Creating Amplify Backend Environment: %s", input)
_, err := conn.CreateBackendEnvironment(input)

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

d.SetId(id)

return resourceAwsAmplifyBackendEnvironmentRead(d, meta)
}

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

appID, environmentName, err := tfamplify.BackendEnvironmentParseResourceID(d.Id())

if err != nil {
return fmt.Errorf("error parsing Amplify Backend Environment ID: %w", err)
}

backendEnvironment, err := finder.BackendEnvironmentByAppIDAndEnvironmentName(conn, appID, environmentName)

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

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

d.Set("app_id", appID)
d.Set("arn", backendEnvironment.BackendEnvironmentArn)
d.Set("deployment_artifacts", backendEnvironment.DeploymentArtifacts)
d.Set("environment_name", backendEnvironment.EnvironmentName)
d.Set("stack_name", backendEnvironment.StackName)

return nil
}

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

appID, environmentName, err := tfamplify.BackendEnvironmentParseResourceID(d.Id())

if err != nil {
return fmt.Errorf("error parsing Amplify Backend Environment ID: %w", err)
}

log.Printf("[DEBUG] Deleting Amplify Backend Environment: %s", d.Id())
_, err = conn.DeleteBackendEnvironment(&amplify.DeleteBackendEnvironmentInput{
AppId: aws.String(appID),
EnvironmentName: aws.String(environmentName),
})

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

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

return nil
}
Loading