/
data_source_huaweicloud_organizations_accounts.go
173 lines (150 loc) · 4.67 KB
/
data_source_huaweicloud_organizations_accounts.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// ---------------------------------------------------------------
// *** AUTO GENERATED CODE ***
// @Product Organizations
// ---------------------------------------------------------------
package organizations
import (
"context"
"fmt"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/chnsz/golangsdk"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/common"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
)
// @API Organizations GET /v1/organizations/accounts
func DataSourceAccounts() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceAccountsRead,
Schema: map[string]*schema.Schema{
"parent_id": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the ID of root or organizational unit.`,
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the name of the account.`,
},
"accounts": {
Type: schema.TypeList,
Elem: organizationsAccountSchema(),
Computed: true,
Description: `List of accounts in an organization.`,
},
},
}
}
func organizationsAccountSchema() *schema.Resource {
sc := schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: `Unique ID of an account`,
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: `Name of the account`,
},
"urn": {
Type: schema.TypeString,
Computed: true,
Description: `Uniform resource name of the account`,
},
"join_method": {
Type: schema.TypeString,
Computed: true,
Description: `How an account joined an organization`,
},
"joined_at": {
Type: schema.TypeString,
Computed: true,
Description: `Time when an account joined an organization`,
},
},
}
return &sc
}
func dataSourceAccountsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
listAccountsHttpUrl := "v1/organizations/accounts"
listAccountsProduct := "organizations"
listAccountsClient, err := cfg.NewServiceClient(listAccountsProduct, region)
if err != nil {
return diag.Errorf("error creating Organizations client: %s", err)
}
listAccountsPath := listAccountsClient.Endpoint + listAccountsHttpUrl
listAccountsOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
}
filterName := d.Get("name").(string)
var orgAccounts []interface{}
var marker string
var queryPath string
for {
queryPath = listAccountsPath + buildListAccountsQueryParams(d, marker)
listAccountsResp, err := listAccountsClient.Request("GET", queryPath, &listAccountsOpt)
if err != nil {
return common.CheckDeletedDiag(d, err, "error retrieving Organizations accounts")
}
listAccountsRespBody, err := utils.FlattenResponse(listAccountsResp)
if err != nil {
return diag.FromErr(err)
}
onePageAccounts := flattenAccountsResp(listAccountsRespBody, filterName)
orgAccounts = append(orgAccounts, onePageAccounts...)
marker = utils.PathSearch("page_info.next_marker", listAccountsRespBody, "").(string)
if marker == "" {
break
}
}
uuid, err := uuid.GenerateUUID()
if err != nil {
return diag.Errorf("unable to generate ID: %s", err)
}
d.SetId(uuid)
mErr := multierror.Append(nil,
d.Set("accounts", orgAccounts),
)
return diag.FromErr(mErr.ErrorOrNil())
}
func flattenAccountsResp(resp interface{}, name string) []interface{} {
if resp == nil {
return nil
}
jsonPath := "accounts"
if name != "" {
jsonPath += fmt.Sprintf("[?name=='%s']", name)
}
curJson := utils.PathSearch(jsonPath, resp, make([]interface{}, 0))
curArray := curJson.([]interface{})
rst := make([]interface{}, 0, len(curArray))
for _, v := range curArray {
rst = append(rst, map[string]interface{}{
"id": utils.PathSearch("id", v, nil),
"name": utils.PathSearch("name", v, nil),
"urn": utils.PathSearch("urn", v, nil),
"join_method": utils.PathSearch("join_method", v, nil),
"joined_at": utils.PathSearch("joined_at", v, nil),
})
}
return rst
}
func buildListAccountsQueryParams(d *schema.ResourceData, marker string) string {
// the default value of limit is 200
res := "?limit=200"
if marker != "" {
res = fmt.Sprintf("%s&marker=%v", res, marker)
}
if v, ok := d.GetOk("parent_id"); ok {
res = fmt.Sprintf("%s&parent_id=%v", res, v)
}
return res
}