blob: d74ee6c223a278087f6a12fa3e0ba55f0f9ea7f2 [file] [log] [blame]
Jon Perritt35e27e42014-12-05 11:10:46 -07001package stacks
2
3import (
4 "time"
5
6 "github.com/mitchellh/mapstructure"
7 "github.com/rackspace/gophercloud"
8 "github.com/rackspace/gophercloud/pagination"
9)
10
Jon Perritt01972e22015-01-28 10:30:45 -070011type CreatedStack struct {
Jon Perritt35e27e42014-12-05 11:10:46 -070012 ID string `mapstructure:"id"`
13 Links []gophercloud.Link `mapstructure:"links"`
14}
15
16type CreateResult struct {
17 gophercloud.Result
18}
19
20func (r CreateResult) Extract() (*CreateStack, error) {
21 if r.Err != nil {
22 return nil, r.Err
23 }
24
25 var res struct {
Jon Perritt01972e22015-01-28 10:30:45 -070026 Stack *CreateStack `mapstructure:"stack"`
Jon Perritt35e27e42014-12-05 11:10:46 -070027 }
28
29 if err := mapstructure.Decode(r.Body, &res); err != nil {
30 return nil, err
31 }
32
33 return res.Stack, nil
34}
35
36type AdoptResult struct {
37 gophercloud.Result
38}
39
Jon Perritt01972e22015-01-28 10:30:45 -070040
41
Jon Perritt35e27e42014-12-05 11:10:46 -070042// StackPage is a pagination.Pager that is returned from a call to the List function.
43type StackPage struct {
44 pagination.SinglePageBase
45}
46
47// IsEmpty returns true if a ListResult contains no Stacks.
48func (r StackPage) IsEmpty() (bool, error) {
49 stacks, err := ExtractStacks(r)
50 if err != nil {
51 return true, err
52 }
53 return len(stacks) == 0, nil
54}
55
Jon Perritt01972e22015-01-28 10:30:45 -070056type ListedStack struct {
Jon Perritt35e27e42014-12-05 11:10:46 -070057 CreationTime time.Time `mapstructure:"-"`
58 Description string `mapstructure:"description"`
59 ID string `mapstructure:"id"`
60 Links []gophercloud.Link `mapstructure:"links"`
61 Name string `mapstructure:"stack_name"`
62 Status string `mapstructure:"stack_status"`
Jon Perritt01972e22015-01-28 10:30:45 -070063 StatusReason string `mapstructure:"stack_status_reason"`
Jon Perritt35e27e42014-12-05 11:10:46 -070064 UpdatedTime time.Time `mapstructure:"-"`
65}
66
67// ExtractStacks extracts and returns a slice of Stacks. It is used while iterating
68// over a stacks.List call.
69func ExtractStacks(page pagination.Page) ([]ListStack, error) {
70 var res struct {
Jon Perritt01972e22015-01-28 10:30:45 -070071 Stacks []ListStack `mapstructure:"stacks"`
Jon Perritt35e27e42014-12-05 11:10:46 -070072 }
73
74 err := mapstructure.Decode(page.(StackPage).Body, &res)
Jon Perritt01972e22015-01-28 10:30:45 -070075 if err != nil {
76 return nil, err
77 }
78
79 rawStacks := (((page.(StackPage).Body).(map[string]interface{}))["stacks"]).([]interface{})
80 for i := range rawStacks {
81 creationTime, err := time.Parse(time.RFC3339, ((rawStacks[i]).(map[string]interface{}))["creation_time"].(string))
82 if err != nil {
83 return res.Stacks, err
84 }
85 res.Stacks[i].CreationTime = creationTime
86
87 updatedTime, err := time.Parse(time.RFC3339, ((rawStacks[i]).(map[string]interface{}))["updated_time"].(string))
88 if err != nil {
89 return res.Stacks, err
90 }
91 res.Stacks[i].UpdatedTime = updatedTime
92 }
93
94 return res.Stacks, nil
Jon Perritt35e27e42014-12-05 11:10:46 -070095}
96
Jon Perritt01972e22015-01-28 10:30:45 -070097type RetrievedStack struct {
Jon Perritt35e27e42014-12-05 11:10:46 -070098 Capabilities []interface{} `mapstructure:"capabilities"`
99 CreationTime time.Time `mapstructure:"-"`
100 Description string `mapstructure:"description"`
101 DisableRollback bool `mapstructure:"disable_rollback"`
102 ID string `mapstructure:"id"`
103 Links []gophercloud.Link `mapstructure:"links"`
104 NotificationTopics []interface{} `mapstructure:"notification_topics"`
105 Outputs []map[string]string `mapstructure:"outputs"`
106 Parameters map[string]string `mapstructure:"parameters"`
107 Name string `mapstructure:"stack_name"`
108 Status string `mapstructure:"stack_status"`
109 StausReason string `mapstructure:"stack_status_reason"`
110 TemplateDescription string `mapstructure:"template_description"`
111 Timeout int `mapstructure:"timeout_mins"`
112 UpdatedTime time.Time `mapstructure:"-"`
113}
114
115type GetResult struct {
116 gophercloud.Result
117}
118
119func (r GetResult) Extract() (*GetStack, error) {
120 if r.Err != nil {
121 return nil, r.Err
122 }
123
124 var res struct {
Jon Perritt01972e22015-01-28 10:30:45 -0700125 Stack *GetStack `mapstructure:"stack"`
Jon Perritt35e27e42014-12-05 11:10:46 -0700126 }
127
128 config := &mapstructure.DecoderConfig{
129 Result: &res,
130 WeaklyTypedInput: true,
131 }
132 decoder, err := mapstructure.NewDecoder(config)
133 if err != nil {
134 return nil, err
135 }
136
137 if err := decoder.Decode(r.Body); err != nil {
138 return nil, err
139 }
140
141 b := r.Body.(map[string]interface{})["stack"].(map[string]interface{})
142
143 if date, ok := b["creation_time"]; ok && date != nil {
144 t, err := time.Parse(time.RFC3339, date.(string))
145 if err != nil {
146 return nil, err
147 }
148 res.Stack.CreationTime = t
149 }
150
151 if date, ok := b["updated_time"]; ok && date != nil {
152 t, err := time.Parse(time.RFC3339, date.(string))
153 if err != nil {
154 return nil, err
155 }
156 res.Stack.UpdatedTime = t
157 }
158
159 return res.Stack, err
160}
161
162type UpdateResult struct {
163 gophercloud.ErrResult
164}
165
166type DeleteResult struct {
167 gophercloud.ErrResult
168}
169
Jon Perritt01972e22015-01-28 10:30:45 -0700170type PreviewedStack struct {
Jon Perritt35e27e42014-12-05 11:10:46 -0700171 Capabilities []interface{} `mapstructure:"capabilities"`
172 CreationTime time.Time `mapstructure:"-"`
173 Description string `mapstructure:"description"`
174 DisableRollback bool `mapstructure:"disable_rollback"`
175 ID string `mapstructure:"id"`
176 Links []gophercloud.Link `mapstructure:"links"`
177 Name string `mapstructure:"stack_name"`
178 NotificationTopics []interface{} `mapstructure:"notification_topics"`
179 Parameters map[string]string `mapstructure:"parameters"`
180 Resources []map[string]string `mapstructure:"resources"`
181 Status string `mapstructure:"stack_status"`
182 StausReason string `mapstructure:"stack_status_reason"`
183 TemplateDescription string `mapstructure:"template_description"`
184 Timeout int `mapstructure:"timeout_mins"`
185 UpdatedTime time.Time `mapstructure:"-"`
186}
187
188type PreviewResult struct {
189 gophercloud.Result
190}
191
192func (r PreviewResult) Extract() (*PreviewStack, error) {
193 if r.Err != nil {
194 return nil, r.Err
195 }
196
197 var res struct {
Jon Perritt01972e22015-01-28 10:30:45 -0700198 Stack *PreviewStack `mapstructure:"stack"`
Jon Perritt35e27e42014-12-05 11:10:46 -0700199 }
200
201 config := &mapstructure.DecoderConfig{
202 Result: &res,
203 WeaklyTypedInput: true,
204 }
205 decoder, err := mapstructure.NewDecoder(config)
206 if err != nil {
207 return nil, err
208 }
209
210 if err := decoder.Decode(r.Body); err != nil {
211 return nil, err
212 }
213
214 b := r.Body.(map[string]interface{})["stack"].(map[string]interface{})
215
216 if date, ok := b["creation_time"]; ok && date != nil {
217 t, err := time.Parse(time.RFC3339, date.(string))
218 if err != nil {
219 return nil, err
220 }
221 res.Stack.CreationTime = t
222 }
223
224 if date, ok := b["updated_time"]; ok && date != nil {
225 t, err := time.Parse(time.RFC3339, date.(string))
226 if err != nil {
227 return nil, err
228 }
229 res.Stack.UpdatedTime = t
230 }
231
232 return res.Stack, err
233}
234
Jon Perritt01972e22015-01-28 10:30:45 -0700235type AbandonedStack struct {
Jon Perritt35e27e42014-12-05 11:10:46 -0700236}
237
238type AbandonResult struct {
239 gophercloud.Result
240}