/
delegated_administrators_data_source.go
121 lines (106 loc) · 3.52 KB
/
delegated_administrators_data_source.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
package organizations
import (
"context"
"fmt"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/organizations"
"github.com/cloudposse/terraform-provider-awsutils/internal/conns"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func DataSourceDelegatedAdministrators() *schema.Resource {
return &schema.Resource{
ReadWithoutTimeout: dataSourceDelegatedAdministratorsRead,
Schema: map[string]*schema.Schema{
"service_principal": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(1, 128),
},
"delegated_administrators": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"delegation_enabled_date": {
Type: schema.TypeString,
Computed: true,
},
"email": {
Type: schema.TypeString,
Computed: true,
},
"id": {
Type: schema.TypeString,
Computed: true,
},
"joined_method": {
Type: schema.TypeString,
Computed: true,
},
"joined_timestamp": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
func dataSourceDelegatedAdministratorsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).OrganizationsConn
input := &organizations.ListDelegatedAdministratorsInput{}
if v, ok := d.GetOk("service_principal"); ok {
input.ServicePrincipal = aws.String(v.(string))
}
var delegators []*organizations.DelegatedAdministrator
err := conn.ListDelegatedAdministratorsPagesWithContext(ctx, input, func(page *organizations.ListDelegatedAdministratorsOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}
delegators = append(delegators, page.DelegatedAdministrators...)
return !lastPage
})
if err != nil {
return diag.FromErr(fmt.Errorf("error describing organizations delegated Administrators: %w", err))
}
if err = d.Set("delegated_administrators", flattenDelegatedAdministrators(delegators)); err != nil {
return diag.FromErr(fmt.Errorf("error setting delegated_administrators: %w", err))
}
d.SetId(meta.(*conns.AWSClient).AccountID)
return nil
}
func flattenDelegatedAdministrators(delegatedAdministrators []*organizations.DelegatedAdministrator) []map[string]interface{} {
if len(delegatedAdministrators) == 0 {
return nil
}
var result []map[string]interface{}
for _, delegated := range delegatedAdministrators {
result = append(result, map[string]interface{}{
"arn": aws.StringValue(delegated.Arn),
"delegation_enabled_date": aws.TimeValue(delegated.DelegationEnabledDate).Format(time.RFC3339),
"email": aws.StringValue(delegated.Email),
"id": aws.StringValue(delegated.Id),
"joined_method": aws.StringValue(delegated.JoinedMethod),
"joined_timestamp": aws.TimeValue(delegated.JoinedTimestamp).Format(time.RFC3339),
"name": aws.StringValue(delegated.Name),
"status": aws.StringValue(delegated.Status),
})
}
return result
}