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/aws_elastic_beanstalk_configuration_template: Map InvalidParameterValue: No Platform named ... to NotFoundError #29863

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/29863.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_elastic_beanstalk_configuration_template: Map errors like `InvalidParameterValue: No Platform named '...' found.` to `resource.NotFoundError` so `terraform refesh` correctly removes the resource from state
```
91 changes: 35 additions & 56 deletions internal/service/elasticbeanstalk/configuration_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,36 +63,32 @@ func resourceConfigurationTemplateCreate(ctx context.Context, d *schema.Resource
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).ElasticBeanstalkConn()

// Get the relevant properties
name := d.Get("name").(string)
appName := d.Get("application").(string)

optionSettings := gatherOptionSettings(d)

opts := elasticbeanstalk.CreateConfigurationTemplateInput{
ApplicationName: aws.String(appName),
input := &elasticbeanstalk.CreateConfigurationTemplateInput{
ApplicationName: aws.String(d.Get("application").(string)),
OptionSettings: gatherOptionSettings(d),
TemplateName: aws.String(name),
OptionSettings: optionSettings,
}

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

if attr, ok := d.GetOk("environment_id"); ok {
opts.EnvironmentId = aws.String(attr.(string))
input.EnvironmentId = aws.String(attr.(string))
}

if attr, ok := d.GetOk("solution_stack_name"); ok {
opts.SolutionStackName = aws.String(attr.(string))
input.SolutionStackName = aws.String(attr.(string))
}

log.Printf("[DEBUG] Elastic Beanstalk configuration template create opts: %s", opts)
if _, err := conn.CreateConfigurationTemplateWithContext(ctx, &opts); err != nil {
return sdkdiag.AppendErrorf(diags, "creating Elastic Beanstalk configuration template: %s", err)
output, err := conn.CreateConfigurationTemplateWithContext(ctx, input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "creating Elastic Beanstalk Configuration Template (%s): %s", name, err)
}

d.SetId(name)
d.SetId(aws.StringValue(output.TemplateName))

return append(diags, resourceConfigurationTemplateRead(ctx, d, meta)...)
}
Expand Down Expand Up @@ -125,44 +121,21 @@ func resourceConfigurationTemplateUpdate(ctx context.Context, d *schema.Resource
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).ElasticBeanstalkConn()

log.Printf("[DEBUG] Elastic Beanstalk configuration template update: %s", d.Get("name").(string))

if d.HasChange("description") {
if err := resourceConfigurationTemplateDescriptionUpdate(ctx, conn, d); err != nil {
return sdkdiag.AppendErrorf(diags, "updating Elastic Beanstalk Configuration Template (%s): %s", d.Id(), err)
input := &elasticbeanstalk.UpdateConfigurationTemplateInput{
ApplicationName: aws.String(d.Get("application").(string)),
Description: aws.String(d.Get("description").(string)),
TemplateName: aws.String(d.Id()),
}
}

if d.HasChange("setting") {
if err := resourceConfigurationTemplateOptionSettingsUpdate(ctx, conn, d); err != nil {
_, err := conn.UpdateConfigurationTemplateWithContext(ctx, input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "updating Elastic Beanstalk Configuration Template (%s): %s", d.Id(), err)
}
}

return append(diags, resourceConfigurationTemplateRead(ctx, d, meta)...)
}

func resourceConfigurationTemplateDescriptionUpdate(ctx context.Context, conn *elasticbeanstalk.ElasticBeanstalk, d *schema.ResourceData) error {
_, err := conn.UpdateConfigurationTemplateWithContext(ctx, &elasticbeanstalk.UpdateConfigurationTemplateInput{
ApplicationName: aws.String(d.Get("application").(string)),
TemplateName: aws.String(d.Get("name").(string)),
Description: aws.String(d.Get("description").(string)),
})

return err
}

func resourceConfigurationTemplateOptionSettingsUpdate(ctx context.Context, conn *elasticbeanstalk.ElasticBeanstalk, d *schema.ResourceData) error {
if d.HasChange("setting") {
_, err := conn.ValidateConfigurationSettingsWithContext(ctx, &elasticbeanstalk.ValidateConfigurationSettingsInput{
ApplicationName: aws.String(d.Get("application").(string)),
TemplateName: aws.String(d.Get("name").(string)),
OptionSettings: gatherOptionSettings(d),
})
if err != nil {
return err
}

o, n := d.GetChange("setting")
if o == nil {
o = new(schema.Set)
Expand Down Expand Up @@ -196,44 +169,50 @@ func resourceConfigurationTemplateOptionSettingsUpdate(ctx context.Context, conn
}
}

req := &elasticbeanstalk.UpdateConfigurationTemplateInput{
input := &elasticbeanstalk.UpdateConfigurationTemplateInput{
ApplicationName: aws.String(d.Get("application").(string)),
TemplateName: aws.String(d.Get("name").(string)),
OptionSettings: add,
TemplateName: aws.String(d.Id()),
}

for _, elem := range remove {
req.OptionsToRemove = append(req.OptionsToRemove, &elasticbeanstalk.OptionSpecification{
input.OptionsToRemove = append(input.OptionsToRemove, &elasticbeanstalk.OptionSpecification{
Namespace: elem.Namespace,
OptionName: elem.OptionName,
})
}

log.Printf("[DEBUG] Update Configuration Template request: %s", req)
if _, err := conn.UpdateConfigurationTemplateWithContext(ctx, req); err != nil {
return err
_, err := conn.UpdateConfigurationTemplateWithContext(ctx, input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "updating Elastic Beanstalk Configuration Template (%s): %s", d.Id(), err)
}
}

return nil
return append(diags, resourceConfigurationTemplateRead(ctx, d, meta)...)
}

func resourceConfigurationTemplateDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).ElasticBeanstalkConn()

application := d.Get("application").(string)

log.Printf("[INFO] Deleting Elastic Beanstalk Configuration Template: %s", d.Id())
_, err := conn.DeleteConfigurationTemplateWithContext(ctx, &elasticbeanstalk.DeleteConfigurationTemplateInput{
ApplicationName: aws.String(application),
ApplicationName: aws.String(d.Get("application").(string)),
TemplateName: aws.String(d.Id()),
})

if err != nil {
return sdkdiag.AppendErrorf(diags, "deleting Elastic Beanstalk Configuration Template (%s): %s", d.Id(), err)
}

return diags
}

const (
errCodeInvalidParameterValue = "InvalidParameterValue"
)

func FindConfigurationSettingsByTwoPartKey(ctx context.Context, conn *elasticbeanstalk.ElasticBeanstalk, applicationName, templateName string) (*elasticbeanstalk.ConfigurationSettingsDescription, error) {
input := &elasticbeanstalk.DescribeConfigurationSettingsInput{
ApplicationName: aws.String(applicationName),
Expand All @@ -242,7 +221,7 @@ func FindConfigurationSettingsByTwoPartKey(ctx context.Context, conn *elasticbea

output, err := conn.DescribeConfigurationSettingsWithContext(ctx, input)

if tfawserr.ErrMessageContains(err, "InvalidParameterValue", "No Configuration Template named") || tfawserr.ErrMessageContains(err, "InvalidParameterValue", "No Application named") {
if tfawserr.ErrMessageContains(err, errCodeInvalidParameterValue, "No Configuration Template named") || tfawserr.ErrMessageContains(err, errCodeInvalidParameterValue, "No Application named") || tfawserr.ErrMessageContains(err, errCodeInvalidParameterValue, "No Platform named") {
return nil, &resource.NotFoundError{
LastError: err,
LastRequest: input,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,30 @@ func TestAccElasticBeanstalkConfigurationTemplate_disappears(t *testing.T) {
})
}

func TestAccElasticBeanstalkConfigurationTemplate_Disappears_application(t *testing.T) {
ctx := acctest.Context(t)
var config elasticbeanstalk.ConfigurationSettingsDescription
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_elastic_beanstalk_configuration_template.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, elasticbeanstalk.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckConfigurationTemplateDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccConfigurationTemplateConfig_basic(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckConfigurationTemplateExists(ctx, resourceName, &config),
acctest.CheckResourceDisappears(ctx, acctest.Provider, tfelasticbeanstalk.ResourceApplication(), "aws_elastic_beanstalk_application.test"),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func TestAccElasticBeanstalkConfigurationTemplate_vpc(t *testing.T) {
ctx := acctest.Context(t)
var config elasticbeanstalk.ConfigurationSettingsDescription
Expand Down