This repository has been archived by the owner on Oct 2, 2020. It is now read-only.
forked from baidubce/terraform-provider-baiducloud
/
service_baiducloud_snapshot.go
76 lines (63 loc) · 1.96 KB
/
service_baiducloud_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
package baiducloud
import (
"github.com/baidubce/bce-sdk-go/services/bcc"
"github.com/baidubce/bce-sdk-go/services/bcc/api"
"github.com/hashicorp/terraform/helper/resource"
)
func (s *BccService) SnapshotStateRefreshFunc(id string, failState []string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
raw, err := s.client.WithBccClient(func(client *bcc.Client) (i interface{}, e error) {
return client.GetSnapshotDetail(id)
})
if err != nil {
return nil, "", WrapError(err)
}
status := raw.(*api.GetSnapshotDetailResult).Snapshot.Status
for _, state := range failState {
if string(status) == state {
return string(status), string(status), WrapError(Error(GetFailTargetStatus, string(status)))
}
}
return raw, string(status), nil
}
}
func (s *BccService) ListAllSnapshots(volumeId string) ([]api.SnapshotModel, error) {
result := make([]api.SnapshotModel, 0)
args := &api.ListSnapshotArgs{
VolumeId: volumeId,
}
action := "Query all Snapshots"
for {
raw, err := s.client.WithBccClient(func(bccClient *bcc.Client) (interface{}, error) {
return bccClient.ListSnapshot(args)
})
addDebug(action, raw)
if err != nil {
return nil, WrapError(err)
}
response, _ := raw.(*api.ListSnapshotResult)
result = append(result, response.Snapshots...)
if response.IsTruncated {
args.Marker = response.Marker
args.MaxKeys = response.MaxKeys
} else {
return result, nil
}
}
}
func (s *BccService) FlattenSnapshotModelToMap(snapshots []api.SnapshotModel) []map[string]interface{} {
result := make([]map[string]interface{}, 0, len(snapshots))
for _, sp := range snapshots {
result = append(result, map[string]interface{}{
"id": sp.Id,
"name": sp.Name,
"size_in_gb": sp.SizeInGB,
"create_time": sp.CreateTime,
"status": sp.Status,
"create_method": sp.CreateMethod,
"volume_id": sp.VolumeId,
"description": sp.Description,
})
}
return result
}