This repository has been archived by the owner on Aug 12, 2020. It is now read-only.
forked from huaweicloud/terraform-provider-huaweicloud
/
data_source_huaweicloud_dms_product_v1.go
194 lines (177 loc) · 4.95 KB
/
data_source_huaweicloud_dms_product_v1.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
package huaweicloud
import (
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/huaweicloud/golangsdk/openstack/dms/v1/products"
)
func dataSourceDmsProductV1() *schema.Resource {
return &schema.Resource{
Read: dataSourceDmsProductV1Read,
Schema: map[string]*schema.Schema{
"engine": {
Type: schema.TypeString,
Required: true,
},
"version": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"instance_type": {
Type: schema.TypeString,
Required: true,
},
"vm_specification": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"storage": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"bandwidth": {
Type: schema.TypeString,
Optional: true,
},
"partition_num": {
Type: schema.TypeString,
Optional: true,
},
"storage_spec_code": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"io_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"node_num": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
}
}
func getIObyIOtype(d *schema.ResourceData, IOs []products.IO) []products.IO {
io_type := d.Get("io_type").(string)
storage_spec_code := d.Get("storage_spec_code").(string)
if io_type != "" || storage_spec_code != "" {
var getIOs []products.IO
for _, io := range IOs {
if io_type == io.IOType || storage_spec_code == io.StorageSpecCode {
getIOs = append(getIOs, io)
}
}
return getIOs
}
return IOs
}
func dataSourceDmsProductV1Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
dmsV1Client, err := config.dmsV1Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error get HuaweiCloud dms product client: %s", err)
}
instance_engine := d.Get("engine").(string)
if instance_engine != "rabbitmq" && instance_engine != "kafka" {
return fmt.Errorf("The instance_engine value should be 'rabbitmq' or 'kafka', not: %s", instance_engine)
}
v, err := products.Get(dmsV1Client, instance_engine).Extract()
if err != nil {
return err
}
Products := v.Hourly
log.Printf("[DEBUG] Dms get products : %+v", Products)
instance_type := d.Get("instance_type").(string)
if instance_type != "single" && instance_type != "cluster" {
return fmt.Errorf("The instance_type value should be 'single' or 'cluster', not: %s", instance_type)
}
var FilteredPd []products.Detail
var FilteredPdInfo []products.ProductInfo
for _, pd := range Products {
version := d.Get("version").(string)
if version != "" && pd.Version != version {
continue
}
for _, value := range pd.Values {
if value.Name != instance_type {
continue
}
for _, detail := range value.Details {
vm_specification := d.Get("vm_specification").(string)
if vm_specification != "" && detail.VMSpecification != vm_specification {
continue
}
bandwidth := d.Get("bandwidth").(string)
if bandwidth != "" && detail.Bandwidth != bandwidth {
continue
}
if instance_type == "single" || instance_engine == "kafka" {
storage := d.Get("storage").(string)
if storage != "" && detail.Storage != storage {
continue
}
IOs := getIObyIOtype(d, detail.IOs)
if len(IOs) == 0 {
continue
}
detail.IOs = IOs
} else {
for _, pdInfo := range detail.ProductInfos {
storage := d.Get("storage").(string)
if storage != "" && pdInfo.Storage != storage {
continue
}
node_num := d.Get("node_num").(string)
if node_num != "" && pdInfo.NodeNum != node_num {
continue
}
IOs := getIObyIOtype(d, pdInfo.IOs)
if len(IOs) == 0 {
continue
}
pdInfo.IOs = IOs
FilteredPdInfo = append(FilteredPdInfo, pdInfo)
}
if len(FilteredPdInfo) == 0 {
continue
}
detail.ProductInfos = FilteredPdInfo
}
FilteredPd = append(FilteredPd, detail)
}
}
}
if len(FilteredPd) < 1 {
return fmt.Errorf("Your query returned no results. Please change your filters and try again.")
}
pd := FilteredPd[0]
d.Set("vm_specification", pd.VMSpecification)
if instance_type == "single" || instance_engine == "kafka" {
d.SetId(pd.ProductID)
d.Set("storage", pd.Storage)
d.Set("partition_num", pd.PartitionNum)
d.Set("bandwidth", pd.Bandwidth)
d.Set("storage_spec_code", pd.IOs[0].StorageSpecCode)
d.Set("io_type", pd.IOs[0].IOType)
log.Printf("[DEBUG] Dms product : %+v", pd)
} else {
if len(pd.ProductInfos) < 1 {
return fmt.Errorf("Your query returned no results. Please change your filters and try again.")
}
pdInfo := pd.ProductInfos[0]
d.SetId(pdInfo.ProductID)
d.Set("storage", pdInfo.Storage)
d.Set("io_type", pdInfo.IOs[0].IOType)
d.Set("node_num", pdInfo.NodeNum)
d.Set("storage_spec_code", pdInfo.IOs[0].StorageSpecCode)
log.Printf("[DEBUG] Dms product : %+v", pdInfo)
}
return nil
}