forked from hashicorp/terraform-provider-aws
/
resource_aws_config_aggregate_authorization.go
141 lines (115 loc) · 3.85 KB
/
resource_aws_config_aggregate_authorization.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package aws
import (
"fmt"
"log"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/configservice"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsConfigAggregateAuthorization() *schema.Resource {
return &schema.Resource{
Create: resourceAwsConfigAggregateAuthorizationPut,
Read: resourceAwsConfigAggregateAuthorizationRead,
Delete: resourceAwsConfigAggregateAuthorizationDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"account_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateAwsAccountId,
},
"region": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceAwsConfigAggregateAuthorizationPut(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).configconn
accountId := d.Get("account_id").(string)
region := d.Get("region").(string)
req := &configservice.PutAggregationAuthorizationInput{
AuthorizedAccountId: aws.String(accountId),
AuthorizedAwsRegion: aws.String(region),
}
_, err := conn.PutAggregationAuthorization(req)
if err != nil {
return fmt.Errorf("Error creating aggregate authorization: %s", err)
}
d.SetId(fmt.Sprintf("%s:%s", accountId, region))
return resourceAwsConfigAggregateAuthorizationRead(d, meta)
}
func resourceAwsConfigAggregateAuthorizationRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).configconn
accountId, region, err := resourceAwsConfigAggregateAuthorizationParseID(d.Id())
if err != nil {
return err
}
d.Set("account_id", accountId)
d.Set("region", region)
aggregateAuthorizations, err := describeConfigAggregateAuthorizations(conn)
if err != nil {
return fmt.Errorf("Error retrieving list of aggregate authorizations: %s", err)
}
// Check for existing authorization
for _, auth := range aggregateAuthorizations {
if accountId == aws.StringValue(auth.AuthorizedAccountId) && region == aws.StringValue(auth.AuthorizedAwsRegion) {
d.Set("arn", auth.AggregationAuthorizationArn)
return nil
}
}
log.Printf("[WARN] Aggregate Authorization not found, removing from state: %s", d.Id())
d.SetId("")
return nil
}
func resourceAwsConfigAggregateAuthorizationDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).configconn
accountId, region, err := resourceAwsConfigAggregateAuthorizationParseID(d.Id())
if err != nil {
return err
}
req := &configservice.DeleteAggregationAuthorizationInput{
AuthorizedAccountId: aws.String(accountId),
AuthorizedAwsRegion: aws.String(region),
}
_, err = conn.DeleteAggregationAuthorization(req)
if err != nil {
return fmt.Errorf("Error deleting aggregate authorization: %s", err)
}
return nil
}
func describeConfigAggregateAuthorizations(conn *configservice.ConfigService) ([]*configservice.AggregationAuthorization, error) {
aggregationAuthorizations := []*configservice.AggregationAuthorization{}
input := &configservice.DescribeAggregationAuthorizationsInput{}
for {
output, err := conn.DescribeAggregationAuthorizations(input)
if err != nil {
return aggregationAuthorizations, err
}
aggregationAuthorizations = append(aggregationAuthorizations, output.AggregationAuthorizations...)
if output.NextToken == nil {
break
}
input.NextToken = output.NextToken
}
return aggregationAuthorizations, nil
}
func resourceAwsConfigAggregateAuthorizationParseID(id string) (string, string, error) {
idParts := strings.Split(id, ":")
if len(idParts) != 2 {
return "", "", fmt.Errorf("Please make sure the ID is in the form account_id:region (i.e. 123456789012:us-east-1")
}
accountId := idParts[0]
region := idParts[1]
return accountId, region, nil
}