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" |
Jon Perritt | a11b5df | 2015-03-05 13:51:59 -0700 | [diff] [blame] | 5 | "fmt" |
| 6 | "reflect" |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 7 | "time" |
| 8 | |
| 9 | "github.com/mitchellh/mapstructure" |
| 10 | "github.com/rackspace/gophercloud" |
| 11 | "github.com/rackspace/gophercloud/pagination" |
| 12 | ) |
| 13 | |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 14 | // CreatedStack represents the object extracted from a Create operation. |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame] | 15 | type CreatedStack struct { |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 16 | ID string `mapstructure:"id"` |
| 17 | Links []gophercloud.Link `mapstructure:"links"` |
| 18 | } |
| 19 | |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 20 | // CreateResult represents the result of a Create operation. |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 21 | type CreateResult struct { |
| 22 | gophercloud.Result |
| 23 | } |
| 24 | |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 25 | // Extract returns a pointer to a CreatedStack object and is called after a |
| 26 | // Create operation. |
Jon Perritt | 22325f4 | 2015-01-29 14:48:18 -0700 | [diff] [blame] | 27 | func (r CreateResult) Extract() (*CreatedStack, error) { |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 28 | if r.Err != nil { |
| 29 | return nil, r.Err |
| 30 | } |
| 31 | |
| 32 | var res struct { |
Jon Perritt | 22325f4 | 2015-01-29 14:48:18 -0700 | [diff] [blame] | 33 | Stack *CreatedStack `mapstructure:"stack"` |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | if err := mapstructure.Decode(r.Body, &res); err != nil { |
| 37 | return nil, err |
| 38 | } |
| 39 | |
| 40 | return res.Stack, nil |
| 41 | } |
| 42 | |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 43 | // AdoptResult represents the result of an Adopt operation. AdoptResult has the |
| 44 | // same form as CreateResult. |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 45 | type AdoptResult struct { |
Jon Perritt | 9741dd9 | 2015-02-04 12:05:47 -0700 | [diff] [blame] | 46 | CreateResult |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | // StackPage is a pagination.Pager that is returned from a call to the List function. |
| 50 | type StackPage struct { |
| 51 | pagination.SinglePageBase |
| 52 | } |
| 53 | |
| 54 | // IsEmpty returns true if a ListResult contains no Stacks. |
| 55 | func (r StackPage) IsEmpty() (bool, error) { |
| 56 | stacks, err := ExtractStacks(r) |
| 57 | if err != nil { |
| 58 | return true, err |
| 59 | } |
| 60 | return len(stacks) == 0, nil |
| 61 | } |
| 62 | |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 63 | // ListedStack represents an element in the slice extracted from a List operation. |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame] | 64 | type ListedStack struct { |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 65 | CreationTime time.Time `mapstructure:"-"` |
| 66 | Description string `mapstructure:"description"` |
| 67 | ID string `mapstructure:"id"` |
| 68 | Links []gophercloud.Link `mapstructure:"links"` |
| 69 | Name string `mapstructure:"stack_name"` |
| 70 | Status string `mapstructure:"stack_status"` |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame] | 71 | StatusReason string `mapstructure:"stack_status_reason"` |
Pratik Mallya | 827c03e | 2015-09-17 00:10:47 -0500 | [diff] [blame] | 72 | Tags []string `mapstructure:"tags"` |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 73 | UpdatedTime time.Time `mapstructure:"-"` |
| 74 | } |
| 75 | |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 76 | // 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] | 77 | // over a stacks.List call. |
Jon Perritt | 22325f4 | 2015-01-29 14:48:18 -0700 | [diff] [blame] | 78 | func ExtractStacks(page pagination.Page) ([]ListedStack, error) { |
Jon Perritt | a11b5df | 2015-03-05 13:51:59 -0700 | [diff] [blame] | 79 | casted := page.(StackPage).Body |
| 80 | |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 81 | var res struct { |
Jon Perritt | 22325f4 | 2015-01-29 14:48:18 -0700 | [diff] [blame] | 82 | Stacks []ListedStack `mapstructure:"stacks"` |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Pratik Mallya | 827c03e | 2015-09-17 00:10:47 -0500 | [diff] [blame] | 85 | err := mapstructure.Decode(casted, &res) |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame] | 86 | if err != nil { |
| 87 | return nil, err |
| 88 | } |
| 89 | |
Jon Perritt | a11b5df | 2015-03-05 13:51:59 -0700 | [diff] [blame] | 90 | var rawStacks []interface{} |
| 91 | switch casted.(type) { |
| 92 | case map[string]interface{}: |
| 93 | rawStacks = casted.(map[string]interface{})["stacks"].([]interface{}) |
| 94 | case map[string][]interface{}: |
| 95 | rawStacks = casted.(map[string][]interface{})["stacks"] |
| 96 | default: |
| 97 | return res.Stacks, fmt.Errorf("Unknown type: %v", reflect.TypeOf(casted)) |
| 98 | } |
| 99 | |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame] | 100 | for i := range rawStacks { |
Jon Perritt | 9cd3d38 | 2015-02-04 15:49:41 -0700 | [diff] [blame] | 101 | thisStack := (rawStacks[i]).(map[string]interface{}) |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame] | 102 | |
Jon Perritt | 9cd3d38 | 2015-02-04 15:49:41 -0700 | [diff] [blame] | 103 | if t, ok := thisStack["creation_time"].(string); ok && t != "" { |
Pratik Mallya | e1b6cbb | 2015-09-09 14:24:14 -0500 | [diff] [blame] | 104 | creationTime, err := time.Parse(gophercloud.STACK_TIME_FMT, t) |
Jon Perritt | 9cd3d38 | 2015-02-04 15:49:41 -0700 | [diff] [blame] | 105 | if err != nil { |
| 106 | return res.Stacks, err |
| 107 | } |
| 108 | res.Stacks[i].CreationTime = creationTime |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame] | 109 | } |
Jon Perritt | 9cd3d38 | 2015-02-04 15:49:41 -0700 | [diff] [blame] | 110 | |
| 111 | if t, ok := thisStack["updated_time"].(string); ok && t != "" { |
Pratik Mallya | e1b6cbb | 2015-09-09 14:24:14 -0500 | [diff] [blame] | 112 | updatedTime, err := time.Parse(gophercloud.STACK_TIME_FMT, t) |
Jon Perritt | 9cd3d38 | 2015-02-04 15:49:41 -0700 | [diff] [blame] | 113 | if err != nil { |
| 114 | return res.Stacks, err |
| 115 | } |
| 116 | res.Stacks[i].UpdatedTime = updatedTime |
| 117 | } |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | return res.Stacks, nil |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 123 | // RetrievedStack represents the object extracted from a Get operation. |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame] | 124 | type RetrievedStack struct { |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 125 | Capabilities []interface{} `mapstructure:"capabilities"` |
| 126 | CreationTime time.Time `mapstructure:"-"` |
| 127 | Description string `mapstructure:"description"` |
| 128 | DisableRollback bool `mapstructure:"disable_rollback"` |
| 129 | ID string `mapstructure:"id"` |
| 130 | Links []gophercloud.Link `mapstructure:"links"` |
| 131 | NotificationTopics []interface{} `mapstructure:"notification_topics"` |
| 132 | Outputs []map[string]interface{} `mapstructure:"outputs"` |
| 133 | Parameters map[string]string `mapstructure:"parameters"` |
| 134 | Name string `mapstructure:"stack_name"` |
| 135 | Status string `mapstructure:"stack_status"` |
| 136 | StatusReason string `mapstructure:"stack_status_reason"` |
Pratik Mallya | 827c03e | 2015-09-17 00:10:47 -0500 | [diff] [blame] | 137 | Tags []string `mapstructure:"tags"` |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 138 | TemplateDescription string `mapstructure:"template_description"` |
| 139 | Timeout int `mapstructure:"timeout_mins"` |
| 140 | UpdatedTime time.Time `mapstructure:"-"` |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 143 | // GetResult represents the result of a Get operation. |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 144 | type GetResult struct { |
| 145 | gophercloud.Result |
| 146 | } |
| 147 | |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 148 | // Extract returns a pointer to a RetrievedStack object and is called after a |
| 149 | // Get operation. |
Jon Perritt | 22325f4 | 2015-01-29 14:48:18 -0700 | [diff] [blame] | 150 | func (r GetResult) Extract() (*RetrievedStack, error) { |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 151 | if r.Err != nil { |
| 152 | return nil, r.Err |
| 153 | } |
| 154 | |
| 155 | var res struct { |
Jon Perritt | 22325f4 | 2015-01-29 14:48:18 -0700 | [diff] [blame] | 156 | Stack *RetrievedStack `mapstructure:"stack"` |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | config := &mapstructure.DecoderConfig{ |
| 160 | Result: &res, |
| 161 | WeaklyTypedInput: true, |
| 162 | } |
| 163 | decoder, err := mapstructure.NewDecoder(config) |
| 164 | if err != nil { |
| 165 | return nil, err |
| 166 | } |
| 167 | |
| 168 | if err := decoder.Decode(r.Body); err != nil { |
| 169 | return nil, err |
| 170 | } |
| 171 | |
| 172 | b := r.Body.(map[string]interface{})["stack"].(map[string]interface{}) |
| 173 | |
| 174 | if date, ok := b["creation_time"]; ok && date != nil { |
Pratik Mallya | e1b6cbb | 2015-09-09 14:24:14 -0500 | [diff] [blame] | 175 | t, err := time.Parse(gophercloud.STACK_TIME_FMT, date.(string)) |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 176 | if err != nil { |
| 177 | return nil, err |
| 178 | } |
| 179 | res.Stack.CreationTime = t |
| 180 | } |
| 181 | |
| 182 | if date, ok := b["updated_time"]; ok && date != nil { |
Pratik Mallya | e1b6cbb | 2015-09-09 14:24:14 -0500 | [diff] [blame] | 183 | t, err := time.Parse(gophercloud.STACK_TIME_FMT, date.(string)) |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 184 | if err != nil { |
| 185 | return nil, err |
| 186 | } |
| 187 | res.Stack.UpdatedTime = t |
| 188 | } |
| 189 | |
| 190 | return res.Stack, err |
| 191 | } |
| 192 | |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 193 | // UpdateResult represents the result of a Update operation. |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 194 | type UpdateResult struct { |
| 195 | gophercloud.ErrResult |
| 196 | } |
| 197 | |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 198 | // DeleteResult represents the result of a Delete operation. |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 199 | type DeleteResult struct { |
| 200 | gophercloud.ErrResult |
| 201 | } |
| 202 | |
Jon Perritt | 37f9774 | 2015-02-04 18:55:05 -0700 | [diff] [blame] | 203 | // PreviewedStack represents the result of a Preview operation. |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame] | 204 | type PreviewedStack struct { |
Pratik Mallya | 827c03e | 2015-09-17 00:10:47 -0500 | [diff] [blame] | 205 | Capabilities []interface{} `mapstructure:"capabilities"` |
| 206 | CreationTime time.Time `mapstructure:"-"` |
| 207 | Description string `mapstructure:"description"` |
| 208 | DisableRollback bool `mapstructure:"disable_rollback"` |
| 209 | ID string `mapstructure:"id"` |
| 210 | Links []gophercloud.Link `mapstructure:"links"` |
| 211 | Name string `mapstructure:"stack_name"` |
| 212 | NotificationTopics []interface{} `mapstructure:"notification_topics"` |
| 213 | Parameters map[string]string `mapstructure:"parameters"` |
| 214 | Resources []interface{} `mapstructure:"resources"` |
| 215 | TemplateDescription string `mapstructure:"template_description"` |
| 216 | Timeout int `mapstructure:"timeout_mins"` |
| 217 | UpdatedTime time.Time `mapstructure:"-"` |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Jon Perritt | 37f9774 | 2015-02-04 18:55:05 -0700 | [diff] [blame] | 220 | // PreviewResult represents the result of a Preview operation. |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 221 | type PreviewResult struct { |
| 222 | gophercloud.Result |
| 223 | } |
| 224 | |
Jon Perritt | 37f9774 | 2015-02-04 18:55:05 -0700 | [diff] [blame] | 225 | // Extract returns a pointer to a PreviewedStack object and is called after a |
| 226 | // Preview operation. |
Jon Perritt | 22325f4 | 2015-01-29 14:48:18 -0700 | [diff] [blame] | 227 | func (r PreviewResult) Extract() (*PreviewedStack, error) { |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 228 | if r.Err != nil { |
| 229 | return nil, r.Err |
| 230 | } |
| 231 | |
| 232 | var res struct { |
Jon Perritt | 22325f4 | 2015-01-29 14:48:18 -0700 | [diff] [blame] | 233 | Stack *PreviewedStack `mapstructure:"stack"` |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | config := &mapstructure.DecoderConfig{ |
| 237 | Result: &res, |
| 238 | WeaklyTypedInput: true, |
| 239 | } |
| 240 | decoder, err := mapstructure.NewDecoder(config) |
| 241 | if err != nil { |
| 242 | return nil, err |
| 243 | } |
| 244 | |
| 245 | if err := decoder.Decode(r.Body); err != nil { |
| 246 | return nil, err |
| 247 | } |
| 248 | |
| 249 | b := r.Body.(map[string]interface{})["stack"].(map[string]interface{}) |
| 250 | |
| 251 | if date, ok := b["creation_time"]; ok && date != nil { |
Pratik Mallya | e1b6cbb | 2015-09-09 14:24:14 -0500 | [diff] [blame] | 252 | t, err := time.Parse(gophercloud.STACK_TIME_FMT, date.(string)) |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 253 | if err != nil { |
| 254 | return nil, err |
| 255 | } |
| 256 | res.Stack.CreationTime = t |
| 257 | } |
| 258 | |
| 259 | if date, ok := b["updated_time"]; ok && date != nil { |
Pratik Mallya | e1b6cbb | 2015-09-09 14:24:14 -0500 | [diff] [blame] | 260 | t, err := time.Parse(gophercloud.STACK_TIME_FMT, date.(string)) |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 261 | if err != nil { |
| 262 | return nil, err |
| 263 | } |
| 264 | res.Stack.UpdatedTime = t |
| 265 | } |
| 266 | |
| 267 | return res.Stack, err |
| 268 | } |
| 269 | |
Jon Perritt | 9209df4 | 2015-02-05 12:55:33 -0700 | [diff] [blame] | 270 | // AbandonedStack represents the result of an Abandon operation. |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame] | 271 | type AbandonedStack struct { |
Pratik Mallya | 827c03e | 2015-09-17 00:10:47 -0500 | [diff] [blame] | 272 | Status string `mapstructure:"status"` |
| 273 | Name string `mapstructure:"name"` |
| 274 | Template map[string]interface{} `mapstructure:"template"` |
| 275 | Action string `mapstructure:"action"` |
| 276 | ID string `mapstructure:"id"` |
| 277 | Resources map[string]interface{} `mapstructure:"resources"` |
| 278 | Files map[string]string `mapstructure:"files"` |
| 279 | StackUserProjectID string `mapstructure:"stack_user_project_id"` |
| 280 | ProjectID string `mapstructure:"project_id"` |
| 281 | Environment map[string]interface{} `mapstructure:"environment"` |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Jon Perritt | 9209df4 | 2015-02-05 12:55:33 -0700 | [diff] [blame] | 284 | // AbandonResult represents the result of an Abandon operation. |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 285 | type AbandonResult struct { |
| 286 | gophercloud.Result |
| 287 | } |
Jon Perritt | 9209df4 | 2015-02-05 12:55:33 -0700 | [diff] [blame] | 288 | |
| 289 | // Extract returns a pointer to an AbandonedStack object and is called after an |
| 290 | // Abandon operation. |
| 291 | func (r AbandonResult) Extract() (*AbandonedStack, error) { |
| 292 | if r.Err != nil { |
| 293 | return nil, r.Err |
| 294 | } |
| 295 | |
| 296 | var res AbandonedStack |
| 297 | |
| 298 | if err := mapstructure.Decode(r.Body, &res); err != nil { |
| 299 | return nil, err |
| 300 | } |
| 301 | |
| 302 | return &res, nil |
| 303 | } |
Jon Perritt | 3d381d5 | 2015-02-09 13:04:48 -0700 | [diff] [blame] | 304 | |
Jon Perritt | db0d26a | 2015-02-09 13:06:16 -0700 | [diff] [blame] | 305 | // String converts an AbandonResult to a string. This is useful to when passing |
| 306 | // the result of an Abandon operation to an AdoptOpts AdoptStackData field. |
Jon Perritt | 3d381d5 | 2015-02-09 13:04:48 -0700 | [diff] [blame] | 307 | func (r AbandonResult) String() (string, error) { |
| 308 | out, err := json.Marshal(r) |
| 309 | if err != nil { |
| 310 | return "", err |
| 311 | } |
| 312 | return string(out), nil |
| 313 | } |