/
data_source_huaweicloud_rds_storage_types.go
179 lines (157 loc) · 5.19 KB
/
data_source_huaweicloud_rds_storage_types.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
// ---------------------------------------------------------------
// *** AUTO GENERATED CODE ***
// @Product RDS
// ---------------------------------------------------------------
package rds
import (
"context"
"fmt"
"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/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"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 RDS GET /v3/{project_id}/storage-type/{db_type}
func DataSourceStoragetype() *schema.Resource {
return &schema.Resource{
ReadContext: resourceStoragetypeRead,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"db_type": {
Type: schema.TypeString,
Required: true,
Description: `DB engine. The valid values are **MySQL**, **PostgreSQL**, **SQLServer**.`,
ValidateFunc: validation.StringInSlice([]string{
"MySQL", "PostgreSQL", "SQLServer",
}, false),
},
"db_version": {
Type: schema.TypeString,
Required: true,
Description: `DB version number.`,
},
"instance_mode": {
Type: schema.TypeString,
Optional: true,
Description: `HA mode. The valid values are **single**, **ha**, **replica**.`,
ValidateFunc: validation.StringInSlice([]string{
"single", "ha", "replica",
}, false),
},
"storage_types": {
Type: schema.TypeList,
Elem: StoragetypeStorageTypeSchema(),
Computed: true,
Description: `Storage_type list. For details, see Data structure of the storageType field.`,
},
},
}
}
func StoragetypeStorageTypeSchema() *schema.Resource {
sc := schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
Description: `Storage type.`,
},
"az_status": {
Type: schema.TypeMap,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
Description: `The status details of the AZs to which the specification belongs.`,
},
"support_compute_group_type": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
Description: `Performance specifications.`,
},
},
}
return &sc
}
func resourceStoragetypeRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*config.Config)
region := config.GetRegion(d)
var mErr *multierror.Error
// listStorageTypes: Query the List of RDS storage-types.
var (
listStorageTypesHttpUrl = "v3/{project_id}/storage-type/{db_type}"
listStorageTypesProduct = "rds"
)
listStorageTypesClient, err := config.NewServiceClient(listStorageTypesProduct, region)
if err != nil {
return diag.Errorf("error creating Storagetype Client: %s", err)
}
listStorageTypesPath := listStorageTypesClient.Endpoint + listStorageTypesHttpUrl
listStorageTypesPath = strings.ReplaceAll(listStorageTypesPath, "{project_id}", listStorageTypesClient.ProjectID)
listStorageTypesPath = strings.ReplaceAll(listStorageTypesPath, "{db_type}",
fmt.Sprintf("%v", d.Get("db_type")))
listStorageTypesqueryParams := buildListStorageTypesQueryParams(d)
listStorageTypesPath += listStorageTypesqueryParams
listStorageTypesOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
OkCodes: []int{
200,
},
}
listStorageTypesResp, err := listStorageTypesClient.Request("GET", listStorageTypesPath, &listStorageTypesOpt)
if err != nil {
return common.CheckDeletedDiag(d, err, "error retrieving Storagetype")
}
listStorageTypesRespBody, err := utils.FlattenResponse(listStorageTypesResp)
if err != nil {
return diag.FromErr(err)
}
uuid, err := uuid.GenerateUUID()
if err != nil {
return diag.Errorf("unable to generate ID: %s", err)
}
d.SetId(uuid)
mErr = multierror.Append(
mErr,
d.Set("region", region),
d.Set("storage_types", flattenListStorageTypesstorageType(listStorageTypesRespBody)),
)
return diag.FromErr(mErr.ErrorOrNil())
}
func flattenListStorageTypesstorageType(resp interface{}) []interface{} {
if resp == nil {
return nil
}
curJson := utils.PathSearch("storage_type", resp, make([]interface{}, 0))
curArray := curJson.([]interface{})
rst := make([]interface{}, 0, len(curArray))
for _, v := range curArray {
rst = append(rst, map[string]interface{}{
"name": utils.PathSearch("name", v, nil),
"az_status": utils.PathSearch("az_status", v, nil),
"support_compute_group_type": utils.PathSearch("support_compute_group_type", v, nil),
})
}
return rst
}
func buildListStorageTypesQueryParams(d *schema.ResourceData) string {
res := ""
if v, ok := d.GetOk("db_version"); ok {
res = fmt.Sprintf("%s&version_name=%v", res, v)
}
if v, ok := d.GetOk("instance_mode"); ok {
res = fmt.Sprintf("%s&ha_mode=%v", res, v)
}
if res != "" {
res = "?" + res[1:]
}
return res
}