github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/dws/v1/snapshot/CreateSnapshot.go (about) 1 package snapshot 2 3 import ( 4 "fmt" 5 "time" 6 7 golangsdk "github.com/opentelekomcloud/gophertelekomcloud" 8 "github.com/opentelekomcloud/gophertelekomcloud/internal/build" 9 "github.com/opentelekomcloud/gophertelekomcloud/internal/extract" 10 "github.com/opentelekomcloud/gophertelekomcloud/openstack/dws/v1/cluster" 11 ) 12 13 type Snapshot struct { 14 // Snapshot name, which must be unique and start with a letter. 15 // It consists of 4 to 64 characters, which are case-insensitive and contain letters, digits, hyphens (-), and underscores (_) only. 16 Name string `json:"name" required:"true"` 17 // ID of the cluster for which you want to create a snapshot. For details about how to obtain the ID, see 7.6 Obtaining the Cluster ID. 18 ClusterId string `json:"cluster_id" required:"true"` 19 // Snapshot description. If no value is specified, the description is empty. Enter a maximum of 256 characters. 20 // The following special characters are not allowed: !<>'=&" 21 Description string `json:"description,omitempty"` 22 } 23 24 func CreateSnapshot(client *golangsdk.ServiceClient, opts Snapshot) (string, error) { 25 b, err := build.RequestBody(opts, "snapshot") 26 if err != nil { 27 return "", err 28 } 29 30 // POST /v1.0/{project_id}/snapshots 31 raw, err := client.Post(client.ServiceURL("snapshots"), b, nil, &golangsdk.RequestOpts{ 32 OkCodes: []int{200}, 33 }) 34 if err != nil { 35 return "", err 36 } 37 38 var res struct { 39 Id string `json:"id,omitempty"` 40 } 41 err = extract.IntoStructPtr(raw.Body, &res, "snapshot") 42 return res.Id, err 43 } 44 45 func WaitForSnapshot(c *golangsdk.ServiceClient, cid, id string, secs int) error { 46 return golangsdk.WaitFor(secs, func() (bool, error) { 47 current, err := cluster.ListClusterDetails(c, cid) 48 if err != nil { 49 return false, err 50 } 51 52 curSnap, err := ListSnapshotDetails(c, id) 53 if err != nil { 54 return false, err 55 } 56 57 if curSnap.Status == "AVAILABLE" && current.Status == "AVAILABLE" && current.TaskStatus != "SNAPSHOTTING" { 58 return true, nil 59 } 60 61 if curSnap.Status == "UNAVAILABLE" { 62 return false, fmt.Errorf("snapshot creation failed: " + current.FailedReasons.ErrorMsg) 63 } 64 65 time.Sleep(10 * time.Second) 66 67 return false, nil 68 }) 69 }