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 | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 5 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 6 | "github.com/gophercloud/gophercloud" |
| 7 | "github.com/gophercloud/gophercloud/pagination" |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 8 | ) |
| 9 | |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 10 | // CreatedStack represents the object extracted from a Create operation. |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame] | 11 | type CreatedStack struct { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 12 | ID string `json:"id"` |
| 13 | Links []gophercloud.Link `json:"links"` |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 14 | } |
| 15 | |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 16 | // CreateResult represents the result of a Create operation. |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 17 | type CreateResult struct { |
| 18 | gophercloud.Result |
| 19 | } |
| 20 | |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 21 | // Extract returns a pointer to a CreatedStack object and is called after a |
| 22 | // Create operation. |
Jon Perritt | 22325f4 | 2015-01-29 14:48:18 -0700 | [diff] [blame] | 23 | func (r CreateResult) Extract() (*CreatedStack, error) { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 24 | var s struct { |
| 25 | CreatedStack *CreatedStack `json:"stack"` |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 26 | } |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 27 | err := r.ExtractInto(&s) |
| 28 | return s.CreatedStack, err |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 31 | // AdoptResult represents the result of an Adopt operation. AdoptResult has the |
| 32 | // same form as CreateResult. |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 33 | type AdoptResult struct { |
Jon Perritt | 9741dd9 | 2015-02-04 12:05:47 -0700 | [diff] [blame] | 34 | CreateResult |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | // StackPage is a pagination.Pager that is returned from a call to the List function. |
| 38 | type StackPage struct { |
| 39 | pagination.SinglePageBase |
| 40 | } |
| 41 | |
| 42 | // IsEmpty returns true if a ListResult contains no Stacks. |
| 43 | func (r StackPage) IsEmpty() (bool, error) { |
| 44 | stacks, err := ExtractStacks(r) |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 45 | return len(stacks) == 0, err |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 48 | // ListedStack represents an element in the slice extracted from a List operation. |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame] | 49 | type ListedStack struct { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 50 | CreationTime gophercloud.JSONRFC3339NoZ `json:"creation_time"` |
| 51 | Description string `json:"description"` |
| 52 | ID string `json:"id"` |
| 53 | Links []gophercloud.Link `json:"links"` |
| 54 | Name string `json:"stack_name"` |
| 55 | Status string `json:"stack_status"` |
| 56 | StatusReason string `json:"stack_status_reason"` |
| 57 | Tags []string `json:"tags"` |
| 58 | UpdatedTime gophercloud.JSONRFC3339NoZ `json:"updated_time"` |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 61 | // 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] | 62 | // over a stacks.List call. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 63 | func ExtractStacks(r pagination.Page) ([]ListedStack, error) { |
| 64 | var s struct { |
| 65 | ListedStacks []ListedStack `json:"stacks"` |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 66 | } |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 67 | err := (r.(StackPage)).ExtractInto(&s) |
| 68 | return s.ListedStacks, err |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 71 | // RetrievedStack represents the object extracted from a Get operation. |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame] | 72 | type RetrievedStack struct { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 73 | Capabilities []interface{} `json:"capabilities"` |
| 74 | CreationTime gophercloud.JSONRFC3339NoZ `json:"creation_time"` |
| 75 | Description string `json:"description"` |
| 76 | DisableRollback bool `json:"disable_rollback"` |
| 77 | ID string `json:"id"` |
| 78 | Links []gophercloud.Link `json:"links"` |
| 79 | NotificationTopics []interface{} `json:"notification_topics"` |
| 80 | Outputs []map[string]interface{} `json:"outputs"` |
| 81 | Parameters map[string]string `json:"parameters"` |
| 82 | Name string `json:"stack_name"` |
| 83 | Status string `json:"stack_status"` |
| 84 | StatusReason string `json:"stack_status_reason"` |
| 85 | Tags []string `json:"tags"` |
| 86 | TemplateDescription string `json:"template_description"` |
| 87 | Timeout int `json:"timeout_mins"` |
| 88 | UpdatedTime gophercloud.JSONRFC3339NoZ `json:"updated_time"` |
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 | // GetResult represents the result of a Get operation. |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 92 | type GetResult struct { |
| 93 | gophercloud.Result |
| 94 | } |
| 95 | |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 96 | // Extract returns a pointer to a RetrievedStack object and is called after a |
| 97 | // Get operation. |
Jon Perritt | 22325f4 | 2015-01-29 14:48:18 -0700 | [diff] [blame] | 98 | func (r GetResult) Extract() (*RetrievedStack, error) { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 99 | var s struct { |
| 100 | Stack *RetrievedStack `json:"stack"` |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 101 | } |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 102 | err := r.ExtractInto(&s) |
| 103 | return s.Stack, err |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 106 | // UpdateResult represents the result of a Update operation. |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 107 | type UpdateResult struct { |
| 108 | gophercloud.ErrResult |
| 109 | } |
| 110 | |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 111 | // DeleteResult represents the result of a Delete operation. |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 112 | type DeleteResult struct { |
| 113 | gophercloud.ErrResult |
| 114 | } |
| 115 | |
Jon Perritt | 37f9774 | 2015-02-04 18:55:05 -0700 | [diff] [blame] | 116 | // PreviewedStack represents the result of a Preview operation. |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame] | 117 | type PreviewedStack struct { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 118 | Capabilities []interface{} `json:"capabilities"` |
| 119 | CreationTime gophercloud.JSONRFC3339NoZ `json:"creation_time"` |
| 120 | Description string `json:"description"` |
| 121 | DisableRollback bool `json:"disable_rollback"` |
| 122 | ID string `json:"id"` |
| 123 | Links []gophercloud.Link `json:"links"` |
| 124 | Name string `json:"stack_name"` |
| 125 | NotificationTopics []interface{} `json:"notification_topics"` |
| 126 | Parameters map[string]string `json:"parameters"` |
| 127 | Resources []interface{} `json:"resources"` |
| 128 | TemplateDescription string `json:"template_description"` |
| 129 | Timeout int `json:"timeout_mins"` |
| 130 | UpdatedTime gophercloud.JSONRFC3339NoZ `json:"updated_time"` |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Jon Perritt | 37f9774 | 2015-02-04 18:55:05 -0700 | [diff] [blame] | 133 | // PreviewResult represents the result of a Preview operation. |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 134 | type PreviewResult struct { |
| 135 | gophercloud.Result |
| 136 | } |
| 137 | |
Jon Perritt | 37f9774 | 2015-02-04 18:55:05 -0700 | [diff] [blame] | 138 | // Extract returns a pointer to a PreviewedStack object and is called after a |
| 139 | // Preview operation. |
Jon Perritt | 22325f4 | 2015-01-29 14:48:18 -0700 | [diff] [blame] | 140 | func (r PreviewResult) Extract() (*PreviewedStack, error) { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 141 | var s struct { |
| 142 | PreviewedStack *PreviewedStack `json:"stack"` |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 143 | } |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 144 | err := r.ExtractInto(&s) |
| 145 | return s.PreviewedStack, err |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Jon Perritt | 9209df4 | 2015-02-05 12:55:33 -0700 | [diff] [blame] | 148 | // AbandonedStack represents the result of an Abandon operation. |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame] | 149 | type AbandonedStack struct { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 150 | Status string `json:"status"` |
| 151 | Name string `json:"name"` |
| 152 | Template map[string]interface{} `json:"template"` |
| 153 | Action string `json:"action"` |
| 154 | ID string `json:"id"` |
| 155 | Resources map[string]interface{} `json:"resources"` |
| 156 | Files map[string]string `json:"files"` |
| 157 | StackUserProjectID string `json:"stack_user_project_id"` |
| 158 | ProjectID string `json:"project_id"` |
| 159 | Environment map[string]interface{} `json:"environment"` |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Jon Perritt | 9209df4 | 2015-02-05 12:55:33 -0700 | [diff] [blame] | 162 | // AbandonResult represents the result of an Abandon operation. |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 163 | type AbandonResult struct { |
| 164 | gophercloud.Result |
| 165 | } |
Jon Perritt | 9209df4 | 2015-02-05 12:55:33 -0700 | [diff] [blame] | 166 | |
| 167 | // Extract returns a pointer to an AbandonedStack object and is called after an |
| 168 | // Abandon operation. |
| 169 | func (r AbandonResult) Extract() (*AbandonedStack, error) { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 170 | var s *AbandonedStack |
| 171 | err := r.ExtractInto(&s) |
| 172 | return s, err |
Jon Perritt | 9209df4 | 2015-02-05 12:55:33 -0700 | [diff] [blame] | 173 | } |
Jon Perritt | 3d381d5 | 2015-02-09 13:04:48 -0700 | [diff] [blame] | 174 | |
Jon Perritt | db0d26a | 2015-02-09 13:06:16 -0700 | [diff] [blame] | 175 | // String converts an AbandonResult to a string. This is useful to when passing |
| 176 | // the result of an Abandon operation to an AdoptOpts AdoptStackData field. |
Jon Perritt | 3d381d5 | 2015-02-09 13:04:48 -0700 | [diff] [blame] | 177 | func (r AbandonResult) String() (string, error) { |
| 178 | out, err := json.Marshal(r) |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 179 | return string(out), err |
Jon Perritt | 3d381d5 | 2015-02-09 13:04:48 -0700 | [diff] [blame] | 180 | } |