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_account_alternate_contact #21789

Merged
merged 7 commits into from
Nov 19, 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/21789.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_account_alternate_contact
```
2 changes: 2 additions & 0 deletions .github/labeler-issue-triage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ sweeper:
# resource "aws_XXX"
service/accessanalyzer:
- '((\*|-) ?`?|(data|resource) "?)aws_accessanalyzer_'
service/account:
- '((\*|-) ?`?|(data|resource) "?)aws_account_'
service/acm:
- '((\*|-) ?`?|(data|resource) "?)aws_acm_'
service/acmpca:
Expand Down
3 changes: 3 additions & 0 deletions .github/labeler-pr-triage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ repository:
service/accessanalyzer:
- 'internal/service/accessanalyzer/**/*'
- 'website/**/accessanalyzer_*'
service/account:
- 'internal/service/account/**/*'
- 'website/**/account_*'
service/acm:
- 'internal/service/acm/**/*'
- 'website/**/acm_*'
Expand Down
1 change: 1 addition & 0 deletions infrastructure/repository/labels-service.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
variable "service_labels" {
default = [
"accessanalyzer",
"account",
"acm",
"acmpca",
"alexaforbusiness",
Expand Down
5 changes: 5 additions & 0 deletions internal/conns/conns.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/accessanalyzer"
"github.com/aws/aws-sdk-go/service/account"
"github.com/aws/aws-sdk-go/service/acm"
"github.com/aws/aws-sdk-go/service/acmpca"
"github.com/aws/aws-sdk-go/service/alexaforbusiness"
Expand Down Expand Up @@ -286,6 +287,7 @@ import (

const (
AccessAnalyzer = "accessanalyzer"
Account = "account"
ACM = "acm"
ACMPCA = "acmpca"
AlexaForBusiness = "alexaforbusiness"
Expand Down Expand Up @@ -573,6 +575,7 @@ func init() {
serviceData = make(map[string]*ServiceDatum)

serviceData[AccessAnalyzer] = &ServiceDatum{AWSClientName: "AccessAnalyzer", AWSServiceName: accessanalyzer.ServiceName, AWSEndpointsID: accessanalyzer.EndpointsID, AWSServiceID: accessanalyzer.ServiceID, ProviderNameUpper: "AccessAnalyzer", HCLKeys: []string{"accessanalyzer"}}
serviceData[Account] = &ServiceDatum{AWSClientName: "Account", AWSServiceName: account.ServiceName, AWSEndpointsID: account.EndpointsID, AWSServiceID: account.ServiceID, ProviderNameUpper: "Account", HCLKeys: []string{"account"}}
serviceData[ACM] = &ServiceDatum{AWSClientName: "ACM", AWSServiceName: acm.ServiceName, AWSEndpointsID: acm.EndpointsID, AWSServiceID: acm.ServiceID, ProviderNameUpper: "ACM", HCLKeys: []string{"acm"}}
serviceData[ACMPCA] = &ServiceDatum{AWSClientName: "ACMPCA", AWSServiceName: acmpca.ServiceName, AWSEndpointsID: acmpca.EndpointsID, AWSServiceID: acmpca.ServiceID, ProviderNameUpper: "ACMPCA", HCLKeys: []string{"acmpca"}}
serviceData[AlexaForBusiness] = &ServiceDatum{AWSClientName: "AlexaForBusiness", AWSServiceName: alexaforbusiness.ServiceName, AWSEndpointsID: alexaforbusiness.EndpointsID, AWSServiceID: alexaforbusiness.ServiceID, ProviderNameUpper: "AlexaForBusiness", HCLKeys: []string{"alexaforbusiness"}}
Expand Down Expand Up @@ -881,6 +884,7 @@ type Config struct {

type AWSClient struct {
AccessAnalyzerConn *accessanalyzer.AccessAnalyzer
AccountConn *account.Account
AccountID string
ACMConn *acm.ACM
ACMPCAConn *acmpca.ACMPCA
Expand Down Expand Up @@ -1234,6 +1238,7 @@ func (c *Config) Client() (interface{}, error) {

client := &AWSClient{
AccessAnalyzerConn: accessanalyzer.New(sess.Copy(&aws.Config{Endpoint: aws.String(c.Endpoints[AccessAnalyzer])})),
AccountConn: account.New(sess.Copy(&aws.Config{Endpoint: aws.String(c.Endpoints[Account])})),
AccountID: accountID,
ACMConn: acm.New(sess.Copy(&aws.Config{Endpoint: aws.String(c.Endpoints[ACM])})),
ACMPCAConn: acmpca.New(sess.Copy(&aws.Config{Endpoint: aws.String(c.Endpoints[ACMPCA])})),
Expand Down
3 changes: 3 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/service/accessanalyzer"
"github.com/hashicorp/terraform-provider-aws/internal/service/account"
"github.com/hashicorp/terraform-provider-aws/internal/service/acm"
"github.com/hashicorp/terraform-provider-aws/internal/service/acmpca"
"github.com/hashicorp/terraform-provider-aws/internal/service/amp"
Expand Down Expand Up @@ -712,6 +713,8 @@ func Provider() *schema.Provider {
ResourcesMap: map[string]*schema.Resource{
"aws_accessanalyzer_analyzer": accessanalyzer.ResourceAnalyzer(),

"aws_account_alternate_contact": account.ResourceAlternateContact(),

"aws_acm_certificate": acm.ResourceCertificate(),
"aws_acm_certificate_validation": acm.ResourceCertificateValidation(),

Expand Down
171 changes: 171 additions & 0 deletions internal/service/account/alternate_contact.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
package account

import (
"context"
"log"
"regexp"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/account"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
)

func ResourceAlternateContact() *schema.Resource {
return &schema.Resource{
CreateContext: resourceAlternateContactCreate,
ReadContext: resourceAlternateContactRead,
UpdateContext: resourceAlternateContactUpdate,
DeleteContext: resourceAlternateContactDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},

Schema: map[string]*schema.Schema{
"alternate_contact_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice(account.AlternateContactType_Values(), false),
},
"email_address": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringMatch(regexp.MustCompile(`[\w+=,.-]+@[\w.-]+\.[\w]+`), "must be a valid email address"),
},
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 64),
},
"phone_number": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringMatch(regexp.MustCompile(`^[\s0-9()+-]+$`), "must be a valid phone number"),
},
"title": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 50),
},
},
}
}

func resourceAlternateContactCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).AccountConn

contactType := d.Get("alternate_contact_type").(string)
input := &account.PutAlternateContactInput{
AlternateContactType: aws.String(contactType),
EmailAddress: aws.String(d.Get("email_address").(string)),
Name: aws.String(d.Get("name").(string)),
PhoneNumber: aws.String(d.Get("phone_number").(string)),
Title: aws.String(d.Get("title").(string)),
}

log.Printf("[DEBUG] Creating Account Alternate Contact: %s", input)
_, err := conn.PutAlternateContactWithContext(ctx, input)

if err != nil {
return diag.Errorf("error creating Account Alternate Contact (%s): %s", contactType, err)
}

d.SetId(contactType)

return resourceAlternateContactRead(ctx, d, meta)
}

func resourceAlternateContactRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).AccountConn

output, err := FindAlternateContactByContactType(ctx, conn, d.Id())

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

if err != nil {
return diag.Errorf("error reading Account Alternate Contact (%s): %s", d.Id(), err)
}

d.Set("alternate_contact_type", output.AlternateContactType)
d.Set("email_address", output.EmailAddress)
d.Set("name", output.Name)
d.Set("phone_number", output.PhoneNumber)
d.Set("title", output.Title)

return nil
}

func resourceAlternateContactUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).AccountConn

input := &account.PutAlternateContactInput{
AlternateContactType: aws.String(d.Id()),
EmailAddress: aws.String(d.Get("email_address").(string)),
Name: aws.String(d.Get("name").(string)),
PhoneNumber: aws.String(d.Get("phone_number").(string)),
Title: aws.String(d.Get("title").(string)),
}

log.Printf("[DEBUG] Updating Account Alternate Contact: %s", input)
_, err := conn.PutAlternateContactWithContext(ctx, input)

if err != nil {
return diag.Errorf("error updating Account Alternate Contact (%s): %s", d.Id(), err)
}

return resourceAlternateContactRead(ctx, d, meta)
}

func resourceAlternateContactDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).AccountConn

log.Printf("[DEBUG] Deleting Account Alternate Contact: %s", d.Id())
_, err := conn.DeleteAlternateContactWithContext(ctx, &account.DeleteAlternateContactInput{
AlternateContactType: aws.String(d.Id()),
})

if tfawserr.ErrCodeEquals(err, account.ErrCodeResourceNotFoundException) {
return nil
}

if err != nil {
return diag.Errorf("error deleting Account Alternate Contact (%s): %s", d.Id(), err)
}

return nil
}

func FindAlternateContactByContactType(ctx context.Context, conn *account.Account, contactType string) (*account.AlternateContact, error) {
input := &account.GetAlternateContactInput{
AlternateContactType: aws.String(contactType),
}

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

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

if err != nil {
return nil, err
}

if output == nil || output.AlternateContact == nil {
return nil, tfresource.NewEmptyResultError(input)
}

return output.AlternateContact, nil
}
Loading