This repository has been archived by the owner on Sep 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
data_source_flexibleengine_cce_node_v3.go
210 lines (192 loc) · 4.86 KB
/
data_source_flexibleengine_cce_node_v3.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package flexibleengine
import (
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/huaweicloud/golangsdk/openstack/cce/v3/nodes"
)
func dataSourceCceNodesV3() *schema.Resource {
return &schema.Resource{
Read: dataSourceCceNodesV3Read,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"cluster_id": {
Type: schema.TypeString,
Required: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
},
"node_id": {
Type: schema.TypeString,
Optional: true,
},
"flavor_id": {
Type: schema.TypeString,
Computed: true,
},
"availability_zone": {
Type: schema.TypeString,
Computed: true,
},
"key_pair": {
Type: schema.TypeString,
Computed: true,
},
"charge_mode": {
Type: schema.TypeString,
Computed: true,
},
"bandwidth_size": {
Type: schema.TypeInt,
Computed: true,
},
"share_type": {
Type: schema.TypeString,
Computed: true,
},
"ip_type": {
Type: schema.TypeString,
Computed: true,
},
"disk_size": {
Type: schema.TypeInt,
Computed: true,
},
"volume_type": {
Type: schema.TypeString,
Computed: true,
},
"extend_param": {
Type: schema.TypeString,
Computed: true,
},
"data_volumes": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"disk_size": {
Type: schema.TypeInt,
Computed: true,
},
"volume_type": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"billing_mode": {
Type: schema.TypeInt,
Computed: true,
},
"status": {
Type: schema.TypeString,
Optional: true,
},
"server_id": {
Type: schema.TypeString,
Computed: true,
},
"public_ip": {
Type: schema.TypeString,
Computed: true,
},
"private_ip": {
Type: schema.TypeString,
Computed: true,
},
"eip_ids": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"spec_extend_param": {
Type: schema.TypeString,
Computed: true,
},
"eip_count": {
Type: schema.TypeInt,
Computed: true,
},
},
}
}
func dataSourceCceNodesV3Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
cceClient, err := config.cceV3Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Unable to create flexibleengine CCE client : %s", err)
}
listOpts := nodes.ListOpts{
Uid: d.Get("node_id").(string),
Name: d.Get("name").(string),
Phase: d.Get("status").(string),
}
if v, ok := d.GetOk("name"); ok {
listOpts.Name = v.(string)
}
if v, ok := d.GetOk("node_id"); ok {
listOpts.Uid = v.(string)
}
if v, ok := d.GetOk("status"); ok {
listOpts.Phase = v.(string)
}
refinedNodes, err := nodes.List(cceClient, d.Get("cluster_id").(string), listOpts)
if err != nil {
return fmt.Errorf("Unable to retrieve Nodes: %s", err)
}
if len(refinedNodes) < 1 {
return fmt.Errorf("Your query returned no results. " +
"Please change your search criteria and try again.")
}
if len(refinedNodes) > 1 {
return fmt.Errorf("Your query returned more than one result." +
" Please try a more specific search criteria")
}
Node := refinedNodes[0]
var v []map[string]interface{}
for _, volume := range Node.Spec.DataVolumes {
mapping := map[string]interface{}{
"disk_size": volume.Size,
"volume_type": volume.VolumeType,
}
v = append(v, mapping)
}
pids := Node.Spec.PublicIP.Ids
PublicIDs := make([]string, len(pids))
for i, val := range pids {
PublicIDs[i] = val
}
log.Printf("[DEBUG] Retrieved Nodes using given filter %s: %+v", Node.Metadata.Id, Node)
d.SetId(Node.Metadata.Id)
d.Set("node_id", Node.Metadata.Id)
d.Set("name", Node.Metadata.Name)
d.Set("flavor_id", Node.Spec.Flavor)
d.Set("availability_zone", Node.Spec.Az)
d.Set("billing_mode", Node.Spec.BillingMode)
d.Set("status", Node.Status.Phase)
d.Set("data_volumes", v)
d.Set("disk_size", Node.Spec.RootVolume.Size)
d.Set("volume_type", Node.Spec.RootVolume.VolumeType)
d.Set("extend_param", Node.Spec.RootVolume.ExtendParam)
d.Set("key_pair", Node.Spec.Login.SshKey)
d.Set("charge_mode", Node.Spec.PublicIP.Eip.Bandwidth.ChargeMode)
d.Set("bandwidth_size", Node.Spec.PublicIP.Eip.Bandwidth.Size)
d.Set("share_type", Node.Spec.PublicIP.Eip.Bandwidth.ShareType)
d.Set("ip_type", Node.Spec.PublicIP.Eip.IpType)
d.Set("server_id", Node.Status.ServerID)
d.Set("public_ip", Node.Status.PublicIP)
d.Set("private_ip", Node.Status.PrivateIP)
d.Set("spec_extend_param", Node.Spec.ExtendParam)
d.Set("eip_count", Node.Spec.PublicIP.Count)
d.Set("eip_ids", PublicIDs)
return nil
}