Jamie Hannaford | b0d9912 | 2014-09-25 10:49:14 +0200 | [diff] [blame] | 1 | package provider |
| 2 | |
| 3 | import ( |
Joe Topjian | d5be3fe | 2016-08-29 09:41:13 -0600 | [diff] [blame] | 4 | "encoding/json" |
| 5 | "strconv" |
| 6 | |
Krzysztof Szukiełojć | 24a29ce | 2017-05-07 14:24:02 +0200 | [diff] [blame^] | 7 | "gerrit.mcp.mirantis.net/debian/gophercloud.git/openstack/networking/v2/networks" |
| 8 | "gerrit.mcp.mirantis.net/debian/gophercloud.git/pagination" |
Jamie Hannaford | b0d9912 | 2014-09-25 10:49:14 +0200 | [diff] [blame] | 9 | ) |
| 10 | |
| 11 | // NetworkExtAttrs represents an extended form of a Network with additional fields. |
| 12 | type NetworkExtAttrs struct { |
| 13 | // UUID for the network |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 14 | ID string `json:"id"` |
Jamie Hannaford | b0d9912 | 2014-09-25 10:49:14 +0200 | [diff] [blame] | 15 | |
| 16 | // Human-readable name for the network. Might not be unique. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 17 | Name string `json:"name"` |
Jamie Hannaford | b0d9912 | 2014-09-25 10:49:14 +0200 | [diff] [blame] | 18 | |
| 19 | // The administrative state of network. If false (down), the network does not forward packets. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 20 | AdminStateUp bool `json:"admin_state_up"` |
Jamie Hannaford | b0d9912 | 2014-09-25 10:49:14 +0200 | [diff] [blame] | 21 | |
| 22 | // Indicates whether network is currently operational. Possible values include |
| 23 | // `ACTIVE', `DOWN', `BUILD', or `ERROR'. Plug-ins might define additional values. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 24 | Status string `json:"status"` |
Jamie Hannaford | b0d9912 | 2014-09-25 10:49:14 +0200 | [diff] [blame] | 25 | |
| 26 | // Subnets associated with this network. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 27 | Subnets []string `json:"subnets"` |
Jamie Hannaford | b0d9912 | 2014-09-25 10:49:14 +0200 | [diff] [blame] | 28 | |
| 29 | // Owner of network. Only admin users can specify a tenant_id other than its own. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 30 | TenantID string `json:"tenant_id"` |
Jamie Hannaford | b0d9912 | 2014-09-25 10:49:14 +0200 | [diff] [blame] | 31 | |
| 32 | // Specifies whether the network resource can be accessed by any tenant or not. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 33 | Shared bool `json:"shared"` |
Jamie Hannaford | b0d9912 | 2014-09-25 10:49:14 +0200 | [diff] [blame] | 34 | |
| 35 | // Specifies the nature of the physical network mapped to this network |
| 36 | // resource. Examples are flat, vlan, or gre. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 37 | NetworkType string `json:"provider:network_type"` |
Jamie Hannaford | b0d9912 | 2014-09-25 10:49:14 +0200 | [diff] [blame] | 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. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 45 | PhysicalNetwork string `json:"provider:physical_network"` |
Jamie Hannaford | b0d9912 | 2014-09-25 10:49:14 +0200 | [diff] [blame] | 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. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 51 | SegmentationID string `json:"provider:segmentation_id"` |
Jamie Hannaford | b0d9912 | 2014-09-25 10:49:14 +0200 | [diff] [blame] | 52 | } |
| 53 | |
Joe Topjian | d5be3fe | 2016-08-29 09:41:13 -0600 | [diff] [blame] | 54 | func (n *NetworkExtAttrs) UnmarshalJSON(b []byte) error { |
| 55 | type tmp NetworkExtAttrs |
| 56 | var networkExtAttrs *struct { |
| 57 | tmp |
| 58 | SegmentationID interface{} `json:"provider:segmentation_id"` |
| 59 | } |
| 60 | |
| 61 | if err := json.Unmarshal(b, &networkExtAttrs); err != nil { |
| 62 | return err |
| 63 | } |
| 64 | |
| 65 | *n = NetworkExtAttrs(networkExtAttrs.tmp) |
| 66 | |
| 67 | switch t := networkExtAttrs.SegmentationID.(type) { |
| 68 | case float64: |
| 69 | n.SegmentationID = strconv.FormatFloat(t, 'f', -1, 64) |
| 70 | case string: |
| 71 | n.SegmentationID = string(t) |
| 72 | } |
| 73 | |
| 74 | return nil |
| 75 | } |
| 76 | |
Jamie Hannaford | 5a53190 | 2014-09-26 15:03:31 +0200 | [diff] [blame] | 77 | // ExtractGet decorates a GetResult struct returned from a networks.Get() |
| 78 | // function with extended attributes. |
Jamie Hannaford | b0d9912 | 2014-09-25 10:49:14 +0200 | [diff] [blame] | 79 | func ExtractGet(r networks.GetResult) (*NetworkExtAttrs, error) { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 80 | var s struct { |
Jamie Hannaford | b0d9912 | 2014-09-25 10:49:14 +0200 | [diff] [blame] | 81 | Network *NetworkExtAttrs `json:"network"` |
| 82 | } |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 83 | err := r.ExtractInto(&s) |
| 84 | return s.Network, err |
Jamie Hannaford | b0d9912 | 2014-09-25 10:49:14 +0200 | [diff] [blame] | 85 | } |
| 86 | |
Jamie Hannaford | c7c49a7 | 2014-10-06 15:49:53 +0200 | [diff] [blame] | 87 | // ExtractCreate decorates a CreateResult struct returned from a networks.Create() |
Jamie Hannaford | 5a53190 | 2014-09-26 15:03:31 +0200 | [diff] [blame] | 88 | // function with extended attributes. |
Jamie Hannaford | b0d9912 | 2014-09-25 10:49:14 +0200 | [diff] [blame] | 89 | func ExtractCreate(r networks.CreateResult) (*NetworkExtAttrs, error) { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 90 | var s struct { |
Jamie Hannaford | b0d9912 | 2014-09-25 10:49:14 +0200 | [diff] [blame] | 91 | Network *NetworkExtAttrs `json:"network"` |
| 92 | } |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 93 | err := r.ExtractInto(&s) |
| 94 | return s.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) { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 100 | var s struct { |
Jamie Hannaford | b0d9912 | 2014-09-25 10:49:14 +0200 | [diff] [blame] | 101 | Network *NetworkExtAttrs `json:"network"` |
| 102 | } |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 103 | err := r.ExtractInto(&s) |
| 104 | return s.Network, err |
Jamie Hannaford | b0d9912 | 2014-09-25 10:49:14 +0200 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | // ExtractList accepts a Page struct, specifically a NetworkPage struct, and |
| 108 | // extracts the elements into a slice of NetworkExtAttrs structs. In other |
| 109 | // words, a generic collection is mapped into a relevant slice. |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 110 | func ExtractList(r pagination.Page) ([]NetworkExtAttrs, error) { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 111 | var s struct { |
| 112 | Networks []NetworkExtAttrs `json:"networks" json:"networks"` |
Jamie Hannaford | b0d9912 | 2014-09-25 10:49:14 +0200 | [diff] [blame] | 113 | } |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 114 | err := (r.(networks.NetworkPage)).ExtractInto(&s) |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 115 | return s.Networks, err |
Jamie Hannaford | b0d9912 | 2014-09-25 10:49:14 +0200 | [diff] [blame] | 116 | } |