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