github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/dms/v2/rabbitmq/instances/requests.go (about)

     1  package instances
     2  
     3  import (
     4  	"github.com/chnsz/golangsdk"
     5  	"github.com/chnsz/golangsdk/openstack/common/tags"
     6  	"github.com/chnsz/golangsdk/pagination"
     7  )
     8  
     9  var requestOpts = golangsdk.RequestOpts{
    10  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
    11  }
    12  
    13  // CreateOpsBuilder is used for creating instance parameters.
    14  // any struct providing the parameters should implement this interface
    15  type CreateOpsBuilder interface {
    16  	ToInstanceCreateMap() (map[string]interface{}, error)
    17  }
    18  
    19  // CreateOps is a struct that contains all the parameters.
    20  type CreateOps struct {
    21  	// Indicates the name of an instance.
    22  	// An instance name starts with a letter,
    23  	// consists of 4 to 64 characters, and supports
    24  	// only letters, digits, hyphens (-), and underscores (_).
    25  	Name string `json:"name" required:"true"`
    26  
    27  	// Indicates the description of an instance.
    28  	// It is a character string containing not more than 1024 characters.
    29  	Description string `json:"description,omitempty"`
    30  
    31  	// Indicates a message engine.
    32  	Engine string `json:"engine" required:"true"`
    33  
    34  	// Indicates the version of a message engine.
    35  	EngineVersion string `json:"engine_version" required:"true"`
    36  
    37  	// Indicates the message storage space.
    38  	StorageSpace int `json:"storage_space" required:"true"`
    39  
    40  	// Indicates a username.
    41  	// A username consists of 1 to 64 characters
    42  	// and supports only letters, digits, and hyphens (-).
    43  	AccessUser string `json:"access_user" required:"true"`
    44  
    45  	// Indicates the password of an instance.
    46  	// An instance password must meet the following complexity requirements:
    47  	// Must be 6 to 32 characters long.
    48  	// Must contain at least two of the following character types:
    49  	// Lowercase letters
    50  	// Uppercase letters
    51  	// Digits
    52  	// Special characters (`~!@#$%^&*()-_=+\|[{}]:'",<.>/?)
    53  	Password string `json:"password" required:"true"`
    54  
    55  	// Indicates the ID of a VPC.
    56  	VPCID string `json:"vpc_id" required:"true"`
    57  
    58  	// Indicates the ID of a security group.
    59  	SecurityGroupID string `json:"security_group_id" required:"true"`
    60  
    61  	// Indicates the ID of a subnet.
    62  	SubnetID string `json:"subnet_id" required:"true"`
    63  
    64  	// Indicates the ID of an AZ.
    65  	// The parameter value can be left blank or an empty array.
    66  	AvailableZones []string `json:"available_zones" required:"true"`
    67  
    68  	// Indicates a product ID.
    69  	ProductID string `json:"product_id" required:"true"`
    70  
    71  	// Indicates the maximum number of brokers in a RabbitMQ instance.
    72  	BrokerNum int `json:"broker_num,omitempty"`
    73  
    74  	// Indicates the time at which a maintenance time window starts.
    75  	// Format: HH:mm:ss
    76  	MaintainBegin string `json:"maintain_begin,omitempty"`
    77  
    78  	// Indicates the time at which a maintenance time window ends.
    79  	// Format: HH:mm:ss
    80  	MaintainEnd string `json:"maintain_end,omitempty"`
    81  
    82  	// Indicates whether to open the public network access function. Default to false.
    83  	EnablePublicIP bool `json:"enable_publicip,omitempty"`
    84  
    85  	// Indicates the ID of the Elastic IP address bound to the instance.
    86  	PublicIpID string `json:"publicip_id,omitempty"`
    87  
    88  	// Indicates whether to enable SSL-encrypted access.
    89  	SslEnable bool `json:"ssl_enable,omitempty"`
    90  
    91  	//Indicates the storage I/O specification. For details on how to select a disk type
    92  	StorageSpecCode string `json:"storage_spec_code" required:"true"`
    93  
    94  	// Indicates the enterprise project ID.
    95  	EnterpriseProjectID string `json:"enterprise_project_id,omitempty"`
    96  
    97  	// Indicates the tags of the instance
    98  	Tags []tags.ResourceTag `json:"tags,omitempty"`
    99  
   100  	// Indicates the parameter related to the yearly/monthly billing mode.
   101  	BssParam *BssParam `json:"bss_param,omitempty"`
   102  }
   103  
   104  type BssParam struct {
   105  	// Indicates the charging mode of the instance.
   106  	ChargingMode string `json:"charging_mode" required:"true"`
   107  
   108  	// Indicates the charging period unit of the instance
   109  	PeriodType string `json:"period_type,omitempty"`
   110  
   111  	// Indicates the charging period of the instance.
   112  	PeriodNum int `json:"period_num,omitempty"`
   113  
   114  	// Indicates whether auto renew is enabled.
   115  	IsAutoRenew *bool `json:"is_auto_renew,omitempty"`
   116  
   117  	// Indicates whether the order is automatically or manually paid.
   118  	IsAutoPay *bool `json:"is_auto_pay,omitempty"`
   119  }
   120  
   121  // ToInstanceCreateMap is used for type convert
   122  func (ops CreateOps) ToInstanceCreateMap() (map[string]interface{}, error) {
   123  	return golangsdk.BuildRequestBody(ops, "")
   124  }
   125  
   126  // Create an instance with given parameters.
   127  func Create(client *golangsdk.ServiceClient, ops CreateOpsBuilder) (r CreateResult) {
   128  	b, err := ops.ToInstanceCreateMap()
   129  	if err != nil {
   130  		r.Err = err
   131  		return
   132  	}
   133  
   134  	_, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{
   135  		OkCodes: []int{200},
   136  	})
   137  
   138  	return
   139  }
   140  
   141  // Create an instance with given parameters.
   142  func CreateWithEngine(client *golangsdk.ServiceClient, ops CreateOpsBuilder) (r CreateResult) {
   143  	b, err := ops.ToInstanceCreateMap()
   144  	if err != nil {
   145  		r.Err = err
   146  		return
   147  	}
   148  
   149  	_, r.Err = client.Post(createWithEngineURL(client), b, &r.Body, &golangsdk.RequestOpts{
   150  		OkCodes: []int{200},
   151  	})
   152  
   153  	return
   154  }
   155  
   156  // Delete an instance by id
   157  func Delete(client *golangsdk.ServiceClient, id string) (r DeleteResult) {
   158  	_, r.Err = client.Delete(deleteURL(client, id), &golangsdk.RequestOpts{
   159  		OkCodes: []int{204},
   160  	})
   161  	return
   162  }
   163  
   164  // UpdateOptsBuilder is an interface which can build the map paramter of update function
   165  type UpdateOptsBuilder interface {
   166  	ToInstanceUpdateMap() (map[string]interface{}, error)
   167  }
   168  
   169  // UpdateOpts is a struct which represents the parameters of update function
   170  type UpdateOpts struct {
   171  	// Indicates the name of an instance.
   172  	// An instance name starts with a letter,
   173  	// consists of 4 to 64 characters,
   174  	// and supports only letters, digits, and hyphens (-).
   175  	Name string `json:"name,omitempty"`
   176  
   177  	// Indicates the description of an instance.
   178  	// It is a character string containing not more than 1024 characters.
   179  	Description *string `json:"description,omitempty"`
   180  
   181  	// Indicates the time at which a maintenance time window starts.
   182  	// Format: HH:mm:ss
   183  	MaintainBegin string `json:"maintain_begin,omitempty"`
   184  
   185  	// Indicates the time at which a maintenance time window ends.
   186  	// Format: HH:mm:ss
   187  	MaintainEnd string `json:"maintain_end,omitempty"`
   188  
   189  	// Indicates the ID of a security group.
   190  	SecurityGroupID string `json:"security_group_id,omitempty"`
   191  
   192  	// Indicates whether to open the public network access function. Default to false.
   193  	EnablePublicIP *bool `json:"enable_publicip,omitempty"`
   194  
   195  	// Indicates the ID of the Elastic IP address bound to the instance.
   196  	PublicIpID string `json:"publicip_id,omitempty"`
   197  
   198  	// Indicates the enterprise project ID.
   199  	EnterpriseProjectID string `json:"enterprise_project_id,omitempty"`
   200  }
   201  
   202  // ToInstanceUpdateMap is used for type convert
   203  func (opts UpdateOpts) ToInstanceUpdateMap() (map[string]interface{}, error) {
   204  	return golangsdk.BuildRequestBody(opts, "")
   205  }
   206  
   207  // Update is a method which can be able to update the instance
   208  // via accessing to the service with Put method and parameters
   209  func Update(client *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   210  	body, err := opts.ToInstanceUpdateMap()
   211  	if err != nil {
   212  		r.Err = err
   213  		return
   214  	}
   215  
   216  	_, r.Err = client.Put(updateURL(client, id), body, nil, &golangsdk.RequestOpts{
   217  		OkCodes: []int{204},
   218  	})
   219  	return
   220  }
   221  
   222  // Get a instance with detailed information by id
   223  func Get(client *golangsdk.ServiceClient, id string) (r GetResult) {
   224  	_, r.Err = client.Get(getURL(client, id), &r.Body, nil)
   225  	return
   226  }
   227  
   228  type ListOpts struct {
   229  	InstanceId          string `q:"instance_id"`
   230  	Name                string `q:"name"`
   231  	Engine              string `q:"engine"`
   232  	Status              string `q:"status"`
   233  	IncludeFailure      string `q:"include_failure"`
   234  	ExactMatchName      string `q:"exact_match_name"`
   235  	EnterpriseProjectID string `q:"enterprise_project_id"`
   236  }
   237  
   238  type ListOpsBuilder interface {
   239  	ToListDetailQuery() (string, error)
   240  }
   241  
   242  func (opts ListOpts) ToListDetailQuery() (string, error) {
   243  	q, err := golangsdk.BuildQueryString(opts)
   244  	if err != nil {
   245  		return "", err
   246  	}
   247  	return q.String(), err
   248  }
   249  
   250  func List(client *golangsdk.ServiceClient, opts ListOpsBuilder) pagination.Pager {
   251  	url := listURL(client)
   252  	if opts != nil {
   253  		query, err := opts.ToListDetailQuery()
   254  
   255  		if err != nil {
   256  			return pagination.Pager{Err: err}
   257  		}
   258  		url += query
   259  	}
   260  
   261  	pageList := pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   262  		return Page{pagination.SinglePageBase(r)}
   263  	})
   264  
   265  	return pageList
   266  }
   267  
   268  type ResizeInstanceOpts struct {
   269  	OperType        *string `json:"oper_type,omitempty"`
   270  	NewSpecCode     *string `json:"new_spec_code,omitempty"`
   271  	NewStorageSpace *int    `json:"new_storage_space,omitempty"`
   272  	NewBrokerNum    *int    `json:"new_broker_num,omitempty"`
   273  	NewProductID    *string `json:"new_product_id,omitempty"`
   274  }
   275  
   276  func Resize(client *golangsdk.ServiceClient, id string, opts ResizeInstanceOpts) (string, error) {
   277  	b, err := golangsdk.BuildRequestBody(opts, "")
   278  	if err != nil {
   279  		return "", err
   280  	}
   281  
   282  	var rst golangsdk.Result
   283  	_, err = client.Post(extend(client, id), b, &rst.Body, &golangsdk.RequestOpts{
   284  		MoreHeaders: requestOpts.MoreHeaders,
   285  	})
   286  
   287  	if err == nil {
   288  		var r struct {
   289  			JobID string `json:"job_id"`
   290  		}
   291  		if err = rst.ExtractInto(&r); err != nil {
   292  			return "", err
   293  		}
   294  		return r.JobID, nil
   295  	}
   296  	return "", err
   297  }
   298  
   299  // ResetPasswordOpts is a struct which represents the parameter of ResetPassword function
   300  type ResetPasswordOpts struct {
   301  	// Indicates the new password of an instance.
   302  	NewPassword string `json:"new_password" required:"true"`
   303  }
   304  
   305  // ConvertToResetPasswordMap is used for type convert
   306  func (opts ResetPasswordOpts) ConvertToResetPasswordMap() (map[string]interface{}, error) {
   307  	return golangsdk.BuildRequestBody(opts, "")
   308  }
   309  
   310  // ResetPasswordOptsBuilder is an interface which can build the map parameter of ResetPassword function
   311  type ResetPasswordOptsBuilder interface {
   312  	ConvertToResetPasswordMap() (map[string]interface{}, error)
   313  }
   314  
   315  // ResetPassword is used to reset password for the instance
   316  // via accessing to the service with POST method and parameters
   317  func ResetPassword(client *golangsdk.ServiceClient, id string, opts ResetPasswordOptsBuilder) (r ResetPasswordResult) {
   318  	body, err := opts.ConvertToResetPasswordMap()
   319  	if err != nil {
   320  		r.Err = err
   321  		return
   322  	}
   323  
   324  	_, r.Err = client.Post(resetPasswordURL(client, id), body, nil, &golangsdk.RequestOpts{
   325  		OkCodes: []int{204},
   326  	})
   327  	return
   328  }