github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/dms/v2/rabbitmq/instances/requests.go (about)

     1  package instances
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  	"github.com/huaweicloud/golangsdk/openstack/common/tags"
     6  	"github.com/huaweicloud/golangsdk/pagination"
     7  )
     8  
     9  // CreateOpsBuilder is used for creating instance parameters.
    10  // any struct providing the parameters should implement this interface
    11  type CreateOpsBuilder interface {
    12  	ToInstanceCreateMap() (map[string]interface{}, error)
    13  }
    14  
    15  // CreateOps is a struct that contains all the parameters.
    16  type CreateOps struct {
    17  	// Indicates the name of an instance.
    18  	// An instance name starts with a letter,
    19  	// consists of 4 to 64 characters, and supports
    20  	// only letters, digits, hyphens (-), and underscores (_).
    21  	Name string `json:"name" required:"true"`
    22  
    23  	// Indicates the description of an instance.
    24  	// It is a character string containing not more than 1024 characters.
    25  	Description string `json:"description,omitempty"`
    26  
    27  	// Indicates a message engine.
    28  	Engine string `json:"engine" required:"true"`
    29  
    30  	// Indicates the version of a message engine.
    31  	EngineVersion string `json:"engine_version" required:"true"`
    32  
    33  	// Indicates the message storage space.
    34  	StorageSpace int `json:"storage_space" required:"true"`
    35  
    36  	// Indicates a username.
    37  	// A username consists of 1 to 64 characters
    38  	// and supports only letters, digits, and hyphens (-).
    39  	AccessUser string `json:"access_user" required:"true"`
    40  
    41  	// Indicates the password of an instance.
    42  	// An instance password must meet the following complexity requirements:
    43  	// Must be 6 to 32 characters long.
    44  	// Must contain at least two of the following character types:
    45  	// Lowercase letters
    46  	// Uppercase letters
    47  	// Digits
    48  	// Special characters (`~!@#$%^&*()-_=+\|[{}]:'",<.>/?)
    49  	Password string `json:"password" required:"true"`
    50  
    51  	// Indicates the ID of a VPC.
    52  	VPCID string `json:"vpc_id" required:"true"`
    53  
    54  	// Indicates the ID of a security group.
    55  	SecurityGroupID string `json:"security_group_id" required:"true"`
    56  
    57  	// Indicates the ID of a subnet.
    58  	SubnetID string `json:"subnet_id" required:"true"`
    59  
    60  	// Indicates the ID of an AZ.
    61  	// The parameter value can be left blank or an empty array.
    62  	AvailableZones []string `json:"available_zones" required:"true"`
    63  
    64  	// Indicates a product ID.
    65  	ProductID string `json:"product_id" required:"true"`
    66  
    67  	// Indicates the time at which a maintenance time window starts.
    68  	// Format: HH:mm:ss
    69  	MaintainBegin string `json:"maintain_begin,omitempty"`
    70  
    71  	// Indicates the time at which a maintenance time window ends.
    72  	// Format: HH:mm:ss
    73  	MaintainEnd string `json:"maintain_end,omitempty"`
    74  
    75  	// Indicates whether to open the public network access function. Default to false.
    76  	EnablePublicIP bool `json:"enable_publicip,omitempty"`
    77  
    78  	// Indicates the ID of the Elastic IP address bound to the instance.
    79  	PublicIpID string `json:"publicip_id,omitempty"`
    80  
    81  	// Indicates whether to enable SSL-encrypted access.
    82  	SslEnable bool `json:"ssl_enable,omitempty"`
    83  
    84  	//Indicates the storage I/O specification. For details on how to select a disk type
    85  	StorageSpecCode string `json:"storage_spec_code" required:"true"`
    86  
    87  	// Indicates the enterprise project ID.
    88  	EnterpriseProjectID string `json:"enterprise_project_id,omitempty"`
    89  
    90  	// Indicates the tags of the instance
    91  	Tags []tags.ResourceTag `json:"tags,omitempty"`
    92  }
    93  
    94  // ToInstanceCreateMap is used for type convert
    95  func (ops CreateOps) ToInstanceCreateMap() (map[string]interface{}, error) {
    96  	return golangsdk.BuildRequestBody(ops, "")
    97  }
    98  
    99  // Create an instance with given parameters.
   100  func Create(client *golangsdk.ServiceClient, ops CreateOpsBuilder) (r CreateResult) {
   101  	b, err := ops.ToInstanceCreateMap()
   102  	if err != nil {
   103  		r.Err = err
   104  		return
   105  	}
   106  
   107  	_, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{
   108  		OkCodes: []int{200},
   109  	})
   110  
   111  	return
   112  }
   113  
   114  // Delete an instance by id
   115  func Delete(client *golangsdk.ServiceClient, id string) (r DeleteResult) {
   116  	_, r.Err = client.Delete(deleteURL(client, id), &golangsdk.RequestOpts{
   117  		OkCodes: []int{204},
   118  	})
   119  	return
   120  }
   121  
   122  //UpdateOptsBuilder is an interface which can build the map paramter of update function
   123  type UpdateOptsBuilder interface {
   124  	ToInstanceUpdateMap() (map[string]interface{}, error)
   125  }
   126  
   127  //UpdateOpts is a struct which represents the parameters of update function
   128  type UpdateOpts struct {
   129  	// Indicates the name of an instance.
   130  	// An instance name starts with a letter,
   131  	// consists of 4 to 64 characters,
   132  	// and supports only letters, digits, and hyphens (-).
   133  	Name string `json:"name,omitempty"`
   134  
   135  	// Indicates the description of an instance.
   136  	// It is a character string containing not more than 1024 characters.
   137  	Description *string `json:"description,omitempty"`
   138  
   139  	// Indicates the time at which a maintenance time window starts.
   140  	// Format: HH:mm:ss
   141  	MaintainBegin string `json:"maintain_begin,omitempty"`
   142  
   143  	// Indicates the time at which a maintenance time window ends.
   144  	// Format: HH:mm:ss
   145  	MaintainEnd string `json:"maintain_end,omitempty"`
   146  
   147  	// Indicates the ID of a security group.
   148  	SecurityGroupID string `json:"security_group_id,omitempty"`
   149  
   150  	// Indicates whether to open the public network access function. Default to false.
   151  	EnablePublicIP *bool `json:"enable_publicip,omitempty"`
   152  
   153  	// Indicates the ID of the Elastic IP address bound to the instance.
   154  	PublicIpID string `json:"publicip_id,omitempty"`
   155  
   156  	// Indicates the enterprise project ID.
   157  	EnterpriseProjectID string `json:"enterprise_project_id,omitempty"`
   158  }
   159  
   160  // ToInstanceUpdateMap is used for type convert
   161  func (opts UpdateOpts) ToInstanceUpdateMap() (map[string]interface{}, error) {
   162  	return golangsdk.BuildRequestBody(opts, "")
   163  }
   164  
   165  // Update is a method which can be able to update the instance
   166  // via accessing to the service with Put method and parameters
   167  func Update(client *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   168  	body, err := opts.ToInstanceUpdateMap()
   169  	if err != nil {
   170  		r.Err = err
   171  		return
   172  	}
   173  
   174  	_, r.Err = client.Put(updateURL(client, id), body, nil, &golangsdk.RequestOpts{
   175  		OkCodes: []int{204},
   176  	})
   177  	return
   178  }
   179  
   180  // Get a instance with detailed information by id
   181  func Get(client *golangsdk.ServiceClient, id string) (r GetResult) {
   182  	_, r.Err = client.Get(getURL(client, id), &r.Body, nil)
   183  	return
   184  }
   185  
   186  type ListOpts struct {
   187  	InstanceId          string `q:"instance_id"`
   188  	Name                string `q:"name"`
   189  	Engine              string `q:"engine"`
   190  	Status              string `q:"status"`
   191  	IncludeFailure      string `q:"include_failure"`
   192  	ExactMatchName      string `q:"exact_match_name"`
   193  	EnterpriseProjectID string `q:"enterprise_project_id"`
   194  }
   195  
   196  type ListOpsBuilder interface {
   197  	ToListDetailQuery() (string, error)
   198  }
   199  
   200  func (opts ListOpts) ToListDetailQuery() (string, error) {
   201  	q, err := golangsdk.BuildQueryString(opts)
   202  	if err != nil {
   203  		return "", err
   204  	}
   205  	return q.String(), err
   206  }
   207  
   208  func List(client *golangsdk.ServiceClient, opts ListOpsBuilder) pagination.Pager {
   209  	url := listURL(client)
   210  	if opts != nil {
   211  		query, err := opts.ToListDetailQuery()
   212  
   213  		if err != nil {
   214  			return pagination.Pager{Err: err}
   215  		}
   216  		url += query
   217  	}
   218  
   219  	pageList := pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   220  		return Page{pagination.SinglePageBase(r)}
   221  	})
   222  
   223  	return pageList
   224  }