blob: 5e5c8f506ee881c77b860d6e72a66c6b5f315fd9 [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
Jon Perritt27249f42016-02-18 10:35:59 -06007 "github.com/gophercloud/gophercloud"
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
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 Perritt35e27e42014-12-05 11:10:46 -070079}
80
Jon Perritt7726e492015-02-04 17:54:28 -070081// ExtractStacks extracts and returns a slice of ListedStack. It is used while iterating
Jon Perritt35e27e42014-12-05 11:10:46 -070082// over a stacks.List call.
Jon Perritt3c166472016-02-25 03:07:41 -060083func ExtractStacks(r pagination.Page) ([]ListedStack, error) {
84 var s struct {
85 ListedStacks []ListedStack `json:"stacks"`
Jon Perritt35e27e42014-12-05 11:10:46 -070086 }
Jon Perritt3c166472016-02-25 03:07:41 -060087 err := (r.(StackPage)).ExtractInto(&s)
88 return s.ListedStacks, err
Jon Perritt35e27e42014-12-05 11:10:46 -070089}
90
Jon Perritt7726e492015-02-04 17:54:28 -070091// RetrievedStack represents the object extracted from a Get operation.
Jon Perritt01972e22015-01-28 10:30:45 -070092type RetrievedStack struct {
jrperritt98d01622017-01-12 14:24:42 -060093 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
111func (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 Perritt35e27e42014-12-05 11:10:46 -0700128}
129
Jon Perritt7726e492015-02-04 17:54:28 -0700130// GetResult represents the result of a Get operation.
Jon Perritt35e27e42014-12-05 11:10:46 -0700131type GetResult struct {
132 gophercloud.Result
133}
134
Jon Perritt7726e492015-02-04 17:54:28 -0700135// Extract returns a pointer to a RetrievedStack object and is called after a
136// Get operation.
Jon Perritt22325f42015-01-29 14:48:18 -0700137func (r GetResult) Extract() (*RetrievedStack, error) {
Jon Perritt3c166472016-02-25 03:07:41 -0600138 var s struct {
139 Stack *RetrievedStack `json:"stack"`
Jon Perritt35e27e42014-12-05 11:10:46 -0700140 }
Jon Perritt3c166472016-02-25 03:07:41 -0600141 err := r.ExtractInto(&s)
142 return s.Stack, err
Jon Perritt35e27e42014-12-05 11:10:46 -0700143}
144
Jon Perritt7726e492015-02-04 17:54:28 -0700145// UpdateResult represents the result of a Update operation.
Jon Perritt35e27e42014-12-05 11:10:46 -0700146type UpdateResult struct {
147 gophercloud.ErrResult
148}
149
Jon Perritt7726e492015-02-04 17:54:28 -0700150// DeleteResult represents the result of a Delete operation.
Jon Perritt35e27e42014-12-05 11:10:46 -0700151type DeleteResult struct {
152 gophercloud.ErrResult
153}
154
Jon Perritt37f97742015-02-04 18:55:05 -0700155// PreviewedStack represents the result of a Preview operation.
Jon Perritt01972e22015-01-28 10:30:45 -0700156type PreviewedStack struct {
jrperritt98d01622017-01-12 14:24:42 -0600157 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
172func (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 Perritt35e27e42014-12-05 11:10:46 -0700189}
190
Jon Perritt37f97742015-02-04 18:55:05 -0700191// PreviewResult represents the result of a Preview operation.
Jon Perritt35e27e42014-12-05 11:10:46 -0700192type PreviewResult struct {
193 gophercloud.Result
194}
195
Jon Perritt37f97742015-02-04 18:55:05 -0700196// Extract returns a pointer to a PreviewedStack object and is called after a
197// Preview operation.
Jon Perritt22325f42015-01-29 14:48:18 -0700198func (r PreviewResult) Extract() (*PreviewedStack, error) {
Jon Perritt3c166472016-02-25 03:07:41 -0600199 var s struct {
200 PreviewedStack *PreviewedStack `json:"stack"`
Jon Perritt35e27e42014-12-05 11:10:46 -0700201 }
Jon Perritt3c166472016-02-25 03:07:41 -0600202 err := r.ExtractInto(&s)
203 return s.PreviewedStack, err
Jon Perritt35e27e42014-12-05 11:10:46 -0700204}
205
Jon Perritt9209df42015-02-05 12:55:33 -0700206// AbandonedStack represents the result of an Abandon operation.
Jon Perritt01972e22015-01-28 10:30:45 -0700207type AbandonedStack struct {
Jon Perritt3c166472016-02-25 03:07:41 -0600208 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 Perritt35e27e42014-12-05 11:10:46 -0700218}
219
Jon Perritt9209df42015-02-05 12:55:33 -0700220// AbandonResult represents the result of an Abandon operation.
Jon Perritt35e27e42014-12-05 11:10:46 -0700221type AbandonResult struct {
222 gophercloud.Result
223}
Jon Perritt9209df42015-02-05 12:55:33 -0700224
225// Extract returns a pointer to an AbandonedStack object and is called after an
226// Abandon operation.
227func (r AbandonResult) Extract() (*AbandonedStack, error) {
Jon Perritt3c166472016-02-25 03:07:41 -0600228 var s *AbandonedStack
229 err := r.ExtractInto(&s)
230 return s, err
Jon Perritt9209df42015-02-05 12:55:33 -0700231}
Jon Perritt3d381d52015-02-09 13:04:48 -0700232
Jon Perrittdb0d26a2015-02-09 13:06:16 -0700233// 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 Perritt3d381d52015-02-09 13:04:48 -0700235func (r AbandonResult) String() (string, error) {
236 out, err := json.Marshal(r)
Jon Perritt31b66462016-02-25 22:25:30 -0600237 return string(out), err
Jon Perritt3d381d52015-02-09 13:04:48 -0700238}