github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/apigw/dedicated/v2/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  // CreateOpts allows to create an APIG dedicated instance using given parameters.
    10  type CreateOpts struct {
    11  	// Name of the APIG dedicated instance. The name can contains of 3 to 64 characters.
    12  	Name string `json:"instance_name" required:"true"`
    13  	// Edition of the APIG dedicated instance. Currently, the editions are support:
    14  	// (IPv4): BASIC, PROFESSIONAL, ENTERPRISE, PLATINUM
    15  	// (IPv6): BASIC_IPV6, PROFESSIONAL_IPV6, ENTERPRISE_IPV6, PLATINUM_IPV6
    16  	Edition string `json:"spec_id" required:"true"`
    17  	// VPC ID.
    18  	VpcId string `json:"vpc_id" required:"true"`
    19  	// Subnet network ID.
    20  	SubnetId string `json:"subnet_id" required:"true"`
    21  	// ID of the security group to which the APIG dedicated instance belongs to.
    22  	SecurityGroupId string `json:"security_group_id" required:"true"`
    23  	// ID of the APIG dedicated instance, which will be automatically generated if you do not specify this parameter.
    24  	Id string `json:"instance_id,omitempty"`
    25  	// Description about the APIG dedicated instance.
    26  	Description string `json:"description,omitempty"`
    27  	// Start time of the maintenance time window in the format "xx:00:00".
    28  	// The value of xx can be 02, 06, 10, 14, 18, or 22.
    29  	MaintainBegin string `json:"maintain_begin,omitempty"`
    30  	// End time of the maintenance time window in the format "xx:00:00".
    31  	// There is a 4-hour difference between the start time and end time.
    32  	MaintainEnd string `json:"maintain_end,omitempty"`
    33  	// EIP ID.
    34  	EipId string `json:"eip_id,omitempty"`
    35  	// Outbound access bandwidth. This parameter is required if public outbound access is enabled for the APIG
    36  	// dedicated instance.
    37  	// Zero means turn off the egress access.
    38  	BandwidthSize int `json:"bandwidth_size"`
    39  	// Billing type of the public outbound access bandwidth. This parameter is required if public outbound access is enabled for the gateway.
    40  	// + bandwidth: billed by bandwidth
    41  	// + traffic: billed by traffic
    42  	// Defaults to bandwidth.
    43  	BandwidthChargingMode string `json:"bandwidth_charging_mode,omitempty"`
    44  	// Enterprise project ID. This parameter is required if you are using an enterprise account.
    45  	EnterpriseProjectId string `json:"enterprise_project_id,omitempty"`
    46  	// AZs.
    47  	AvailableZoneIds []string `json:"available_zone_ids,omitempty"`
    48  	// Whether public access with an IPv6 address is supported.
    49  	Ipv6Enable bool `json:"ipv6_enable,omitempty"`
    50  	// The type of load balancer used by the instance.
    51  	// The valid values are as follows:
    52  	// + lvs: Linux virtual server
    53  	// + elb: Elastic load balance
    54  	LoadbalancerProvider string `json:"loadbalancer_provider,omitempty"`
    55  	// Tags
    56  	// A maximum of 20 tags can be created for a gateway.
    57  	Tags []tags.ResourceTag `json:"tags,omitempty"`
    58  	// Name of the VPC endpoint service.
    59  	// It can contain a maximum of 16 characters, including letters, digits, underscores (_), and hyphens (-).
    60  	// If this parameter is not specified, the system automatically generates a name in the
    61  	// "{region}.apig.{service_id}" format. If this parameter is specified, the system automatically generates a name
    62  	// in the "{region}.{vpcep_service_name}.{service_id}" format.
    63  	// After the gateway is created, you can modify this name on the Gateways > VPC Endpoints page.
    64  	VpcepServiceName string `json:"vpcep_service_name,omitempty"`
    65  	// Public inbound access bandwidth.
    66  	// This parameter is required if public inbound access is enabled for the gateway and loadbalancer_provider is set
    67  	// to elb. After you bind an EIP to the gateway, users can access APIs in the gateway from public networks using
    68  	// the EIP.
    69  	// Defaults to 5.
    70  	IngressBandwithSize int `json:"ingress_bandwidth_size,omitempty"`
    71  	// Billing type of the public inbound access bandwidth.
    72  	// This parameter is required if public inbound access is enabled for the gateway and loadbalancer_provider is set
    73  	// to elb.
    74  	// + bandwidth: billed by bandwidth
    75  	// + traffic: billed by traffic
    76  	IngressBandwithChargingMode string `json:"ingress_bandwidth_charging_mode,omitempty"`
    77  }
    78  
    79  type CreateOptsBuilder interface {
    80  	ToInstanceCreateMap() (map[string]interface{}, error)
    81  }
    82  
    83  func (opts CreateOpts) ToInstanceCreateMap() (map[string]interface{}, error) {
    84  	return golangsdk.BuildRequestBody(opts, "")
    85  }
    86  
    87  // Create is a method by which to create function that create a APIG dedicated instance.
    88  func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    89  	reqBody, err := opts.ToInstanceCreateMap()
    90  	if err != nil {
    91  		r.Err = err
    92  		return
    93  	}
    94  	_, r.Err = client.Post(rootURL(client), reqBody, &r.Body, nil)
    95  	return
    96  }
    97  
    98  // Get is a method to obtain the specified APIG dedicated instance according to the instance Id.
    99  func Get(client *golangsdk.ServiceClient, id string) (r GetResult) {
   100  	_, r.Err = client.Get(resourceURL(client, id), &r.Body, nil)
   101  	return
   102  }
   103  
   104  // ListOpts allows to filter list data using given parameters.
   105  type ListOpts struct {
   106  	// ID of the APIG dedicated instance.
   107  	Id string `q:"instance_id"`
   108  	// Name of the APIG dedicated instance.
   109  	Name string `q:"instance_name"`
   110  	// Instance status.
   111  	Status string `q:"status"`
   112  	// Offset from which the query starts.
   113  	// If the offset is less than 0, the value is automatically converted to 0. Default to 0.
   114  	Offset int `q:"offset"`
   115  	// Number of items displayed on each page.
   116  	Limit int `q:"limit"`
   117  }
   118  
   119  type ListOptsBuilder interface {
   120  	ToInstanceListQuery() (string, error)
   121  }
   122  
   123  func (opts ListOpts) ToInstanceListQuery() (string, error) {
   124  	q, err := golangsdk.BuildQueryString(opts)
   125  	if err != nil {
   126  		return "", err
   127  	}
   128  	return q.String(), err
   129  }
   130  
   131  // List is a method to obtain an array of one or more APIG dedicated instance according to the query parameters.
   132  func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
   133  	url := rootURL(client)
   134  	if opts != nil {
   135  		query, err := opts.ToInstanceListQuery()
   136  		if err != nil {
   137  			return pagination.Pager{Err: err}
   138  		}
   139  		url += query
   140  	}
   141  
   142  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   143  		return InstancePage{pagination.SinglePageBase(r)}
   144  	})
   145  }
   146  
   147  // UpdateOpts allows to update an existing APIG dedicated instance using given parameters.
   148  type UpdateOpts struct {
   149  	// Description about the APIG dedicated instance.
   150  	Description *string `json:"description,omitempty"`
   151  	// Start time of the maintenance time window in the format "xx:00:00".
   152  	// The value of xx can be 02, 06, 10, 14, 18, or 22.
   153  	MaintainBegin string `json:"maintain_begin,omitempty"`
   154  	// End time of the maintenance time window in the format "xx:00:00".
   155  	// There is a 4-hour difference between the start time and end time.
   156  	MaintainEnd string `json:"maintain_end,omitempty"`
   157  	// Description about the APIG dedicated instance.
   158  	Name string `json:"instance_name,omitempty"`
   159  	// ID of the security group to which the APIG dedicated instance belongs to.
   160  	SecurityGroupId string `json:"security_group_id,omitempty"`
   161  	// Name of the VPC endpoint service.
   162  	// It can contain a maximum of 16 characters, including letters, digits, underscores (_), and hyphens (-).
   163  	// If this parameter is not specified, the system automatically generates a name in the
   164  	// "{region}.apig.{service_id}" format. If this parameter is specified, the system automatically generates a name
   165  	// in the "{region}.{vpcep_service_name}.{service_id}" format.
   166  	VpcepServiceName string `json:"vpcep_service_name,omitempty"`
   167  }
   168  
   169  type UpdateOptsBuilder interface {
   170  	ToInstanceUpdateMap() (map[string]interface{}, error)
   171  }
   172  
   173  func (opts UpdateOpts) ToInstanceUpdateMap() (map[string]interface{}, error) {
   174  	return golangsdk.BuildRequestBody(opts, "")
   175  }
   176  
   177  // Update is a method by which to update an existing APIG dedicated instance.
   178  func Update(client *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   179  	reqBody, err := opts.ToInstanceUpdateMap()
   180  	if err != nil {
   181  		r.Err = err
   182  		return
   183  	}
   184  	_, r.Err = client.Put(resourceURL(client, id), reqBody, &r.Body, &golangsdk.RequestOpts{
   185  		OkCodes: []int{200},
   186  	})
   187  	return
   188  }
   189  
   190  // Delete is a method to delete an existing APIG dedicated instance
   191  func Delete(client *golangsdk.ServiceClient, id string) (r DeleteResult) {
   192  	_, r.Err = client.Delete(resourceURL(client, id), nil)
   193  	return
   194  }
   195  
   196  // EgressAccessOpts allows the bandwidth size of an existing APIG dedicated instance to be updated with the given
   197  // parameters.
   198  type EgressAccessOpts struct {
   199  	// Outbound access bandwidth, in Mbit/s.
   200  	BandwidthSize string `json:"bandwidth_size,omitempty"`
   201  }
   202  
   203  type EgressAccessOptsBuilder interface {
   204  	ToEgressAccessMap() (map[string]interface{}, error)
   205  }
   206  
   207  func (opts EgressAccessOpts) ToEgressAccessMap() (map[string]interface{}, error) {
   208  	return golangsdk.BuildRequestBody(opts, "")
   209  }
   210  
   211  // EnableEgressAccess is a method by which to enable the egress access of an existing APIG dedicated instance.
   212  func EnableEgressAccess(client *golangsdk.ServiceClient, id string, opts EgressAccessOptsBuilder) (r EnableEgressResult) {
   213  	reqBody, err := opts.ToEgressAccessMap()
   214  	if err != nil {
   215  		r.Err = err
   216  		return
   217  	}
   218  	_, r.Err = client.Post(egressURL(client, id), reqBody, &r.Body, &golangsdk.RequestOpts{
   219  		OkCodes: []int{200, 201},
   220  	})
   221  	return
   222  }
   223  
   224  // UpdateEgressBandwidth is a method by which to update the egress bandwidth size of an existing APIG dedicated instance.
   225  func UpdateEgressBandwidth(client *golangsdk.ServiceClient, id string, opts EgressAccessOptsBuilder) (r UdpateEgressResult) {
   226  	reqBody, err := opts.ToEgressAccessMap()
   227  	if err != nil {
   228  		r.Err = err
   229  		return
   230  	}
   231  	_, r.Err = client.Put(egressURL(client, id), reqBody, &r.Body, &golangsdk.RequestOpts{
   232  		OkCodes: []int{200},
   233  	})
   234  	return
   235  }
   236  
   237  // DisableEgressAccess is a method by which to disable the egress access of an existing APIG dedicated instance.
   238  func DisableEgressAccess(client *golangsdk.ServiceClient, id string) (r DisableEgressResult) {
   239  	_, r.Err = client.Delete(egressURL(client, id), nil)
   240  	return
   241  }
   242  
   243  // IngressAccessOpts allows binding and updating the eip associated with an existing APIG dedicated instance with the
   244  // given parameters.
   245  type IngressAccessOpts struct {
   246  	// EIP ID
   247  	EipId string `json:"eip_id,omitempty"`
   248  }
   249  
   250  type IngressAccessOptsBuilder interface {
   251  	ToEnableIngressAccessMap() (map[string]interface{}, error)
   252  }
   253  
   254  func (opts IngressAccessOpts) ToEnableIngressAccessMap() (map[string]interface{}, error) {
   255  	return golangsdk.BuildRequestBody(opts, "")
   256  }
   257  
   258  // UpdateIngressAccess is a method to bind and update the eip associated with an existing APIG dedicated instance.
   259  func EnableIngressAccess(client *golangsdk.ServiceClient, id string, opts IngressAccessOptsBuilder) (r EnableIngressResult) {
   260  	reqBody, err := opts.ToEnableIngressAccessMap()
   261  	if err != nil {
   262  		r.Err = err
   263  		return
   264  	}
   265  	_, r.Err = client.Put(ingressURL(client, id), reqBody, &r.Body, &golangsdk.RequestOpts{
   266  		OkCodes: []int{200},
   267  	})
   268  	return
   269  }
   270  
   271  // DisableIngressAccess is a method to unbind the eip associated with an existing APIG dedicated instance.
   272  func DisableIngressAccess(client *golangsdk.ServiceClient, id string) (r DisableIngressResult) {
   273  	_, r.Err = client.Delete(ingressURL(client, id), &golangsdk.RequestOpts{
   274  		OkCodes: []int{200},
   275  	})
   276  	return
   277  }
   278  
   279  // FeatureOpts allows to update the dedicated APIG instance features.
   280  type FeatureOpts struct {
   281  	// Feature name.
   282  	Name string `json:"name" required:"true"`
   283  	// Whether to enable the feature.
   284  	Enable *bool `json:"enable" required:"true"`
   285  	// Parameter configuration.
   286  	Config string `json:"config,omitempty"`
   287  }
   288  
   289  var requestOpts = golangsdk.RequestOpts{
   290  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
   291  }
   292  
   293  // UpdateFeature is a method used to update the feature configuration.
   294  func UpdateFeature(c *golangsdk.ServiceClient, instanceId string, opts FeatureOpts) (*Feature, error) {
   295  	b, err := golangsdk.BuildRequestBody(opts, "")
   296  	if err != nil {
   297  		return nil, err
   298  	}
   299  
   300  	var r Feature
   301  	_, err = c.Post(featureURL(c, instanceId), b, &r, &golangsdk.RequestOpts{
   302  		MoreHeaders: requestOpts.MoreHeaders,
   303  	})
   304  	return &r, err
   305  }
   306  
   307  type ListFeaturesOpts struct {
   308  	// Offset from which the query starts.
   309  	// If the offset is less than 0, the value is automatically converted to 0. Defaults to 0.
   310  	Offset int `q:"offset"`
   311  	// Number of items displayed on each page.
   312  	// Defaults to 20. The maximum value is 500.
   313  	Limit int `q:"limit"`
   314  }
   315  
   316  // ListFeatures is a method used to obtain the list of feature configuration details.
   317  func ListFeatures(c *golangsdk.ServiceClient, instanceId string, opts ListFeaturesOpts) ([]Feature, error) {
   318  	url := featureURL(c, instanceId)
   319  	query, err := golangsdk.BuildQueryString(opts)
   320  	if err != nil {
   321  		return nil, err
   322  	}
   323  	url += query.String()
   324  
   325  	pages, err := pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
   326  		p := FeaturePage{pagination.OffsetPageBase{PageResult: r}}
   327  		return p
   328  	}).AllPages()
   329  
   330  	if err != nil {
   331  		return nil, err
   332  	}
   333  	return ExtractFeatures(pages)
   334  }
   335  
   336  // TagsUpdateOpts is the structure used to modify instance tags.
   337  type TagsUpdateOpts struct {
   338  	// Dedicated instance ID.
   339  	InstanceId string `json:"-" required:"true"`
   340  	// Operation identification.
   341  	// + create
   342  	// + delete
   343  	Action string `json:"action" required:"true"`
   344  	// Tag list.
   345  	// An instance supports the creation of up to 20 tags by default.
   346  	Tags []tags.ResourceTag `json:"tags" required:"true"`
   347  }
   348  
   349  // UpdateTags
   350  func UpdateTags(c *golangsdk.ServiceClient, opts *TagsUpdateOpts) error {
   351  	b, err := golangsdk.BuildRequestBody(opts, "")
   352  	if err != nil {
   353  		return err
   354  	}
   355  
   356  	_, err = c.Post(modifyTagsURL(c, opts.InstanceId), b, nil, &golangsdk.RequestOpts{
   357  		MoreHeaders: requestOpts.MoreHeaders,
   358  		OkCodes:     []int{200, 201, 204},
   359  	})
   360  	return err
   361  }
   362  
   363  // GetTags is a method used to obtain the list of instance tags.
   364  func GetTags(c *golangsdk.ServiceClient, instanceId string) ([]tags.ResourceTag, error) {
   365  	var r struct {
   366  		Tags []tags.ResourceTag `json:"tags"`
   367  	}
   368  	_, err := c.Get(queryTagsURL(c, instanceId), &r, nil)
   369  	return r.Tags, err
   370  }
   371  
   372  // ElbIngressAccessOpts is the structure that used to bind ingress EIP to instance when loadbalancer_provider is set to elb.
   373  type ElbIngressAccessOpts struct {
   374  	// The APIG dedicated instance ID.
   375  	InstanceId string `json:"-"`
   376  	// Public inbound access bandwidth.
   377  	IngressBandwithSize int `json:"bandwidth_size" required:"true"`
   378  	// Billing type of the public inbound access bandwidth.
   379  	// + bandwidth: billed by bandwidth.
   380  	// + traffic: billed by traffic.
   381  	IngressBandwithChargingMode string `json:"bandwidth_charging_mode" required:"true"`
   382  }
   383  
   384  // EnableElbIngressAccess is a method to bind the ingress eip associated with an existing APIG dedicated instance.
   385  // Supported only when loadbalancer_provider is set to elb.
   386  func EnableElbIngressAccess(client *golangsdk.ServiceClient, opts ElbIngressAccessOpts) (*EnableElbIngressResp, error) {
   387  	b, err := golangsdk.BuildRequestBody(opts, "")
   388  	if err != nil {
   389  		return nil, err
   390  	}
   391  	var r EnableElbIngressResp
   392  	_, err = client.Post(elbIngressURL(client, opts.InstanceId), b, &r, &golangsdk.RequestOpts{})
   393  	return &r, err
   394  }
   395  
   396  // DisableElbIngressAccess is a method to unbind the ingress eip associated with an existing APIG dedicated instance.
   397  // Supported only when loadbalancer_provider is set to elb.
   398  func DisableElbIngressAccess(client *golangsdk.ServiceClient, instanceId string) error {
   399  	_, err := client.Delete(elbIngressURL(client, instanceId), nil)
   400  	return err
   401  }