github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/routes/requests.go (about)

     1  package routes
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  	"github.com/huaweicloud/golangsdk/pagination"
     6  )
     7  
     8  // ListOpts allows the filtering and sorting of paginated collections through
     9  // the API. Filtering is achieved by passing in struct field values that map to
    10  // the attributes you want to see returned.
    11  type ListOpts struct {
    12  	// Specifies the route type.
    13  	Type string `q:"type"`
    14  
    15  	// Specifies the next hop. If the route type is peering, enter the VPC peering connection ID.
    16  	//NextHop string `q:"nexthop"`
    17  
    18  	//Specifies the destination IP address or CIDR block.
    19  	Destination string `q:"destination"`
    20  
    21  	// Specifies the VPC for which a route is to be added.
    22  	VPC_ID string `q:"vpc_id"`
    23  
    24  	//Specifies the tenant ID. Only the administrator can specify the tenant ID of other tenants.
    25  	Tenant_Id string `q:"tenant_id"`
    26  
    27  	//Specifies the route ID.
    28  	RouteID string `q:"id"`
    29  }
    30  type ListOptsBuilder interface {
    31  	ToRouteListQuery() (string, error)
    32  }
    33  
    34  // ToRouteListQuery formats a ListOpts into a query string.
    35  func (opts ListOpts) ToRouteListQuery() (string, error) {
    36  	q, err := golangsdk.BuildQueryString(opts)
    37  	if err != nil {
    38  		return "", err
    39  	}
    40  	return q.String(), nil
    41  }
    42  
    43  // List returns a Pager which allows you to iterate over a collection of
    44  // vpc routes  resources. It accepts a ListOpts struct, which allows you to
    45  // filter  the returned collection for greater efficiency.
    46  func List(c *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    47  	url := rootURL(c)
    48  
    49  	if opts != nil {
    50  		query, err := opts.ToRouteListQuery()
    51  		if err != nil {
    52  			return pagination.Pager{Err: err}
    53  		}
    54  		url += query
    55  	}
    56  
    57  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
    58  		return RoutePage{pagination.LinkedPageBase{PageResult: r}}
    59  	})
    60  }
    61  
    62  // CreateOptsBuilder allows extensions to add additional parameters to the
    63  // Create request.
    64  type CreateOptsBuilder interface {
    65  	ToRouteCreateMap() (map[string]interface{}, error)
    66  }
    67  
    68  // CreateOpts contains all the values needed to create a new routes. There are
    69  // no required values.
    70  type CreateOpts struct {
    71  	Type        string `json:"type,omitempty" required:"true"`
    72  	NextHop     string `json:"nexthop,omitempty" required:"true"`
    73  	Destination string `json:"destination,omitempty" required:"true"`
    74  	Tenant_Id   string `json:"tenant_id,omitempty"`
    75  	VPC_ID      string `json:"vpc_id,omitempty" required:"true"`
    76  }
    77  
    78  // ToRouteCreateMap builds a create request body from CreateOpts.
    79  func (opts CreateOpts) ToRouteCreateMap() (map[string]interface{}, error) {
    80  	return golangsdk.BuildRequestBody(opts, "route")
    81  }
    82  
    83  // Create accepts a CreateOpts struct and uses the values to create a new
    84  // logical routes. When it is created, the routes does not have an internal
    85  // interface - it is not associated to any routes.
    86  //
    87  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    88  	b, err := opts.ToRouteCreateMap()
    89  	if err != nil {
    90  		r.Err = err
    91  		return
    92  	}
    93  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{201}}
    94  	_, r.Err = c.Post(rootURL(c), b, &r.Body, reqOpt)
    95  	return
    96  }
    97  
    98  // Get retrieves a particular route based on its unique ID.
    99  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
   100  	_, r.Err = c.Get(resourceURL(c, id), &r.Body, nil)
   101  	return
   102  }
   103  
   104  // Delete will permanently delete a particular route based on its unique ID.
   105  func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) {
   106  	_, r.Err = c.Delete(resourceURL(c, id), nil)
   107  	return
   108  }