Jamie Hannaford | b0d9912 | 2014-09-25 10:49:14 +0200 | [diff] [blame] | 1 | package provider |
| 2 | |
| 3 | import ( |
Jamie Hannaford | b0d9912 | 2014-09-25 10:49:14 +0200 | [diff] [blame] | 4 | "github.com/mitchellh/mapstructure" |
| 5 | "github.com/rackspace/gophercloud/openstack/networking/v2/networks" |
| 6 | "github.com/rackspace/gophercloud/pagination" |
| 7 | ) |
| 8 | |
Jamie Hannaford | c7c49a7 | 2014-10-06 15:49:53 +0200 | [diff] [blame] | 9 | // AdminState gives users a solid type to work with for create and update |
| 10 | // operations. It is recommended that users use the `Up` and `Down` enums. |
Jamie Hannaford | a241e31 | 2014-10-01 16:54:33 +0200 | [diff] [blame] | 11 | type AdminState *bool |
| 12 | |
| 13 | // Convenience vars for AdminStateUp values. |
| 14 | var ( |
| 15 | iTrue = true |
| 16 | iFalse = false |
| 17 | |
Jamie Hannaford | c7c49a7 | 2014-10-06 15:49:53 +0200 | [diff] [blame] | 18 | Up AdminState = &iTrue |
| 19 | Down AdminState = &iFalse |
Jamie Hannaford | a241e31 | 2014-10-01 16:54:33 +0200 | [diff] [blame] | 20 | ) |
| 21 | |
Jamie Hannaford | b0d9912 | 2014-09-25 10:49:14 +0200 | [diff] [blame] | 22 | // NetworkExtAttrs represents an extended form of a Network with additional fields. |
| 23 | type NetworkExtAttrs struct { |
| 24 | // UUID for the network |
| 25 | ID string `mapstructure:"id" json:"id"` |
| 26 | |
| 27 | // Human-readable name for the network. Might not be unique. |
| 28 | Name string `mapstructure:"name" json:"name"` |
| 29 | |
| 30 | // The administrative state of network. If false (down), the network does not forward packets. |
| 31 | AdminStateUp bool `mapstructure:"admin_state_up" json:"admin_state_up"` |
| 32 | |
| 33 | // Indicates whether network is currently operational. Possible values include |
| 34 | // `ACTIVE', `DOWN', `BUILD', or `ERROR'. Plug-ins might define additional values. |
| 35 | Status string `mapstructure:"status" json:"status"` |
| 36 | |
| 37 | // Subnets associated with this network. |
| 38 | Subnets []string `mapstructure:"subnets" json:"subnets"` |
| 39 | |
| 40 | // Owner of network. Only admin users can specify a tenant_id other than its own. |
| 41 | TenantID string `mapstructure:"tenant_id" json:"tenant_id"` |
| 42 | |
| 43 | // Specifies whether the network resource can be accessed by any tenant or not. |
| 44 | Shared bool `mapstructure:"shared" json:"shared"` |
| 45 | |
| 46 | // Specifies the nature of the physical network mapped to this network |
| 47 | // resource. Examples are flat, vlan, or gre. |
| 48 | NetworkType string `json:"provider:network_type" mapstructure:"provider:network_type"` |
| 49 | |
| 50 | // Identifies the physical network on top of which this network object is |
| 51 | // being implemented. The OpenStack Networking API does not expose any facility |
| 52 | // for retrieving the list of available physical networks. As an example, in |
| 53 | // the Open vSwitch plug-in this is a symbolic name which is then mapped to |
| 54 | // specific bridges on each compute host through the Open vSwitch plug-in |
| 55 | // configuration file. |
| 56 | PhysicalNetwork string `json:"provider:physical_network" mapstructure:"provider:physical_network"` |
| 57 | |
| 58 | // Identifies an isolated segment on the physical network; the nature of the |
| 59 | // segment depends on the segmentation model defined by network_type. For |
| 60 | // instance, if network_type is vlan, then this is a vlan identifier; |
| 61 | // otherwise, if network_type is gre, then this will be a gre key. |
| 62 | SegmentationID string `json:"provider:segmentation_id" mapstructure:"provider:segmentation_id"` |
| 63 | } |
| 64 | |
Jamie Hannaford | 5a53190 | 2014-09-26 15:03:31 +0200 | [diff] [blame] | 65 | // ExtractGet decorates a GetResult struct returned from a networks.Get() |
| 66 | // function with extended attributes. |
Jamie Hannaford | b0d9912 | 2014-09-25 10:49:14 +0200 | [diff] [blame] | 67 | func ExtractGet(r networks.GetResult) (*NetworkExtAttrs, error) { |
| 68 | if r.Err != nil { |
| 69 | return nil, r.Err |
| 70 | } |
Jamie Hannaford | c1f3649 | 2014-10-08 15:48:48 +0200 | [diff] [blame] | 71 | |
Jamie Hannaford | b0d9912 | 2014-09-25 10:49:14 +0200 | [diff] [blame] | 72 | var res struct { |
| 73 | Network *NetworkExtAttrs `json:"network"` |
| 74 | } |
Jamie Hannaford | c1f3649 | 2014-10-08 15:48:48 +0200 | [diff] [blame] | 75 | |
Jon Perritt | b0b9c0d | 2015-03-10 17:24:08 -0600 | [diff] [blame] | 76 | err := mapstructure.WeakDecode(r.Body, &res) |
Jamie Hannaford | c1f3649 | 2014-10-08 15:48:48 +0200 | [diff] [blame] | 77 | |
| 78 | return res.Network, err |
Jamie Hannaford | b0d9912 | 2014-09-25 10:49:14 +0200 | [diff] [blame] | 79 | } |
| 80 | |
Jamie Hannaford | c7c49a7 | 2014-10-06 15:49:53 +0200 | [diff] [blame] | 81 | // ExtractCreate decorates a CreateResult struct returned from a networks.Create() |
Jamie Hannaford | 5a53190 | 2014-09-26 15:03:31 +0200 | [diff] [blame] | 82 | // function with extended attributes. |
Jamie Hannaford | b0d9912 | 2014-09-25 10:49:14 +0200 | [diff] [blame] | 83 | func ExtractCreate(r networks.CreateResult) (*NetworkExtAttrs, error) { |
| 84 | if r.Err != nil { |
| 85 | return nil, r.Err |
| 86 | } |
Jamie Hannaford | c1f3649 | 2014-10-08 15:48:48 +0200 | [diff] [blame] | 87 | |
Jamie Hannaford | b0d9912 | 2014-09-25 10:49:14 +0200 | [diff] [blame] | 88 | var res struct { |
| 89 | Network *NetworkExtAttrs `json:"network"` |
| 90 | } |
Jamie Hannaford | c1f3649 | 2014-10-08 15:48:48 +0200 | [diff] [blame] | 91 | |
Jon Perritt | b0b9c0d | 2015-03-10 17:24:08 -0600 | [diff] [blame] | 92 | err := mapstructure.WeakDecode(r.Body, &res) |
Jamie Hannaford | c1f3649 | 2014-10-08 15:48:48 +0200 | [diff] [blame] | 93 | |
| 94 | return res.Network, err |
Jamie Hannaford | b0d9912 | 2014-09-25 10:49:14 +0200 | [diff] [blame] | 95 | } |
| 96 | |
Jamie Hannaford | 5a53190 | 2014-09-26 15:03:31 +0200 | [diff] [blame] | 97 | // ExtractUpdate decorates a UpdateResult struct returned from a |
| 98 | // networks.Update() function with extended attributes. |
Jamie Hannaford | b0d9912 | 2014-09-25 10:49:14 +0200 | [diff] [blame] | 99 | func ExtractUpdate(r networks.UpdateResult) (*NetworkExtAttrs, error) { |
| 100 | if r.Err != nil { |
| 101 | return nil, r.Err |
| 102 | } |
Jamie Hannaford | c1f3649 | 2014-10-08 15:48:48 +0200 | [diff] [blame] | 103 | |
Jamie Hannaford | b0d9912 | 2014-09-25 10:49:14 +0200 | [diff] [blame] | 104 | var res struct { |
| 105 | Network *NetworkExtAttrs `json:"network"` |
| 106 | } |
Jamie Hannaford | c1f3649 | 2014-10-08 15:48:48 +0200 | [diff] [blame] | 107 | |
Jon Perritt | b0b9c0d | 2015-03-10 17:24:08 -0600 | [diff] [blame] | 108 | err := mapstructure.WeakDecode(r.Body, &res) |
Jamie Hannaford | c1f3649 | 2014-10-08 15:48:48 +0200 | [diff] [blame] | 109 | |
| 110 | return res.Network, err |
Jamie Hannaford | b0d9912 | 2014-09-25 10:49:14 +0200 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | // ExtractList accepts a Page struct, specifically a NetworkPage struct, and |
| 114 | // extracts the elements into a slice of NetworkExtAttrs structs. In other |
| 115 | // words, a generic collection is mapped into a relevant slice. |
| 116 | func ExtractList(page pagination.Page) ([]NetworkExtAttrs, error) { |
| 117 | var resp struct { |
| 118 | Networks []NetworkExtAttrs `mapstructure:"networks" json:"networks"` |
| 119 | } |
| 120 | |
Jon Perritt | b0b9c0d | 2015-03-10 17:24:08 -0600 | [diff] [blame] | 121 | err := mapstructure.WeakDecode(page.(networks.NetworkPage).Body, &resp) |
Jamie Hannaford | b0d9912 | 2014-09-25 10:49:14 +0200 | [diff] [blame] | 122 | |
Jamie Hannaford | c1f3649 | 2014-10-08 15:48:48 +0200 | [diff] [blame] | 123 | return resp.Networks, err |
Jamie Hannaford | b0d9912 | 2014-09-25 10:49:14 +0200 | [diff] [blame] | 124 | } |