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

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