-
Notifications
You must be signed in to change notification settings - Fork 154
/
data_source_huaweicloud_rms_services.go
201 lines (187 loc) · 6.09 KB
/
data_source_huaweicloud_rms_services.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
195
196
197
198
199
200
201
// Generated by PMS #62
package rms
import (
"context"
"strings"
"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/tidwall/gjson"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/filters"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/httphelper"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/schemas"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
)
func DataSourceRmsServices() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceRmsServicesRead,
Schema: map[string]*schema.Schema{
"track": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies whether resources are collected by default.`,
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the service name.`,
},
"services": {
Type: schema.TypeList,
Computed: true,
Description: `The service details list.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
Description: `The service name.`,
},
"display_name": {
Type: schema.TypeString,
Computed: true,
Description: `The display name of the cloud service.`,
},
"category_display_name": {
Type: schema.TypeString,
Computed: true,
Description: `The display name of the service category.`,
},
"resource_types": {
Type: schema.TypeList,
Computed: true,
Description: `The resource type list.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
Description: `The resource type name.`,
},
"display_name": {
Type: schema.TypeString,
Computed: true,
Description: `The display name of the resource type.`,
},
"global": {
Type: schema.TypeBool,
Computed: true,
Description: `Indicates whether a resource is a global resource.`,
},
"regions": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: `The list of supported regions.`,
},
"track": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates whether resources are collected by default.`,
},
"console_endpoint_id": {
Type: schema.TypeString,
Computed: true,
Description: `The endpoint ID of the console.`,
},
"console_detail_url": {
Type: schema.TypeString,
Computed: true,
Description: `The URL of the resource details page.`,
},
"console_list_url": {
Type: schema.TypeString,
Computed: true,
Description: `The URL of the resource list page.`,
},
},
},
},
},
},
},
},
}
}
type ServicesDSWrapper struct {
*schemas.ResourceDataWrapper
Config *config.Config
}
func newServicesDSWrapper(d *schema.ResourceData, meta interface{}) *ServicesDSWrapper {
return &ServicesDSWrapper{
ResourceDataWrapper: schemas.NewSchemaWrapper(d),
Config: meta.(*config.Config),
}
}
func dataSourceRmsServicesRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
wrapper := newServicesDSWrapper(d, meta)
lisProRst, err := wrapper.ListProviders()
if err != nil {
return diag.FromErr(err)
}
id, err := uuid.GenerateUUID()
if err != nil {
return diag.FromErr(err)
}
d.SetId(id)
err = wrapper.listProvidersToSchema(lisProRst)
if err != nil {
return diag.FromErr(err)
}
return nil
}
// @API CONFIG GET /v1/resource-manager/domains/{domain_id}/providers
func (w *ServicesDSWrapper) ListProviders() (*gjson.Result, error) {
client, err := w.NewClient(w.Config, "rms")
if err != nil {
return nil, err
}
uri := "/v1/resource-manager/domains/{domain_id}/providers"
uri = strings.ReplaceAll(uri, "{domain_id}", w.Config.DomainID)
params := map[string]any{
"track": w.Get("track"),
}
params = utils.RemoveNil(params)
return httphelper.New(client).
Method("GET").
URI(uri).
Query(params).
OffsetPager("resource_providers", "offset", "limit", 0).
Filter(
filters.New().From("resource_providers").
Where("provider", "=", w.Get("name")),
).
Request().
Result()
}
func (w *ServicesDSWrapper) listProvidersToSchema(body *gjson.Result) error {
d := w.ResourceData
mErr := multierror.Append(nil,
d.Set("services", schemas.SliceToList(body.Get("resource_providers"),
func(service gjson.Result) any {
return map[string]any{
"name": service.Get("provider").Value(),
"display_name": service.Get("display_name").Value(),
"category_display_name": service.Get("category_display_name").Value(),
"resource_types": schemas.SliceToList(service.Get("resource_types"),
func(resourceType gjson.Result) any {
return map[string]any{
"name": resourceType.Get("name").Value(),
"display_name": resourceType.Get("display_name").Value(),
"global": resourceType.Get("global").Value(),
"regions": schemas.SliceToStrList(resourceType.Get("regions")),
"track": resourceType.Get("track").Value(),
"console_endpoint_id": resourceType.Get("console_endpoint_id").Value(),
"console_detail_url": resourceType.Get("console_detail_url").Value(),
"console_list_url": resourceType.Get("console_list_url").Value(),
}
},
),
}
},
)),
)
return mErr.ErrorOrNil()
}