blob: 67f534d8e12688c1802325659f016ca7491e9393 [file] [log] [blame]
Jamie Hannaforda7f671a2014-09-11 10:25:08 +02001package networks
2
Jamie Hannaford4721abc2014-09-16 16:29:04 +02003import (
Jon Perritt27249f42016-02-18 10:35:59 -06004 "github.com/gophercloud/gophercloud"
Krzysztof Szukiełojć24a29ce2017-05-07 14:24:02 +02005 "gerrit.mcp.mirantis.net/debian/gophercloud.git/pagination"
Jamie Hannaford4721abc2014-09-16 16:29:04 +02006)
7
Jamie Hannafordd9036422014-09-23 17:50:24 +02008type commonResult struct {
Ash Wilsonf548aad2014-10-20 08:35:34 -04009 gophercloud.Result
Jamie Hannafordd9036422014-09-23 17:50:24 +020010}
11
Jamie Hannafordf3114832014-09-24 11:00:43 +020012// Extract is a function that accepts a result and extracts a network resource.
Jamie Hannafordd9036422014-09-23 17:50:24 +020013func (r commonResult) Extract() (*Network, error) {
Jon Perritt3c166472016-02-25 03:07:41 -060014 var s struct {
Jamie Hannafordd9036422014-09-23 17:50:24 +020015 Network *Network `json:"network"`
16 }
Jon Perritt3c166472016-02-25 03:07:41 -060017 err := r.ExtractInto(&s)
18 return s.Network, err
Jamie Hannafordd9036422014-09-23 17:50:24 +020019}
20
Jamie Hannafordf3114832014-09-24 11:00:43 +020021// CreateResult represents the result of a create operation.
Jamie Hannafordd9036422014-09-23 17:50:24 +020022type CreateResult struct {
23 commonResult
24}
25
Jamie Hannafordf3114832014-09-24 11:00:43 +020026// GetResult represents the result of a get operation.
Jamie Hannafordd9036422014-09-23 17:50:24 +020027type GetResult struct {
28 commonResult
29}
30
Jamie Hannafordf3114832014-09-24 11:00:43 +020031// UpdateResult represents the result of an update operation.
Jamie Hannafordd9036422014-09-23 17:50:24 +020032type UpdateResult struct {
33 commonResult
34}
35
Jamie Hannafordf3114832014-09-24 11:00:43 +020036// DeleteResult represents the result of a delete operation.
Jamie Hannafordd6c81b22014-10-27 14:02:53 +010037type DeleteResult struct {
Jon Perrittba2395e2014-10-27 15:23:21 -050038 gophercloud.ErrResult
Jamie Hannafordd6c81b22014-10-27 14:02:53 +010039}
Jamie Hannafordd9036422014-09-23 17:50:24 +020040
Jamie Hannaford686c4962014-09-23 10:46:20 +020041// Network represents, well, a network.
Jamie Hannaford79475052014-09-15 17:08:06 +020042type Network struct {
Jamie Hannaford965ae702014-09-22 14:58:19 +020043 // UUID for the network
Jon Perritt3c166472016-02-25 03:07:41 -060044 ID string `json:"id"`
Jamie Hannaford9823bb62014-09-26 17:06:36 +020045
Jamie Hannaford965ae702014-09-22 14:58:19 +020046 // Human-readable name for the network. Might not be unique.
Jon Perritt3c166472016-02-25 03:07:41 -060047 Name string `json:"name"`
Jamie Hannaford9823bb62014-09-26 17:06:36 +020048
Jamie Hannaford965ae702014-09-22 14:58:19 +020049 // The administrative state of network. If false (down), the network does not forward packets.
Jon Perritt3c166472016-02-25 03:07:41 -060050 AdminStateUp bool `json:"admin_state_up"`
Jamie Hannaford9823bb62014-09-26 17:06:36 +020051
Jamie Hannaford965ae702014-09-22 14:58:19 +020052 // Indicates whether network is currently operational. Possible values include
53 // `ACTIVE', `DOWN', `BUILD', or `ERROR'. Plug-ins might define additional values.
Jon Perritt3c166472016-02-25 03:07:41 -060054 Status string `json:"status"`
Jamie Hannaford9823bb62014-09-26 17:06:36 +020055
Jamie Hannaford965ae702014-09-22 14:58:19 +020056 // Subnets associated with this network.
Jon Perritt3c166472016-02-25 03:07:41 -060057 Subnets []string `json:"subnets"`
Jamie Hannaford9823bb62014-09-26 17:06:36 +020058
Jamie Hannaford965ae702014-09-22 14:58:19 +020059 // Owner of network. Only admin users can specify a tenant_id other than its own.
Jon Perritt3c166472016-02-25 03:07:41 -060060 TenantID string `json:"tenant_id"`
Jamie Hannaford9823bb62014-09-26 17:06:36 +020061
Jamie Hannaford965ae702014-09-22 14:58:19 +020062 // Specifies whether the network resource can be accessed by any tenant or not.
Jon Perritt3c166472016-02-25 03:07:41 -060063 Shared bool `json:"shared"`
Jamie Hannaforda7f671a2014-09-11 10:25:08 +020064}
Jamie Hannaford4721abc2014-09-16 16:29:04 +020065
Jamie Hannaford686c4962014-09-23 10:46:20 +020066// NetworkPage is the page returned by a pager when traversing over a
67// collection of networks.
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020068type NetworkPage struct {
69 pagination.LinkedPageBase
70}
71
Jamie Hannaford686c4962014-09-23 10:46:20 +020072// NextPageURL is invoked when a paginated collection of networks has reached
73// the end of a page and the pager seeks to traverse over a new one. In order
74// to do this, it needs to construct the next page's URL.
Jon Perritt31b66462016-02-25 22:25:30 -060075func (r NetworkPage) NextPageURL() (string, error) {
Jon Perritt3c166472016-02-25 03:07:41 -060076 var s struct {
77 Links []gophercloud.Link `json:"networks_links"`
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020078 }
Jon Perritt31b66462016-02-25 22:25:30 -060079 err := r.ExtractInto(&s)
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020080 if err != nil {
81 return "", err
82 }
Jon Perritt3c166472016-02-25 03:07:41 -060083 return gophercloud.ExtractNextURL(s.Links)
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020084}
85
Jamie Hannaford686c4962014-09-23 10:46:20 +020086// IsEmpty checks whether a NetworkPage struct is empty.
Jon Perritt31b66462016-02-25 22:25:30 -060087func (r NetworkPage) IsEmpty() (bool, error) {
88 is, err := ExtractNetworks(r)
Jon Perritt3c166472016-02-25 03:07:41 -060089 return len(is) == 0, err
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020090}
91
Jamie Hannaford686c4962014-09-23 10:46:20 +020092// ExtractNetworks accepts a Page struct, specifically a NetworkPage struct,
93// and extracts the elements into a slice of Network structs. In other words,
94// a generic collection is mapped into a relevant slice.
Jon Perritt31b66462016-02-25 22:25:30 -060095func ExtractNetworks(r pagination.Page) ([]Network, error) {
Jon Perritt3c166472016-02-25 03:07:41 -060096 var s struct {
97 Networks []Network `json:"networks"`
Jamie Hannaford4721abc2014-09-16 16:29:04 +020098 }
Jon Perritt31b66462016-02-25 22:25:30 -060099 err := (r.(NetworkPage)).ExtractInto(&s)
Jon Perritt3c166472016-02-25 03:07:41 -0600100 return s.Networks, err
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200101}