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