/
resource_huaweicloud_evs_snapshot.go
222 lines (192 loc) · 6.34 KB
/
resource_huaweicloud_evs_snapshot.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
219
220
221
222
package evs
import (
"context"
"errors"
"fmt"
"log"
"time"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/chnsz/golangsdk"
"github.com/chnsz/golangsdk/openstack/evs/v2/snapshots"
"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 EVS DELETE /v2/{project_id}/cloudsnapshots/{snapshot_id}
// @API EVS GET /v2/{project_id}/cloudsnapshots/{snapshot_id}
// @API EVS PUT /v2/{project_id}/cloudsnapshots/{snapshot_id}
// @API EVS POST /v2/{project_id}/cloudsnapshots
func ResourceEvsSnapshotV2() *schema.Resource {
return &schema.Resource{
CreateContext: resourceEvsSnapshotV2Create,
ReadContext: resourceEvsSnapshotV2Read,
UpdateContext: resourceEvsSnapshotV2Update,
DeleteContext: resourceEvsSnapshotV2Delete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(3 * time.Minute),
},
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"volume_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Required: true,
},
// To avoid triggering changes metadata is not backfilled during read.
"metadata": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"force": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"size": {
Type: schema.TypeInt,
Computed: true,
},
},
}
}
func resourceEvsSnapshotV2Create(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
evsClient, err := cfg.BlockStorageV2Client(cfg.GetRegion(d))
if err != nil {
return diag.Errorf("error creating EVS storage client: %s", err)
}
createOpts := &snapshots.CreateOpts{
VolumeID: d.Get("volume_id").(string),
Name: d.Get("name").(string),
Description: d.Get("description").(string),
Force: d.Get("force").(bool),
Metadata: utils.ExpandToStringMap(d.Get("metadata").(map[string]interface{})),
}
log.Printf("[DEBUG] Create Options: %#v", createOpts)
v, err := snapshots.Create(evsClient, createOpts).Extract()
if err != nil {
return diag.Errorf("error creating EVS snapshot: %s", err)
}
// Wait for the snapshot to become available.
log.Printf("[DEBUG] Waiting for volume to become available")
err = snapshots.WaitForStatus(evsClient, v.ID, "available", int(d.Timeout(schema.TimeoutCreate)/time.Second))
if err != nil {
return diag.FromErr(err)
}
// Store the ID now
d.SetId(v.ID)
return resourceEvsSnapshotV2Read(ctx, d, meta)
}
func resourceEvsSnapshotV2Read(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
evsClient, err := cfg.BlockStorageV2Client(cfg.GetRegion(d))
if err != nil {
return diag.Errorf("error creating EVS storage client: %s", err)
}
v, err := snapshots.Get(evsClient, d.Id()).Extract()
if err != nil {
return common.CheckDeletedDiag(d, err, "snapshot")
}
log.Printf("[DEBUG] Retrieved volume %s: %+v", d.Id(), v)
mErr := multierror.Append(nil,
d.Set("volume_id", v.VolumeID),
d.Set("name", v.Name),
d.Set("description", v.Description),
d.Set("status", v.Status),
d.Set("size", v.Size),
)
if mErr.ErrorOrNil() != nil {
diag.FromErr(mErr)
}
return nil
}
func resourceEvsSnapshotV2Update(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
evsClient, err := cfg.BlockStorageV2Client(cfg.GetRegion(d))
if err != nil {
return diag.Errorf("error creating EVS storage client: %s", err)
}
updateOpts := snapshots.UpdateOpts{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
}
_, err = snapshots.Update(evsClient, d.Id(), updateOpts).Extract()
if err != nil {
return diag.Errorf("error updating EVS snapshot: %s", err)
}
return resourceEvsSnapshotV2Read(ctx, d, meta)
}
func resourceEvsSnapshotV2Delete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
evsClient, err := cfg.BlockStorageV2Client(cfg.GetRegion(d))
if err != nil {
return diag.Errorf("error creating EVS storage client: %s", err)
}
if err := snapshots.Delete(evsClient, d.Id()).ExtractErr(); err != nil {
return common.CheckDeletedDiag(d, err, "snapshot")
}
// Wait for the snapshot to delete before moving on.
log.Printf("[DEBUG] Waiting for snapshot (%s) to delete", d.Id())
stateConf := &resource.StateChangeConf{
Pending: []string{"available", "deleting"},
Target: []string{"deleted"},
Refresh: snapshotStateRefreshFunc(evsClient, d.Id()),
Timeout: d.Timeout(schema.TimeoutDelete),
Delay: 2 * time.Second,
MinTimeout: 3 * time.Second,
}
_, err = stateConf.WaitForStateContext(ctx)
if err != nil {
return diag.Errorf(
"error waiting for snapshot (%s) to delete: %s",
d.Id(), err)
}
d.SetId("")
return nil
}
// snapshotStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
// an snapshot.
func snapshotStateRefreshFunc(client *golangsdk.ServiceClient, id string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
v, err := snapshots.Get(client, id).Extract()
if err != nil {
var errDefault404 golangsdk.ErrDefault404
if errors.As(err, &errDefault404) {
return v, "deleted", nil
}
return nil, "", err
}
if v.Status == "error" || v.Status == "error_deleting" {
return v, v.Status, fmt.Errorf("there was an error creating or deleting the snapshot. " +
"Please check with your cloud admin or check the API logs " +
"to see why this error occurred")
}
return v, v.Status, nil
}
}