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