github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/networks/results.go (about) 1 package networks 2 3 import ( 4 "github.com/huaweicloud/golangsdk" 5 "github.com/huaweicloud/golangsdk/pagination" 6 ) 7 8 type commonResult struct { 9 golangsdk.Result 10 } 11 12 // Extract is a function that accepts a result and extracts a network resource. 13 func (r commonResult) Extract() (*Network, error) { 14 var s Network 15 err := r.ExtractInto(&s) 16 return &s, err 17 } 18 19 func (r commonResult) ExtractInto(v interface{}) error { 20 return r.Result.ExtractIntoStructPtr(v, "network") 21 } 22 23 // CreateResult represents the result of a create operation. Call its Extract 24 // method to interpret it as a Network. 25 type CreateResult struct { 26 commonResult 27 } 28 29 // GetResult represents the result of a get operation. Call its Extract 30 // method to interpret it as a Network. 31 type GetResult struct { 32 commonResult 33 } 34 35 // UpdateResult represents the result of an update operation. Call its Extract 36 // method to interpret it as a Network. 37 type UpdateResult struct { 38 commonResult 39 } 40 41 // DeleteResult represents the result of a delete operation. Call its 42 // ExtractErr method to determine if the request succeeded or failed. 43 type DeleteResult struct { 44 golangsdk.ErrResult 45 } 46 47 // Network represents, well, a network. 48 type Network struct { 49 // UUID for the network 50 ID string `json:"id"` 51 52 // Human-readable name for the network. Might not be unique. 53 Name string `json:"name"` 54 55 // The administrative state of network. If false (down), the network does not 56 // forward packets. 57 AdminStateUp bool `json:"admin_state_up"` 58 59 // Indicates whether network is currently operational. Possible values include 60 // `ACTIVE', `DOWN', `BUILD', or `ERROR'. Plug-ins might define additional 61 // values. 62 Status string `json:"status"` 63 64 // Subnets associated with this network. 65 Subnets []string `json:"subnets"` 66 67 // TenantID is the project owner of the network. 68 TenantID string `json:"tenant_id"` 69 70 // ProjectID is the project owner of the network. 71 ProjectID string `json:"project_id"` 72 73 // Specifies whether the network resource can be accessed by any tenant. 74 Shared bool `json:"shared"` 75 76 // Availability zone hints groups network nodes that run services like DHCP, L3, FW, and others. 77 // Used to make network resources highly available. 78 AvailabilityZoneHints []string `json:"availability_zone_hints"` 79 } 80 81 // NetworkPage is the page returned by a pager when traversing over a 82 // collection of networks. 83 type NetworkPage struct { 84 pagination.LinkedPageBase 85 } 86 87 // NextPageURL is invoked when a paginated collection of networks has reached 88 // the end of a page and the pager seeks to traverse over a new one. In order 89 // to do this, it needs to construct the next page's URL. 90 func (r NetworkPage) NextPageURL() (string, error) { 91 var s struct { 92 Links []golangsdk.Link `json:"networks_links"` 93 } 94 err := r.ExtractInto(&s) 95 if err != nil { 96 return "", err 97 } 98 return golangsdk.ExtractNextURL(s.Links) 99 } 100 101 // IsEmpty checks whether a NetworkPage struct is empty. 102 func (r NetworkPage) IsEmpty() (bool, error) { 103 is, err := ExtractNetworks(r) 104 return len(is) == 0, err 105 } 106 107 // ExtractNetworks accepts a Page struct, specifically a NetworkPage struct, 108 // and extracts the elements into a slice of Network structs. In other words, 109 // a generic collection is mapped into a relevant slice. 110 func ExtractNetworks(r pagination.Page) ([]Network, error) { 111 var s []Network 112 err := ExtractNetworksInto(r, &s) 113 return s, err 114 } 115 116 func ExtractNetworksInto(r pagination.Page, v interface{}) error { 117 return r.(NetworkPage).Result.ExtractIntoSlicePtr(v, "networks") 118 }