blob: e00eff2866e9761ae4d4f619f3af3553045bc1c9 [file] [log] [blame]
Jon Perritt35e27e42014-12-05 11:10:46 -07001package stacks
2
3import (
Jon Perritt3d381d52015-02-09 13:04:48 -07004 "encoding/json"
jrperritt98d01622017-01-12 14:24:42 -06005 "time"
Jon Perritt35e27e42014-12-05 11:10:46 -07006
Krzysztof Szukiełojć3f41d082017-05-07 14:43:06 +02007 "gerrit.mcp.mirantis.net/debian/gophercloud.git"
Krzysztof Szukiełojć24a29ce2017-05-07 14:24:02 +02008 "gerrit.mcp.mirantis.net/debian/gophercloud.git/pagination"
Jon Perritt35e27e42014-12-05 11:10:46 -07009)
10
Jon Perritt7726e492015-02-04 17:54:28 -070011// CreatedStack represents the object extracted from a Create operation.
Jon Perritt01972e22015-01-28 10:30:45 -070012type CreatedStack struct {
Jon Perritt3c166472016-02-25 03:07:41 -060013 ID string `json:"id"`
14 Links []gophercloud.Link `json:"links"`
Jon Perritt35e27e42014-12-05 11:10:46 -070015}
16
Jon Perritt7726e492015-02-04 17:54:28 -070017// CreateResult represents the result of a Create operation.
Jon Perritt35e27e42014-12-05 11:10:46 -070018type CreateResult struct {
19 gophercloud.Result
20}
21
Jon Perritt7726e492015-02-04 17:54:28 -070022// Extract returns a pointer to a CreatedStack object and is called after a
23// Create operation.
Jon Perritt22325f42015-01-29 14:48:18 -070024func (r CreateResult) Extract() (*CreatedStack, error) {
Jon Perritt3c166472016-02-25 03:07:41 -060025 var s struct {
26 CreatedStack *CreatedStack `json:"stack"`
Jon Perritt35e27e42014-12-05 11:10:46 -070027 }
Jon Perritt3c166472016-02-25 03:07:41 -060028 err := r.ExtractInto(&s)
29 return s.CreatedStack, err
Jon Perritt35e27e42014-12-05 11:10:46 -070030}
31
Jon Perritt7726e492015-02-04 17:54:28 -070032// AdoptResult represents the result of an Adopt operation. AdoptResult has the
33// same form as CreateResult.
Jon Perritt35e27e42014-12-05 11:10:46 -070034type AdoptResult struct {
Jon Perritt9741dd92015-02-04 12:05:47 -070035 CreateResult
Jon Perritt35e27e42014-12-05 11:10:46 -070036}
37
38// StackPage is a pagination.Pager that is returned from a call to the List function.
39type StackPage struct {
40 pagination.SinglePageBase
41}
42
43// IsEmpty returns true if a ListResult contains no Stacks.
44func (r StackPage) IsEmpty() (bool, error) {
45 stacks, err := ExtractStacks(r)
Jon Perritt3c166472016-02-25 03:07:41 -060046 return len(stacks) == 0, err
Jon Perritt35e27e42014-12-05 11:10:46 -070047}
48
Jon Perritt7726e492015-02-04 17:54:28 -070049// ListedStack represents an element in the slice extracted from a List operation.
Jon Perritt01972e22015-01-28 10:30:45 -070050type ListedStack struct {
jrperritt98d01622017-01-12 14:24:42 -060051 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
62func (r *ListedStack) UnmarshalJSON(b []byte) error {
63 type tmp ListedStack
64 var s struct {
65 tmp
Ildar Svetlovd2139692020-11-10 15:16:16 +040066 CreationTime string `json:"creation_time"`
67 UpdatedTime string `json:"updated_time"`
jrperritt98d01622017-01-12 14:24:42 -060068 }
Ildar Svetlovd2139692020-11-10 15:16:16 +040069
jrperritt98d01622017-01-12 14:24:42 -060070 err := json.Unmarshal(b, &s)
71 if err != nil {
72 return err
73 }
Ildar Svetlovd2139692020-11-10 15:16:16 +040074
jrperritt98d01622017-01-12 14:24:42 -060075 *r = ListedStack(s.tmp)
76
Ildar Svetlovd2139692020-11-10 15:16:16 +040077 if s.CreationTime != "" {
78 t, err := time.Parse(time.RFC3339, s.CreationTime)
79 if err != nil {
80 t, err = time.Parse(gophercloud.RFC3339NoZ, s.CreationTime)
81 if err != nil {
82 return err
83 }
84 }
85 r.CreationTime = t
86 }
87
88 if s.UpdatedTime != "" {
89 t, err := time.Parse(time.RFC3339, s.UpdatedTime)
90 if err != nil {
91 t, err = time.Parse(gophercloud.RFC3339NoZ, s.UpdatedTime)
92 if err != nil {
93 return err
94 }
95 }
96 r.UpdatedTime = t
97 }
jrperritt98d01622017-01-12 14:24:42 -060098
99 return nil
Jon Perritt35e27e42014-12-05 11:10:46 -0700100}
101
Jon Perritt7726e492015-02-04 17:54:28 -0700102// ExtractStacks extracts and returns a slice of ListedStack. It is used while iterating
Jon Perritt35e27e42014-12-05 11:10:46 -0700103// over a stacks.List call.
Jon Perritt3c166472016-02-25 03:07:41 -0600104func ExtractStacks(r pagination.Page) ([]ListedStack, error) {
105 var s struct {
106 ListedStacks []ListedStack `json:"stacks"`
Jon Perritt35e27e42014-12-05 11:10:46 -0700107 }
Jon Perritt3c166472016-02-25 03:07:41 -0600108 err := (r.(StackPage)).ExtractInto(&s)
109 return s.ListedStacks, err
Jon Perritt35e27e42014-12-05 11:10:46 -0700110}
111
Jon Perritt7726e492015-02-04 17:54:28 -0700112// RetrievedStack represents the object extracted from a Get operation.
Jon Perritt01972e22015-01-28 10:30:45 -0700113type RetrievedStack struct {
jrperritt98d01622017-01-12 14:24:42 -0600114 Capabilities []interface{} `json:"capabilities"`
115 CreationTime time.Time `json:"-"`
116 Description string `json:"description"`
117 DisableRollback bool `json:"disable_rollback"`
118 ID string `json:"id"`
119 Links []gophercloud.Link `json:"links"`
120 NotificationTopics []interface{} `json:"notification_topics"`
121 Outputs []map[string]interface{} `json:"outputs"`
122 Parameters map[string]string `json:"parameters"`
123 Name string `json:"stack_name"`
124 Status string `json:"stack_status"`
125 StatusReason string `json:"stack_status_reason"`
126 Tags []string `json:"tags"`
127 TemplateDescription string `json:"template_description"`
128 Timeout int `json:"timeout_mins"`
129 UpdatedTime time.Time `json:"-"`
130}
131
132func (r *RetrievedStack) UnmarshalJSON(b []byte) error {
133 type tmp RetrievedStack
134 var s struct {
135 tmp
Ildar Svetlovd2139692020-11-10 15:16:16 +0400136 CreationTime string `json:"creation_time"`
137 UpdatedTime string `json:"updated_time"`
jrperritt98d01622017-01-12 14:24:42 -0600138 }
Ildar Svetlovd2139692020-11-10 15:16:16 +0400139
jrperritt98d01622017-01-12 14:24:42 -0600140 err := json.Unmarshal(b, &s)
141 if err != nil {
142 return err
143 }
Ildar Svetlovd2139692020-11-10 15:16:16 +0400144
jrperritt98d01622017-01-12 14:24:42 -0600145 *r = RetrievedStack(s.tmp)
146
Ildar Svetlovd2139692020-11-10 15:16:16 +0400147 if s.CreationTime != "" {
148 t, err := time.Parse(time.RFC3339, s.CreationTime)
149 if err != nil {
150 t, err = time.Parse(gophercloud.RFC3339NoZ, s.CreationTime)
151 if err != nil {
152 return err
153 }
154 }
155 r.CreationTime = t
156 }
157
158 if s.UpdatedTime != "" {
159 t, err := time.Parse(time.RFC3339, s.UpdatedTime)
160 if err != nil {
161 t, err = time.Parse(gophercloud.RFC3339NoZ, s.UpdatedTime)
162 if err != nil {
163 return err
164 }
165 }
166 r.UpdatedTime = t
167 }
jrperritt98d01622017-01-12 14:24:42 -0600168
169 return nil
Jon Perritt35e27e42014-12-05 11:10:46 -0700170}
171
Jon Perritt7726e492015-02-04 17:54:28 -0700172// GetResult represents the result of a Get operation.
Jon Perritt35e27e42014-12-05 11:10:46 -0700173type GetResult struct {
174 gophercloud.Result
175}
176
Jon Perritt7726e492015-02-04 17:54:28 -0700177// Extract returns a pointer to a RetrievedStack object and is called after a
178// Get operation.
Jon Perritt22325f42015-01-29 14:48:18 -0700179func (r GetResult) Extract() (*RetrievedStack, error) {
Jon Perritt3c166472016-02-25 03:07:41 -0600180 var s struct {
181 Stack *RetrievedStack `json:"stack"`
Jon Perritt35e27e42014-12-05 11:10:46 -0700182 }
Jon Perritt3c166472016-02-25 03:07:41 -0600183 err := r.ExtractInto(&s)
184 return s.Stack, err
Jon Perritt35e27e42014-12-05 11:10:46 -0700185}
186
Jon Perritt7726e492015-02-04 17:54:28 -0700187// UpdateResult represents the result of a Update operation.
Jon Perritt35e27e42014-12-05 11:10:46 -0700188type UpdateResult struct {
189 gophercloud.ErrResult
190}
191
Jon Perritt7726e492015-02-04 17:54:28 -0700192// DeleteResult represents the result of a Delete operation.
Jon Perritt35e27e42014-12-05 11:10:46 -0700193type DeleteResult struct {
194 gophercloud.ErrResult
195}
196
Jon Perritt37f97742015-02-04 18:55:05 -0700197// PreviewedStack represents the result of a Preview operation.
Jon Perritt01972e22015-01-28 10:30:45 -0700198type PreviewedStack struct {
jrperritt98d01622017-01-12 14:24:42 -0600199 Capabilities []interface{} `json:"capabilities"`
200 CreationTime time.Time `json:"-"`
201 Description string `json:"description"`
202 DisableRollback bool `json:"disable_rollback"`
203 ID string `json:"id"`
204 Links []gophercloud.Link `json:"links"`
205 Name string `json:"stack_name"`
206 NotificationTopics []interface{} `json:"notification_topics"`
207 Parameters map[string]string `json:"parameters"`
208 Resources []interface{} `json:"resources"`
209 TemplateDescription string `json:"template_description"`
210 Timeout int `json:"timeout_mins"`
211 UpdatedTime time.Time `json:"-"`
212}
213
214func (r *PreviewedStack) UnmarshalJSON(b []byte) error {
215 type tmp PreviewedStack
216 var s struct {
217 tmp
Ildar Svetlovd2139692020-11-10 15:16:16 +0400218 CreationTime string `json:"creation_time"`
219 UpdatedTime string `json:"updated_time"`
jrperritt98d01622017-01-12 14:24:42 -0600220 }
Ildar Svetlovd2139692020-11-10 15:16:16 +0400221
jrperritt98d01622017-01-12 14:24:42 -0600222 err := json.Unmarshal(b, &s)
223 if err != nil {
224 return err
225 }
Ildar Svetlovd2139692020-11-10 15:16:16 +0400226
jrperritt98d01622017-01-12 14:24:42 -0600227 *r = PreviewedStack(s.tmp)
228
Ildar Svetlovd2139692020-11-10 15:16:16 +0400229 if s.CreationTime != "" {
230 t, err := time.Parse(time.RFC3339, s.CreationTime)
231 if err != nil {
232 t, err = time.Parse(gophercloud.RFC3339NoZ, s.CreationTime)
233 if err != nil {
234 return err
235 }
236 }
237 r.CreationTime = t
238 }
239
240 if s.UpdatedTime != "" {
241 t, err := time.Parse(time.RFC3339, s.UpdatedTime)
242 if err != nil {
243 t, err = time.Parse(gophercloud.RFC3339NoZ, s.UpdatedTime)
244 if err != nil {
245 return err
246 }
247 }
248 r.UpdatedTime = t
249 }
jrperritt98d01622017-01-12 14:24:42 -0600250
251 return nil
Jon Perritt35e27e42014-12-05 11:10:46 -0700252}
253
Jon Perritt37f97742015-02-04 18:55:05 -0700254// PreviewResult represents the result of a Preview operation.
Jon Perritt35e27e42014-12-05 11:10:46 -0700255type PreviewResult struct {
256 gophercloud.Result
257}
258
Jon Perritt37f97742015-02-04 18:55:05 -0700259// Extract returns a pointer to a PreviewedStack object and is called after a
260// Preview operation.
Jon Perritt22325f42015-01-29 14:48:18 -0700261func (r PreviewResult) Extract() (*PreviewedStack, error) {
Jon Perritt3c166472016-02-25 03:07:41 -0600262 var s struct {
263 PreviewedStack *PreviewedStack `json:"stack"`
Jon Perritt35e27e42014-12-05 11:10:46 -0700264 }
Jon Perritt3c166472016-02-25 03:07:41 -0600265 err := r.ExtractInto(&s)
266 return s.PreviewedStack, err
Jon Perritt35e27e42014-12-05 11:10:46 -0700267}
268
Jon Perritt9209df42015-02-05 12:55:33 -0700269// AbandonedStack represents the result of an Abandon operation.
Jon Perritt01972e22015-01-28 10:30:45 -0700270type AbandonedStack struct {
Jon Perritt3c166472016-02-25 03:07:41 -0600271 Status string `json:"status"`
272 Name string `json:"name"`
273 Template map[string]interface{} `json:"template"`
274 Action string `json:"action"`
275 ID string `json:"id"`
276 Resources map[string]interface{} `json:"resources"`
277 Files map[string]string `json:"files"`
278 StackUserProjectID string `json:"stack_user_project_id"`
279 ProjectID string `json:"project_id"`
280 Environment map[string]interface{} `json:"environment"`
Jon Perritt35e27e42014-12-05 11:10:46 -0700281}
282
Jon Perritt9209df42015-02-05 12:55:33 -0700283// AbandonResult represents the result of an Abandon operation.
Jon Perritt35e27e42014-12-05 11:10:46 -0700284type AbandonResult struct {
285 gophercloud.Result
286}
Jon Perritt9209df42015-02-05 12:55:33 -0700287
288// Extract returns a pointer to an AbandonedStack object and is called after an
289// Abandon operation.
290func (r AbandonResult) Extract() (*AbandonedStack, error) {
Jon Perritt3c166472016-02-25 03:07:41 -0600291 var s *AbandonedStack
292 err := r.ExtractInto(&s)
293 return s, err
Jon Perritt9209df42015-02-05 12:55:33 -0700294}
Jon Perritt3d381d52015-02-09 13:04:48 -0700295
Jon Perrittdb0d26a2015-02-09 13:06:16 -0700296// String converts an AbandonResult to a string. This is useful to when passing
297// the result of an Abandon operation to an AdoptOpts AdoptStackData field.
Jon Perritt3d381d52015-02-09 13:04:48 -0700298func (r AbandonResult) String() (string, error) {
299 out, err := json.Marshal(r)
Jon Perritt31b66462016-02-25 22:25:30 -0600300 return string(out), err
Jon Perritt3d381d52015-02-09 13:04:48 -0700301}