/
data_source_flexibleengine_dcs_product_v1.go
134 lines (119 loc) · 3.35 KB
/
data_source_flexibleengine_dcs_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
package flexibleengine
import (
"fmt"
"log"
"strconv"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/chnsz/golangsdk/openstack/dcs/v2/flavors"
hw_utils "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
)
func dataSourceDcsProductV1() *schema.Resource {
return &schema.Resource{
Read: dataSourceDcsProductRead,
Schema: map[string]*schema.Schema{
"engine": {
Type: schema.TypeString,
Optional: true,
Default: "redis",
ValidateFunc: validation.StringInSlice([]string{
"redis", "memcached",
}, true),
},
"engine_version": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
"3.0", "4.0;5.0",
}, false),
Computed: true,
},
"cache_mode": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"capacity": {
Type: schema.TypeFloat,
Optional: true,
Computed: true,
},
"replica_count": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"spec_code": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"cpu_architecture": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceDcsProductRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
dcsV2Client, err := config.DcsV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error get DCS v2 client: %s", err)
}
var capacity string
if v, ok := d.GetOk("capacity"); ok {
capacity = strconv.FormatFloat(v.(float64), 'f', -1, 64)
}
// build a list options
opts := flavors.ListOpts{
CacheMode: d.Get("cache_mode").(string),
Engine: d.Get("engine").(string),
EngineVersion: d.Get("engine_version").(string),
Capacity: capacity,
SpecCode: d.Get("spec_code").(string),
}
log.Printf("[DEBUG] The options of list DCS flavors: %#v", opts)
list, err := flavors.List(dcsV2Client, opts).Extract()
if err != nil {
return fmt.Errorf("Error while filtering data: %s", err)
}
if len(list) < 1 {
return fmt.Errorf("Your query returned no results. " +
"Please change your search criteria and try again.")
}
var pd flavors.Flavor
if v, ok := d.GetOk("replica_count"); ok {
filteredPds, err := hw_utils.FilterSliceWithField(list, map[string]interface{}{
"ReplicaCount": v.(int),
})
if err != nil {
return fmt.Errorf("Error while filtering data: %s", err)
}
if len(filteredPds) < 1 {
return fmt.Errorf("Your query returned no results with replica_count=%d. "+
"Please change your search criteria and try again.", v)
}
pd = filteredPds[0].(flavors.Flavor)
} else {
pd = list[0]
}
log.Printf("[DEBUG] querying DCS product: %+v", pd)
productID := pd.SpecCode + "-h"
d.SetId(productID)
cap, _ := strconv.ParseFloat(pd.Capacity[0], 64)
mErr := multierror.Append(nil,
d.Set("capacity", cap),
d.Set("spec_code", pd.SpecCode),
d.Set("engine", pd.Engine),
d.Set("engine_version", pd.EngineVersion),
d.Set("cache_mode", pd.CacheMode),
d.Set("replica_count", pd.ReplicaCount),
d.Set("cpu_architecture", pd.CPUType),
)
if mErr.ErrorOrNil() != nil {
return fmt.Errorf("error setting DCS product attributes: %s", mErr)
}
return nil
}