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
/
resource_flexibleengine_css_snapshot_v1.go
218 lines (188 loc) · 5.55 KB
/
resource_flexibleengine_css_snapshot_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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package flexibleengine
import (
"fmt"
"log"
"strings"
"time"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/huaweicloud/golangsdk"
"github.com/huaweicloud/golangsdk/openstack/css/v1/snapshots"
)
func resourceCssSnapshotV1() *schema.Resource {
return &schema.Resource{
Create: resourceCssSnapshotV1Create,
Read: resourceCssSnapshotV1Read,
Delete: resourceCssSnapshotV1Delete,
Importer: &schema.ResourceImporter{
State: resourceCssSnapshotImport,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(3 * time.Minute),
},
Schema: map[string]*schema.Schema{
"cluster_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"indices": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"cluster_name": {
Type: schema.TypeString,
Computed: true,
},
"backup_type": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceCssSnapshotV1Create(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
cssClient, err := config.sdkClient(GetRegion(d, config), "css")
if err != nil {
return fmt.Errorf("Error creating FlexibleEngine CSS client: %s", err)
}
clusterId := d.Get("cluster_id").(string)
createOpts := &snapshots.CreateOpts{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
Indices: d.Get("indices").(string),
}
log.Printf("[DEBUG] Create Options: %#v", createOpts)
snap, err := snapshots.Create(cssClient, createOpts, clusterId).Extract()
if err != nil {
return fmt.Errorf("Error creating FlexibleEngine CSS snapshot: %s", err)
}
// Store the snapshot ID
d.SetId(snap.ID)
log.Printf("[DEBUG] Waiting for snapshot (%s) to complete", d.Id())
stateConf := &resource.StateChangeConf{
Pending: []string{"BUILDING"},
Target: []string{"COMPLETED"},
Refresh: cssSnapshotStateRefreshFunc(cssClient, clusterId, d.Id()),
Timeout: d.Timeout(schema.TimeoutCreate),
Delay: 10 * time.Second,
MinTimeout: 5 * time.Second,
}
_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf(
"Error waiting for snapshot (%s) to complete: %s",
d.Id(), err)
}
return resourceCssSnapshotV1Read(d, meta)
}
func resourceCssSnapshotV1Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
cssClient, err := config.sdkClient(GetRegion(d, config), "css")
if err != nil {
return fmt.Errorf("Error creating FlexibleEngine CSS client: %s", err)
}
clusterId := d.Get("cluster_id").(string)
snapList, err := snapshots.List(cssClient, clusterId).Extract()
if err != nil {
return CheckDeleted(d, err, "snapshot")
}
// find the snapshot by ID
var snap snapshots.Snapshot
for _, v := range snapList {
if v.ID == d.Id() {
snap = v
break
}
}
if snap.ID == "" {
log.Printf("[INFO] the snapshot %s does not exist", d.Id())
d.SetId("")
return nil
}
log.Printf("[DEBUG] Retrieved the sanpshot %s: %+v", d.Id(), snap)
d.Set("name", snap.Name)
d.Set("description", snap.Description)
d.Set("status", snap.Status)
d.Set("indices", snap.Indices)
d.Set("cluster_id", snap.ClusterID)
d.Set("cluster_name", snap.ClusterName)
// Method is more suitable for backup_type
d.Set("backup_type", snap.Method)
return nil
}
func resourceCssSnapshotV1Delete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
cssClient, err := config.sdkClient(GetRegion(d, config), "css")
if err != nil {
return fmt.Errorf("Error creating FlexibleEngine CSS storage client: %s", err)
}
clusterId := d.Get("cluster_id").(string)
if err := snapshots.Delete(cssClient, clusterId, d.Id()).ExtractErr(); err != nil {
return CheckDeleted(d, err, "snapshot")
}
d.SetId("")
return nil
}
// cssSnapshotStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
// an CSS cluster snapshot.
func cssSnapshotStateRefreshFunc(client *golangsdk.ServiceClient, clusterId, id string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
snapList, err := snapshots.List(client, clusterId).Extract()
if err != nil {
return nil, "FAILED", err
}
// find the snapshot by ID
var snap snapshots.Snapshot
for _, v := range snapList {
if v.ID == id {
snap = v
break
}
}
if snap.ID == "" {
return nil, "NOTEXIST", fmt.Errorf("The specified snapshot %s not exist", id)
}
return snap, snap.Status, nil
}
}
func resourceCssSnapshotImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
parts := strings.SplitN(d.Id(), "/", 2)
if len(parts) != 2 {
err := fmt.Errorf("Invalid format specified for CSS snapshot. Format must be <cluster id>/<snapshot id>")
return nil, err
}
clusterID := parts[0]
snapshotID := parts[1]
config := meta.(*Config)
client, err := config.sdkClient(GetRegion(d, config), "css")
if err != nil {
return nil, fmt.Errorf("Error creating css client, err=%s", err)
}
// check the css cluster whether exists
d.SetId(clusterID)
if _, err := sendCssClusterV1ReadRequest(d, client); err != nil {
return nil, err
}
d.Set("cluster_id", clusterID)
d.SetId(snapshotID)
return []*schema.ResourceData{d}, nil
}