/
resource_huaweicloud_organizations_trusted_service.go
194 lines (162 loc) · 6.21 KB
/
resource_huaweicloud_organizations_trusted_service.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// ---------------------------------------------------------------
// *** AUTO GENERATED CODE ***
// @Product Organizations
// ---------------------------------------------------------------
package organizations
import (
"context"
"fmt"
"github.com/hashicorp/go-multierror"
"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/trusted-services
// @API Organizations POST /v1/organizations/trusted-services/disable
// @API Organizations POST /v1/organizations/trusted-services/enable
func ResourceTrustedService() *schema.Resource {
return &schema.Resource{
CreateContext: resourceTrustedServiceCreate,
ReadContext: resourceTrustedServiceRead,
DeleteContext: resourceTrustedServiceDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"service": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `Specifies the name of the trusted service principal.`,
},
"enabled_at": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the date when the trusted service was integrated with Organizations.`,
},
},
}
}
func resourceTrustedServiceCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
// createTrustedService: create Organizations trusted service
var (
createTrustedServiceHttpUrl = "v1/organizations/trusted-services/enable"
createTrustedServiceProduct = "organizations"
)
createTrustedServiceClient, err := cfg.NewServiceClient(createTrustedServiceProduct, region)
if err != nil {
return diag.Errorf("error creating Organizations Client: %s", err)
}
createTrustedServicePath := createTrustedServiceClient.Endpoint + createTrustedServiceHttpUrl
createTrustedServiceOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
}
createTrustedServiceOpt.JSONBody = utils.RemoveNil(buildTrustedServiceBodyParams(d))
_, err = createTrustedServiceClient.Request("POST", createTrustedServicePath, &createTrustedServiceOpt)
if err != nil {
return diag.Errorf("error creating TrustedService: %s", err)
}
serviceName := d.Get("service").(string)
d.SetId(serviceName)
return resourceTrustedServiceRead(ctx, d, meta)
}
func buildTrustedServiceBodyParams(d *schema.ResourceData) map[string]interface{} {
bodyParams := map[string]interface{}{
"service_principal": utils.ValueIngoreEmpty(d.Get("service")),
}
return bodyParams
}
func resourceTrustedServiceRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
var mErr *multierror.Error
// getTrustedService: Query Organizations trusted service
var (
getTrustedServiceHttpUrl = "v1/organizations/trusted-services"
getTrustedServiceProduct = "organizations"
)
getTrustedServiceClient, err := cfg.NewServiceClient(getTrustedServiceProduct, region)
if err != nil {
return diag.Errorf("error creating Organizations Client: %s", err)
}
getTrustedServiceBasePath := getTrustedServiceClient.Endpoint + getTrustedServiceHttpUrl
getTrustedServicePath := getTrustedServiceBasePath + buildGetTrustedServiceQueryParams("")
getTrustedServiceOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
}
var serviceName string
var enabledAt string
getTrustedServicesLoop:
for {
getTrustedServiceResp, err := getTrustedServiceClient.Request("GET", getTrustedServicePath,
&getTrustedServiceOpt)
if err != nil {
return common.CheckDeletedDiag(d, err, "error retrieving TrustedService")
}
getTrustedServiceRespBody, err := utils.FlattenResponse(getTrustedServiceResp)
if err != nil {
return diag.FromErr(err)
}
trustedServices := utils.PathSearch("trusted_services", getTrustedServiceRespBody, nil)
if trustedServices == nil {
return common.CheckDeletedDiag(d, golangsdk.ErrDefault404{}, "")
}
for _, trustedService := range trustedServices.([]interface{}) {
servicePrincipal := utils.PathSearch("service_principal", trustedService, "").(string)
if servicePrincipal == d.Id() {
serviceName = servicePrincipal
enabledAt = utils.PathSearch("enabled_at", trustedService, "").(string)
break getTrustedServicesLoop
}
}
marker := utils.PathSearch("page_info.next_marker", getTrustedServiceRespBody, nil)
if marker == nil {
break
}
getTrustedServicePath = getTrustedServiceBasePath + buildGetTrustedServiceQueryParams(marker.(string))
}
if serviceName == "" {
return common.CheckDeletedDiag(d, golangsdk.ErrDefault404{}, "")
}
mErr = multierror.Append(
mErr,
d.Set("service", serviceName),
d.Set("enabled_at", enabledAt),
)
return diag.FromErr(mErr.ErrorOrNil())
}
func buildGetTrustedServiceQueryParams(marker string) string {
res := "?limit=100"
if marker != "" {
res = fmt.Sprintf("%s&marker=%s", res, marker)
}
return res
}
func resourceTrustedServiceDelete(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
// deleteTrustedService: Delete Organizations trusted service
var (
deleteTrustedServiceHttpUrl = "v1/organizations/trusted-services/disable"
deleteTrustedServiceProduct = "organizations"
)
deleteTrustedServiceClient, err := cfg.NewServiceClient(deleteTrustedServiceProduct, region)
if err != nil {
return diag.Errorf("error creating Organizations Client: %s", err)
}
deleteTrustedServicePath := deleteTrustedServiceClient.Endpoint + deleteTrustedServiceHttpUrl
deleteTrustedServiceOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
}
deleteTrustedServiceOpt.JSONBody = utils.RemoveNil(buildTrustedServiceBodyParams(d))
_, err = deleteTrustedServiceClient.Request("POST", deleteTrustedServicePath, &deleteTrustedServiceOpt)
if err != nil {
return diag.Errorf("error deleting TrustedService: %s", err)
}
return nil
}