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