github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/dws/v1/snapshot/RestoreCluster.go (about)

     1  package snapshot
     2  
     3  import (
     4  	"time"
     5  
     6  	golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
     7  	"github.com/opentelekomcloud/gophertelekomcloud/internal/build"
     8  	"github.com/opentelekomcloud/gophertelekomcloud/openstack/dws/v1/cluster"
     9  )
    10  
    11  type RestoreClusterOpts struct {
    12  	// ID of the snapshot to be restored
    13  	SnapshotId string `json:"-"`
    14  	// Cluster name, which must be unique. The cluster name must contain 4 to 64 characters, which must start with a letter.
    15  	// Only letters, digits, hyphens (-), and underscores (_) are allowed.
    16  	Name string `json:"name" required:"true"`
    17  	// Subnet ID, which is used for configuring cluster network. The default value is the same as that of the original cluster.
    18  	SubnetId string `json:"subnet_id,omitempty"`
    19  	// Security group ID, which is used for configuring cluster network. The default value is the same as that of the original cluster.
    20  	SecurityGroupId string `json:"security_group_id,omitempty"`
    21  	// VPC ID, which is used for configuring cluster network. The default value is the same as that of the original cluster.
    22  	VpcId string `json:"vpc_id,omitempty"`
    23  	// AZ of a cluster. The default value is the same as that of the original cluster.
    24  	AvailabilityZone string `json:"availability_zone,omitempty"`
    25  	// Service port of a cluster. The value ranges from 8000 to 30000. The default value is 8000.
    26  	Port int `json:"port,omitempty"`
    27  	// Public IP address. If the parameter is not specified, public connection is not used by default.
    28  	PublicIp cluster.PublicIp `json:"public_ip,omitempty"`
    29  	// Enterprise project. The default enterprise project ID is 0.
    30  	EnterpriseProjectId string `json:"enterprise_project_id,omitempty"`
    31  }
    32  
    33  func RestoreCluster(client *golangsdk.ServiceClient, opts RestoreClusterOpts) (string, error) {
    34  	b, err := build.RequestBody(opts, "restore")
    35  	if err != nil {
    36  		return "", err
    37  	}
    38  
    39  	// POST /v1.0/{project_id}/snapshots/{snapshot_id}/actions
    40  	raw, err := client.Post(client.ServiceURL("snapshots", opts.SnapshotId, "actions"), b, nil, &golangsdk.RequestOpts{
    41  		OkCodes: []int{200},
    42  	})
    43  	return cluster.ExtractClusterId(err, raw)
    44  }
    45  
    46  func WaitForRestore(c *golangsdk.ServiceClient, id string, secs int) error {
    47  	return golangsdk.WaitFor(secs, func() (bool, error) {
    48  		current, err := cluster.ListClusterDetails(c, id)
    49  		if err != nil {
    50  			return false, err
    51  		}
    52  
    53  		if current.Status == "AVAILABLE" && current.TaskStatus != "RESTORING" {
    54  			return true, nil
    55  		}
    56  
    57  		time.Sleep(10 * time.Second)
    58  
    59  		return false, nil
    60  	})
    61  }