-
Notifications
You must be signed in to change notification settings - Fork 154
/
data_source_huaweicloud_rms_resource_aggregators.go
168 lines (154 loc) · 5 KB
/
data_source_huaweicloud_rms_resource_aggregators.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
// Generated by PMS #94
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 DataSourceRmsAggregators() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceRmsAggregatorsRead,
Schema: map[string]*schema.Schema{
"aggregator_id": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the resource aggregator ID.`,
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the resource aggregator name.`,
},
"type": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the resource aggregator type, which can be ACCOUNT or ORGANIZATION.`,
},
"aggregators": {
Type: schema.TypeList,
Computed: true,
Description: `The resource aggregators.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"account_ids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: `The source account list being aggregated.`,
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: `The resource aggregator name.`,
},
"id": {
Type: schema.TypeString,
Computed: true,
Description: `The resource aggregator ID.`,
},
"type": {
Type: schema.TypeString,
Computed: true,
Description: `The resource aggregator type.`,
},
"urn": {
Type: schema.TypeString,
Computed: true,
Description: `The resource aggregator identifier.`,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: `The time when the resource aggregator was created.`,
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
Description: `The time when the resource aggregator was updated.`,
},
},
},
},
},
}
}
type AggregatorsDSWrapper struct {
*schemas.ResourceDataWrapper
Config *config.Config
}
func newAggregatorsDSWrapper(d *schema.ResourceData, meta interface{}) *AggregatorsDSWrapper {
return &AggregatorsDSWrapper{
ResourceDataWrapper: schemas.NewSchemaWrapper(d),
Config: meta.(*config.Config),
}
}
func dataSourceRmsAggregatorsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
wrapper := newAggregatorsDSWrapper(d, meta)
lisConAggRst, err := wrapper.ListConfigurationAggregators()
if err != nil {
return diag.FromErr(err)
}
id, err := uuid.GenerateUUID()
if err != nil {
return diag.FromErr(err)
}
d.SetId(id)
err = wrapper.listConfigurationAggregatorsToSchema(lisConAggRst)
if err != nil {
return diag.FromErr(err)
}
return nil
}
// @API CONFIG GET /v1/resource-manager/domains/{domain_id}/aggregators
func (w *AggregatorsDSWrapper) ListConfigurationAggregators() (*gjson.Result, error) {
client, err := w.NewClient(w.Config, "rms")
if err != nil {
return nil, err
}
uri := "/v1/resource-manager/domains/{domain_id}/aggregators"
uri = strings.ReplaceAll(uri, "{domain_id}", w.Config.DomainID)
params := map[string]any{
"aggregator_name": w.Get("name"),
}
params = utils.RemoveNil(params)
return httphelper.New(client).
Method("GET").
URI(uri).
Query(params).
MarkerPager("configuration_aggregators", " page_info.next_marker", "marker").
Filter(
filters.New().From("configuration_aggregators").
Where("aggregator_id", "=", w.Get("aggregator_id")).
Where("aggregator_type", "=", w.Get("type")),
).
Request().
Result()
}
func (w *AggregatorsDSWrapper) listConfigurationAggregatorsToSchema(body *gjson.Result) error {
d := w.ResourceData
mErr := multierror.Append(nil,
d.Set("aggregators", schemas.SliceToList(body.Get("configuration_aggregators"),
func(aggregator gjson.Result) any {
return map[string]any{
"account_ids": schemas.SliceToStrList(aggregator.Get("account_aggregation_sources.domain_ids")),
"name": aggregator.Get("aggregator_name").Value(),
"id": aggregator.Get("aggregator_id").Value(),
"type": aggregator.Get("aggregator_type").Value(),
"urn": aggregator.Get("aggregator_urn").Value(),
"created_at": aggregator.Get("created_at").Value(),
"updated_at": aggregator.Get("updated_at").Value(),
}
},
)),
)
return mErr.ErrorOrNil()
}