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