blob: bc1c8388c0a905cc1fcf18b2c8f532f5cdd23400 [file] [log] [blame]
Jon Perritt35e27e42014-12-05 11:10:46 -07001package stacks
2
3import (
Pratik Mallya827c03e2015-09-17 00:10:47 -05004 "strings"
Jon Perritt35e27e42014-12-05 11:10:46 -07005
Krzysztof Szukiełojć3f41d082017-05-07 14:43:06 +02006 "gerrit.mcp.mirantis.net/debian/gophercloud.git"
Krzysztof Szukiełojć24a29ce2017-05-07 14:24:02 +02007 "gerrit.mcp.mirantis.net/debian/gophercloud.git/pagination"
Jon Perritt35e27e42014-12-05 11:10:46 -07008)
9
10// CreateOptsBuilder is the interface options structs have to satisfy in order
11// to be used in the main Create operation in this package. Since many
12// extensions decorate or modify the common logic, it is useful for them to
13// satisfy a basic interface in order for them to be used.
14type CreateOptsBuilder interface {
15 ToStackCreateMap() (map[string]interface{}, error)
16}
17
18// CreateOpts is the common options struct used in this package's Create
19// operation.
20type CreateOpts struct {
Jon Perrittfea90732016-03-15 02:57:05 -050021 // The name of the stack. It must start with an alphabetic character.
22 Name string `json:"stack_name" required:"true"`
23 // A structure that contains either the template file or url. Call the
Pratik Mallya5fddb2a2015-09-14 14:04:49 -050024 // associated methods to extract the information relevant to send in a create request.
Jon Perrittfea90732016-03-15 02:57:05 -050025 TemplateOpts *Template `json:"-" required:"true"`
26 // Enables or disables deletion of all stack resources when a stack
Jon Perritt952f3e12015-02-03 12:13:24 -070027 // creation fails. Default is true, meaning all resources are not deleted when
28 // stack creation fails.
Jon Perrittfea90732016-03-15 02:57:05 -050029 DisableRollback *bool `json:"disable_rollback,omitempty"`
30 // A structure that contains details for the environment of the stack.
31 EnvironmentOpts *Environment `json:"-"`
32 // User-defined parameters to pass to the template.
Ildar Svetlovd2139692020-11-10 15:16:16 +040033 Parameters map[string]interface{} `json:"parameters,omitempty"`
Jon Perrittfea90732016-03-15 02:57:05 -050034 // The timeout for stack creation in minutes.
35 Timeout int `json:"timeout_mins,omitempty"`
36 // A list of tags to assosciate with the Stack
37 Tags []string `json:"-"`
Jon Perritt35e27e42014-12-05 11:10:46 -070038}
39
40// ToStackCreateMap casts a CreateOpts struct to a map.
41func (opts CreateOpts) ToStackCreateMap() (map[string]interface{}, error) {
Jon Perrittfea90732016-03-15 02:57:05 -050042 b, err := gophercloud.BuildRequestBody(opts, "")
43 if err != nil {
Jon Perritt58611da2016-03-09 00:49:57 -060044 return nil, err
Jon Perritt35e27e42014-12-05 11:10:46 -070045 }
Pratik Mallya5fddb2a2015-09-14 14:04:49 -050046
Jon Perrittfea90732016-03-15 02:57:05 -050047 if err := opts.TemplateOpts.Parse(); err != nil {
48 return nil, err
Pratik Mallya5fddb2a2015-09-14 14:04:49 -050049 }
Jon Perrittfea90732016-03-15 02:57:05 -050050
51 if err := opts.TemplateOpts.getFileContents(opts.TemplateOpts.Parsed, ignoreIfTemplate, true); err != nil {
52 return nil, err
53 }
54 opts.TemplateOpts.fixFileRefs()
55 b["template"] = string(opts.TemplateOpts.Bin)
56
57 files := make(map[string]string)
58 for k, v := range opts.TemplateOpts.Files {
59 files[k] = v
Pratik Mallya5fddb2a2015-09-14 14:04:49 -050060 }
61
62 if opts.EnvironmentOpts != nil {
63 if err := opts.EnvironmentOpts.Parse(); err != nil {
64 return nil, err
65 }
Pratik Mallyaa979f5b2015-09-22 03:10:55 -050066 if err := opts.EnvironmentOpts.getRRFileContents(ignoreIfEnvironment); err != nil {
Pratik Mallya5fddb2a2015-09-14 14:04:49 -050067 return nil, err
68 }
Pratik Mallyaa979f5b2015-09-22 03:10:55 -050069 opts.EnvironmentOpts.fixFileRefs()
Pratik Mallya5fddb2a2015-09-14 14:04:49 -050070 for k, v := range opts.EnvironmentOpts.Files {
Jon Perrittfea90732016-03-15 02:57:05 -050071 files[k] = v
Pratik Mallya5fddb2a2015-09-14 14:04:49 -050072 }
Jon Perrittfea90732016-03-15 02:57:05 -050073 b["environment"] = string(opts.EnvironmentOpts.Bin)
Pratik Mallya5fddb2a2015-09-14 14:04:49 -050074 }
75
Jon Perrittfea90732016-03-15 02:57:05 -050076 if len(files) > 0 {
77 b["files"] = files
Jon Perritt35e27e42014-12-05 11:10:46 -070078 }
79
Pratik Mallya827c03e2015-09-17 00:10:47 -050080 if opts.Tags != nil {
Jon Perrittfea90732016-03-15 02:57:05 -050081 b["tags"] = strings.Join(opts.Tags, ",")
Pratik Mallya827c03e2015-09-17 00:10:47 -050082 }
Jon Perrittfea90732016-03-15 02:57:05 -050083
84 return b, nil
Jon Perritt35e27e42014-12-05 11:10:46 -070085}
86
87// Create accepts a CreateOpts struct and creates a new stack using the values
88// provided.
Jon Perritt3860b512016-03-29 12:01:48 -050089func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
Jon Perrittfea90732016-03-15 02:57:05 -050090 b, err := opts.ToStackCreateMap()
Jon Perritt35e27e42014-12-05 11:10:46 -070091 if err != nil {
Jon Perrittfea90732016-03-15 02:57:05 -050092 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -050093 return
Jon Perritt35e27e42014-12-05 11:10:46 -070094 }
Jon Perrittfea90732016-03-15 02:57:05 -050095 _, r.Err = c.Post(createURL(c), b, &r.Body, nil)
jrperritt29ae6b32016-04-13 12:59:37 -050096 return
Jon Perritt35e27e42014-12-05 11:10:46 -070097}
98
99// AdoptOptsBuilder is the interface options structs have to satisfy in order
100// to be used in the Adopt function in this package. Since many
101// extensions decorate or modify the common logic, it is useful for them to
102// satisfy a basic interface in order for them to be used.
103type AdoptOptsBuilder interface {
104 ToStackAdoptMap() (map[string]interface{}, error)
105}
106
107// AdoptOpts is the common options struct used in this package's Adopt
108// operation.
109type AdoptOpts struct {
Jon Perrittfea90732016-03-15 02:57:05 -0500110 // Existing resources data represented as a string to add to the
Jon Perritt9741dd92015-02-04 12:05:47 -0700111 // new stack. Data returned by Abandon could be provided as AdoptsStackData.
Jon Perrittfea90732016-03-15 02:57:05 -0500112 AdoptStackData string `json:"adopt_stack_data" required:"true"`
113 // The name of the stack. It must start with an alphabetic character.
114 Name string `json:"stack_name" required:"true"`
Jon Perritt397ade62016-03-15 06:55:02 -0500115 // A structure that contains either the template file or url. Call the
116 // associated methods to extract the information relevant to send in a create request.
117 TemplateOpts *Template `json:"-" required:"true"`
Jon Perrittfea90732016-03-15 02:57:05 -0500118 // The timeout for stack creation in minutes.
119 Timeout int `json:"timeout_mins,omitempty"`
120 // A structure that contains either the template file or url. Call the
Pratik Mallya5fddb2a2015-09-14 14:04:49 -0500121 // associated methods to extract the information relevant to send in a create request.
Jon Perrittfea90732016-03-15 02:57:05 -0500122 //TemplateOpts *Template `json:"-" required:"true"`
123 // Enables or disables deletion of all stack resources when a stack
Jon Perritt9741dd92015-02-04 12:05:47 -0700124 // creation fails. Default is true, meaning all resources are not deleted when
125 // stack creation fails.
Jon Perrittfea90732016-03-15 02:57:05 -0500126 DisableRollback *bool `json:"disable_rollback,omitempty"`
127 // A structure that contains details for the environment of the stack.
128 EnvironmentOpts *Environment `json:"-"`
129 // User-defined parameters to pass to the template.
Ildar Svetlovd2139692020-11-10 15:16:16 +0400130 Parameters map[string]interface{} `json:"parameters,omitempty"`
Jon Perritt35e27e42014-12-05 11:10:46 -0700131}
132
133// ToStackAdoptMap casts a CreateOpts struct to a map.
134func (opts AdoptOpts) ToStackAdoptMap() (map[string]interface{}, error) {
Jon Perrittfea90732016-03-15 02:57:05 -0500135 b, err := gophercloud.BuildRequestBody(opts, "")
136 if err != nil {
Jon Perritt58611da2016-03-09 00:49:57 -0600137 return nil, err
Jon Perritt35e27e42014-12-05 11:10:46 -0700138 }
Jon Perrittfea90732016-03-15 02:57:05 -0500139
Jon Perritt397ade62016-03-15 06:55:02 -0500140 if err := opts.TemplateOpts.Parse(); err != nil {
141 return nil, err
142 }
143
144 if err := opts.TemplateOpts.getFileContents(opts.TemplateOpts.Parsed, ignoreIfTemplate, true); err != nil {
145 return nil, err
146 }
147 opts.TemplateOpts.fixFileRefs()
148 b["template"] = string(opts.TemplateOpts.Bin)
149
Jon Perrittfea90732016-03-15 02:57:05 -0500150 files := make(map[string]string)
Jon Perritt397ade62016-03-15 06:55:02 -0500151 for k, v := range opts.TemplateOpts.Files {
152 files[k] = v
153 }
Jon Perritt35e27e42014-12-05 11:10:46 -0700154
Pratik Mallya5fddb2a2015-09-14 14:04:49 -0500155 if opts.EnvironmentOpts != nil {
156 if err := opts.EnvironmentOpts.Parse(); err != nil {
157 return nil, err
158 }
Pratik Mallyaa979f5b2015-09-22 03:10:55 -0500159 if err := opts.EnvironmentOpts.getRRFileContents(ignoreIfEnvironment); err != nil {
Pratik Mallya5fddb2a2015-09-14 14:04:49 -0500160 return nil, err
161 }
Pratik Mallyaa979f5b2015-09-22 03:10:55 -0500162 opts.EnvironmentOpts.fixFileRefs()
Pratik Mallya5fddb2a2015-09-14 14:04:49 -0500163 for k, v := range opts.EnvironmentOpts.Files {
Jon Perrittfea90732016-03-15 02:57:05 -0500164 files[k] = v
Pratik Mallya5fddb2a2015-09-14 14:04:49 -0500165 }
Jon Perrittfea90732016-03-15 02:57:05 -0500166 b["environment"] = string(opts.EnvironmentOpts.Bin)
Jon Perritt35e27e42014-12-05 11:10:46 -0700167 }
Pratik Mallya5fddb2a2015-09-14 14:04:49 -0500168
Jon Perrittfea90732016-03-15 02:57:05 -0500169 if len(files) > 0 {
170 b["files"] = files
Jon Perritt35e27e42014-12-05 11:10:46 -0700171 }
Pratik Mallya5fddb2a2015-09-14 14:04:49 -0500172
Jon Perrittfea90732016-03-15 02:57:05 -0500173 return b, nil
Jon Perritt35e27e42014-12-05 11:10:46 -0700174}
175
176// Adopt accepts an AdoptOpts struct and creates a new stack using the resources
177// from another stack.
Jon Perritt3860b512016-03-29 12:01:48 -0500178func Adopt(c *gophercloud.ServiceClient, opts AdoptOptsBuilder) (r AdoptResult) {
Jon Perrittfea90732016-03-15 02:57:05 -0500179 b, err := opts.ToStackAdoptMap()
Jon Perritt35e27e42014-12-05 11:10:46 -0700180 if err != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500181 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500182 return
Jon Perritt35e27e42014-12-05 11:10:46 -0700183 }
Jon Perrittfea90732016-03-15 02:57:05 -0500184 _, r.Err = c.Post(adoptURL(c), b, &r.Body, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500185 return
Jon Perritt35e27e42014-12-05 11:10:46 -0700186}
187
188// SortDir is a type for specifying in which direction to sort a list of stacks.
189type SortDir string
190
191// SortKey is a type for specifying by which key to sort a list of stacks.
192type SortKey string
193
194var (
195 // SortAsc is used to sort a list of stacks in ascending order.
196 SortAsc SortDir = "asc"
197 // SortDesc is used to sort a list of stacks in descending order.
198 SortDesc SortDir = "desc"
199 // SortName is used to sort a list of stacks by name.
200 SortName SortKey = "name"
201 // SortStatus is used to sort a list of stacks by status.
202 SortStatus SortKey = "status"
203 // SortCreatedAt is used to sort a list of stacks by date created.
204 SortCreatedAt SortKey = "created_at"
205 // SortUpdatedAt is used to sort a list of stacks by date updated.
206 SortUpdatedAt SortKey = "updated_at"
207)
208
209// ListOptsBuilder allows extensions to add additional parameters to the
210// List request.
211type ListOptsBuilder interface {
212 ToStackListQuery() (string, error)
213}
214
215// ListOpts allows the filtering and sorting of paginated collections through
216// the API. Filtering is achieved by passing in struct field values that map to
217// the network attributes you want to see returned. SortKey allows you to sort
218// by a particular network attribute. SortDir sets the direction, and is either
219// `asc' or `desc'. Marker and Limit are used for pagination.
220type ListOpts struct {
221 Status string `q:"status"`
222 Name string `q:"name"`
223 Marker string `q:"marker"`
224 Limit int `q:"limit"`
225 SortKey SortKey `q:"sort_keys"`
226 SortDir SortDir `q:"sort_dir"`
227}
228
229// ToStackListQuery formats a ListOpts into a query string.
230func (opts ListOpts) ToStackListQuery() (string, error) {
231 q, err := gophercloud.BuildQueryString(opts)
232 if err != nil {
233 return "", err
234 }
235 return q.String(), nil
236}
237
238// List returns a Pager which allows you to iterate over a collection of
239// stacks. It accepts a ListOpts struct, which allows you to filter and sort
240// the returned collection for greater efficiency.
241func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
242 url := listURL(c)
243 if opts != nil {
244 query, err := opts.ToStackListQuery()
245 if err != nil {
246 return pagination.Pager{Err: err}
247 }
248 url += query
249 }
Jon Perritt35e27e42014-12-05 11:10:46 -0700250 createPage := func(r pagination.PageResult) pagination.Page {
251 return StackPage{pagination.SinglePageBase(r)}
252 }
253 return pagination.NewPager(c, url, createPage)
254}
255
256// Get retreives a stack based on the stack name and stack ID.
jrperritt29ae6b32016-04-13 12:59:37 -0500257func Get(c *gophercloud.ServiceClient, stackName, stackID string) (r GetResult) {
Jon Perrittfea90732016-03-15 02:57:05 -0500258 _, r.Err = c.Get(getURL(c, stackName, stackID), &r.Body, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500259 return
Jon Perritt35e27e42014-12-05 11:10:46 -0700260}
261
262// UpdateOptsBuilder is the interface options structs have to satisfy in order
263// to be used in the Update operation in this package.
264type UpdateOptsBuilder interface {
265 ToStackUpdateMap() (map[string]interface{}, error)
266}
267
Ildar Svetlovd2139692020-11-10 15:16:16 +0400268// UpdatePatchOptsBuilder is the interface options structs have to satisfy in order
269// to be used in the UpdatePatch operation in this package
270type UpdatePatchOptsBuilder interface {
271 ToStackUpdatePatchMap() (map[string]interface{}, error)
272}
273
Jon Perritt35e27e42014-12-05 11:10:46 -0700274// UpdateOpts contains the common options struct used in this package's Update
Ildar Svetlovd2139692020-11-10 15:16:16 +0400275// and UpdatePatch operations.
Jon Perritt35e27e42014-12-05 11:10:46 -0700276type UpdateOpts struct {
Jon Perrittfea90732016-03-15 02:57:05 -0500277 // A structure that contains either the template file or url. Call the
Pratik Mallya5fddb2a2015-09-14 14:04:49 -0500278 // associated methods to extract the information relevant to send in a create request.
Ildar Svetlovd2139692020-11-10 15:16:16 +0400279 TemplateOpts *Template `json:"-"`
Jon Perrittfea90732016-03-15 02:57:05 -0500280 // A structure that contains details for the environment of the stack.
281 EnvironmentOpts *Environment `json:"-"`
282 // User-defined parameters to pass to the template.
Ildar Svetlovd2139692020-11-10 15:16:16 +0400283 Parameters map[string]interface{} `json:"parameters,omitempty"`
Jon Perrittfea90732016-03-15 02:57:05 -0500284 // The timeout for stack creation in minutes.
285 Timeout int `json:"timeout_mins,omitempty"`
Ildar Svetlovd2139692020-11-10 15:16:16 +0400286 // A list of tags to associate with the Stack
Jon Perrittfea90732016-03-15 02:57:05 -0500287 Tags []string `json:"-"`
Jon Perritt35e27e42014-12-05 11:10:46 -0700288}
289
Ildar Svetlovd2139692020-11-10 15:16:16 +0400290// ToStackUpdateMap validates that a template was supplied and calls
291// the toStackUpdateMap private function.
Jon Perritt35e27e42014-12-05 11:10:46 -0700292func (opts UpdateOpts) ToStackUpdateMap() (map[string]interface{}, error) {
Ildar Svetlovd2139692020-11-10 15:16:16 +0400293 if opts.TemplateOpts == nil {
294 return nil, ErrTemplateRequired{}
295 }
296 return toStackUpdateMap(opts)
297}
298
299// ToStackUpdatePatchMap calls the private function toStackUpdateMap
300// directly.
301func (opts UpdateOpts) ToStackUpdatePatchMap() (map[string]interface{}, error) {
302 return toStackUpdateMap(opts)
303}
304
305// ToStackUpdateMap casts a CreateOpts struct to a map.
306func toStackUpdateMap(opts UpdateOpts) (map[string]interface{}, error) {
Jon Perrittfea90732016-03-15 02:57:05 -0500307 b, err := gophercloud.BuildRequestBody(opts, "")
308 if err != nil {
309 return nil, err
310 }
Pratik Mallya5fddb2a2015-09-14 14:04:49 -0500311
Jon Perrittfea90732016-03-15 02:57:05 -0500312 files := make(map[string]string)
Ildar Svetlovd2139692020-11-10 15:16:16 +0400313
314 if opts.TemplateOpts != nil {
315 if err := opts.TemplateOpts.Parse(); err != nil {
316 return nil, err
317 }
318
319 if err := opts.TemplateOpts.getFileContents(opts.TemplateOpts.Parsed, ignoreIfTemplate, true); err != nil {
320 return nil, err
321 }
322 opts.TemplateOpts.fixFileRefs()
323 b["template"] = string(opts.TemplateOpts.Bin)
324
325 for k, v := range opts.TemplateOpts.Files {
326 files[k] = v
327 }
Jon Perritt35e27e42014-12-05 11:10:46 -0700328 }
329
Pratik Mallya5fddb2a2015-09-14 14:04:49 -0500330 if opts.EnvironmentOpts != nil {
331 if err := opts.EnvironmentOpts.Parse(); err != nil {
332 return nil, err
333 }
Pratik Mallyaa979f5b2015-09-22 03:10:55 -0500334 if err := opts.EnvironmentOpts.getRRFileContents(ignoreIfEnvironment); err != nil {
Pratik Mallya5fddb2a2015-09-14 14:04:49 -0500335 return nil, err
336 }
Pratik Mallyaa979f5b2015-09-22 03:10:55 -0500337 opts.EnvironmentOpts.fixFileRefs()
Pratik Mallya5fddb2a2015-09-14 14:04:49 -0500338 for k, v := range opts.EnvironmentOpts.Files {
Jon Perrittfea90732016-03-15 02:57:05 -0500339 files[k] = v
Pratik Mallya5fddb2a2015-09-14 14:04:49 -0500340 }
Jon Perrittfea90732016-03-15 02:57:05 -0500341 b["environment"] = string(opts.EnvironmentOpts.Bin)
Jon Perritt35e27e42014-12-05 11:10:46 -0700342 }
343
Jon Perrittfea90732016-03-15 02:57:05 -0500344 if len(files) > 0 {
345 b["files"] = files
Jon Perritt35e27e42014-12-05 11:10:46 -0700346 }
347
Pratik Mallya827c03e2015-09-17 00:10:47 -0500348 if opts.Tags != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500349 b["tags"] = strings.Join(opts.Tags, ",")
Pratik Mallya827c03e2015-09-17 00:10:47 -0500350 }
351
Jon Perrittfea90732016-03-15 02:57:05 -0500352 return b, nil
Jon Perritt35e27e42014-12-05 11:10:46 -0700353}
354
Ildar Svetlovd2139692020-11-10 15:16:16 +0400355// Update accepts an UpdateOpts struct and updates an existing stack using the
356// http PUT verb with the values provided. opts.TemplateOpts is required.
Jon Perritt3860b512016-03-29 12:01:48 -0500357func Update(c *gophercloud.ServiceClient, stackName, stackID string, opts UpdateOptsBuilder) (r UpdateResult) {
Jon Perrittfea90732016-03-15 02:57:05 -0500358 b, err := opts.ToStackUpdateMap()
Jon Perritt35e27e42014-12-05 11:10:46 -0700359 if err != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500360 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500361 return
Jon Perritt35e27e42014-12-05 11:10:46 -0700362 }
Jon Perrittfea90732016-03-15 02:57:05 -0500363 _, r.Err = c.Put(updateURL(c, stackName, stackID), b, nil, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500364 return
Jon Perritt35e27e42014-12-05 11:10:46 -0700365}
366
Ildar Svetlovd2139692020-11-10 15:16:16 +0400367// Update accepts an UpdateOpts struct and updates an existing stack using the
368// http PATCH verb with the values provided. opts.TemplateOpts is not required.
369func UpdatePatch(c *gophercloud.ServiceClient, stackName, stackID string, opts UpdatePatchOptsBuilder) (r UpdateResult) {
370 b, err := opts.ToStackUpdatePatchMap()
371 if err != nil {
372 r.Err = err
373 return
374 }
375 _, r.Err = c.Patch(updateURL(c, stackName, stackID), b, nil, nil)
376 return
377}
378
Jon Perritt35e27e42014-12-05 11:10:46 -0700379// Delete deletes a stack based on the stack name and stack ID.
Jon Perritt3860b512016-03-29 12:01:48 -0500380func Delete(c *gophercloud.ServiceClient, stackName, stackID string) (r DeleteResult) {
Jon Perrittfea90732016-03-15 02:57:05 -0500381 _, r.Err = c.Delete(deleteURL(c, stackName, stackID), nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500382 return
Jon Perritt35e27e42014-12-05 11:10:46 -0700383}
384
385// PreviewOptsBuilder is the interface options structs have to satisfy in order
386// to be used in the Preview operation in this package.
387type PreviewOptsBuilder interface {
388 ToStackPreviewMap() (map[string]interface{}, error)
389}
390
391// PreviewOpts contains the common options struct used in this package's Preview
392// operation.
393type PreviewOpts struct {
Jon Perrittfea90732016-03-15 02:57:05 -0500394 // The name of the stack. It must start with an alphabetic character.
395 Name string `json:"stack_name" required:"true"`
396 // The timeout for stack creation in minutes.
397 Timeout int `json:"timeout_mins" required:"true"`
398 // A structure that contains either the template file or url. Call the
Pratik Mallya5fddb2a2015-09-14 14:04:49 -0500399 // associated methods to extract the information relevant to send in a create request.
Jon Perrittfea90732016-03-15 02:57:05 -0500400 TemplateOpts *Template `json:"-" required:"true"`
401 // Enables or disables deletion of all stack resources when a stack
Jon Perritt37f97742015-02-04 18:55:05 -0700402 // creation fails. Default is true, meaning all resources are not deleted when
403 // stack creation fails.
Jon Perrittfea90732016-03-15 02:57:05 -0500404 DisableRollback *bool `json:"disable_rollback,omitempty"`
405 // A structure that contains details for the environment of the stack.
406 EnvironmentOpts *Environment `json:"-"`
407 // User-defined parameters to pass to the template.
Ildar Svetlovd2139692020-11-10 15:16:16 +0400408 Parameters map[string]interface{} `json:"parameters,omitempty"`
Jon Perritt35e27e42014-12-05 11:10:46 -0700409}
410
411// ToStackPreviewMap casts a PreviewOpts struct to a map.
412func (opts PreviewOpts) ToStackPreviewMap() (map[string]interface{}, error) {
Jon Perrittfea90732016-03-15 02:57:05 -0500413 b, err := gophercloud.BuildRequestBody(opts, "")
414 if err != nil {
Jon Perritt58611da2016-03-09 00:49:57 -0600415 return nil, err
Jon Perritt35e27e42014-12-05 11:10:46 -0700416 }
Jon Perritt35e27e42014-12-05 11:10:46 -0700417
Jon Perrittfea90732016-03-15 02:57:05 -0500418 if err := opts.TemplateOpts.Parse(); err != nil {
419 return nil, err
Pratik Mallya5fddb2a2015-09-14 14:04:49 -0500420 }
Jon Perrittfea90732016-03-15 02:57:05 -0500421
422 if err := opts.TemplateOpts.getFileContents(opts.TemplateOpts.Parsed, ignoreIfTemplate, true); err != nil {
423 return nil, err
424 }
425 opts.TemplateOpts.fixFileRefs()
426 b["template"] = string(opts.TemplateOpts.Bin)
427
428 files := make(map[string]string)
429 for k, v := range opts.TemplateOpts.Files {
430 files[k] = v
Jon Perritt35e27e42014-12-05 11:10:46 -0700431 }
432
Pratik Mallya5fddb2a2015-09-14 14:04:49 -0500433 if opts.EnvironmentOpts != nil {
434 if err := opts.EnvironmentOpts.Parse(); err != nil {
435 return nil, err
436 }
Pratik Mallyaa979f5b2015-09-22 03:10:55 -0500437 if err := opts.EnvironmentOpts.getRRFileContents(ignoreIfEnvironment); err != nil {
Pratik Mallya5fddb2a2015-09-14 14:04:49 -0500438 return nil, err
439 }
Pratik Mallyaa979f5b2015-09-22 03:10:55 -0500440 opts.EnvironmentOpts.fixFileRefs()
Pratik Mallya5fddb2a2015-09-14 14:04:49 -0500441 for k, v := range opts.EnvironmentOpts.Files {
Jon Perrittfea90732016-03-15 02:57:05 -0500442 files[k] = v
Pratik Mallya5fddb2a2015-09-14 14:04:49 -0500443 }
Jon Perrittfea90732016-03-15 02:57:05 -0500444 b["environment"] = string(opts.EnvironmentOpts.Bin)
Jon Perritt35e27e42014-12-05 11:10:46 -0700445 }
Pratik Mallya5fddb2a2015-09-14 14:04:49 -0500446
Jon Perrittfea90732016-03-15 02:57:05 -0500447 if len(files) > 0 {
448 b["files"] = files
Jon Perritt35e27e42014-12-05 11:10:46 -0700449 }
Pratik Mallya5fddb2a2015-09-14 14:04:49 -0500450
Jon Perrittfea90732016-03-15 02:57:05 -0500451 return b, nil
Jon Perritt35e27e42014-12-05 11:10:46 -0700452}
453
454// Preview accepts a PreviewOptsBuilder interface and creates a preview of a stack using the values
455// provided.
Jon Perritt3860b512016-03-29 12:01:48 -0500456func Preview(c *gophercloud.ServiceClient, opts PreviewOptsBuilder) (r PreviewResult) {
Jon Perrittfea90732016-03-15 02:57:05 -0500457 b, err := opts.ToStackPreviewMap()
Jon Perritt35e27e42014-12-05 11:10:46 -0700458 if err != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500459 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500460 return
Jon Perritt35e27e42014-12-05 11:10:46 -0700461 }
Jon Perrittfea90732016-03-15 02:57:05 -0500462 _, r.Err = c.Post(previewURL(c), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford1d27afa2015-03-24 16:20:45 +0100463 OkCodes: []int{200},
Jon Perritt35e27e42014-12-05 11:10:46 -0700464 })
jrperritt29ae6b32016-04-13 12:59:37 -0500465 return
Jon Perritt35e27e42014-12-05 11:10:46 -0700466}
467
468// Abandon deletes the stack with the provided stackName and stackID, but leaves its
469// resources intact, and returns data describing the stack and its resources.
Jon Perritt3860b512016-03-29 12:01:48 -0500470func Abandon(c *gophercloud.ServiceClient, stackName, stackID string) (r AbandonResult) {
Jon Perrittfea90732016-03-15 02:57:05 -0500471 _, r.Err = c.Delete(abandonURL(c, stackName, stackID), &gophercloud.RequestOpts{
472 JSONResponse: &r.Body,
Ash Wilsondecfed72015-02-13 09:14:55 -0500473 OkCodes: []int{200},
Jon Perritt35e27e42014-12-05 11:10:46 -0700474 })
jrperritt29ae6b32016-04-13 12:59:37 -0500475 return
Jon Perritt35e27e42014-12-05 11:10:46 -0700476}