ehdou | 10f1f85 | 2016-10-14 20:58:23 +0300 | [diff] [blame] | 1 | package sharenetworks |
| 2 | |
ehdou | 0794333 | 2016-10-24 21:21:58 +0300 | [diff] [blame] | 3 | import ( |
| 4 | "net/url" |
| 5 | "strconv" |
| 6 | |
| 7 | "github.com/gophercloud/gophercloud" |
| 8 | "github.com/gophercloud/gophercloud/pagination" |
| 9 | ) |
ehdou | 10f1f85 | 2016-10-14 20:58:23 +0300 | [diff] [blame] | 10 | |
| 11 | // ShareNetwork contains all the information associated with an OpenStack |
| 12 | // ShareNetwork. |
| 13 | type ShareNetwork struct { |
| 14 | // The Share Network ID |
| 15 | ID string `json:"id"` |
| 16 | // The UUID of the project where the share network was created |
| 17 | ProjectID string `json:"project_id"` |
| 18 | // The neutron network ID |
| 19 | NeutronNetID string `json:"neutron_net_id"` |
| 20 | // The neutron subnet ID |
| 21 | NeutronSubnetID string `json:"neutron_subnet_id"` |
| 22 | // The nova network ID |
| 23 | NovaNetID string `json:"nova_net_id"` |
| 24 | // The network type. A valid value is VLAN, VXLAN, GRE or flat |
| 25 | NetworkType string `json:"network_type"` |
| 26 | // The segmentation ID |
| 27 | SegmentationID int `json:"segmentation_id"` |
| 28 | // The IP block from which to allocate the network, in CIDR notation |
| 29 | CIDR string `json:"cidr"` |
| 30 | // The IP version of the network. A valid value is 4 or 6 |
| 31 | IPVersion int `json:"ip_version"` |
| 32 | // The Share Network name |
| 33 | Name string `json:"name"` |
| 34 | // The Share Network description |
| 35 | Description string `json:"description"` |
| 36 | // The date and time stamp when the Share Network was created |
ehdou | 0794333 | 2016-10-24 21:21:58 +0300 | [diff] [blame] | 37 | CreatedAt gophercloud.JSONRFC3339MilliNoZ `json:"created_at"` |
ehdou | 10f1f85 | 2016-10-14 20:58:23 +0300 | [diff] [blame] | 38 | // The date and time stamp when the Share Network was updated |
ehdou | 0794333 | 2016-10-24 21:21:58 +0300 | [diff] [blame] | 39 | UpdatedAt gophercloud.JSONRFC3339MilliNoZ `json:"updated_at"` |
ehdou | 10f1f85 | 2016-10-14 20:58:23 +0300 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | type commonResult struct { |
| 43 | gophercloud.Result |
| 44 | } |
| 45 | |
ehdou | 0794333 | 2016-10-24 21:21:58 +0300 | [diff] [blame] | 46 | // ShareNetworkPage is a pagination.pager that is returned from a call to the List function. |
| 47 | type ShareNetworkPage struct { |
| 48 | pagination.MarkerPageBase |
| 49 | } |
| 50 | |
| 51 | // NextPageURL generates the URL for the page of results after this one. |
| 52 | func (r ShareNetworkPage) NextPageURL() (string, error) { |
| 53 | currentURL := r.URL |
| 54 | mark, err := r.Owner.LastMarker() |
| 55 | if err != nil { |
| 56 | return "", err |
| 57 | } |
| 58 | |
| 59 | q := currentURL.Query() |
| 60 | q.Set("offset", mark) |
| 61 | currentURL.RawQuery = q.Encode() |
| 62 | return currentURL.String(), nil |
| 63 | } |
| 64 | |
| 65 | // LastMarker returns the last offset in a ListResult. |
| 66 | func (r ShareNetworkPage) LastMarker() (string, error) { |
| 67 | maxInt := strconv.Itoa(int(^uint(0) >> 1)) |
| 68 | shareNetworks, err := ExtractShareNetworks(r) |
| 69 | if err != nil { |
| 70 | return maxInt, err |
| 71 | } |
| 72 | if len(shareNetworks) == 0 { |
| 73 | return maxInt, nil |
| 74 | } |
| 75 | |
| 76 | u, err := url.Parse(r.URL.String()) |
| 77 | if err != nil { |
| 78 | return maxInt, err |
| 79 | } |
| 80 | queryParams := u.Query() |
| 81 | offset := queryParams.Get("offset") |
| 82 | limit := queryParams.Get("limit") |
| 83 | |
| 84 | // Limit is not present, only one page required |
| 85 | if limit == "" { |
| 86 | return maxInt, nil |
| 87 | } |
| 88 | |
| 89 | iOffset := 0 |
| 90 | if offset != "" { |
| 91 | iOffset, err = strconv.Atoi(offset) |
| 92 | if err != nil { |
| 93 | return maxInt, err |
| 94 | } |
| 95 | } |
| 96 | iLimit, err := strconv.Atoi(limit) |
| 97 | if err != nil { |
| 98 | return maxInt, err |
| 99 | } |
| 100 | iOffset = iOffset + iLimit |
| 101 | offset = strconv.Itoa(iOffset) |
| 102 | |
| 103 | return offset, nil |
| 104 | } |
| 105 | |
| 106 | // IsEmpty satisifies the IsEmpty method of the Page interface |
| 107 | func (r ShareNetworkPage) IsEmpty() (bool, error) { |
| 108 | shareNetworks, err := ExtractShareNetworks(r) |
| 109 | return len(shareNetworks) == 0, err |
| 110 | } |
| 111 | |
| 112 | // ExtractShareNetworks extracts and returns ShareNetworks. It is used while |
| 113 | // iterating over a sharenetworks.List call. |
| 114 | func ExtractShareNetworks(r pagination.Page) ([]ShareNetwork, error) { |
| 115 | var s struct { |
| 116 | ShareNetworks []ShareNetwork `json:"share_networks"` |
| 117 | } |
| 118 | err := (r.(ShareNetworkPage)).ExtractInto(&s) |
| 119 | return s.ShareNetworks, err |
| 120 | } |
| 121 | |
ehdou | 10f1f85 | 2016-10-14 20:58:23 +0300 | [diff] [blame] | 122 | // Extract will get the ShareNetwork object out of the commonResult object. |
| 123 | func (r commonResult) Extract() (*ShareNetwork, error) { |
| 124 | var s struct { |
| 125 | ShareNetwork *ShareNetwork `json:"share_network"` |
| 126 | } |
| 127 | err := r.ExtractInto(&s) |
| 128 | return s.ShareNetwork, err |
| 129 | } |
| 130 | |
| 131 | // CreateResult contains the response body and error from a Create request. |
| 132 | type CreateResult struct { |
| 133 | commonResult |
| 134 | } |
ehdou | 5368725 | 2016-10-14 22:10:13 +0300 | [diff] [blame] | 135 | |
| 136 | // DeleteResult contains the response body and error from a Delete request. |
| 137 | type DeleteResult struct { |
| 138 | gophercloud.ErrResult |
| 139 | } |
ehdou | a088cec | 2016-10-25 21:02:05 +0300 | [diff] [blame] | 140 | |
| 141 | // GetResult contains the response body and error from a Get request. |
| 142 | type GetResult struct { |
| 143 | commonResult |
| 144 | } |
ehdou | fd6d86c | 2016-11-03 21:35:45 +0200 | [diff] [blame^] | 145 | |
| 146 | // UpdateResult contains the response body and error from an Update request. |
| 147 | type UpdateResult struct { |
| 148 | commonResult |
| 149 | } |