Jamie Hannaford | 9823bb6 | 2014-09-26 17:06:36 +0200 | [diff] [blame] | 1 | package external |
| 2 | |
| 3 | import ( |
Jamie Hannaford | 9823bb6 | 2014-09-26 17:06:36 +0200 | [diff] [blame] | 4 | "github.com/mitchellh/mapstructure" |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame^] | 5 | "github.com/gophercloud/gophercloud/openstack/networking/v2/networks" |
| 6 | "github.com/gophercloud/gophercloud/pagination" |
Jamie Hannaford | 9823bb6 | 2014-09-26 17:06:36 +0200 | [diff] [blame] | 7 | ) |
| 8 | |
| 9 | // NetworkExternal represents a decorated form of a Network with based on the |
| 10 | // "external-net" extension. |
| 11 | type NetworkExternal struct { |
| 12 | // UUID for the network |
| 13 | ID string `mapstructure:"id" json:"id"` |
| 14 | |
| 15 | // Human-readable name for the network. Might not be unique. |
| 16 | Name string `mapstructure:"name" json:"name"` |
| 17 | |
| 18 | // The administrative state of network. If false (down), the network does not forward packets. |
| 19 | AdminStateUp bool `mapstructure:"admin_state_up" json:"admin_state_up"` |
| 20 | |
| 21 | // Indicates whether network is currently operational. Possible values include |
| 22 | // `ACTIVE', `DOWN', `BUILD', or `ERROR'. Plug-ins might define additional values. |
| 23 | Status string `mapstructure:"status" json:"status"` |
| 24 | |
| 25 | // Subnets associated with this network. |
| 26 | Subnets []string `mapstructure:"subnets" json:"subnets"` |
| 27 | |
| 28 | // Owner of network. Only admin users can specify a tenant_id other than its own. |
| 29 | TenantID string `mapstructure:"tenant_id" json:"tenant_id"` |
| 30 | |
| 31 | // Specifies whether the network resource can be accessed by any tenant or not. |
| 32 | Shared bool `mapstructure:"shared" json:"shared"` |
| 33 | |
| 34 | // Specifies whether the network is an external network or not. |
| 35 | External bool `mapstructure:"router:external" json:"router:external"` |
| 36 | } |
| 37 | |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 38 | func commonExtract(e error, response interface{}) (*NetworkExternal, error) { |
Jamie Hannaford | 91cd64e | 2014-10-08 13:08:52 +0200 | [diff] [blame] | 39 | if e != nil { |
| 40 | return nil, e |
Jamie Hannaford | 9823bb6 | 2014-09-26 17:06:36 +0200 | [diff] [blame] | 41 | } |
Jamie Hannaford | 91cd64e | 2014-10-08 13:08:52 +0200 | [diff] [blame] | 42 | |
Jamie Hannaford | 9823bb6 | 2014-09-26 17:06:36 +0200 | [diff] [blame] | 43 | var res struct { |
| 44 | Network *NetworkExternal `json:"network"` |
| 45 | } |
Jamie Hannaford | 91cd64e | 2014-10-08 13:08:52 +0200 | [diff] [blame] | 46 | |
| 47 | err := mapstructure.Decode(response, &res) |
Jamie Hannaford | 91cd64e | 2014-10-08 13:08:52 +0200 | [diff] [blame] | 48 | |
Jamie Hannaford | c1f3649 | 2014-10-08 15:48:48 +0200 | [diff] [blame] | 49 | return res.Network, err |
Jamie Hannaford | 9823bb6 | 2014-09-26 17:06:36 +0200 | [diff] [blame] | 50 | } |
| 51 | |
Jamie Hannaford | 91cd64e | 2014-10-08 13:08:52 +0200 | [diff] [blame] | 52 | // ExtractGet decorates a GetResult struct returned from a networks.Get() |
| 53 | // function with extended attributes. |
| 54 | func ExtractGet(r networks.GetResult) (*NetworkExternal, error) { |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 55 | return commonExtract(r.Err, r.Body) |
Jamie Hannaford | 91cd64e | 2014-10-08 13:08:52 +0200 | [diff] [blame] | 56 | } |
| 57 | |
Jamie Hannaford | 9823bb6 | 2014-09-26 17:06:36 +0200 | [diff] [blame] | 58 | // ExtractCreate decorates a CreateResult struct returned from a networks.Create() |
| 59 | // function with extended attributes. |
| 60 | func ExtractCreate(r networks.CreateResult) (*NetworkExternal, error) { |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 61 | return commonExtract(r.Err, r.Body) |
Jamie Hannaford | 9823bb6 | 2014-09-26 17:06:36 +0200 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | // ExtractUpdate decorates a UpdateResult struct returned from a |
| 65 | // networks.Update() function with extended attributes. |
| 66 | func ExtractUpdate(r networks.UpdateResult) (*NetworkExternal, error) { |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 67 | return commonExtract(r.Err, r.Body) |
Jamie Hannaford | 9823bb6 | 2014-09-26 17:06:36 +0200 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | // ExtractList accepts a Page struct, specifically a NetworkPage struct, and |
Jon Perritt | b0b9c0d | 2015-03-10 17:24:08 -0600 | [diff] [blame] | 71 | // extracts the elements into a slice of NetworkExternal structs. In other |
Jamie Hannaford | 9823bb6 | 2014-09-26 17:06:36 +0200 | [diff] [blame] | 72 | // words, a generic collection is mapped into a relevant slice. |
| 73 | func ExtractList(page pagination.Page) ([]NetworkExternal, error) { |
| 74 | var resp struct { |
| 75 | Networks []NetworkExternal `mapstructure:"networks" json:"networks"` |
| 76 | } |
| 77 | |
| 78 | err := mapstructure.Decode(page.(networks.NetworkPage).Body, &resp) |
Jamie Hannaford | 9823bb6 | 2014-09-26 17:06:36 +0200 | [diff] [blame] | 79 | |
Jamie Hannaford | c1f3649 | 2014-10-08 15:48:48 +0200 | [diff] [blame] | 80 | return resp.Networks, err |
Jamie Hannaford | 9823bb6 | 2014-09-26 17:06:36 +0200 | [diff] [blame] | 81 | } |