-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
organizational_units_data_source.go
89 lines (74 loc) · 2.12 KB
/
organizational_units_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
package organizations
import (
"fmt"
"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/helper/schema"
)
func DataSourceOrganizationalUnits() *schema.Resource {
return &schema.Resource{
Read: dataSourceOrganizationalUnitsRead,
Schema: map[string]*schema.Schema{
"parent_id": {
Type: schema.TypeString,
Required: true,
},
"children": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"id": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
func dataSourceOrganizationalUnitsRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).OrganizationsConn
parent_id := d.Get("parent_id").(string)
params := &organizations.ListOrganizationalUnitsForParentInput{
ParentId: aws.String(parent_id),
}
var children []*organizations.OrganizationalUnit
err := conn.ListOrganizationalUnitsForParentPages(params,
func(page *organizations.ListOrganizationalUnitsForParentOutput, lastPage bool) bool {
children = append(children, page.OrganizationalUnits...)
return !lastPage
})
if err != nil {
return fmt.Errorf("error listing Organizations Organization Units for parent (%s): %w", parent_id, err)
}
d.SetId(parent_id)
if err := d.Set("children", FlattenOrganizationalUnits(children)); err != nil {
return fmt.Errorf("error setting children: %w", err)
}
return nil
}
func FlattenOrganizationalUnits(ous []*organizations.OrganizationalUnit) []map[string]interface{} {
if len(ous) == 0 {
return nil
}
var result []map[string]interface{}
for _, ou := range ous {
result = append(result, map[string]interface{}{
"arn": aws.StringValue(ou.Arn),
"id": aws.StringValue(ou.Id),
"name": aws.StringValue(ou.Name),
})
}
return result
}