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