-
Notifications
You must be signed in to change notification settings - Fork 154
/
data_source_huaweicloud_dli_quotas.go
139 lines (125 loc) · 3.67 KB
/
data_source_huaweicloud_dli_quotas.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
// Generated by PMS #66
package dli
import (
"context"
"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"
)
func DataSourceDliQuotas() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceDliQuotasRead,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: `Specifies the region in which to query the resource quota. If omitted, the provider-level region will be used.`,
},
"type": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the resource type that used to query corresponding quota.`,
},
"quotas": {
Type: schema.TypeList,
Computed: true,
Description: `The list of resource quotas.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"min": {
Type: schema.TypeInt,
Computed: true,
Description: `The minimum quota of resource.`,
},
"max": {
Type: schema.TypeInt,
Computed: true,
Description: `The maximum quota of resource.`,
},
"quota": {
Type: schema.TypeInt,
Computed: true,
Description: `The current quota of resource.`,
},
"used": {
Type: schema.TypeInt,
Computed: true,
Description: `The used quota of resource.`,
},
"type": {
Type: schema.TypeString,
Computed: true,
Description: `The resource type corresponding to quota.`,
},
},
},
},
},
}
}
type QuotasDSWrapper struct {
*schemas.ResourceDataWrapper
Config *config.Config
}
func newQuotasDSWrapper(d *schema.ResourceData, meta interface{}) *QuotasDSWrapper {
return &QuotasDSWrapper{
ResourceDataWrapper: schemas.NewSchemaWrapper(d),
Config: meta.(*config.Config),
}
}
func dataSourceDliQuotasRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
wrapper := newQuotasDSWrapper(d, meta)
showQuotaRst, err := wrapper.ShowQuota()
if err != nil {
return diag.FromErr(err)
}
id, _ := uuid.GenerateUUID()
d.SetId(id)
err = wrapper.showQuotaToSchema(showQuotaRst)
if err != nil {
return diag.FromErr(err)
}
return nil
}
// @API DLI GET /v3/{project_id}/quotas
func (w *QuotasDSWrapper) ShowQuota() (*gjson.Result, error) {
client, err := w.NewClient(w.Config, "dli")
if err != nil {
return nil, err
}
uri := "/v3/{project_id}/quotas"
return httphelper.New(client).
Method("GET").
URI(uri).
Filter(
filters.New().From("quotas.resources").
Where("type", "=", w.Get("type")),
).
Request().
Result()
}
func (w *QuotasDSWrapper) showQuotaToSchema(body *gjson.Result) error {
d := w.ResourceData
mErr := multierror.Append(nil,
d.Set("region", w.Config.GetRegion(w.ResourceData)),
d.Set("quotas", schemas.SliceToList(body.Get("quotas.resources"),
func(quotas gjson.Result) any {
return map[string]any{
"min": quotas.Get("min").Value(),
"max": quotas.Get("max").Value(),
"quota": quotas.Get("quota").Value(),
"used": quotas.Get("used").Value(),
"type": quotas.Get("type").Value(),
}
},
)),
)
return mErr.ErrorOrNil()
}