github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/networking/v2/extensions/layer3/routers/requests.go (about)

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