github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/dms/v2/kafka/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 the baseline bandwidth of a Kafka instance, that is,
    41  	// the maximum amount of data transferred per unit time. Unit: byte/s.
    42  	Specification string `json:"specification,omitempty"`
    43  
    44  	// Indicates the maximum number of brokers in a Kafka instance.
    45  	BrokerNum int `json:"broker_num,omitempty"`
    46  
    47  	// Indicates the maximum number of topics in a Kafka instance.
    48  	PartitionNum int `json:"partition_num,omitempty"`
    49  
    50  	// Indicates a username.
    51  	// A username consists of 1 to 64 characters
    52  	// and supports only letters, digits, and hyphens (-).
    53  	AccessUser string `json:"access_user,omitempty"`
    54  
    55  	// Indicates the password of an instance.
    56  	// An instance password must meet the following complexity requirements:
    57  	// Must be 6 to 32 characters long.
    58  	// Must contain at least two of the following character types:
    59  	// Lowercase letters
    60  	// Uppercase letters
    61  	// Digits
    62  	// Special characters (`~!@#$%^&*()-_=+\|[{}]:'",<.>/?)
    63  	Password string `json:"password,omitempty"`
    64  
    65  	// Indicates the ID of a VPC.
    66  	VPCID string `json:"vpc_id" required:"true"`
    67  
    68  	// Indicates the ID of a security group.
    69  	SecurityGroupID string `json:"security_group_id" required:"true"`
    70  
    71  	// Indicates the ID of a subnet.
    72  	SubnetID string `json:"subnet_id" required:"true"`
    73  
    74  	// Indicates the ID of an AZ.
    75  	// The parameter value can be left blank or an empty array.
    76  	AvailableZones []string `json:"available_zones" required:"true"`
    77  
    78  	// Indicates a product ID.
    79  	ProductID string `json:"product_id" required:"true"`
    80  
    81  	// Indicates the username for logging in to the Kafka Manager.
    82  	// The username consists of 4 to 64 characters and can contain
    83  	//letters, digits, hyphens (-), and underscores (_).
    84  	KafkaManagerUser string `json:"kafka_manager_user,omitempty"`
    85  
    86  	// Indicates the password for logging in to the Kafka Manager.
    87  	// The password must meet the following complexity requirements:
    88  	// Must be a string consisting of 8 to 32 characters.
    89  	// Contains at least three of the following characters:
    90  	// Lowercase letters
    91  	// Uppercase letters
    92  	// Digits
    93  	// Special characters `~!@#$%^&*()-_=+\|[{}];:',<.>/?
    94  	KafkaManagerPassword string `json:"kafka_manager_password,omitempty"`
    95  
    96  	// Indicates the time at which a maintenance time window starts.
    97  	// Format: HH:mm:ss
    98  	MaintainBegin string `json:"maintain_begin,omitempty"`
    99  
   100  	// Indicates the time at which a maintenance time window ends.
   101  	// Format: HH:mm:ss
   102  	MaintainEnd string `json:"maintain_end,omitempty"`
   103  
   104  	// Indicates whether to open the public network access function. Default to false.
   105  	EnablePublicIP bool `json:"enable_publicip,omitempty"`
   106  
   107  	// Indicates the bandwidth of the public network.
   108  	PublicBandWidth int `json:"public_bandwidth,omitempty"`
   109  
   110  	// Indicates the ID of the Elastic IP address bound to the instance.
   111  	PublicIpID string `json:"publicip_id,omitempty"`
   112  
   113  	// Indicates whether to enable SSL-encrypted access.
   114  	SslEnable bool `json:"ssl_enable,omitempty"`
   115  
   116  	// Indicates the protocol to use after SASL is enabled.
   117  	KafkaSecurityProtocol string `json:"kafka_security_protocol,omitempty"`
   118  
   119  	// Indicates the authentication mechanisms to use after SASL is enabled.
   120  	SaslEnabledMechanisms []string `json:"sasl_enabled_mechanisms,omitempty"`
   121  
   122  	// Indicates the action to be taken when the memory usage reaches the disk capacity threshold. Options:
   123  	// time_base: Automatically delete the earliest messages.
   124  	// produce_reject: Stop producing new messages.
   125  	RetentionPolicy string `json:"retention_policy,omitempty"`
   126  
   127  	// Indicates whether to enable dumping.
   128  	ConnectorEnalbe bool `json:"connector_enable,omitempty"`
   129  
   130  	// Indicates whether to enable automatic topic creation.
   131  	EnableAutoTopic bool `json:"enable_auto_topic,omitempty"`
   132  
   133  	//Indicates the storage I/O specification. For details on how to select a disk type
   134  	StorageSpecCode string `json:"storage_spec_code,omitempty"`
   135  
   136  	// Indicates the enterprise project ID.
   137  	EnterpriseProjectID string `json:"enterprise_project_id,omitempty"`
   138  
   139  	// Indicates the tags of the instance
   140  	Tags []tags.ResourceTag `json:"tags,omitempty"`
   141  
   142  	// Indicates the parameter related to the yearly/monthly billing mode.
   143  	BssParam BssParam `json:"bss_param,omitempty"`
   144  }
   145  
   146  type BssParam struct {
   147  	// Indicates the charging mode of the instance.
   148  	ChargingMode string `json:"charging_mode" required:"true"`
   149  
   150  	// Indicates the charging period unit of the instance
   151  	PeriodType string `json:"period_type,omitempty"`
   152  
   153  	// Indicates the charging period of the instance.
   154  	PeriodNum int `json:"period_num,omitempty"`
   155  
   156  	// Indicates whether auto renew is enabled.
   157  	IsAutoRenew *bool `json:"is_auto_renew,omitempty"`
   158  
   159  	// Indicates whether the order is automatically or manually paid.
   160  	IsAutoPay *bool `json:"is_auto_pay,omitempty"`
   161  }
   162  
   163  // ToInstanceCreateMap is used for type convert
   164  func (ops CreateOps) ToInstanceCreateMap() (map[string]interface{}, error) {
   165  	return golangsdk.BuildRequestBody(ops, "")
   166  }
   167  
   168  // Create an instance with given parameters.
   169  func Create(client *golangsdk.ServiceClient, ops CreateOpsBuilder) (r CreateResult) {
   170  	b, err := ops.ToInstanceCreateMap()
   171  	if err != nil {
   172  		r.Err = err
   173  		return
   174  	}
   175  
   176  	_, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{
   177  		OkCodes: []int{200},
   178  	})
   179  
   180  	return
   181  }
   182  
   183  // Delete an instance by id
   184  func Delete(client *golangsdk.ServiceClient, id string) (r DeleteResult) {
   185  	_, r.Err = client.Delete(deleteURL(client, id), &golangsdk.RequestOpts{
   186  		OkCodes: []int{204},
   187  	})
   188  	return
   189  }
   190  
   191  // UpdateOptsBuilder is an interface which can build the map paramter of update function
   192  type UpdateOptsBuilder interface {
   193  	ToInstanceUpdateMap() (map[string]interface{}, error)
   194  }
   195  
   196  // UpdateOpts is a struct which represents the parameters of update function
   197  type UpdateOpts struct {
   198  	// Indicates the name of an instance.
   199  	// An instance name starts with a letter,
   200  	// consists of 4 to 64 characters,
   201  	// and supports only letters, digits, and hyphens (-).
   202  	Name string `json:"name,omitempty"`
   203  
   204  	// Indicates the description of an instance.
   205  	// It is a character string containing not more than 1024 characters.
   206  	Description *string `json:"description,omitempty"`
   207  
   208  	// Indicates the time at which a maintenance time window starts.
   209  	// Format: HH:mm:ss
   210  	MaintainBegin string `json:"maintain_begin,omitempty"`
   211  
   212  	// Indicates the time at which a maintenance time window ends.
   213  	// Format: HH:mm:ss
   214  	MaintainEnd string `json:"maintain_end,omitempty"`
   215  
   216  	// Indicates the ID of a security group.
   217  	SecurityGroupID string `json:"security_group_id,omitempty"`
   218  
   219  	// Indicates the action to be taken when the memory usage reaches the disk capacity threshold. Options:
   220  	// time_base: Automatically delete the earliest messages.
   221  	// produce_reject: Stop producing new messages.
   222  	RetentionPolicy string `json:"retention_policy,omitempty"`
   223  
   224  	// Indicates the enterprise project ID.
   225  	EnterpriseProjectID string `json:"enterprise_project_id,omitempty"`
   226  }
   227  
   228  // ToInstanceUpdateMap is used for type convert
   229  func (opts UpdateOpts) ToInstanceUpdateMap() (map[string]interface{}, error) {
   230  	return golangsdk.BuildRequestBody(opts, "")
   231  }
   232  
   233  // Update is a method which can be able to update the instance
   234  // via accessing to the service with Put method and parameters
   235  func Update(client *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   236  	body, err := opts.ToInstanceUpdateMap()
   237  	if err != nil {
   238  		r.Err = err
   239  		return
   240  	}
   241  
   242  	_, r.Err = client.Put(updateURL(client, id), body, nil, &golangsdk.RequestOpts{
   243  		OkCodes: []int{204},
   244  	})
   245  	return
   246  }
   247  
   248  // Get a instance with detailed information by id
   249  func Get(client *golangsdk.ServiceClient, id string) (r GetResult) {
   250  	_, r.Err = client.Get(getURL(client, id), &r.Body, nil)
   251  	return
   252  }
   253  
   254  type ListOpts struct {
   255  	InstanceId          string `q:"instance_id"`
   256  	Name                string `q:"name"`
   257  	Engine              string `q:"engine"`
   258  	Status              string `q:"status"`
   259  	IncludeFailure      string `q:"include_failure"`
   260  	ExactMatchName      string `q:"exact_match_name"`
   261  	EnterpriseProjectID string `q:"enterprise_project_id"`
   262  }
   263  
   264  type ListOpsBuilder interface {
   265  	ToListDetailQuery() (string, error)
   266  }
   267  
   268  func (opts ListOpts) ToListDetailQuery() (string, error) {
   269  	q, err := golangsdk.BuildQueryString(opts)
   270  	if err != nil {
   271  		return "", err
   272  	}
   273  	return q.String(), err
   274  }
   275  
   276  func List(client *golangsdk.ServiceClient, opts ListOpsBuilder) pagination.Pager {
   277  	url := listURL(client)
   278  	if opts != nil {
   279  		query, err := opts.ToListDetailQuery()
   280  
   281  		if err != nil {
   282  			return pagination.Pager{Err: err}
   283  		}
   284  		url += query
   285  	}
   286  
   287  	pageList := pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   288  		return Page{pagination.SinglePageBase(r)}
   289  	})
   290  
   291  	return pageList
   292  }
   293  
   294  type ResizeInstanceOpts struct {
   295  	NewSpecCode     *string `json:"new_spec_code,omitempty"`
   296  	NewStorageSpace *int    `json:"new_storage_space,omitempty"`
   297  	OperType        *string `json:"oper_type,omitempty"`
   298  	NewBrokerNum    *int    `json:"new_broker_num,omitempty"`
   299  	NewProductID    *string `json:"new_product_id,omitempty"`
   300  	PublicIpID      *string `json:"publicip_id,omitempty"`
   301  }
   302  
   303  func Resize(client *golangsdk.ServiceClient, id string, opts ResizeInstanceOpts) (string, error) {
   304  	b, err := golangsdk.BuildRequestBody(opts, "")
   305  	if err != nil {
   306  		return "", err
   307  	}
   308  
   309  	var rst golangsdk.Result
   310  	_, err = client.Post(extend(client, id), b, &rst.Body, &golangsdk.RequestOpts{
   311  		MoreHeaders: requestOpts.MoreHeaders,
   312  	})
   313  
   314  	if err == nil {
   315  		var r struct {
   316  			JobID string `json:"job_id"`
   317  		}
   318  		if err = rst.ExtractInto(&r); err != nil {
   319  			return "", err
   320  		}
   321  		return r.JobID, nil
   322  	}
   323  	return "", err
   324  }
   325  
   326  // CrossVpcUpdateOpts is the structure required by the UpdateCrossVpc method to update the internal IP address for
   327  // cross-VPC access.
   328  type CrossVpcUpdateOpts struct {
   329  	// User-defined advertised IP contents key-value pair.
   330  	// The key is the listeners IP.
   331  	// The value is advertised.listeners IP, or domain name.
   332  	Contents map[string]string `json:"advertised_ip_contents" required:"true"`
   333  }
   334  
   335  // UpdateCrossVpc is a method to update the internal IP address for cross-VPC access using given parameters.
   336  func UpdateCrossVpc(c *golangsdk.ServiceClient, instanceId string, opts CrossVpcUpdateOpts) (*CrossVpc, error) {
   337  	b, err := golangsdk.BuildRequestBody(opts, "")
   338  	if err != nil {
   339  		return nil, err
   340  	}
   341  
   342  	var r CrossVpc
   343  	_, err = c.Post(crossVpcURL(c, instanceId), b, &r, &golangsdk.RequestOpts{
   344  		MoreHeaders: requestOpts.MoreHeaders,
   345  	})
   346  	return &r, err
   347  }
   348  
   349  // AutoTopicOpts is a struct which represents the parameter of UpdateAutoTopic function
   350  type AutoTopicOpts struct {
   351  	// Indicates whether to enable automatic topic creation.
   352  	EnableAutoTopic *bool `json:"enable_auto_topic" required:"true"`
   353  }
   354  
   355  // UpdateAutoTopic is used to enable or disable automatic topic creation.
   356  // via accessing to the service with POST method and parameters
   357  func UpdateAutoTopic(client *golangsdk.ServiceClient, id string,
   358  	opts AutoTopicOpts) (r AutoTopicResult) {
   359  	body, err := golangsdk.BuildRequestBody(opts, "")
   360  	if err != nil {
   361  		r.Err = err
   362  		return
   363  	}
   364  
   365  	_, r.Err = client.Post(autoTopicURL(client, id), body, nil, &golangsdk.RequestOpts{})
   366  	return
   367  }
   368  
   369  // ResetPasswordOpts is a struct which represents the parameter of ResetPassword function
   370  type ResetPasswordOpts struct {
   371  	// Indicates the new password of an instance.
   372  	NewPassword string `json:"new_password" required:"true"`
   373  }
   374  
   375  // ToResetPasswordMap is used for type convert
   376  func (opts ResetPasswordOpts) ToResetPasswordMap() (map[string]interface{}, error) {
   377  	return golangsdk.BuildRequestBody(opts, "")
   378  }
   379  
   380  // ResetPasswordOptsBuilder is an interface which can build the map parameter of ResetPassword function
   381  type ResetPasswordOptsBuilder interface {
   382  	ToResetPasswordMap() (map[string]interface{}, error)
   383  }
   384  
   385  // ResetPassword is used to reset password for the instance
   386  // via accessing to the service with POST method and parameters
   387  func ResetPassword(client *golangsdk.ServiceClient, id string, opts ResetPasswordOptsBuilder) (r ResetPasswordResult) {
   388  	body, err := opts.ToResetPasswordMap()
   389  	if err != nil {
   390  		r.Err = err
   391  		return
   392  	}
   393  
   394  	_, r.Err = client.Post(resetPasswordURL(client, id), body, nil, &golangsdk.RequestOpts{
   395  		OkCodes: []int{204},
   396  	})
   397  	return
   398  }
   399  
   400  type ConfigParam struct {
   401  	Name  string `json:"name" required:"true"`
   402  	Value string `json:"value" required:"true"`
   403  }
   404  
   405  type KafkaConfigs struct {
   406  	KafkaConfigs []ConfigParam `json:"kafka_configs"`
   407  }
   408  
   409  type RestartInstanceOpts struct {
   410  	Action    string   `json:"action" required:"true"`
   411  	Instances []string `json:"instances" required:"true"`
   412  }
   413  
   414  func ModifyConfiguration(c *golangsdk.ServiceClient, instanceID string, opts KafkaConfigs) (r ModifyConfigurationResult) {
   415  	b, err := golangsdk.BuildRequestBody(opts, "")
   416  	if err != nil {
   417  		r.Err = err
   418  		return
   419  	}
   420  
   421  	_, r.Err = c.Put(configurationsURL(c, instanceID), b, &r.Body, &golangsdk.RequestOpts{})
   422  	return
   423  }
   424  
   425  func GetConfigurations(c *golangsdk.ServiceClient, instanceID string) (r GetConfigurationResult) {
   426  	_, r.Err = c.Get(configurationsURL(c, instanceID), &r.Body, &golangsdk.RequestOpts{
   427  		MoreHeaders: map[string]string{"Content-Type": "application/json"},
   428  	})
   429  	return
   430  }
   431  
   432  func RebootInstance(c *golangsdk.ServiceClient, params RestartInstanceOpts) (r RebootResult) {
   433  	_, r.Err = c.Post(actionURL(c), params, &r.Body, &golangsdk.RequestOpts{})
   434  	return
   435  }
   436  
   437  func GetTasks(c *golangsdk.ServiceClient, instanceID string) (r GetTasksResult) {
   438  	_, r.Err = c.Get(tasksURL(c, instanceID), &r.Body, &golangsdk.RequestOpts{
   439  		MoreHeaders: map[string]string{"Content-Type": "application/json"},
   440  	})
   441  	return
   442  }