github.com/gophercloud/gophercloud@v1.14.1/openstack/networking/v2/extensions/bgpvpns/results.go (about) 1 package bgpvpns 2 3 import ( 4 "net/url" 5 6 "github.com/gophercloud/gophercloud" 7 "github.com/gophercloud/gophercloud/pagination" 8 ) 9 10 const ( 11 invalidMarker = "-1" 12 ) 13 14 type commonResult struct { 15 gophercloud.Result 16 } 17 18 // Extract is a function that accepts a result and extracts a BGP VPN resource. 19 func (r commonResult) Extract() (*BGPVPN, error) { 20 var s BGPVPN 21 err := r.ExtractInto(&s) 22 return &s, err 23 } 24 25 func (r commonResult) ExtractInto(v interface{}) error { 26 return r.Result.ExtractIntoStructPtr(v, "bgpvpn") 27 } 28 29 // BGPVPN represents an MPLS network with which Neutron routers and/or networks 30 // may be associated 31 type BGPVPN struct { 32 // The ID of the BGP VPN. 33 ID string `json:"id"` 34 35 // The user meaningful name of the BGP VPN. 36 Name string `json:"name"` 37 38 // Selection of the type of VPN and the technology behind it. Allowed 39 // values are l2 or l3. 40 Type string `json:"type"` 41 42 // Indicates whether this BGP VPN is shared across tenants. 43 Shared bool `json:"shared"` 44 45 // List of route distinguisher strings. If this parameter is specified, 46 // one of these RDs will be used to advertise VPN routes. 47 RouteDistinguishers []string `json:"route_distinguishers"` 48 49 // Route Targets that will be both imported and used for export. 50 RouteTargets []string `json:"route_targets"` 51 52 // Additional Route Targets that will be imported. 53 ImportTargets []string `json:"import_targets"` 54 55 // Additional Route Targets that will be used for export. 56 ExportTargets []string `json:"export_targets"` 57 58 // This read-only list of network IDs reflects the associations defined 59 // by Network association API resources. 60 Networks []string `json:"networks"` 61 62 // This read-only list of router IDs reflects the associations defined 63 // by Router association API resources. 64 Routers []string `json:"routers"` 65 66 // This read-only list of port IDs reflects the associations defined by 67 // Port association API resources (only present if the 68 // bgpvpn-routes-control API extension is enabled). 69 Ports []string `json:"ports"` 70 71 // The default BGP LOCAL_PREF of routes that will be advertised to the 72 // BGPVPN (unless overridden per-route). 73 LocalPref *int `json:"local_pref"` 74 75 // The globally-assigned VXLAN vni for the BGP VPN. 76 VNI int `json:"vni"` 77 78 // The ID of the project. 79 TenantID string `json:"tenant_id"` 80 81 // The ID of the project. 82 ProjectID string `json:"project_id"` 83 } 84 85 // BGPVPNPage is the page returned by a pager when traversing over a 86 // collection of BGP VPNs. 87 type BGPVPNPage struct { 88 pagination.MarkerPageBase 89 } 90 91 // NextPageURL generates the URL for the page of results after this one. 92 func (r BGPVPNPage) NextPageURL() (string, error) { 93 currentURL := r.URL 94 mark, err := r.Owner.LastMarker() 95 if err != nil { 96 return "", err 97 } 98 if mark == invalidMarker { 99 return "", nil 100 } 101 102 q := currentURL.Query() 103 q.Set("marker", mark) 104 currentURL.RawQuery = q.Encode() 105 return currentURL.String(), nil 106 } 107 108 // LastMarker returns the last offset in a ListResult. 109 func (r BGPVPNPage) LastMarker() (string, error) { 110 results, err := ExtractBGPVPNs(r) 111 if err != nil { 112 return invalidMarker, err 113 } 114 if len(results) == 0 { 115 return invalidMarker, nil 116 } 117 118 u, err := url.Parse(r.URL.String()) 119 if err != nil { 120 return invalidMarker, err 121 } 122 queryParams := u.Query() 123 limit := queryParams.Get("limit") 124 125 // Limit is not present, only one page required 126 if limit == "" { 127 return invalidMarker, nil 128 } 129 130 return results[len(results)-1].ID, nil 131 } 132 133 // IsEmpty checks whether a BGPPage struct is empty. 134 func (r BGPVPNPage) IsEmpty() (bool, error) { 135 if r.StatusCode == 204 { 136 return true, nil 137 } 138 139 is, err := ExtractBGPVPNs(r) 140 return len(is) == 0, err 141 } 142 143 // ExtractBGPVPNs accepts a Page struct, specifically a BGPVPNPage struct, 144 // and extracts the elements into a slice of BGPVPN structs. In other words, 145 // a generic collection is mapped into a relevant slice. 146 func ExtractBGPVPNs(r pagination.Page) ([]BGPVPN, error) { 147 var s []BGPVPN 148 err := ExtractBGPVPNsInto(r, &s) 149 return s, err 150 } 151 152 func ExtractBGPVPNsInto(r pagination.Page, v interface{}) error { 153 return r.(BGPVPNPage).Result.ExtractIntoSlicePtr(v, "bgpvpns") 154 } 155 156 // GetResult represents the result of a get operation. Call its Extract 157 // method to interpret it as a BGPVPN. 158 type GetResult struct { 159 commonResult 160 } 161 162 // CreateResult represents the result of a create operation. Call its Extract 163 // method to intepret it as a BGPVPN. 164 type CreateResult struct { 165 commonResult 166 } 167 168 // DeleteResult represents the result of a delete operation. Call its 169 // ExtractErr method to determine if the request succeeded or failed. 170 type DeleteResult struct { 171 gophercloud.ErrResult 172 } 173 174 // UpdateResult represents the result of an update operation. Call its Extract 175 // method to interpret it as a BGPVPN. 176 type UpdateResult struct { 177 commonResult 178 } 179 180 type commonNetworkAssociationResult struct { 181 gophercloud.Result 182 } 183 184 // Extract is a function that accepts a result and extracts a BGP VPN resource. 185 func (r commonNetworkAssociationResult) Extract() (*NetworkAssociation, error) { 186 var s NetworkAssociation 187 err := r.ExtractInto(&s) 188 return &s, err 189 } 190 191 func (r commonNetworkAssociationResult) ExtractInto(v interface{}) error { 192 return r.Result.ExtractIntoStructPtr(v, "network_association") 193 } 194 195 // NetworkAssociation represents a BGP VPN network association object. 196 type NetworkAssociation struct { 197 ID string `json:"id"` 198 NetworkID string `json:"network_id"` 199 TenantID string `json:"tenant_id"` 200 ProjectID string `json:"project_id"` 201 } 202 203 // NetworkAssociationPage is the page returned by a pager when traversing over a 204 // collection of network associations. 205 type NetworkAssociationPage struct { 206 pagination.MarkerPageBase 207 } 208 209 // NextPageURL generates the URL for the page of results after this one. 210 func (r NetworkAssociationPage) NextPageURL() (string, error) { 211 currentURL := r.URL 212 mark, err := r.Owner.LastMarker() 213 if err != nil { 214 return "", err 215 } 216 if mark == invalidMarker { 217 return "", nil 218 } 219 220 q := currentURL.Query() 221 q.Set("marker", mark) 222 currentURL.RawQuery = q.Encode() 223 return currentURL.String(), nil 224 } 225 226 // LastMarker returns the last offset in a ListResult. 227 func (r NetworkAssociationPage) LastMarker() (string, error) { 228 results, err := ExtractNetworkAssociations(r) 229 if err != nil { 230 return invalidMarker, err 231 } 232 if len(results) == 0 { 233 return invalidMarker, nil 234 } 235 236 u, err := url.Parse(r.URL.String()) 237 if err != nil { 238 return invalidMarker, err 239 } 240 queryParams := u.Query() 241 limit := queryParams.Get("limit") 242 243 // Limit is not present, only one page required 244 if limit == "" { 245 return invalidMarker, nil 246 } 247 248 return results[len(results)-1].ID, nil 249 } 250 251 // IsEmpty checks whether a NetworkAssociationPage struct is empty. 252 func (r NetworkAssociationPage) IsEmpty() (bool, error) { 253 is, err := ExtractNetworkAssociations(r) 254 return len(is) == 0, err 255 } 256 257 // ExtractNetworkAssociations accepts a Page struct, specifically a NetworkAssociationPage struct, 258 // and extracts the elements into a slice of NetworkAssociation structs. In other words, 259 // a generic collection is mapped into a relevant slice. 260 func ExtractNetworkAssociations(r pagination.Page) ([]NetworkAssociation, error) { 261 var s []NetworkAssociation 262 err := ExtractNetworkAssociationsInto(r, &s) 263 return s, err 264 } 265 266 func ExtractNetworkAssociationsInto(r pagination.Page, v interface{}) error { 267 return r.(NetworkAssociationPage).Result.ExtractIntoSlicePtr(v, "network_associations") 268 } 269 270 // CreateNetworkAssociationResult represents the result of a create operation. Call its Extract 271 // method to interpret it as a NetworkAssociation. 272 type CreateNetworkAssociationResult struct { 273 commonNetworkAssociationResult 274 } 275 276 // GetNetworkAssociationResult represents the result of a get operation. Call its Extract 277 // method to interpret it as a NetworkAssociation. 278 type GetNetworkAssociationResult struct { 279 commonNetworkAssociationResult 280 } 281 282 // DeleteNetworkAssociationResult represents the result of a delete operation. Call its 283 // ExtractErr method to determine if the request succeeded or failed. 284 type DeleteNetworkAssociationResult struct { 285 gophercloud.ErrResult 286 } 287 288 type commonRouterAssociationResult struct { 289 gophercloud.Result 290 } 291 292 // Extract is a function that accepts a result and extracts a BGP VPN resource. 293 func (r commonRouterAssociationResult) Extract() (*RouterAssociation, error) { 294 var s RouterAssociation 295 err := r.ExtractInto(&s) 296 return &s, err 297 } 298 299 func (r commonRouterAssociationResult) ExtractInto(v interface{}) error { 300 return r.Result.ExtractIntoStructPtr(v, "router_association") 301 } 302 303 // RouterAssociation represents a BGP VPN router association object. 304 type RouterAssociation struct { 305 ID string `json:"id"` 306 RouterID string `json:"router_id"` 307 TenantID string `json:"tenant_id"` 308 ProjectID string `json:"project_id"` 309 AdvertiseExtraRoutes bool `json:"advertise_extra_routes"` 310 } 311 312 // RouterAssociationPage is the page returned by a pager when traversing over a 313 // collection of router associations. 314 type RouterAssociationPage struct { 315 pagination.MarkerPageBase 316 } 317 318 // NextPageURL generates the URL for the page of results after this one. 319 func (r RouterAssociationPage) NextPageURL() (string, error) { 320 currentURL := r.URL 321 mark, err := r.Owner.LastMarker() 322 if err != nil { 323 return "", err 324 } 325 if mark == invalidMarker { 326 return "", nil 327 } 328 329 q := currentURL.Query() 330 q.Set("marker", mark) 331 currentURL.RawQuery = q.Encode() 332 return currentURL.String(), nil 333 } 334 335 // LastMarker returns the last offset in a ListResult. 336 func (r RouterAssociationPage) LastMarker() (string, error) { 337 results, err := ExtractRouterAssociations(r) 338 if err != nil { 339 return invalidMarker, err 340 } 341 if len(results) == 0 { 342 return invalidMarker, nil 343 } 344 345 u, err := url.Parse(r.URL.String()) 346 if err != nil { 347 return invalidMarker, err 348 } 349 queryParams := u.Query() 350 limit := queryParams.Get("limit") 351 352 // Limit is not present, only one page required 353 if limit == "" { 354 return invalidMarker, nil 355 } 356 357 return results[len(results)-1].ID, nil 358 } 359 360 // IsEmpty checks whether a RouterAssociationPage struct is empty. 361 func (r RouterAssociationPage) IsEmpty() (bool, error) { 362 is, err := ExtractRouterAssociations(r) 363 return len(is) == 0, err 364 } 365 366 // ExtractRouterAssociations accepts a Page struct, specifically a RouterAssociationPage struct, 367 // and extracts the elements into a slice of RouterAssociation structs. In other words, 368 // a generic collection is mapped into a relevant slice. 369 func ExtractRouterAssociations(r pagination.Page) ([]RouterAssociation, error) { 370 var s []RouterAssociation 371 err := ExtractRouterAssociationsInto(r, &s) 372 return s, err 373 } 374 375 func ExtractRouterAssociationsInto(r pagination.Page, v interface{}) error { 376 return r.(RouterAssociationPage).Result.ExtractIntoSlicePtr(v, "router_associations") 377 } 378 379 // CreateRouterAssociationResult represents the result of a create operation. Call its Extract 380 // method to interpret it as a RouterAssociation. 381 type CreateRouterAssociationResult struct { 382 commonRouterAssociationResult 383 } 384 385 // GetRouterAssociationResult represents the result of a get operation. Call its Extract 386 // method to interpret it as a RouterAssociation. 387 type GetRouterAssociationResult struct { 388 commonRouterAssociationResult 389 } 390 391 // DeleteRouterAssociationResult represents the result of a delete operation. Call its 392 // ExtractErr method to determine if the request succeeded or failed. 393 type DeleteRouterAssociationResult struct { 394 gophercloud.ErrResult 395 } 396 397 // UpdateRouterAssociationResult represents the result of an update operation. Call its Extract 398 // method to interpret it as a RouterAssociation. 399 type UpdateRouterAssociationResult struct { 400 commonRouterAssociationResult 401 } 402 403 type commonPortAssociationResult struct { 404 gophercloud.Result 405 } 406 407 // Extract is a function that accepts a result and extracts a BGP VPN resource. 408 func (r commonPortAssociationResult) Extract() (*PortAssociation, error) { 409 var s PortAssociation 410 err := r.ExtractInto(&s) 411 return &s, err 412 } 413 414 func (r commonPortAssociationResult) ExtractInto(v interface{}) error { 415 return r.Result.ExtractIntoStructPtr(v, "port_association") 416 } 417 418 // PortAssociation represents a BGP VPN port association object. 419 type PortAssociation struct { 420 ID string `json:"id"` 421 PortID string `json:"port_id"` 422 TenantID string `json:"tenant_id"` 423 ProjectID string `json:"project_id"` 424 Routes []PortRoutes `json:"routes"` 425 AdvertiseFixedIPs bool `json:"advertise_fixed_ips"` 426 } 427 428 // PortAssociationPage is the page returned by a pager when traversing over a 429 // collection of port associations. 430 type PortAssociationPage struct { 431 pagination.MarkerPageBase 432 } 433 434 // NextPageURL generates the URL for the page of results after this one. 435 func (r PortAssociationPage) NextPageURL() (string, error) { 436 currentURL := r.URL 437 mark, err := r.Owner.LastMarker() 438 if err != nil { 439 return "", err 440 } 441 if mark == invalidMarker { 442 return "", nil 443 } 444 445 q := currentURL.Query() 446 q.Set("marker", mark) 447 currentURL.RawQuery = q.Encode() 448 return currentURL.String(), nil 449 } 450 451 // LastMarker returns the last offset in a ListResult. 452 func (r PortAssociationPage) LastMarker() (string, error) { 453 results, err := ExtractPortAssociations(r) 454 if err != nil { 455 return invalidMarker, err 456 } 457 if len(results) == 0 { 458 return invalidMarker, nil 459 } 460 461 u, err := url.Parse(r.URL.String()) 462 if err != nil { 463 return invalidMarker, err 464 } 465 queryParams := u.Query() 466 limit := queryParams.Get("limit") 467 468 // Limit is not present, only one page required 469 if limit == "" { 470 return invalidMarker, nil 471 } 472 473 return results[len(results)-1].ID, nil 474 } 475 476 // IsEmpty checks whether a PortAssociationPage struct is empty. 477 func (r PortAssociationPage) IsEmpty() (bool, error) { 478 is, err := ExtractPortAssociations(r) 479 return len(is) == 0, err 480 } 481 482 // ExtractPortAssociations accepts a Page struct, specifically a PortAssociationPage struct, 483 // and extracts the elements into a slice of PortAssociation structs. In other words, 484 // a generic collection is mapped into a relevant slice. 485 func ExtractPortAssociations(r pagination.Page) ([]PortAssociation, error) { 486 var s []PortAssociation 487 err := ExtractPortAssociationsInto(r, &s) 488 return s, err 489 } 490 491 func ExtractPortAssociationsInto(r pagination.Page, v interface{}) error { 492 return r.(PortAssociationPage).Result.ExtractIntoSlicePtr(v, "port_associations") 493 } 494 495 // CreatePortAssociationResult represents the result of a create operation. Call its Extract 496 // method to interpret it as a PortAssociation. 497 type CreatePortAssociationResult struct { 498 commonPortAssociationResult 499 } 500 501 // GetPortAssociationResult represents the result of a get operation. Call its Extract 502 // method to interpret it as a PortAssociation. 503 type GetPortAssociationResult struct { 504 commonPortAssociationResult 505 } 506 507 // DeletePortAssociationResult represents the result of a delete operation. Call its 508 // ExtractErr method to determine if the request succeeded or failed. 509 type DeletePortAssociationResult struct { 510 gophercloud.ErrResult 511 } 512 513 // UpdatePortAssociationResult represents the result of an update operation. Call its Extract 514 // method to interpret it as a PortAssociation. 515 type UpdatePortAssociationResult struct { 516 commonPortAssociationResult 517 }