github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/er/v3/vpcattachments/requests.go (about)

     1  package vpcattachments
     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 is the structure required by the Create method to create a VPC attachment under the ER instance.
    10  type CreateOpts struct {
    11  	// The VPC ID corresponding to the VPC attachment.
    12  	VpcId string `json:"vpc_id" required:"true"`
    13  	// The VPC subnet ID corresponding to the VPC attachment.
    14  	SubnetId string `json:"virsubnet_id" required:"true"`
    15  	// The name of the VPC attachment.
    16  	// The value can contain 1 to 64 characters, only english and chinese letters, digits, underscore (_), hyphens (-)
    17  	// and dots (.) are allowed.
    18  	Name string `json:"name" required:"true"`
    19  	// The description of the VPC attachment.
    20  	// The value contain a maximum of 255 characters, and the angle brackets (< and >) are not allowed.
    21  	Description string `json:"description,omitempty"`
    22  	// Whether automatically configure a route pointing to the ER instance for the VPC, defaults to false.
    23  	AutoCreateVpcRoutes *bool `json:"auto_create_vpc_routes,omitempty"`
    24  	// The key/value pairs to associate with the VPC attachment.
    25  	Tags []tags.ResourceTag `json:"tags,omitempty"`
    26  }
    27  
    28  var requestOpts = golangsdk.RequestOpts{
    29  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
    30  }
    31  
    32  // Create is a method to create a VPC attachment under the ER instance using create option.
    33  func Create(client *golangsdk.ServiceClient, instanceId string, opts CreateOpts) (*Attachment, error) {
    34  	b, err := golangsdk.BuildRequestBody(opts, "vpc_attachment")
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  
    39  	var r SingleResp
    40  	_, err = client.Post(rootURL(client, instanceId), b, &r, &golangsdk.RequestOpts{
    41  		MoreHeaders: requestOpts.MoreHeaders,
    42  	})
    43  	return &r.Attachment, err
    44  }
    45  
    46  // Get is a method to obtain the details of a specified VPC attachment under ER instance using its ID.
    47  func Get(client *golangsdk.ServiceClient, instanceId, attachmentId string) (*Attachment, error) {
    48  	var r SingleResp
    49  	_, err := client.Get(resourceURL(client, instanceId, attachmentId), &r, &golangsdk.RequestOpts{
    50  		MoreHeaders: requestOpts.MoreHeaders,
    51  	})
    52  	return &r.Attachment, err
    53  }
    54  
    55  // ListOpts allows to filter list data using given parameters.
    56  type ListOpts struct {
    57  	// Number of records to be queried.
    58  	// The valid value is range from 0 to 2000.
    59  	Limit int `q:"limit"`
    60  	// The ID of the VPC attachment of the last record on the previous page.
    61  	// If it is empty, it is the first page of the query.
    62  	// This parameter must be used together with limit.
    63  	// The valid value is range from 1 to 128.
    64  	Marker string `q:"marker"`
    65  	// The list of VPC IDs corresponding to the VPC attachments, support for querying multiple VPC attachments.
    66  	VpcId []string `q:"vpc_id"`
    67  	// The list of VPC attachment IDs, support for querying multiple VPC attachments.
    68  	AttachmentId []string `q:"id"`
    69  	// The list of current status of the VPC attachments, support for querying multiple VPC attachments.
    70  	Status []string `q:"state"`
    71  	// The list of keyword to sort the VPC attachments result, sort by ID by default.
    72  	// The optional values are as follow:
    73  	// + id
    74  	// + name
    75  	// + state
    76  	SortKey []string `q:"sort_key"`
    77  	// The returned results are arranged in ascending or descending order, the default is asc.
    78  	SortDir []string `q:"sort_dir"`
    79  }
    80  
    81  // List is a method to query the list of the VPC attachments under specified ER instance using given opts.
    82  func List(client *golangsdk.ServiceClient, instanceId string, opts ListOpts) ([]Attachment, error) {
    83  	url := rootURL(client, instanceId)
    84  	query, err := golangsdk.BuildQueryString(opts)
    85  	if err != nil {
    86  		return nil, err
    87  	}
    88  	url += query.String()
    89  
    90  	pages, err := pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
    91  		p := AttachmentPage{pagination.MarkerPageBase{PageResult: r}}
    92  		p.MarkerPageBase.Owner = p
    93  		return p
    94  	}).AllPages()
    95  	if err != nil {
    96  		return nil, err
    97  	}
    98  
    99  	return ExtractAttachments(pages)
   100  }
   101  
   102  // UpdateOpts is the structure required by the Update method to update the VPC attachment configuration.
   103  type UpdateOpts struct {
   104  	// The name of the VPC attachment.
   105  	// The value can contain 1 to 64 characters, only english and chinese letters, digits, underscore (_), hyphens (-)
   106  	// and dots (.) are allowed.
   107  	Name string `json:"name,omitempty"`
   108  	// The description of the VPC attachment.
   109  	// The value contain a maximum of 255 characters, and the angle brackets (< and >) are not allowed.
   110  	Description *string `json:"description,omitempty"`
   111  }
   112  
   113  // Update is a method to update the VPC attachment under the ER instance using update option.
   114  func Update(client *golangsdk.ServiceClient, instanceId, attachmentId string, opts UpdateOpts) (*Attachment, error) {
   115  	b, err := golangsdk.BuildRequestBody(opts, "vpc_attachment")
   116  	if err != nil {
   117  		return nil, err
   118  	}
   119  
   120  	var r SingleResp
   121  	_, err = client.Put(resourceURL(client, instanceId, attachmentId), b, &r, &golangsdk.RequestOpts{
   122  		MoreHeaders: requestOpts.MoreHeaders,
   123  	})
   124  	return &r.Attachment, err
   125  }
   126  
   127  // Delete is a method to remove an existing VPC attachment under specified ER instance.
   128  func Delete(client *golangsdk.ServiceClient, instanceId, attachmentId string) error {
   129  	_, err := client.Delete(resourceURL(client, instanceId, attachmentId), &golangsdk.RequestOpts{
   130  		MoreHeaders: requestOpts.MoreHeaders,
   131  	})
   132  	return err
   133  }