Skip to content

Commit

Permalink
r/aws_amplify_app: Standard attribute handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
ewbankkit committed May 19, 2021
1 parent 5754c4a commit 4f7eda0
Showing 1 changed file with 230 additions and 10 deletions.
240 changes: 230 additions & 10 deletions aws/resource_aws_amplify_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aws
import (
"fmt"
"log"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/amplify"
Expand Down Expand Up @@ -100,13 +101,14 @@ func resourceAwsAmplifyApp() *schema.Resource {
"stage": {
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
// stage is "NONE" by default
if old == "NONE" && new == "" {
return true
}
return old == new
},
//TODO
// DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
// // stage is "NONE" by default
// if old == "NONE" && new == "" {
// return true
// }
// return old == new
// },
ValidateFunc: validation.StringInSlice(amplify.Stage_Values(), false),
},
},
Expand Down Expand Up @@ -259,7 +261,7 @@ func resourceAwsAmplifyApp() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"branch_name": {
Type: schema.TypeBool,
Type: schema.TypeString,
Computed: true,
},

Expand All @@ -274,7 +276,7 @@ func resourceAwsAmplifyApp() *schema.Resource {
},

"thumbnail_url": {
Type: schema.TypeBool,
Type: schema.TypeString,
Computed: true,
},
},
Expand Down Expand Up @@ -306,6 +308,70 @@ func resourceAwsAmplifyAppCreate(d *schema.ResourceData, meta interface{}) error
Name: aws.String(name),
}

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

if v, ok := d.GetOk("auto_branch_creation_config"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
input.AutoBranchCreationConfig = expandAmplifyAutoBranchCreationConfig(v.([]interface{})[0].(map[string]interface{}))
}

if v, ok := d.GetOk("auto_branch_creation_patterns"); ok && v.(*schema.Set).Len() > 0 {
input.AutoBranchCreationPatterns = expandStringSet(v.(*schema.Set))
}

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

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

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

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

if v, ok := d.GetOk("enable_auto_branch_creation"); ok {
input.EnableAutoBranchCreation = aws.Bool(v.(bool))
}

if v, ok := d.GetOk("enable_basic_auth"); ok {
input.EnableBasicAuth = aws.Bool(v.(bool))
}

if v, ok := d.GetOk("enable_branch_auto_build"); ok {
input.EnableBranchAutoBuild = aws.Bool(v.(bool))
}

if v, ok := d.GetOk("enable_branch_auto_deletion"); ok {
input.EnableBranchAutoDeletion = aws.Bool(v.(bool))
}

if v, ok := d.GetOk("environment_variables"); ok && len(v.(map[string]interface{})) > 0 {
input.EnvironmentVariables = expandStringMap(v.(map[string]interface{}))
}

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

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

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

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

/*
if v, ok := d.GetOk("auto_branch_creation_config"); ok {
config, patterns, enable := expandAmplifyAutoBranchCreationConfig(v.([]interface{}))
Expand Down Expand Up @@ -395,9 +461,37 @@ func resourceAwsAmplifyAppRead(d *schema.ResourceData, meta interface{}) error {
}

d.Set("arn", app.AppArn)

if app.AutoBranchCreationConfig != nil {
if err := d.Set("auto_branch_creation_config", []interface{}{flattenAmplifyAutoBranchCreationConfig(app.AutoBranchCreationConfig)}); err != nil {
return fmt.Errorf("error setting auto_branch_creation_config: %w", err)
}
} else {
d.Set("auto_branch_creation_config", nil)
}
d.Set("auto_branch_creation_patterns", aws.StringValueSlice(app.AutoBranchCreationPatterns))
d.Set("basic_auth_credentials", app.BasicAuthCredentials)
d.Set("build_spec", app.BuildSpec)
d.Set("custom_headers", app.CustomHeaders)

d.Set("default_domain", app.DefaultDomain)
d.Set("description", app.Description)
d.Set("enable_auto_branch_creation", app.EnableAutoBranchCreation)
d.Set("enable_basic_auth", app.EnableBasicAuth)
d.Set("enable_branch_auto_build", app.EnableBranchAutoBuild)
d.Set("enable_branch_auto_deletion", app.EnableBranchAutoDeletion)
d.Set("environment_variables", aws.StringValueMap(app.EnvironmentVariables))
d.Set("iam_service_role_arn", app.IamServiceRoleArn)
d.Set("name", app.Name)
d.Set("name_prefix", naming.NamePrefixFromName(aws.StringValue(app.Name)))
d.Set("platform", app.Platform)
if app.ProductionBranch != nil {
if err := d.Set("production_branch", []interface{}{flattenAmplifyProductionBranch(app.ProductionBranch)}); err != nil {
return fmt.Errorf("error setting production_branch: %w", err)
}
} else {
d.Set("production_branch", nil)
}
d.Set("repository", app.Repository)

/*
if err := d.Set("auto_branch_creation_config", flattenAmplifyAutoBranchCreationConfig(resp.App.AutoBranchCreationConfig, resp.App.AutoBranchCreationPatterns, resp.App.EnableAutoBranchCreation)); err != nil {
Expand Down Expand Up @@ -538,6 +632,132 @@ func resourceAwsAmplifyAppDelete(d *schema.ResourceData, meta interface{}) error
return nil
}

func expandAmplifyAutoBranchCreationConfig(tfMap map[string]interface{}) *amplify.AutoBranchCreationConfig {
if tfMap == nil {
return nil
}

apiObject := &amplify.AutoBranchCreationConfig{}

if v, ok := tfMap["basic_auth_credentials"].(string); ok && v != "" {
apiObject.BasicAuthCredentials = aws.String(v)
}

if v, ok := tfMap["build_spec"].(string); ok && v != "" {
apiObject.BuildSpec = aws.String(v)
}

if v, ok := tfMap["enable_auto_build"].(bool); ok && v {
apiObject.EnableAutoBuild = aws.Bool(v)
}

if v, ok := tfMap["enable_basic_auth"].(bool); ok && v {
apiObject.EnableBasicAuth = aws.Bool(v)
}

if v, ok := tfMap["enable_performance_mode"].(bool); ok && v {
apiObject.EnablePerformanceMode = aws.Bool(v)
}

if v, ok := tfMap["enable_pull_request_preview"].(bool); ok && v {
apiObject.EnablePullRequestPreview = aws.Bool(v)
}

if v, ok := tfMap["environment_variables"].(map[string]interface{}); ok && len(v) > 0 {
apiObject.EnvironmentVariables = expandStringMap(v)
}

if v, ok := tfMap["framework"].(string); ok && v != "" {
apiObject.Framework = aws.String(v)
}

if v, ok := tfMap["pull_request_environment_name"].(string); ok && v != "" {
apiObject.PullRequestEnvironmentName = aws.String(v)
}

if v, ok := tfMap["stage"].(string); ok && v != "" {
apiObject.Stage = aws.String(v)
}

return apiObject
}

func flattenAmplifyAutoBranchCreationConfig(apiObject *amplify.AutoBranchCreationConfig) map[string]interface{} {
if apiObject == nil {
return nil
}

tfMap := map[string]interface{}{}

if v := apiObject.BasicAuthCredentials; v != nil {
tfMap["basic_auth_credentials"] = aws.StringValue(v)
}

if v := apiObject.BuildSpec; v != nil {
tfMap["build_spec"] = aws.StringValue(v)
}

if v := apiObject.EnableAutoBuild; v != nil {
tfMap["enable_auto_build"] = aws.BoolValue(v)
}

if v := apiObject.EnableBasicAuth; v != nil {
tfMap["enable_basic_auth"] = aws.BoolValue(v)
}

if v := apiObject.EnablePerformanceMode; v != nil {
tfMap["enable_performance_mode"] = aws.BoolValue(v)
}

if v := apiObject.EnablePullRequestPreview; v != nil {
tfMap["enable_pull_request_preview"] = aws.BoolValue(v)
}

if v := apiObject.EnvironmentVariables; v != nil {
tfMap["environment_variables"] = aws.StringValueMap(v)
}

if v := apiObject.Framework; v != nil {
tfMap["framework"] = aws.StringValue(v)
}

if v := apiObject.PullRequestEnvironmentName; v != nil {
tfMap["pull_request_environment_name"] = aws.StringValue(v)
}

if v := apiObject.Stage; v != nil {
tfMap["stage"] = aws.StringValue(v)
}

return tfMap
}

func flattenAmplifyProductionBranch(apiObject *amplify.ProductionBranch) map[string]interface{} {
if apiObject == nil {
return nil
}

tfMap := map[string]interface{}{}

if v := apiObject.BranchName; v != nil {
tfMap["branch_name"] = aws.StringValue(v)
}

if v := apiObject.LastDeployTime; v != nil {
tfMap["last_deploy_time"] = aws.TimeValue(v).Format(time.RFC3339)
}

if v := apiObject.Status; v != nil {
tfMap["status"] = aws.StringValue(v)
}

if v := apiObject.ThumbnailUrl; v != nil {
tfMap["thumbnail_url"] = aws.StringValue(v)
}

return tfMap
}

/*
func expandAmplifyEnvironmentVariables(envs map[string]interface{}) map[string]*string {
if len(envs) == 0 {
Expand Down

0 comments on commit 4f7eda0

Please sign in to comment.