Skip to content

Commit d532496

Browse files
authored
Merge pull request #42526 from cavcrosby/f-account-primary-contact
Add the `aws_account_primary_contact` data source
2 parents 050e184 + 3838bab commit d532496

File tree

6 files changed

+299
-2
lines changed

6 files changed

+299
-2
lines changed

.changelog/42526.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-data-source
2+
aws_account_primary_contact
3+
```

internal/service/account/account_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ func TestAccAccount_serial(t *testing.T) {
1919
"AccountID": testAccAlternateContact_accountID,
2020
},
2121
"PrimaryContact": {
22-
acctest.CtBasic: testAccPrimaryContact_basic,
22+
acctest.CtBasic: testAccPrimaryContact_basic,
23+
"dataSourceBasic": testAccPrimaryContactDataSource_basic,
24+
"dataSourceAccountID": testAccPrimaryContactDataSource_accountID,
2325
},
2426
"Region": {
2527
acctest.CtBasic: testAccRegion_basic,
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package account
5+
6+
import (
7+
"context"
8+
9+
"github.com/hashicorp/terraform-plugin-framework/datasource"
10+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
11+
"github.com/hashicorp/terraform-plugin-framework/types"
12+
"github.com/hashicorp/terraform-provider-aws/internal/framework"
13+
fwflex "github.com/hashicorp/terraform-provider-aws/internal/framework/flex"
14+
"github.com/hashicorp/terraform-provider-aws/names"
15+
)
16+
17+
// @FrameworkDataSource("aws_account_primary_contact", name="Primary Contact")
18+
func newPrimaryContactDataSource(context.Context) (datasource.DataSourceWithConfigure, error) {
19+
return &primaryContactDataSOurce{}, nil
20+
}
21+
22+
type primaryContactDataSOurce struct {
23+
framework.DataSourceWithConfigure
24+
}
25+
26+
func (d *primaryContactDataSOurce) Schema(ctx context.Context, request datasource.SchemaRequest, response *datasource.SchemaResponse) {
27+
response.Schema = schema.Schema{
28+
Attributes: map[string]schema.Attribute{
29+
names.AttrAccountID: schema.StringAttribute{
30+
Optional: true,
31+
Computed: true,
32+
},
33+
"address_line_1": schema.StringAttribute{
34+
Computed: true,
35+
},
36+
"address_line_2": schema.StringAttribute{
37+
Computed: true,
38+
},
39+
"address_line_3": schema.StringAttribute{
40+
Computed: true,
41+
},
42+
"city": schema.StringAttribute{
43+
Computed: true,
44+
},
45+
"company_name": schema.StringAttribute{
46+
Computed: true,
47+
},
48+
"country_code": schema.StringAttribute{
49+
Computed: true,
50+
},
51+
"district_or_county": schema.StringAttribute{
52+
Computed: true,
53+
},
54+
"full_name": schema.StringAttribute{
55+
Computed: true,
56+
},
57+
"phone_number": schema.StringAttribute{
58+
Computed: true,
59+
},
60+
"postal_code": schema.StringAttribute{
61+
Computed: true,
62+
},
63+
"state_or_region": schema.StringAttribute{
64+
Computed: true,
65+
},
66+
"website_url": schema.StringAttribute{
67+
Computed: true,
68+
},
69+
},
70+
}
71+
}
72+
73+
func (d *primaryContactDataSOurce) Read(ctx context.Context, request datasource.ReadRequest, response *datasource.ReadResponse) {
74+
var data primaryContactDataSourceModel
75+
response.Diagnostics.Append(request.Config.Get(ctx, &data)...)
76+
if response.Diagnostics.HasError() {
77+
return
78+
}
79+
80+
conn := d.Meta().AccountClient(ctx)
81+
82+
output, err := findContactInformation(ctx, conn, fwflex.StringValueFromFramework(ctx, data.AccountID))
83+
84+
if err != nil {
85+
response.Diagnostics.AddError("reading Account Primary Contact", err.Error())
86+
87+
return
88+
}
89+
90+
response.Diagnostics.Append(fwflex.Flatten(ctx, output, &data)...)
91+
if response.Diagnostics.HasError() {
92+
return
93+
}
94+
95+
response.Diagnostics.Append(response.State.Set(ctx, &data)...)
96+
}
97+
98+
type primaryContactDataSourceModel struct {
99+
AccountID types.String `tfsdk:"account_id"`
100+
AddressLine1 types.String `tfsdk:"address_line_1"`
101+
AddressLine2 types.String `tfsdk:"address_line_2"`
102+
AddressLine3 types.String `tfsdk:"address_line_3"`
103+
City types.String `tfsdk:"city"`
104+
CompanyName types.String `tfsdk:"company_name"`
105+
CountryCode types.String `tfsdk:"country_code"`
106+
DistrictOrCounty types.String `tfsdk:"district_or_county"`
107+
FullName types.String `tfsdk:"full_name"`
108+
PhoneNumber types.String `tfsdk:"phone_number"`
109+
PostalCode types.String `tfsdk:"postal_code"`
110+
StateOrRegion types.String `tfsdk:"state_or_region"`
111+
WebsiteURL types.String `tfsdk:"website_url"`
112+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package account_test
5+
6+
import (
7+
"testing"
8+
9+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
10+
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
11+
"github.com/hashicorp/terraform-provider-aws/names"
12+
)
13+
14+
func testAccPrimaryContactDataSource_basic(t *testing.T) {
15+
ctx := acctest.Context(t)
16+
resourceName := "aws_account_primary_contact.test"
17+
dataSourceName := "data.aws_account_primary_contact.test"
18+
19+
resource.Test(t, resource.TestCase{
20+
PreCheck: func() { acctest.PreCheck(ctx, t) },
21+
ErrorCheck: acctest.ErrorCheck(t, names.AccountServiceID),
22+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
23+
CheckDestroy: acctest.CheckDestroyNoop,
24+
Steps: []resource.TestStep{
25+
{
26+
Config: testAccPrimaryContactDataSourceConfig_basic(),
27+
Check: resource.ComposeAggregateTestCheckFunc(
28+
resource.TestCheckResourceAttrPair(dataSourceName, "address_line_1", resourceName, "address_line_1"),
29+
resource.TestCheckResourceAttrPair(dataSourceName, "address_line_2", resourceName, "address_line_2"),
30+
resource.TestCheckResourceAttrPair(dataSourceName, "address_line_3", resourceName, "address_line_3"),
31+
resource.TestCheckResourceAttrPair(dataSourceName, "city", resourceName, "city"),
32+
resource.TestCheckResourceAttrPair(dataSourceName, "company_name", resourceName, "company_name"),
33+
resource.TestCheckResourceAttrPair(dataSourceName, "country_code", resourceName, "country_code"),
34+
resource.TestCheckResourceAttrPair(dataSourceName, "district_or_county", resourceName, "district_or_county"),
35+
resource.TestCheckResourceAttrPair(dataSourceName, "full_name", resourceName, "full_name"),
36+
resource.TestCheckResourceAttrPair(dataSourceName, "phone_number", resourceName, "phone_number"),
37+
resource.TestCheckResourceAttrPair(dataSourceName, "postal_code", resourceName, "postal_code"),
38+
resource.TestCheckResourceAttrPair(dataSourceName, "state_or_region", resourceName, "state_or_region"),
39+
resource.TestCheckResourceAttrPair(dataSourceName, "website_url", resourceName, "website_url"),
40+
),
41+
},
42+
},
43+
})
44+
}
45+
46+
func testAccPrimaryContactDataSource_accountID(t *testing.T) {
47+
ctx := acctest.Context(t)
48+
resourceName := "aws_account_primary_contact.test"
49+
dataSourceName := "data.aws_account_primary_contact.test"
50+
51+
resource.Test(t, resource.TestCase{
52+
PreCheck: func() {
53+
acctest.PreCheck(ctx, t)
54+
acctest.PreCheckAlternateAccount(t)
55+
acctest.PreCheckOrganizationManagementAccount(ctx, t)
56+
acctest.PreCheckOrganizationsEnabledServicePrincipal(ctx, t, "account.amazonaws.com")
57+
},
58+
ErrorCheck: acctest.ErrorCheck(t, names.AccountServiceID),
59+
ProtoV5ProviderFactories: acctest.ProtoV5FactoriesAlternate(ctx, t),
60+
CheckDestroy: acctest.CheckDestroyNoop,
61+
Steps: []resource.TestStep{
62+
{
63+
Config: testAccPrimaryContactDataSourceConfig_organization(),
64+
Check: resource.ComposeAggregateTestCheckFunc(
65+
resource.TestCheckResourceAttrPair(dataSourceName, names.AttrAccountID, resourceName, names.AttrAccountID),
66+
resource.TestCheckResourceAttrPair(dataSourceName, "address_line_1", resourceName, "address_line_1"),
67+
resource.TestCheckResourceAttrPair(dataSourceName, "address_line_2", resourceName, "address_line_2"),
68+
resource.TestCheckResourceAttrPair(dataSourceName, "address_line_3", resourceName, "address_line_3"),
69+
resource.TestCheckResourceAttrPair(dataSourceName, "city", resourceName, "city"),
70+
resource.TestCheckResourceAttrPair(dataSourceName, "company_name", resourceName, "company_name"),
71+
resource.TestCheckResourceAttrPair(dataSourceName, "country_code", resourceName, "country_code"),
72+
resource.TestCheckResourceAttrPair(dataSourceName, "district_or_county", resourceName, "district_or_county"),
73+
resource.TestCheckResourceAttrPair(dataSourceName, "full_name", resourceName, "full_name"),
74+
resource.TestCheckResourceAttrPair(dataSourceName, "phone_number", resourceName, "phone_number"),
75+
resource.TestCheckResourceAttrPair(dataSourceName, "postal_code", resourceName, "postal_code"),
76+
resource.TestCheckResourceAttrPair(dataSourceName, "state_or_region", resourceName, "state_or_region"),
77+
resource.TestCheckResourceAttrPair(dataSourceName, "website_url", resourceName, "website_url"),
78+
),
79+
},
80+
},
81+
})
82+
}
83+
84+
func testAccPrimaryContactDataSourceConfig_basic() string {
85+
return `
86+
resource "aws_account_primary_contact" "test" {
87+
address_line_1 = "123 Any Street"
88+
address_line_2 = "234 Any Street"
89+
address_line_3 = "345 Any Street"
90+
city = "Seattle"
91+
company_name = "Example Corp, Inc."
92+
country_code = "US"
93+
district_or_county = "King"
94+
full_name = "Foo Bar"
95+
phone_number = "+64211111111"
96+
postal_code = "98101"
97+
state_or_region = "WA"
98+
website_url = "https://www.examplecorp.com"
99+
}
100+
101+
data "aws_account_primary_contact" "test" {
102+
depends_on = [aws_account_primary_contact.test]
103+
}
104+
`
105+
}
106+
107+
func testAccPrimaryContactDataSourceConfig_organization() string {
108+
return acctest.ConfigCompose(acctest.ConfigAlternateAccountProvider(), `
109+
data "aws_caller_identity" "test" {
110+
provider = "awsalternate"
111+
}
112+
113+
resource "aws_account_primary_contact" "test" {
114+
account_id = data.aws_caller_identity.test.account_id
115+
address_line_1 = "123 Any Street"
116+
address_line_2 = "234 Any Street"
117+
address_line_3 = "345 Any Street"
118+
city = "Seattle"
119+
company_name = "Example Corp, Inc."
120+
country_code = "US"
121+
district_or_county = "King"
122+
full_name = "Foo Bar"
123+
phone_number = "+64211111111"
124+
postal_code = "98101"
125+
state_or_region = "WA"
126+
website_url = "https://www.examplecorp.com"
127+
}
128+
129+
data "aws_account_primary_contact" "test" {
130+
account_id = data.aws_caller_identity.test.account_id
131+
depends_on = [aws_account_primary_contact.test]
132+
}
133+
`)
134+
}

internal/service/account/service_package_gen.go

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
subcategory: "Account Management"
3+
layout: "aws"
4+
page_title: "AWS: aws_account_primary_contact"
5+
description: |-
6+
Terraform data source for the primary contact information associated with an AWS Account.
7+
---
8+
9+
# Data Source: aws_account_primary_contact
10+
11+
Terraform data source for the primary contact information associated with an AWS Account.
12+
13+
## Example Usage
14+
15+
```terraform
16+
data "aws_account_primary_contact" "test" {}
17+
```
18+
19+
## Argument Reference
20+
21+
This data source supports the following arguments:
22+
23+
* `account_id` - (Optional) The ID of the target account when managing member accounts. Will manage current user's account by default if omitted.
24+
25+
## Attribute Reference
26+
27+
This data source exports the following attributes in addition to the arguments above:
28+
29+
* `address_line_1` - The first line of the primary contact address.
30+
* `address_line_2` - The second line of the primary contact address.
31+
* `address_line_3` - The third line of the primary contact address.
32+
* `city` - The city of the primary contact address.
33+
* `company_name` - The name of the company associated with the primary contact information.
34+
* `country_code` - The ISO-3166 two-letter country code for the primary contact address.
35+
* `district_or_county` - The district or county of the primary contact address.
36+
* `full_name` - The full name of the primary contact address.
37+
* `phone_number` - The phone number of the primary contact information.
38+
* `postal_code` - The postal code of the primary contact address.
39+
* `state_or_region` - The state or region of the primary contact address.
40+
* `website_url` - The URL of the website associated with the primary contact information.

0 commit comments

Comments
 (0)