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

     1  package cluster
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"time"
     7  
     8  	golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
     9  	"github.com/opentelekomcloud/gophertelekomcloud/internal/build"
    10  	"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
    11  )
    12  
    13  type CreateClusterOpts struct {
    14  	// Node type
    15  	NodeType string `json:"node_type" required:"true"`
    16  	// Number of cluster nodes. For a cluster, the value ranges from 3 to 256. For a hybrid data warehouse (standalone), the value is 1.
    17  	NumberOfNode int `json:"number_of_node" required:"true"`
    18  	// Subnet ID, which is used for configuring cluster network.
    19  	SubnetId string `json:"subnet_id" required:"true"`
    20  	// ID of a security group, which is used for configuring cluster network.
    21  	SecurityGroupId string `json:"security_group_id" required:"true"`
    22  	// VPC ID, which is used for configuring cluster network.
    23  	VpcId string `json:"vpc_id" required:"true"`
    24  	// AZ of a cluster.
    25  	AvailabilityZone string `json:"availability_zone,omitempty"`
    26  	// Service port of a cluster. The value ranges from 8000 to 30000. The default value is 8000.
    27  	Port int `json:"port,omitempty"`
    28  	// Cluster name, which must be unique. The cluster name must contain 4 to 64 characters, which must start with a letter.
    29  	// Only letters, digits, hyphens (-), and underscores (_) are allowed.
    30  	Name string `json:"name" required:"true"`
    31  	// Administrator username for logging in to a GaussDB(DWS) cluster. The username must:
    32  	// Consist of lowercase letters, digits, or underscores.
    33  	// Start with a lowercase letter or an underscore.
    34  	// Contain 1 to 63 characters.
    35  	// Cannot be a keyword of the GaussDB(DWS) database.
    36  	UserName string `json:"user_name" required:"true"`
    37  	// Administrator password for logging in to a GaussDB(DWS) cluster
    38  	UserPwd string `json:"user_pwd" required:"true"`
    39  	// Public IP address. If the parameter is not specified, public connection is not used by default.
    40  	PublicIp PublicIp `json:"public_ip,omitempty"`
    41  	// Number of deployed CNs. The value ranges from 2 to the number of cluster nodes minus 1. The maximum value is 20 and the default value is 3.
    42  	NumberOfCn int `json:"number_of_cn,omitempty"`
    43  	// Enterprise project. The default enterprise project ID is 0.
    44  	EnterpriseProjectId string `json:"enterprise_project_id,omitempty"`
    45  }
    46  
    47  type PublicIp struct {
    48  	// Binding type of EIP. The value can be one of the following:
    49  	// auto_assign
    50  	// not_use
    51  	// bind_existing
    52  	PublicBindType string `json:"public_bind_type" required:"true"`
    53  	// EIP ID
    54  	EipId string `json:"eip_id,omitempty"`
    55  }
    56  
    57  // CreateCluster is an asynchronous API. It takes 10 to 15 minutes to create a cluster.
    58  func CreateCluster(client *golangsdk.ServiceClient, opts CreateClusterOpts) (string, error) {
    59  	b, err := build.RequestBody(opts, "cluster")
    60  	if err != nil {
    61  		return "", err
    62  	}
    63  
    64  	// POST /v1.0/{project_id}/clusters
    65  	raw, err := client.Post(client.ServiceURL("clusters"), b, nil, &golangsdk.RequestOpts{
    66  		OkCodes: []int{200},
    67  	})
    68  	return ExtractClusterId(err, raw)
    69  }
    70  
    71  func ExtractClusterId(err error, raw *http.Response) (string, error) {
    72  	if err != nil {
    73  		return "", err
    74  	}
    75  
    76  	var res struct {
    77  		// Cluster ID
    78  		Id string `json:"id"`
    79  	}
    80  	err = extract.IntoStructPtr(raw.Body, &res, "cluster")
    81  	return res.Id, err
    82  }
    83  
    84  func WaitForCreate(c *golangsdk.ServiceClient, id string, secs int) error {
    85  	return golangsdk.WaitFor(secs, func() (bool, error) {
    86  		current, err := ListClusterDetails(c, id)
    87  		if err != nil {
    88  			return false, err
    89  		}
    90  
    91  		if current.Status == "AVAILABLE" {
    92  			return true, nil
    93  		}
    94  
    95  		if current.Status == "CREATION FAILED" {
    96  			return false, fmt.Errorf("cluster creation failed: " + current.FailedReasons.ErrorMsg)
    97  		}
    98  
    99  		time.Sleep(10 * time.Second)
   100  
   101  		return false, nil
   102  	})
   103  }