Jon Perritt | f799b94 | 2015-02-09 11:23:28 -0700 | [diff] [blame] | 1 | package stacktemplates |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | |
Jon Perritt | f799b94 | 2015-02-09 11:23:28 -0700 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud" |
| 7 | ) |
| 8 | |
| 9 | // Get retreives data for the given stack template. |
| 10 | func Get(c *gophercloud.ServiceClient, stackName, stackID string) GetResult { |
| 11 | var res GetResult |
Ash Wilson | decfed7 | 2015-02-13 09:14:55 -0500 | [diff] [blame^] | 12 | _, res.Err = c.Request("GET", getURL(c, stackName, stackID), gophercloud.RequestOpts{ |
| 13 | JSONResponse: &res.Body, |
| 14 | OkCodes: []int{200}, |
Jon Perritt | f799b94 | 2015-02-09 11:23:28 -0700 | [diff] [blame] | 15 | }) |
| 16 | return res |
| 17 | } |
| 18 | |
| 19 | // ValidateOptsBuilder describes struct types that can be accepted by the Validate call. |
| 20 | // The ValidateOpts struct in this package does. |
| 21 | type ValidateOptsBuilder interface { |
| 22 | ToStackTemplateValidateMap() (map[string]interface{}, error) |
| 23 | } |
| 24 | |
| 25 | // ValidateOpts specifies the template validation parameters. |
| 26 | type ValidateOpts struct { |
| 27 | Template map[string]interface{} |
| 28 | TemplateURL string |
| 29 | } |
| 30 | |
| 31 | // ToStackTemplateValidateMap assembles a request body based on the contents of a ValidateOpts. |
| 32 | func (opts ValidateOpts) ToStackTemplateValidateMap() (map[string]interface{}, error) { |
| 33 | vo := make(map[string]interface{}) |
| 34 | if opts.Template != nil { |
| 35 | vo["template"] = opts.Template |
| 36 | return vo, nil |
| 37 | } |
| 38 | if opts.TemplateURL != "" { |
| 39 | vo["template_url"] = opts.TemplateURL |
| 40 | return vo, nil |
| 41 | } |
| 42 | return vo, fmt.Errorf("One of Template or TemplateURL is required.") |
| 43 | } |
| 44 | |
| 45 | // Validate validates the given stack template. |
| 46 | func Validate(c *gophercloud.ServiceClient, opts ValidateOptsBuilder) ValidateResult { |
| 47 | var res ValidateResult |
| 48 | |
| 49 | reqBody, err := opts.ToStackTemplateValidateMap() |
| 50 | if err != nil { |
| 51 | res.Err = err |
| 52 | return res |
| 53 | } |
| 54 | |
Ash Wilson | decfed7 | 2015-02-13 09:14:55 -0500 | [diff] [blame^] | 55 | _, res.Err = c.Request("POST", validateURL(c), gophercloud.RequestOpts{ |
| 56 | JSONBody: reqBody, |
| 57 | JSONResponse: &res.Body, |
| 58 | OkCodes: []int{200}, |
Jon Perritt | f799b94 | 2015-02-09 11:23:28 -0700 | [diff] [blame] | 59 | }) |
| 60 | return res |
| 61 | } |