github.com/gophercloud/gophercloud@v1.11.0/openstack/networking/v2/extensions/layer3/routers/requests.go (about)

     1  package routers
     2  
     3  import (
     4  	"github.com/gophercloud/gophercloud"
     5  	"github.com/gophercloud/gophercloud/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 floating IP attributes you want to see returned. SortKey allows you to
    11  // sort by a particular network attribute. SortDir sets the direction, and is
    12  // either `asc' or `desc'. Marker and Limit are used for pagination.
    13  type ListOpts struct {
    14  	ID           string `q:"id"`
    15  	Name         string `q:"name"`
    16  	Description  string `q:"description"`
    17  	AdminStateUp *bool  `q:"admin_state_up"`
    18  	Distributed  *bool  `q:"distributed"`
    19  	Status       string `q:"status"`
    20  	TenantID     string `q:"tenant_id"`
    21  	ProjectID    string `q:"project_id"`
    22  	Limit        int    `q:"limit"`
    23  	Marker       string `q:"marker"`
    24  	SortKey      string `q:"sort_key"`
    25  	SortDir      string `q:"sort_dir"`
    26  	Tags         string `q:"tags"`
    27  	TagsAny      string `q:"tags-any"`
    28  	NotTags      string `q:"not-tags"`
    29  	NotTagsAny   string `q:"not-tags-any"`
    30  }
    31  
    32  // List returns a Pager which allows you to iterate over a collection of
    33  // routers. It accepts a ListOpts struct, which allows you to filter and sort
    34  // the returned collection for greater efficiency.
    35  //
    36  // Default policy settings return only those routers that are owned by the
    37  // tenant who submits the request, unless an admin user submits the request.
    38  func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
    39  	q, err := gophercloud.BuildQueryString(&opts)
    40  	if err != nil {
    41  		return pagination.Pager{Err: err}
    42  	}
    43  	u := rootURL(c) + q.String()
    44  	return pagination.NewPager(c, u, func(r pagination.PageResult) pagination.Page {
    45  		return RouterPage{pagination.LinkedPageBase{PageResult: r}}
    46  	})
    47  }
    48  
    49  // CreateOptsBuilder allows extensions to add additional parameters to the
    50  // Create request.
    51  type CreateOptsBuilder interface {
    52  	ToRouterCreateMap() (map[string]interface{}, error)
    53  }
    54  
    55  // CreateOpts contains all the values needed to create a new router. There are
    56  // no required values.
    57  type CreateOpts struct {
    58  	Name                  string       `json:"name,omitempty"`
    59  	Description           string       `json:"description,omitempty"`
    60  	AdminStateUp          *bool        `json:"admin_state_up,omitempty"`
    61  	Distributed           *bool        `json:"distributed,omitempty"`
    62  	TenantID              string       `json:"tenant_id,omitempty"`
    63  	ProjectID             string       `json:"project_id,omitempty"`
    64  	GatewayInfo           *GatewayInfo `json:"external_gateway_info,omitempty"`
    65  	AvailabilityZoneHints []string     `json:"availability_zone_hints,omitempty"`
    66  }
    67  
    68  // ToRouterCreateMap builds a create request body from CreateOpts.
    69  func (opts CreateOpts) ToRouterCreateMap() (map[string]interface{}, error) {
    70  	return gophercloud.BuildRequestBody(opts, "router")
    71  }
    72  
    73  // Create accepts a CreateOpts struct and uses the values to create a new
    74  // logical router. When it is created, the router does not have an internal
    75  // interface - it is not associated to any subnet.
    76  //
    77  // You can optionally specify an external gateway for a router using the
    78  // GatewayInfo struct. The external gateway for the router must be plugged into
    79  // an external network (it is external if its `router:external' field is set to
    80  // true).
    81  func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    82  	b, err := opts.ToRouterCreateMap()
    83  	if err != nil {
    84  		r.Err = err
    85  		return
    86  	}
    87  	resp, err := c.Post(rootURL(c), b, &r.Body, nil)
    88  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    89  	return
    90  }
    91  
    92  // Get retrieves a particular router based on its unique ID.
    93  func Get(c *gophercloud.ServiceClient, id string) (r GetResult) {
    94  	resp, err := c.Get(resourceURL(c, id), &r.Body, nil)
    95  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    96  	return
    97  }
    98  
    99  // UpdateOptsBuilder allows extensions to add additional parameters to the
   100  // Update request.
   101  type UpdateOptsBuilder interface {
   102  	ToRouterUpdateMap() (map[string]interface{}, error)
   103  }
   104  
   105  // UpdateOpts contains the values used when updating a router.
   106  type UpdateOpts struct {
   107  	Name         string       `json:"name,omitempty"`
   108  	Description  *string      `json:"description,omitempty"`
   109  	AdminStateUp *bool        `json:"admin_state_up,omitempty"`
   110  	Distributed  *bool        `json:"distributed,omitempty"`
   111  	GatewayInfo  *GatewayInfo `json:"external_gateway_info,omitempty"`
   112  	Routes       *[]Route     `json:"routes,omitempty"`
   113  }
   114  
   115  // ToRouterUpdateMap builds an update body based on UpdateOpts.
   116  func (opts UpdateOpts) ToRouterUpdateMap() (map[string]interface{}, error) {
   117  	return gophercloud.BuildRequestBody(opts, "router")
   118  }
   119  
   120  // Update allows routers to be updated. You can update the name, administrative
   121  // state, and the external gateway. For more information about how to set the
   122  // external gateway for a router, see Create. This operation does not enable
   123  // the update of router interfaces. To do this, use the AddInterface and
   124  // RemoveInterface functions.
   125  func Update(c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   126  	b, err := opts.ToRouterUpdateMap()
   127  	if err != nil {
   128  		r.Err = err
   129  		return
   130  	}
   131  	resp, err := c.Put(resourceURL(c, id), b, &r.Body, &gophercloud.RequestOpts{
   132  		OkCodes: []int{200},
   133  	})
   134  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   135  	return
   136  }
   137  
   138  // Delete will permanently delete a particular router based on its unique ID.
   139  func Delete(c *gophercloud.ServiceClient, id string) (r DeleteResult) {
   140  	resp, err := c.Delete(resourceURL(c, id), nil)
   141  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   142  	return
   143  }
   144  
   145  // AddInterfaceOptsBuilder allows extensions to add additional parameters to
   146  // the AddInterface request.
   147  type AddInterfaceOptsBuilder interface {
   148  	ToRouterAddInterfaceMap() (map[string]interface{}, error)
   149  }
   150  
   151  // AddInterfaceOpts represents the options for adding an interface to a router.
   152  type AddInterfaceOpts struct {
   153  	SubnetID string `json:"subnet_id,omitempty" xor:"PortID"`
   154  	PortID   string `json:"port_id,omitempty" xor:"SubnetID"`
   155  }
   156  
   157  // ToRouterAddInterfaceMap builds a request body from AddInterfaceOpts.
   158  func (opts AddInterfaceOpts) ToRouterAddInterfaceMap() (map[string]interface{}, error) {
   159  	return gophercloud.BuildRequestBody(opts, "")
   160  }
   161  
   162  // AddInterface attaches a subnet to an internal router interface. You must
   163  // specify either a SubnetID or PortID in the request body. If you specify both,
   164  // the operation will fail and an error will be returned.
   165  //
   166  // If you specify a SubnetID, the gateway IP address for that particular subnet
   167  // is used to create the router interface. Alternatively, if you specify a
   168  // PortID, the IP address associated with the port is used to create the router
   169  // interface.
   170  //
   171  // If you reference a port that is associated with multiple IP addresses, or
   172  // if the port is associated with zero IP addresses, the operation will fail and
   173  // a 400 Bad Request error will be returned.
   174  //
   175  // If you reference a port already in use, the operation will fail and a 409
   176  // Conflict error will be returned.
   177  //
   178  // The PortID that is returned after using Extract() on the result of this
   179  // operation can either be the same PortID passed in or, on the other hand, the
   180  // identifier of a new port created by this operation. After the operation
   181  // completes, the device ID of the port is set to the router ID, and the
   182  // device owner attribute is set to `network:router_interface'.
   183  func AddInterface(c *gophercloud.ServiceClient, id string, opts AddInterfaceOptsBuilder) (r InterfaceResult) {
   184  	b, err := opts.ToRouterAddInterfaceMap()
   185  	if err != nil {
   186  		r.Err = err
   187  		return
   188  	}
   189  	resp, err := c.Put(addInterfaceURL(c, id), b, &r.Body, &gophercloud.RequestOpts{
   190  		OkCodes: []int{200},
   191  	})
   192  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   193  	return
   194  }
   195  
   196  // RemoveInterfaceOptsBuilder allows extensions to add additional parameters to
   197  // the RemoveInterface request.
   198  type RemoveInterfaceOptsBuilder interface {
   199  	ToRouterRemoveInterfaceMap() (map[string]interface{}, error)
   200  }
   201  
   202  // RemoveInterfaceOpts represents options for removing an interface from
   203  // a router.
   204  type RemoveInterfaceOpts struct {
   205  	SubnetID string `json:"subnet_id,omitempty" or:"PortID"`
   206  	PortID   string `json:"port_id,omitempty" or:"SubnetID"`
   207  }
   208  
   209  // ToRouterRemoveInterfaceMap builds a request body based on
   210  // RemoveInterfaceOpts.
   211  func (opts RemoveInterfaceOpts) ToRouterRemoveInterfaceMap() (map[string]interface{}, error) {
   212  	return gophercloud.BuildRequestBody(opts, "")
   213  }
   214  
   215  // RemoveInterface removes an internal router interface, which detaches a
   216  // subnet from the router. You must specify either a SubnetID or PortID, since
   217  // these values are used to identify the router interface to remove.
   218  //
   219  // Unlike AddInterface, you can also specify both a SubnetID and PortID. If you
   220  // choose to specify both, the subnet ID must correspond to the subnet ID of
   221  // the first IP address on the port specified by the port ID. Otherwise, the
   222  // operation will fail and return a 409 Conflict error.
   223  //
   224  // If the router, subnet or port which are referenced do not exist or are not
   225  // visible to you, the operation will fail and a 404 Not Found error will be
   226  // returned. After this operation completes, the port connecting the router
   227  // with the subnet is removed from the subnet for the network.
   228  func RemoveInterface(c *gophercloud.ServiceClient, id string, opts RemoveInterfaceOptsBuilder) (r InterfaceResult) {
   229  	b, err := opts.ToRouterRemoveInterfaceMap()
   230  	if err != nil {
   231  		r.Err = err
   232  		return
   233  	}
   234  	resp, err := c.Put(removeInterfaceURL(c, id), b, &r.Body, &gophercloud.RequestOpts{
   235  		OkCodes: []int{200},
   236  	})
   237  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   238  	return
   239  }
   240  
   241  // ListL3Agents returns a list of l3-agents scheduled for a specific router.
   242  func ListL3Agents(c *gophercloud.ServiceClient, id string) (result pagination.Pager) {
   243  	return pagination.NewPager(c, listl3AgentsURL(c, id), func(r pagination.PageResult) pagination.Page {
   244  		return ListL3AgentsPage{pagination.SinglePageBase(r)}
   245  	})
   246  }