Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 1 | package stacks |
| 2 | |
| 3 | import ( |
Jon Perritt | 3d381d5 | 2015-02-09 13:04:48 -0700 | [diff] [blame] | 4 | "encoding/json" |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 5 | "time" |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 6 | |
Krzysztof Szukiełojć | 3f41d08 | 2017-05-07 14:43:06 +0200 | [diff] [blame] | 7 | "gerrit.mcp.mirantis.net/debian/gophercloud.git" |
Krzysztof Szukiełojć | 24a29ce | 2017-05-07 14:24:02 +0200 | [diff] [blame] | 8 | "gerrit.mcp.mirantis.net/debian/gophercloud.git/pagination" |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 9 | ) |
| 10 | |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 11 | // CreatedStack represents the object extracted from a Create operation. |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame] | 12 | type CreatedStack struct { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 13 | ID string `json:"id"` |
| 14 | Links []gophercloud.Link `json:"links"` |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 15 | } |
| 16 | |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 17 | // CreateResult represents the result of a Create operation. |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 18 | type CreateResult struct { |
| 19 | gophercloud.Result |
| 20 | } |
| 21 | |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 22 | // Extract returns a pointer to a CreatedStack object and is called after a |
| 23 | // Create operation. |
Jon Perritt | 22325f4 | 2015-01-29 14:48:18 -0700 | [diff] [blame] | 24 | func (r CreateResult) Extract() (*CreatedStack, error) { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 25 | var s struct { |
| 26 | CreatedStack *CreatedStack `json:"stack"` |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 27 | } |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 28 | err := r.ExtractInto(&s) |
| 29 | return s.CreatedStack, err |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 30 | } |
| 31 | |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 32 | // AdoptResult represents the result of an Adopt operation. AdoptResult has the |
| 33 | // same form as CreateResult. |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 34 | type AdoptResult struct { |
Jon Perritt | 9741dd9 | 2015-02-04 12:05:47 -0700 | [diff] [blame] | 35 | CreateResult |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | // StackPage is a pagination.Pager that is returned from a call to the List function. |
| 39 | type StackPage struct { |
| 40 | pagination.SinglePageBase |
| 41 | } |
| 42 | |
| 43 | // IsEmpty returns true if a ListResult contains no Stacks. |
| 44 | func (r StackPage) IsEmpty() (bool, error) { |
| 45 | stacks, err := ExtractStacks(r) |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 46 | return len(stacks) == 0, err |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 49 | // ListedStack represents an element in the slice extracted from a List operation. |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame] | 50 | type ListedStack struct { |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 51 | CreationTime time.Time `json:"-"` |
| 52 | Description string `json:"description"` |
| 53 | ID string `json:"id"` |
| 54 | Links []gophercloud.Link `json:"links"` |
| 55 | Name string `json:"stack_name"` |
| 56 | Status string `json:"stack_status"` |
| 57 | StatusReason string `json:"stack_status_reason"` |
| 58 | Tags []string `json:"tags"` |
| 59 | UpdatedTime time.Time `json:"-"` |
| 60 | } |
| 61 | |
| 62 | func (r *ListedStack) UnmarshalJSON(b []byte) error { |
| 63 | type tmp ListedStack |
| 64 | var s struct { |
| 65 | tmp |
| 66 | CreationTime gophercloud.JSONRFC3339NoZ `json:"creation_time"` |
| 67 | UpdatedTime gophercloud.JSONRFC3339NoZ `json:"updated_time"` |
| 68 | } |
| 69 | err := json.Unmarshal(b, &s) |
| 70 | if err != nil { |
| 71 | return err |
| 72 | } |
| 73 | *r = ListedStack(s.tmp) |
| 74 | |
| 75 | r.CreationTime = time.Time(s.CreationTime) |
| 76 | r.UpdatedTime = time.Time(s.UpdatedTime) |
| 77 | |
| 78 | return nil |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 81 | // ExtractStacks extracts and returns a slice of ListedStack. It is used while iterating |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 82 | // over a stacks.List call. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 83 | func ExtractStacks(r pagination.Page) ([]ListedStack, error) { |
| 84 | var s struct { |
| 85 | ListedStacks []ListedStack `json:"stacks"` |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 86 | } |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 87 | err := (r.(StackPage)).ExtractInto(&s) |
| 88 | return s.ListedStacks, err |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 91 | // RetrievedStack represents the object extracted from a Get operation. |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame] | 92 | type RetrievedStack struct { |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 93 | Capabilities []interface{} `json:"capabilities"` |
| 94 | CreationTime time.Time `json:"-"` |
| 95 | Description string `json:"description"` |
| 96 | DisableRollback bool `json:"disable_rollback"` |
| 97 | ID string `json:"id"` |
| 98 | Links []gophercloud.Link `json:"links"` |
| 99 | NotificationTopics []interface{} `json:"notification_topics"` |
| 100 | Outputs []map[string]interface{} `json:"outputs"` |
| 101 | Parameters map[string]string `json:"parameters"` |
| 102 | Name string `json:"stack_name"` |
| 103 | Status string `json:"stack_status"` |
| 104 | StatusReason string `json:"stack_status_reason"` |
| 105 | Tags []string `json:"tags"` |
| 106 | TemplateDescription string `json:"template_description"` |
| 107 | Timeout int `json:"timeout_mins"` |
| 108 | UpdatedTime time.Time `json:"-"` |
| 109 | } |
| 110 | |
| 111 | func (r *RetrievedStack) UnmarshalJSON(b []byte) error { |
| 112 | type tmp RetrievedStack |
| 113 | var s struct { |
| 114 | tmp |
| 115 | CreationTime gophercloud.JSONRFC3339NoZ `json:"creation_time"` |
| 116 | UpdatedTime gophercloud.JSONRFC3339NoZ `json:"updated_time"` |
| 117 | } |
| 118 | err := json.Unmarshal(b, &s) |
| 119 | if err != nil { |
| 120 | return err |
| 121 | } |
| 122 | *r = RetrievedStack(s.tmp) |
| 123 | |
| 124 | r.CreationTime = time.Time(s.CreationTime) |
| 125 | r.UpdatedTime = time.Time(s.UpdatedTime) |
| 126 | |
| 127 | return nil |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 130 | // GetResult represents the result of a Get operation. |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 131 | type GetResult struct { |
| 132 | gophercloud.Result |
| 133 | } |
| 134 | |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 135 | // Extract returns a pointer to a RetrievedStack object and is called after a |
| 136 | // Get operation. |
Jon Perritt | 22325f4 | 2015-01-29 14:48:18 -0700 | [diff] [blame] | 137 | func (r GetResult) Extract() (*RetrievedStack, error) { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 138 | var s struct { |
| 139 | Stack *RetrievedStack `json:"stack"` |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 140 | } |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 141 | err := r.ExtractInto(&s) |
| 142 | return s.Stack, err |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 145 | // UpdateResult represents the result of a Update operation. |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 146 | type UpdateResult struct { |
| 147 | gophercloud.ErrResult |
| 148 | } |
| 149 | |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 150 | // DeleteResult represents the result of a Delete operation. |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 151 | type DeleteResult struct { |
| 152 | gophercloud.ErrResult |
| 153 | } |
| 154 | |
Jon Perritt | 37f9774 | 2015-02-04 18:55:05 -0700 | [diff] [blame] | 155 | // PreviewedStack represents the result of a Preview operation. |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame] | 156 | type PreviewedStack struct { |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 157 | Capabilities []interface{} `json:"capabilities"` |
| 158 | CreationTime time.Time `json:"-"` |
| 159 | Description string `json:"description"` |
| 160 | DisableRollback bool `json:"disable_rollback"` |
| 161 | ID string `json:"id"` |
| 162 | Links []gophercloud.Link `json:"links"` |
| 163 | Name string `json:"stack_name"` |
| 164 | NotificationTopics []interface{} `json:"notification_topics"` |
| 165 | Parameters map[string]string `json:"parameters"` |
| 166 | Resources []interface{} `json:"resources"` |
| 167 | TemplateDescription string `json:"template_description"` |
| 168 | Timeout int `json:"timeout_mins"` |
| 169 | UpdatedTime time.Time `json:"-"` |
| 170 | } |
| 171 | |
| 172 | func (r *PreviewedStack) UnmarshalJSON(b []byte) error { |
| 173 | type tmp PreviewedStack |
| 174 | var s struct { |
| 175 | tmp |
| 176 | CreationTime gophercloud.JSONRFC3339NoZ `json:"creation_time"` |
| 177 | UpdatedTime gophercloud.JSONRFC3339NoZ `json:"updated_time"` |
| 178 | } |
| 179 | err := json.Unmarshal(b, &s) |
| 180 | if err != nil { |
| 181 | return err |
| 182 | } |
| 183 | *r = PreviewedStack(s.tmp) |
| 184 | |
| 185 | r.CreationTime = time.Time(s.CreationTime) |
| 186 | r.UpdatedTime = time.Time(s.UpdatedTime) |
| 187 | |
| 188 | return nil |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Jon Perritt | 37f9774 | 2015-02-04 18:55:05 -0700 | [diff] [blame] | 191 | // PreviewResult represents the result of a Preview operation. |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 192 | type PreviewResult struct { |
| 193 | gophercloud.Result |
| 194 | } |
| 195 | |
Jon Perritt | 37f9774 | 2015-02-04 18:55:05 -0700 | [diff] [blame] | 196 | // Extract returns a pointer to a PreviewedStack object and is called after a |
| 197 | // Preview operation. |
Jon Perritt | 22325f4 | 2015-01-29 14:48:18 -0700 | [diff] [blame] | 198 | func (r PreviewResult) Extract() (*PreviewedStack, error) { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 199 | var s struct { |
| 200 | PreviewedStack *PreviewedStack `json:"stack"` |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 201 | } |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 202 | err := r.ExtractInto(&s) |
| 203 | return s.PreviewedStack, err |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Jon Perritt | 9209df4 | 2015-02-05 12:55:33 -0700 | [diff] [blame] | 206 | // AbandonedStack represents the result of an Abandon operation. |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame] | 207 | type AbandonedStack struct { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 208 | Status string `json:"status"` |
| 209 | Name string `json:"name"` |
| 210 | Template map[string]interface{} `json:"template"` |
| 211 | Action string `json:"action"` |
| 212 | ID string `json:"id"` |
| 213 | Resources map[string]interface{} `json:"resources"` |
| 214 | Files map[string]string `json:"files"` |
| 215 | StackUserProjectID string `json:"stack_user_project_id"` |
| 216 | ProjectID string `json:"project_id"` |
| 217 | Environment map[string]interface{} `json:"environment"` |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Jon Perritt | 9209df4 | 2015-02-05 12:55:33 -0700 | [diff] [blame] | 220 | // AbandonResult represents the result of an Abandon operation. |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 221 | type AbandonResult struct { |
| 222 | gophercloud.Result |
| 223 | } |
Jon Perritt | 9209df4 | 2015-02-05 12:55:33 -0700 | [diff] [blame] | 224 | |
| 225 | // Extract returns a pointer to an AbandonedStack object and is called after an |
| 226 | // Abandon operation. |
| 227 | func (r AbandonResult) Extract() (*AbandonedStack, error) { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 228 | var s *AbandonedStack |
| 229 | err := r.ExtractInto(&s) |
| 230 | return s, err |
Jon Perritt | 9209df4 | 2015-02-05 12:55:33 -0700 | [diff] [blame] | 231 | } |
Jon Perritt | 3d381d5 | 2015-02-09 13:04:48 -0700 | [diff] [blame] | 232 | |
Jon Perritt | db0d26a | 2015-02-09 13:06:16 -0700 | [diff] [blame] | 233 | // String converts an AbandonResult to a string. This is useful to when passing |
| 234 | // the result of an Abandon operation to an AdoptOpts AdoptStackData field. |
Jon Perritt | 3d381d5 | 2015-02-09 13:04:48 -0700 | [diff] [blame] | 235 | func (r AbandonResult) String() (string, error) { |
| 236 | out, err := json.Marshal(r) |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 237 | return string(out), err |
Jon Perritt | 3d381d5 | 2015-02-09 13:04:48 -0700 | [diff] [blame] | 238 | } |