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

     1  package associations
     2  
     3  import (
     4  	"github.com/chnsz/golangsdk"
     5  	"github.com/chnsz/golangsdk/pagination"
     6  )
     7  
     8  // CreateOpts is the structure used to create an association to a specified route table.
     9  type CreateOpts struct {
    10  	// The ID of the VPC attachment.
    11  	AttachmentId string `json:"attachment_id,omitempty"`
    12  	// The export routing policy.
    13  	RoutePolicy ExportRoutePolicy `json:"route_policy,omitempty"`
    14  }
    15  
    16  // ExportRoutePolicy is an object that represents the configuration of the export routing policy.
    17  type ExportRoutePolicy struct {
    18  	// The export routing policy ID.
    19  	ExportPoilicyId string `json:"export_policy_id,omitempty"`
    20  }
    21  
    22  var requestOpts = golangsdk.RequestOpts{
    23  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
    24  }
    25  
    26  // Create is a method to create a new association under a specified route table.
    27  func Create(client *golangsdk.ServiceClient, instanceId, routeTableId string, opts CreateOpts) (*Association, error) {
    28  	b, err := golangsdk.BuildRequestBody(opts, "")
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  
    33  	var r createResp
    34  	_, err = client.Post(enableURL(client, instanceId, routeTableId), b, &r, &golangsdk.RequestOpts{
    35  		MoreHeaders: requestOpts.MoreHeaders,
    36  	})
    37  	return &r.Association, err
    38  }
    39  
    40  // ListOpts allows to filter list data using given parameters.
    41  type ListOpts struct {
    42  	// Number of records to be queried.
    43  	// The valid value is range from 0 to 2000.
    44  	Limit int `q:"limit"`
    45  	// The ID of the association of the last record on the previous page.
    46  	// If it is empty, it is the first page of the query.
    47  	// This parameter must be used together with limit.
    48  	// The valid value is range from 1 to 128.
    49  	Marker string `q:"marker"`
    50  	// The list of attachment IDs, support for querying multiple associations.
    51  	AttachmentIds []string `q:"attachment_id"`
    52  	// The list of attachment resource types, support for querying multiple associations.
    53  	ResourceTypes []string `q:"resource_type"`
    54  	// The list of current status of the associations, support for querying multiple associations.
    55  	Statuses []string `q:"state"`
    56  	// The list of keyword to sort the associations result, sort by ID by default.
    57  	// The optional values are as follow:
    58  	// + id
    59  	// + name
    60  	// + state
    61  	SortKey []string `q:"sort_key"`
    62  	// The returned results are arranged in ascending or descending order, the default is asc.
    63  	SortDir []string `q:"sort_dir"`
    64  }
    65  
    66  // List is a method to query the list of the associations under specified route table using given parameters.
    67  func List(client *golangsdk.ServiceClient, instanceId, routeTableId string, opts ListOpts) ([]Association, error) {
    68  	url := queryURL(client, instanceId, routeTableId)
    69  	query, err := golangsdk.BuildQueryString(opts)
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  	url += query.String()
    74  
    75  	pages, err := pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
    76  		p := AssociationPage{pagination.MarkerPageBase{PageResult: r}}
    77  		p.MarkerPageBase.Owner = p
    78  		return p
    79  	}).AllPages()
    80  
    81  	if err != nil {
    82  		return nil, err
    83  	}
    84  	return extractAssociations(pages)
    85  }
    86  
    87  // DeleteOpts is the structure used to remove an association from a specified route table.
    88  type DeleteOpts struct {
    89  	// The ID of the VPC attachment.
    90  	AttachmentId string `json:"attachment_id,omitempty"`
    91  	// The export routing policy.
    92  	RoutePolicy ExportRoutePolicy `json:"route_policy,omitempty"`
    93  }
    94  
    95  // Delete is a method to remove an existing association from a specified route table.
    96  func Delete(client *golangsdk.ServiceClient, instanceId, routeTableId string, opts DeleteOpts) error {
    97  	b, err := golangsdk.BuildRequestBody(opts, "")
    98  	if err != nil {
    99  		return err
   100  	}
   101  
   102  	_, err = client.Post(disableURL(client, instanceId, routeTableId), b, nil,
   103  		&golangsdk.RequestOpts{
   104  			MoreHeaders: requestOpts.MoreHeaders,
   105  		})
   106  	return err
   107  }