openstack adopt stack op and unit test
diff --git a/openstack/orchestration/v1/stacks/requests.go b/openstack/orchestration/v1/stacks/requests.go
index 8a65793..8d27e7b 100644
--- a/openstack/orchestration/v1/stacks/requests.go
+++ b/openstack/orchestration/v1/stacks/requests.go
@@ -131,15 +131,39 @@
// AdoptOpts is the common options struct used in this package's Adopt
// operation.
type AdoptOpts struct {
- AdoptStackData string
+ // (REQUIRED) Existing resources data represented as a string to add to the
+ // new stack. Data returned by Abandon could be provided as AdoptsStackData.
+ AdoptStackData string
+ // (REQUIRED) The name of the stack. It must start with an alphabetic character.
+ Name string
+ // (REQUIRED) The timeout for stack creation in minutes.
+ Timeout int
+ // (OPTIONAL; REQUIRED IF Template IS EMPTY) The URL of the template to instantiate.
+ // This value is ignored if Template is supplied inline.
+ TemplateURL string
+ // (OPTIONAL; REQUIRED IF TemplateURL IS EMPTY) A template to instantiate. The value
+ // is a stringified version of the JSON/YAML template. Since the template will likely
+ // be located in a file, one way to set this variable is by using ioutil.ReadFile:
+ // import "io/ioutil"
+ // var opts stacks.CreateOpts
+ // b, err := ioutil.ReadFile("path/to/you/template/file.json")
+ // if err != nil {
+ // // handle error...
+ // }
+ // opts.Template = string(b)
+ Template string
+ // (OPTIONAL) Enables or disables deletion of all stack resources when a stack
+ // creation fails. Default is true, meaning all resources are not deleted when
+ // stack creation fails.
DisableRollback *bool
- Environment string
- Files map[string]interface{}
- Name string
- Parameters map[string]string
- Template string
- TemplateURL string
- Timeout int
+ // (OPTIONAL) A stringified JSON environment for the stack.
+ Environment string
+ // (OPTIONAL) A map that maps file names to file contents. It can also be used
+ // to pass provider template contents. Example:
+ // Files: `{"myfile": "#!/bin/bash\necho 'Hello world' > /root/testfile.txt"}`
+ Files map[string]interface{}
+ // (OPTIONAL) User-defined parameters to pass to the template.
+ Parameters map[string]string
}
// ToStackAdoptMap casts a CreateOpts struct to a map.
@@ -178,17 +202,18 @@
s["parameters"] = opts.Parameters
}
- if opts.Timeout != 0 {
- s["timeout_mins"] = opts.Timeout
+ if opts.Timeout == 0 {
+ return nil, errors.New("Required field 'Timeout' not provided.")
}
+ s["timeout_mins"] = opts.Timeout
return map[string]interface{}{"stack": s}, nil
}
// Adopt accepts an AdoptOpts struct and creates a new stack using the resources
// from another stack.
-func Adopt(c *gophercloud.ServiceClient, opts AdoptOptsBuilder) CreateResult {
- var res CreateResult
+func Adopt(c *gophercloud.ServiceClient, opts AdoptOptsBuilder) AdoptResult {
+ var res AdoptResult
reqBody, err := opts.ToStackAdoptMap()
if err != nil {
diff --git a/openstack/orchestration/v1/stacks/requests_test.go b/openstack/orchestration/v1/stacks/requests_test.go
index 1d54eea..74df24e 100644
--- a/openstack/orchestration/v1/stacks/requests_test.go
+++ b/openstack/orchestration/v1/stacks/requests_test.go
@@ -50,3 +50,48 @@
expected := CreateExpected
th.AssertDeepEquals(t, expected, actual)
}
+
+func TestAdoptStack(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ HandleCreateSuccessfully(t, CreateOutput)
+
+ adoptOpts := AdoptOpts{
+ AdoptStackData: `{environment{parameters{}}}`,
+ Name: "stackcreated",
+ Timeout: 60,
+ Template: `
+ {
+ "stack_name": "postman_stack",
+ "template": {
+ "heat_template_version": "2013-05-23",
+ "description": "Simple template to test heat commands",
+ "parameters": {
+ "flavor": {
+ "default": "m1.tiny",
+ "type": "string"
+ }
+ },
+ "resources": {
+ "hello_world": {
+ "type":"OS::Nova::Server",
+ "properties": {
+ "key_name": "heat_key",
+ "flavor": {
+ "get_param": "flavor"
+ },
+ "image": "ad091b52-742f-469e-8f3c-fd81cadf0743",
+ "user_data": "#!/bin/bash -xv\necho \"hello world\" > /root/hello-world.txt\n"
+ }
+ }
+ }
+ }
+ }`,
+ DisableRollback: Disable,
+ }
+ actual, err := Adopt(fake.ServiceClient(), adoptOpts).Extract()
+ th.AssertNoErr(t, err)
+
+ expected := CreateExpected
+ th.AssertDeepEquals(t, expected, actual)
+}
diff --git a/openstack/orchestration/v1/stacks/results.go b/openstack/orchestration/v1/stacks/results.go
index 5f9c48e..62f4f6b 100644
--- a/openstack/orchestration/v1/stacks/results.go
+++ b/openstack/orchestration/v1/stacks/results.go
@@ -34,7 +34,7 @@
}
type AdoptResult struct {
- gophercloud.Result
+ CreateResult
}
// StackPage is a pagination.Pager that is returned from a call to the List function.