objectstorage v1 and orchestration v1 struct tags
diff --git a/openstack/objectstorage/v1/accounts/requests.go b/openstack/objectstorage/v1/accounts/requests.go
index f50cf97..41479a2 100644
--- a/openstack/objectstorage/v1/accounts/requests.go
+++ b/openstack/objectstorage/v1/accounts/requests.go
@@ -24,30 +24,27 @@
// all the headers that are returned (including the metadata), call the
// ExtractHeader method on the GetResult.
func Get(c *gophercloud.ServiceClient, opts GetOptsBuilder) GetResult {
- var res GetResult
- h := c.AuthenticatedHeaders()
-
+ var r GetResult
+ h := make(map[string]string)
if opts != nil {
headers, err := opts.ToAccountGetMap()
if err != nil {
- res.Err = err
- return res
+ r.Err = err
+ return r
}
-
for k, v := range headers {
h[k] = v
}
}
-
resp, err := c.Request("HEAD", getURL(c), &gophercloud.RequestOpts{
MoreHeaders: h,
OkCodes: []int{204},
})
if resp != nil {
- res.Header = resp.Header
+ r.Header = resp.Header
}
- res.Err = err
- return res
+ r.Err = err
+ return r
}
// UpdateOptsBuilder allows extensions to add additional headers to the Update
@@ -81,27 +78,25 @@
// Update is a function that creates, updates, or deletes an account's metadata.
// To extract the headers returned, call the Extract method on the UpdateResult.
func Update(c *gophercloud.ServiceClient, opts UpdateOptsBuilder) UpdateResult {
- var res UpdateResult
+ var r UpdateResult
h := make(map[string]string)
-
if opts != nil {
headers, err := opts.ToAccountUpdateMap()
if err != nil {
- res.Err = err
- return res
+ r.Err = err
+ return r
}
for k, v := range headers {
h[k] = v
}
}
-
resp, err := c.Request("POST", updateURL(c), &gophercloud.RequestOpts{
MoreHeaders: h,
OkCodes: []int{201, 202, 204},
})
if resp != nil {
- res.Header = resp.Header
+ r.Header = resp.Header
}
- res.Err = err
- return res
+ r.Err = err
+ return r
}
diff --git a/openstack/objectstorage/v1/containers/requests.go b/openstack/objectstorage/v1/containers/requests.go
index b2160c3..f575ea7 100644
--- a/openstack/objectstorage/v1/containers/requests.go
+++ b/openstack/objectstorage/v1/containers/requests.go
@@ -26,10 +26,7 @@
// representing whether to list complete information for each container.
func (opts ListOpts) ToContainerListParams() (bool, string, error) {
q, err := gophercloud.BuildQueryString(opts)
- if err != nil {
- return false, "", err
- }
- return opts.Full, q.String(), nil
+ return opts.Full, q.String(), err
}
// List is a function that retrieves containers associated with the account as
@@ -51,13 +48,11 @@
}
}
- createPage := func(r pagination.PageResult) pagination.Page {
+ pager := pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
p := ContainerPage{pagination.MarkerPageBase{PageResult: r}}
p.MarkerPageBase.Owner = p
return p
- }
-
- pager := pagination.NewPager(c, url, createPage)
+ })
pager.Headers = headers
return pager
}
@@ -95,37 +90,34 @@
// Create is a function that creates a new container.
func Create(c *gophercloud.ServiceClient, containerName string, opts CreateOptsBuilder) CreateResult {
- var res CreateResult
- h := c.AuthenticatedHeaders()
-
+ var r CreateResult
+ h := make(map[string]string)
if opts != nil {
headers, err := opts.ToContainerCreateMap()
if err != nil {
- res.Err = err
- return res
+ r.Err = err
+ return r
}
-
for k, v := range headers {
h[k] = v
}
}
-
resp, err := c.Request("PUT", createURL(c, containerName), &gophercloud.RequestOpts{
MoreHeaders: h,
OkCodes: []int{201, 202, 204},
})
if resp != nil {
- res.Header = resp.Header
+ r.Header = resp.Header
}
- res.Err = err
- return res
+ r.Err = err
+ return r
}
// Delete is a function that deletes a container.
func Delete(c *gophercloud.ServiceClient, containerName string) DeleteResult {
- var res DeleteResult
- _, res.Err = c.Delete(deleteURL(c, containerName), nil)
- return res
+ var r DeleteResult
+ _, r.Err = c.Delete(deleteURL(c, containerName), nil)
+ return r
}
// UpdateOptsBuilder allows extensions to add additional parameters to the
@@ -163,43 +155,41 @@
// Update is a function that creates, updates, or deletes a container's
// metadata.
func Update(c *gophercloud.ServiceClient, containerName string, opts UpdateOptsBuilder) UpdateResult {
- var res UpdateResult
- h := c.AuthenticatedHeaders()
-
+ var r UpdateResult
+ h := make(map[string]string)
if opts != nil {
headers, err := opts.ToContainerUpdateMap()
if err != nil {
- res.Err = err
- return res
+ r.Err = err
+ return r
}
for k, v := range headers {
h[k] = v
}
}
-
resp, err := c.Request("POST", updateURL(c, containerName), &gophercloud.RequestOpts{
MoreHeaders: h,
OkCodes: []int{201, 202, 204},
})
if resp != nil {
- res.Header = resp.Header
+ r.Header = resp.Header
}
- res.Err = err
- return res
+ r.Err = err
+ return r
}
// Get is a function that retrieves the metadata of a container. To extract just
// the custom metadata, pass the GetResult response to the ExtractMetadata
// function.
func Get(c *gophercloud.ServiceClient, containerName string) GetResult {
- var res GetResult
+ var r GetResult
resp, err := c.Request("HEAD", getURL(c, containerName), &gophercloud.RequestOpts{
OkCodes: []int{200, 204},
})
if resp != nil {
- res.Header = resp.Header
+ r.Header = resp.Header
}
- res.Err = err
- return res
+ r.Err = err
+ return r
}
diff --git a/openstack/objectstorage/v1/objects/requests.go b/openstack/objectstorage/v1/objects/requests.go
index d82aeb2..2505bdb 100644
--- a/openstack/objectstorage/v1/objects/requests.go
+++ b/openstack/objectstorage/v1/objects/requests.go
@@ -1,13 +1,12 @@
package objects
import (
- "bufio"
+ "bytes"
"crypto/hmac"
"crypto/md5"
"crypto/sha1"
"fmt"
"io"
- "io/ioutil"
"strings"
"time"
@@ -42,10 +41,7 @@
// representing whether to list complete information for each object.
func (opts ListOpts) ToObjectListParams() (bool, string, error) {
q, err := gophercloud.BuildQueryString(opts)
- if err != nil {
- return false, "", err
- }
- return opts.Full, q.String(), nil
+ return opts.Full, q.String(), err
}
// List is a function that retrieves all objects in a container. It also returns the details
@@ -67,13 +63,11 @@
}
}
- createPage := func(r pagination.PageResult) pagination.Page {
+ pager := pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
p := ObjectPage{pagination.MarkerPageBase{PageResult: r}}
p.MarkerPageBase.Owner = p
return p
- }
-
- pager := pagination.NewPager(c, url, createPage)
+ })
pager.Headers = headers
return pager
}
@@ -114,46 +108,42 @@
// To extract just the content, pass the DownloadResult response to the
// ExtractContent function.
func Download(c *gophercloud.ServiceClient, containerName, objectName string, opts DownloadOptsBuilder) DownloadResult {
- var res DownloadResult
-
+ var r DownloadResult
url := downloadURL(c, containerName, objectName)
- h := c.AuthenticatedHeaders()
-
+ h := make(map[string]string)
if opts != nil {
headers, query, err := opts.ToObjectDownloadParams()
if err != nil {
- res.Err = err
- return res
+ r.Err = err
+ return r
}
-
for k, v := range headers {
h[k] = v
}
-
url += query
}
- resp, err := c.Request("GET", url, &gophercloud.RequestOpts{
+ resp, err := c.Get(url, nil, &gophercloud.RequestOpts{
MoreHeaders: h,
OkCodes: []int{200, 304},
})
if resp != nil {
- res.Header = resp.Header
- res.Body = resp.Body
+ r.Header = resp.Header
+ r.Body = resp.Body
}
- res.Err = err
-
- return res
+ r.Err = err
+ return r
}
// CreateOptsBuilder allows extensions to add additional parameters to the
// Create request.
type CreateOptsBuilder interface {
- ToObjectCreateParams() (map[string]string, string, error)
+ ToObjectCreateParams() (io.Reader, map[string]string, string, error)
}
// CreateOpts is a structure that holds parameters for creating an object.
type CreateOpts struct {
+ Content io.Reader
Metadata map[string]string
ContentDisposition string `h:"Content-Disposition"`
ContentEncoding string `h:"Content-Encoding"`
@@ -174,81 +164,61 @@
// ToObjectCreateParams formats a CreateOpts into a query string and map of
// headers.
-func (opts CreateOpts) ToObjectCreateParams() (map[string]string, string, error) {
+func (opts CreateOpts) ToObjectCreateParams() (io.Reader, map[string]string, string, error) {
q, err := gophercloud.BuildQueryString(opts)
if err != nil {
- return nil, "", err
+ return nil, nil, "", err
}
h, err := gophercloud.BuildHeaders(opts)
if err != nil {
- return nil, q.String(), err
+ return nil, nil, "", err
}
for k, v := range opts.Metadata {
h["X-Object-Meta-"+k] = v
}
- return h, q.String(), nil
+ hash := md5.New()
+ buf := bytes.NewBuffer([]byte{})
+ _, err = io.Copy(io.MultiWriter(hash, buf), opts.Content)
+ if err != nil {
+ return nil, nil, "", err
+ }
+ localChecksum := fmt.Sprintf("%x", hash.Sum(nil))
+ h["ETag"] = localChecksum
+
+ return buf, h, q.String(), nil
}
// Create is a function that creates a new object or replaces an existing object. If the returned response's ETag
// header fails to match the local checksum, the failed request will automatically be retried up to a maximum of 3 times.
-func Create(c *gophercloud.ServiceClient, containerName, objectName string, content io.ReadSeeker, opts CreateOptsBuilder) CreateResult {
- var res CreateResult
-
+func Create(c *gophercloud.ServiceClient, containerName, objectName string, opts CreateOptsBuilder) CreateResult {
+ var r CreateResult
url := createURL(c, containerName, objectName)
h := make(map[string]string)
-
+ var b io.Reader
if opts != nil {
- headers, query, err := opts.ToObjectCreateParams()
+ tmpB, headers, query, err := opts.ToObjectCreateParams()
if err != nil {
- res.Err = err
- return res
+ r.Err = err
+ return r
}
-
for k, v := range headers {
h[k] = v
}
-
url += query
+ b = tmpB
}
- hash := md5.New()
- bufioReader := bufio.NewReader(io.TeeReader(content, hash))
- io.Copy(ioutil.Discard, bufioReader)
- localChecksum := hash.Sum(nil)
-
- h["ETag"] = fmt.Sprintf("%x", localChecksum)
-
- _, err := content.Seek(0, 0)
- if err != nil {
- res.Err = err
- return res
- }
-
- ropts := &gophercloud.RequestOpts{
- RawBody: content,
+ resp, err := c.Put(url, nil, nil, &gophercloud.RequestOpts{
+ RawBody: b,
MoreHeaders: h,
- }
-
- resp, err := c.Request("PUT", url, ropts)
- if err != nil {
- res.Err = err
- return res
- }
+ })
+ r.Err = err
if resp != nil {
- res.Header = resp.Header
- if resp.Header.Get("ETag") == fmt.Sprintf("%x", localChecksum) {
- res.Err = err
- return res
- }
- err := ErrWrongChecksum{}
- err.Function = "objects.Create"
- res.Err = err
-
+ r.Header = resp.Header
}
-
- return res
+ return r
}
// CopyOptsBuilder allows extensions to add additional parameters to the
@@ -264,17 +234,11 @@
ContentDisposition string `h:"Content-Disposition"`
ContentEncoding string `h:"Content-Encoding"`
ContentType string `h:"Content-Type"`
- Destination string `h:"Destination,required"`
+ Destination string `h:"Destination" required:"true"`
}
// ToObjectCopyMap formats a CopyOpts into a map of headers.
func (opts CopyOpts) ToObjectCopyMap() (map[string]string, error) {
- if opts.Destination == "" {
- err := gophercloud.ErrMissingInput{}
- err.Function = "objects.ToObjectCopyMap"
- err.Argument = "objects.CopyOpts.Destination"
- return nil, err
- }
h, err := gophercloud.BuildHeaders(opts)
if err != nil {
return nil, err
@@ -287,13 +251,12 @@
// Copy is a function that copies one object to another.
func Copy(c *gophercloud.ServiceClient, containerName, objectName string, opts CopyOptsBuilder) CopyResult {
- var res CopyResult
- h := c.AuthenticatedHeaders()
-
+ var r CopyResult
+ h := make(map[string]string)
headers, err := opts.ToObjectCopyMap()
if err != nil {
- res.Err = err
- return res
+ r.Err = err
+ return r
}
for k, v := range headers {
@@ -306,10 +269,10 @@
OkCodes: []int{201},
})
if resp != nil {
- res.Header = resp.Header
+ r.Header = resp.Header
}
- res.Err = err
- return res
+ r.Err = err
+ return r
}
// DeleteOptsBuilder allows extensions to add additional parameters to the
@@ -326,32 +289,27 @@
// ToObjectDeleteQuery formats a DeleteOpts into a query string.
func (opts DeleteOpts) ToObjectDeleteQuery() (string, error) {
q, err := gophercloud.BuildQueryString(opts)
- if err != nil {
- return "", err
- }
- return q.String(), nil
+ return q.String(), err
}
// Delete is a function that deletes an object.
func Delete(c *gophercloud.ServiceClient, containerName, objectName string, opts DeleteOptsBuilder) DeleteResult {
- var res DeleteResult
+ var r DeleteResult
url := deleteURL(c, containerName, objectName)
-
if opts != nil {
query, err := opts.ToObjectDeleteQuery()
if err != nil {
- res.Err = err
- return res
+ r.Err = err
+ return r
}
url += query
}
-
resp, err := c.Delete(url, nil)
if resp != nil {
- res.Header = resp.Header
+ r.Header = resp.Header
}
- res.Err = err
- return res
+ r.Err = err
+ return r
}
// GetOptsBuilder allows extensions to add additional parameters to the
@@ -369,35 +327,30 @@
// ToObjectGetQuery formats a GetOpts into a query string.
func (opts GetOpts) ToObjectGetQuery() (string, error) {
q, err := gophercloud.BuildQueryString(opts)
- if err != nil {
- return "", err
- }
- return q.String(), nil
+ return q.String(), err
}
// Get is a function that retrieves the metadata of an object. To extract just the custom
// metadata, pass the GetResult response to the ExtractMetadata function.
func Get(c *gophercloud.ServiceClient, containerName, objectName string, opts GetOptsBuilder) GetResult {
- var res GetResult
+ var r GetResult
url := getURL(c, containerName, objectName)
-
if opts != nil {
query, err := opts.ToObjectGetQuery()
if err != nil {
- res.Err = err
- return res
+ r.Err = err
+ return r
}
url += query
}
-
resp, err := c.Request("HEAD", url, &gophercloud.RequestOpts{
OkCodes: []int{200, 204},
})
if resp != nil {
- res.Header = resp.Header
+ r.Header = resp.Header
}
- res.Err = err
- return res
+ r.Err = err
+ return r
}
// UpdateOptsBuilder allows extensions to add additional parameters to the
@@ -432,30 +385,28 @@
// Update is a function that creates, updates, or deletes an object's metadata.
func Update(c *gophercloud.ServiceClient, containerName, objectName string, opts UpdateOptsBuilder) UpdateResult {
- var res UpdateResult
+ var r UpdateResult
h := c.AuthenticatedHeaders()
-
if opts != nil {
headers, err := opts.ToObjectUpdateMap()
if err != nil {
- res.Err = err
- return res
+ r.Err = err
+ return r
}
for k, v := range headers {
h[k] = v
}
}
-
url := updateURL(c, containerName, objectName)
- resp, err := c.Request("POST", url, &gophercloud.RequestOpts{
+ resp, err := c.Post(url, nil, nil, &gophercloud.RequestOpts{
MoreHeaders: h,
})
if resp != nil {
- res.Header = resp.Header
+ r.Header = resp.Header
}
- res.Err = err
- return res
+ r.Err = err
+ return r
}
// HTTPMethod represents an HTTP method string (e.g. "GET").
diff --git a/openstack/objectstorage/v1/objects/results.go b/openstack/objectstorage/v1/objects/results.go
index c0d532b..3cfe1f4 100644
--- a/openstack/objectstorage/v1/objects/results.go
+++ b/openstack/objectstorage/v1/objects/results.go
@@ -200,12 +200,16 @@
// CreateResult represents the result of a create operation.
type CreateResult struct {
+ checksum string
gophercloud.HeaderResult
}
// Extract will return a struct of headers returned from a call to Create. To obtain
// a map of headers, call the ExtractHeader method on the CreateResult.
func (r CreateResult) Extract() (*CreateHeader, error) {
+ //if r.Header.Get("ETag") != fmt.Sprintf("%x", localChecksum) {
+ // return nil, ErrWrongChecksum{}
+ //}
var s *CreateHeader
err := r.ExtractInto(&s)
return s, err
diff --git a/openstack/orchestration/v1/buildinfo/requests.go b/openstack/orchestration/v1/buildinfo/requests.go
index 9d28760..ad12a73 100644
--- a/openstack/orchestration/v1/buildinfo/requests.go
+++ b/openstack/orchestration/v1/buildinfo/requests.go
@@ -4,7 +4,7 @@
// Get retreives data for the given stack template.
func Get(c *gophercloud.ServiceClient) GetResult {
- var res GetResult
- _, res.Err = c.Get(getURL(c), &res.Body, nil)
- return res
+ var r GetResult
+ _, r.Err = c.Get(getURL(c), &r.Body, nil)
+ return r
}
diff --git a/openstack/orchestration/v1/stackevents/requests.go b/openstack/orchestration/v1/stackevents/requests.go
index 6112571..794d4c8 100644
--- a/openstack/orchestration/v1/stackevents/requests.go
+++ b/openstack/orchestration/v1/stackevents/requests.go
@@ -7,12 +7,9 @@
// Find retrieves stack events for the given stack name.
func Find(c *gophercloud.ServiceClient, stackName string) FindResult {
- var res FindResult
-
- _, res.Err = c.Request("GET", findURL(c, stackName), &gophercloud.RequestOpts{
- JSONResponse: &res.Body,
- })
- return res
+ var r FindResult
+ _, r.Err = c.Get(findURL(c, stackName), &r.Body, nil)
+ return r
}
// SortDir is a type for specifying in which direction to sort a list of events.
@@ -102,16 +99,12 @@
// ToStackEventListQuery formats a ListOpts into a query string.
func (opts ListOpts) ToStackEventListQuery() (string, error) {
q, err := gophercloud.BuildQueryString(opts)
- if err != nil {
- return "", err
- }
- return q.String(), nil
+ return q.String(), err
}
// List makes a request against the API to list resources for the given stack.
func List(client *gophercloud.ServiceClient, stackName, stackID string, opts ListOptsBuilder) pagination.Pager {
url := listURL(client, stackName, stackID)
-
if opts != nil {
query, err := opts.ToStackEventListQuery()
if err != nil {
@@ -119,14 +112,11 @@
}
url += query
}
-
- createPageFn := func(r pagination.PageResult) pagination.Page {
+ return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
p := EventPage{pagination.MarkerPageBase{PageResult: r}}
p.MarkerPageBase.Owner = p
return p
- }
-
- return pagination.NewPager(client, url, createPageFn)
+ })
}
// ListResourceEventsOptsBuilder allows extensions to add additional parameters to the
@@ -166,16 +156,12 @@
// ToResourceEventListQuery formats a ListResourceEventsOpts into a query string.
func (opts ListResourceEventsOpts) ToResourceEventListQuery() (string, error) {
q, err := gophercloud.BuildQueryString(opts)
- if err != nil {
- return "", err
- }
- return q.String(), nil
+ return q.String(), err
}
// ListResourceEvents makes a request against the API to list resources for the given stack.
func ListResourceEvents(client *gophercloud.ServiceClient, stackName, stackID, resourceName string, opts ListResourceEventsOptsBuilder) pagination.Pager {
url := listResourceEventsURL(client, stackName, stackID, resourceName)
-
if opts != nil {
query, err := opts.ToResourceEventListQuery()
if err != nil {
@@ -183,21 +169,18 @@
}
url += query
}
-
- createPageFn := func(r pagination.PageResult) pagination.Page {
+ return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
p := EventPage{pagination.MarkerPageBase{PageResult: r}}
p.MarkerPageBase.Owner = p
return p
- }
-
- return pagination.NewPager(client, url, createPageFn)
+ })
}
// Get retreives data for the given stack resource.
func Get(c *gophercloud.ServiceClient, stackName, stackID, resourceName, eventID string) GetResult {
- var res GetResult
- _, res.Err = c.Get(getURL(c, stackName, stackID, resourceName, eventID), &res.Body, &gophercloud.RequestOpts{
+ var r GetResult
+ _, r.Err = c.Get(getURL(c, stackName, stackID, resourceName, eventID), &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{200},
})
- return res
+ return r
}
diff --git a/openstack/orchestration/v1/stackresources/requests.go b/openstack/orchestration/v1/stackresources/requests.go
index 3ce40eb..f33c170 100644
--- a/openstack/orchestration/v1/stackresources/requests.go
+++ b/openstack/orchestration/v1/stackresources/requests.go
@@ -7,13 +7,9 @@
// Find retrieves stack resources for the given stack name.
func Find(c *gophercloud.ServiceClient, stackName string) FindResult {
- var res FindResult
-
- // Send request to API
- _, res.Err = c.Request("GET", findURL(c, stackName), &gophercloud.RequestOpts{
- JSONResponse: &res.Body,
- })
- return res
+ var r FindResult
+ _, res.Err = c.Get(findURL(c, stackName), &res.Body, nil)
+ return r
}
// ListOptsBuilder allows extensions to add additional parameters to the
@@ -32,16 +28,12 @@
// ToStackResourceListQuery formats a ListOpts into a query string.
func (opts ListOpts) ToStackResourceListQuery() (string, error) {
q, err := gophercloud.BuildQueryString(opts)
- if err != nil {
- return "", err
- }
- return q.String(), nil
+ return q.String(), err
}
// List makes a request against the API to list resources for the given stack.
func List(client *gophercloud.ServiceClient, stackName, stackID string, opts ListOptsBuilder) pagination.Pager {
url := listURL(client, stackName, stackID)
-
if opts != nil {
query, err := opts.ToStackResourceListQuery()
if err != nil {
@@ -49,65 +41,42 @@
}
url += query
}
-
- createPageFn := func(r pagination.PageResult) pagination.Page {
+ return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
return ResourcePage{pagination.SinglePageBase(r)}
- }
-
- return pagination.NewPager(client, url, createPageFn)
+ })
}
// Get retreives data for the given stack resource.
func Get(c *gophercloud.ServiceClient, stackName, stackID, resourceName string) GetResult {
- var res GetResult
-
- // Send request to API
- _, res.Err = c.Get(getURL(c, stackName, stackID, resourceName), &res.Body, &gophercloud.RequestOpts{
- OkCodes: []int{200},
- })
- return res
+ var r GetResult
+ _, r.Err = c.Get(getURL(c, stackName, stackID, resourceName), &r.Body, nil)
+ return r
}
// Metadata retreives the metadata for the given stack resource.
func Metadata(c *gophercloud.ServiceClient, stackName, stackID, resourceName string) MetadataResult {
- var res MetadataResult
-
- // Send request to API
- _, res.Err = c.Get(metadataURL(c, stackName, stackID, resourceName), &res.Body, &gophercloud.RequestOpts{
- OkCodes: []int{200},
- })
- return res
+ var r MetadataResult
+ _, r.Err = c.Get(metadataURL(c, stackName, stackID, resourceName), &r.Body, nil)
+ return r
}
// ListTypes makes a request against the API to list resource types.
func ListTypes(client *gophercloud.ServiceClient) pagination.Pager {
- url := listTypesURL(client)
-
- createPageFn := func(r pagination.PageResult) pagination.Page {
+ return pagination.NewPager(client, listTypesURL(client), func(r pagination.PageResult) pagination.Page {
return ResourceTypePage{pagination.SinglePageBase(r)}
- }
-
- return pagination.NewPager(client, url, createPageFn)
+ })
}
// Schema retreives the schema for the given resource type.
func Schema(c *gophercloud.ServiceClient, resourceType string) SchemaResult {
- var res SchemaResult
-
- // Send request to API
- _, res.Err = c.Get(schemaURL(c, resourceType), &res.Body, &gophercloud.RequestOpts{
- OkCodes: []int{200},
- })
- return res
+ var r SchemaResult
+ _, r.Err = c.Get(schemaURL(c, resourceType), &r.Body, nil)
+ return r
}
// Template retreives the template representation for the given resource type.
func Template(c *gophercloud.ServiceClient, resourceType string) TemplateResult {
- var res TemplateResult
-
- // Send request to API
- _, res.Err = c.Get(templateURL(c, resourceType), &res.Body, &gophercloud.RequestOpts{
- OkCodes: []int{200},
- })
- return res
+ var r TemplateResult
+ _, r.Err = c.Get(templateURL(c, resourceType), &r.Body, nil)
+ return r
}
diff --git a/openstack/orchestration/v1/stacks/environment.go b/openstack/orchestration/v1/stacks/environment.go
index abaff20..8698918 100644
--- a/openstack/orchestration/v1/stacks/environment.go
+++ b/openstack/orchestration/v1/stacks/environment.go
@@ -1,9 +1,6 @@
package stacks
-import (
- "fmt"
- "strings"
-)
+import "strings"
// Environment is a structure that represents stack environments
type Environment struct {
@@ -26,7 +23,7 @@
}
for key := range e.Parsed {
if _, ok := EnvironmentSections[key]; !ok {
- return fmt.Errorf("Environment has wrong section: %s", key)
+ return ErrInvalidEnvironment{Section: key}
}
}
return nil
diff --git a/openstack/orchestration/v1/stacks/errors.go b/openstack/orchestration/v1/stacks/errors.go
new file mode 100644
index 0000000..cd6c18f
--- /dev/null
+++ b/openstack/orchestration/v1/stacks/errors.go
@@ -0,0 +1,33 @@
+package stacks
+
+import (
+ "fmt"
+
+ "github.com/gophercloud/gophercloud"
+)
+
+type ErrInvalidEnvironment struct {
+ gophercloud.BaseError
+ Section string
+}
+
+func (e ErrInvalidEnvironment) Error() string {
+ return fmt.Sprintf("Environment has wrong section: %s", e.Section)
+}
+
+type ErrInvalidDataFormat struct {
+ gophercloud.BaseError
+}
+
+func (e ErrInvalidDataFormat) Error() string {
+ return fmt.Sprintf("Data in neither json nor yaml format.")
+}
+
+type ErrInvalidTemplateFormatVersion struct {
+ gophercloud.BaseError
+ Version string
+}
+
+func (e ErrInvalidTemplateFormatVersion) Error() string {
+ return fmt.Sprintf("Template format version not found.")
+}
diff --git a/openstack/orchestration/v1/stacks/requests.go b/openstack/orchestration/v1/stacks/requests.go
index 650763c..47d545e 100644
--- a/openstack/orchestration/v1/stacks/requests.go
+++ b/openstack/orchestration/v1/stacks/requests.go
@@ -7,18 +7,6 @@
"github.com/gophercloud/gophercloud/pagination"
)
-// Rollback is used to specify whether or not a stack can be rolled back.
-type Rollback *bool
-
-var (
- disable = true
- // Disable is used to specify that a stack cannot be rolled back.
- Disable Rollback = &disable
- enable = false
- // Enable is used to specify that a stack can be rolled back.
- Enable Rollback = &enable
-)
-
// CreateOptsBuilder is the interface options structs have to satisfy in order
// to be used in the main Create operation in this package. Since many
// extensions decorate or modify the common logic, it is useful for them to
@@ -30,92 +18,45 @@
// CreateOpts is the common options struct used in this package's Create
// operation.
type CreateOpts struct {
- // (REQUIRED) The name of the stack. It must start with an alphabetic character.
- Name string
- // (REQUIRED) A structure that contains either the template file or url. Call the
+ // The name of the stack. It must start with an alphabetic character.
+ Name string `json:"stack_name" required:"true"`
+ // A structure that contains either the template file or url. Call the
// associated methods to extract the information relevant to send in a create request.
- TemplateOpts *Template
- // (DEPRECATED): Please use TemplateOpts for providing the template. If
- // TemplateOpts is provided, TemplateURL will be ignored
- // (OPTIONAL; REQUIRED IF Template IS EMPTY) The URL of the template to instantiate.
- // This value is ignored if Template is supplied inline.
- TemplateURL string
- // (DEPRECATED): Please use TemplateOpts for providing the template. If
- // TemplateOpts is provided, Template will be ignored
- // (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
+ TemplateOpts *Template `json:"-" required:"true"`
+ // 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 Rollback
- // (OPTIONAL) A structure that contains details for the environment of the stack.
- EnvironmentOpts *Environment
- // (DEPRECATED): Please use EnvironmentOpts to provide Environment data
- // (OPTIONAL) A stringified JSON environment for the stack.
- Environment string
- // (DEPRECATED): Files is automatically determined
- // by parsing the template and environment passed as TemplateOpts and
- // EnvironmentOpts respectively.
- // (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
- // (OPTIONAL) The timeout for stack creation in minutes.
- Timeout int
- // (OPTIONAL) A list of tags to assosciate with the Stack
- Tags []string
+ DisableRollback *bool `json:"disable_rollback,omitempty"`
+ // A structure that contains details for the environment of the stack.
+ EnvironmentOpts *Environment `json:"-"`
+ // User-defined parameters to pass to the template.
+ Parameters map[string]string `json:"parameters,omitempty"`
+ // The timeout for stack creation in minutes.
+ Timeout int `json:"timeout_mins,omitempty"`
+ // A list of tags to assosciate with the Stack
+ Tags []string `json:"-"`
}
// ToStackCreateMap casts a CreateOpts struct to a map.
func (opts CreateOpts) ToStackCreateMap() (map[string]interface{}, error) {
- s := make(map[string]interface{})
-
- if opts.Name == "" {
- err := gophercloud.ErrMissingInput{}
- err.Argument = "stacks.CreateOpts.Name"
+ b, err := gophercloud.BuildRequestBody(opts, "")
+ if err != nil {
return nil, err
}
- s["stack_name"] = opts.Name
- Files := make(map[string]string)
- if opts.TemplateOpts == nil {
- if opts.Template != "" {
- s["template"] = opts.Template
- } else if opts.TemplateURL != "" {
- s["template_url"] = opts.TemplateURL
- } else {
- err := gophercloud.ErrMissingInput{}
- err.Argument = "stacks.CreateOpts.Template/stacks.CreateOpts.TemplateURL"
- err.Info = "Either Template or TemplateURL must be provided"
- return nil, err
- }
- } else {
- if err := opts.TemplateOpts.Parse(); err != nil {
- return nil, err
- }
- if err := opts.TemplateOpts.getFileContents(opts.TemplateOpts.Parsed, ignoreIfTemplate, true); err != nil {
- return nil, err
- }
- opts.TemplateOpts.fixFileRefs()
- s["template"] = string(opts.TemplateOpts.Bin)
-
- for k, v := range opts.TemplateOpts.Files {
- Files[k] = v
- }
+ if err := opts.TemplateOpts.Parse(); err != nil {
+ return nil, err
}
- if opts.DisableRollback != nil {
- s["disable_rollback"] = &opts.DisableRollback
+
+ if err := opts.TemplateOpts.getFileContents(opts.TemplateOpts.Parsed, ignoreIfTemplate, true); err != nil {
+ return nil, err
+ }
+ opts.TemplateOpts.fixFileRefs()
+ b["template"] = string(opts.TemplateOpts.Bin)
+
+ files := make(map[string]string)
+ for k, v := range opts.TemplateOpts.Files {
+ files[k] = v
}
if opts.EnvironmentOpts != nil {
@@ -127,50 +68,33 @@
}
opts.EnvironmentOpts.fixFileRefs()
for k, v := range opts.EnvironmentOpts.Files {
- Files[k] = v
+ files[k] = v
}
- s["environment"] = string(opts.EnvironmentOpts.Bin)
- } else if opts.Environment != "" {
- s["environment"] = opts.Environment
+ b["environment"] = string(opts.EnvironmentOpts.Bin)
}
- if opts.Files != nil {
- s["files"] = opts.Files
- } else {
- s["files"] = Files
- }
-
- if opts.DisableRollback != nil {
- s["disable_rollback"] = &opts.DisableRollback
- }
-
- if opts.Parameters != nil {
- s["parameters"] = opts.Parameters
- }
-
- if opts.Timeout != 0 {
- s["timeout_mins"] = opts.Timeout
+ if len(files) > 0 {
+ b["files"] = files
}
if opts.Tags != nil {
- s["tags"] = strings.Join(opts.Tags, ",")
+ b["tags"] = strings.Join(opts.Tags, ",")
}
- return s, nil
+
+ return b, nil
}
// Create accepts a CreateOpts struct and creates a new stack using the values
// provided.
func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
- var res CreateResult
-
- reqBody, err := opts.ToStackCreateMap()
+ var r CreateResult
+ b, err := opts.ToStackCreateMap()
if err != nil {
- res.Err = err
- return res
+ r.Err = err
+ return r
}
-
- _, res.Err = c.Post(createURL(c), reqBody, &res.Body, nil)
- return res
+ _, r.Err = c.Post(createURL(c), b, &r.Body, nil)
+ return r
}
// AdoptOptsBuilder is the interface options structs have to satisfy in order
@@ -184,79 +108,36 @@
// AdoptOpts is the common options struct used in this package's Adopt
// operation.
type AdoptOpts struct {
- // (REQUIRED) Existing resources data represented as a string to add to the
+ // 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
- // (REQUIRED) A structure that contains either the template file or url. Call the
+ AdoptStackData string `json:"adopt_stack_data" required:"true"`
+ // The name of the stack. It must start with an alphabetic character.
+ Name string `json:"stack_name" required:"true"`
+ // The timeout for stack creation in minutes.
+ Timeout int `json:"timeout_mins,omitempty"`
+ // A structure that contains either the template file or url. Call the
// associated methods to extract the information relevant to send in a create request.
- TemplateOpts *Template
- // (DEPRECATED): Please use TemplateOpts for providing the template. If
- // TemplateOpts is provided, TemplateURL will be ignored
- // (OPTIONAL; REQUIRED IF Template IS EMPTY) The URL of the template to instantiate.
- // This value is ignored if Template is supplied inline.
- TemplateURL string
- // (DEPRECATED): Please use TemplateOpts for providing the template. If
- // TemplateOpts is provided, Template will be ignored
- // (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
+ //TemplateOpts *Template `json:"-" required:"true"`
+ // 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 Rollback
- // (OPTIONAL) A structure that contains details for the environment of the stack.
- EnvironmentOpts *Environment
- // (DEPRECATED): Please use EnvironmentOpts to provide Environment data
- // (OPTIONAL) A stringified JSON environment for the stack.
- Environment string
- // (DEPRECATED): Files is automatically determined
- // by parsing the template and environment passed as TemplateOpts and
- // EnvironmentOpts respectively.
- // (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
+ DisableRollback *bool `json:"disable_rollback,omitempty"`
+ // A structure that contains details for the environment of the stack.
+ EnvironmentOpts *Environment `json:"-"`
+ // User-defined parameters to pass to the template.
+ Parameters map[string]string `json:"parameters,omitempty"`
}
// ToStackAdoptMap casts a CreateOpts struct to a map.
func (opts AdoptOpts) ToStackAdoptMap() (map[string]interface{}, error) {
- s := make(map[string]interface{})
-
- if opts.Name == "" {
- err := gophercloud.ErrMissingInput{}
- err.Argument = "stacks.AdoptOpts.Name"
+ b, err := gophercloud.BuildRequestBody(opts, "")
+ if err != nil {
return nil, err
}
- s["stack_name"] = opts.Name
- Files := make(map[string]string)
- if opts.AdoptStackData != "" {
- s["adopt_stack_data"] = opts.AdoptStackData
- } else if opts.TemplateOpts == nil {
- if opts.Template != "" {
- s["template"] = opts.Template
- } else if opts.TemplateURL != "" {
- s["template_url"] = opts.TemplateURL
- } else {
- err := gophercloud.ErrMissingInput{}
- err.Argument = "stacks.AdoptOpts"
- err.Info = "One of AdoptStackData, Template, TemplateURL or TemplateOpts must be provided"
- return nil, err
- }
- } else {
+
+ files := make(map[string]string)
+
+ /*
if err := opts.TemplateOpts.Parse(); err != nil {
return nil, err
}
@@ -265,16 +146,12 @@
return nil, err
}
opts.TemplateOpts.fixFileRefs()
- s["template"] = string(opts.TemplateOpts.Bin)
+ b["template"] = string(opts.TemplateOpts.Bin)
for k, v := range opts.TemplateOpts.Files {
- Files[k] = v
+ files[k] = v
}
- }
-
- if opts.DisableRollback != nil {
- s["disable_rollback"] = &opts.DisableRollback
- }
+ */
if opts.EnvironmentOpts != nil {
if err := opts.EnvironmentOpts.Parse(); err != nil {
@@ -285,44 +162,29 @@
}
opts.EnvironmentOpts.fixFileRefs()
for k, v := range opts.EnvironmentOpts.Files {
- Files[k] = v
+ files[k] = v
}
- s["environment"] = string(opts.EnvironmentOpts.Bin)
- } else if opts.Environment != "" {
- s["environment"] = opts.Environment
+ b["environment"] = string(opts.EnvironmentOpts.Bin)
}
- if opts.Files != nil {
- s["files"] = opts.Files
- } else {
- s["files"] = Files
+ if len(files) > 0 {
+ b["files"] = files
}
- if opts.Parameters != nil {
- s["parameters"] = opts.Parameters
- }
-
- if opts.Timeout != 0 {
- s["timeout"] = opts.Timeout
- }
- s["timeout_mins"] = opts.Timeout
-
- return s, nil
+ return b, nil
}
// Adopt accepts an AdoptOpts struct and creates a new stack using the resources
// from another stack.
func Adopt(c *gophercloud.ServiceClient, opts AdoptOptsBuilder) AdoptResult {
- var res AdoptResult
-
- reqBody, err := opts.ToStackAdoptMap()
+ var r AdoptResult
+ b, err := opts.ToStackAdoptMap()
if err != nil {
- res.Err = err
- return res
+ r.Err = err
+ return r
}
-
- _, res.Err = c.Post(adoptURL(c), reqBody, &res.Body, nil)
- return res
+ _, r.Err = c.Post(adoptURL(c), b, &r.Body, nil)
+ return r
}
// SortDir is a type for specifying in which direction to sort a list of stacks.
@@ -387,7 +249,6 @@
}
url += query
}
-
createPage := func(r pagination.PageResult) pagination.Page {
return StackPage{pagination.SinglePageBase(r)}
}
@@ -396,9 +257,9 @@
// Get retreives a stack based on the stack name and stack ID.
func Get(c *gophercloud.ServiceClient, stackName, stackID string) GetResult {
- var res GetResult
- _, res.Err = c.Get(getURL(c, stackName, stackID), &res.Body, nil)
- return res
+ var r GetResult
+ _, r.Err = c.Get(getURL(c, stackName, stackID), &r.Body, nil)
+ return r
}
// UpdateOptsBuilder is the interface options structs have to satisfy in order
@@ -410,76 +271,39 @@
// UpdateOpts contains the common options struct used in this package's Update
// operation.
type UpdateOpts struct {
- // (REQUIRED) A structure that contains either the template file or url. Call the
+ // A structure that contains either the template file or url. Call the
// associated methods to extract the information relevant to send in a create request.
- TemplateOpts *Template
- // (DEPRECATED): Please use TemplateOpts for providing the template. If
- // TemplateOpts is provided, TemplateURL will be ignored
- // (OPTIONAL; REQUIRED IF Template IS EMPTY) The URL of the template to instantiate.
- // This value is ignored if Template is supplied inline.
- TemplateURL string
- // (DEPRECATED): Please use TemplateOpts for providing the template. If
- // TemplateOpts is provided, Template will be ignored
- // (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) A structure that contains details for the environment of the stack.
- EnvironmentOpts *Environment
- // (DEPRECATED): Please use EnvironmentOpts to provide Environment data
- // (OPTIONAL) A stringified JSON environment for the stack.
- Environment string
- // (DEPRECATED): Files is automatically determined
- // by parsing the template and environment passed as TemplateOpts and
- // EnvironmentOpts respectively.
- // (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
- // (OPTIONAL) The timeout for stack creation in minutes.
- Timeout int
- // (OPTIONAL) A list of tags to assosciate with the Stack
- Tags []string
+ TemplateOpts *Template `json:"-" required:"true"`
+ // A structure that contains details for the environment of the stack.
+ EnvironmentOpts *Environment `json:"-"`
+ // User-defined parameters to pass to the template.
+ Parameters map[string]string `json:"parameters,omitempty"`
+ // The timeout for stack creation in minutes.
+ Timeout int `json:"timeout_mins,omitempty"`
+ // A list of tags to assosciate with the Stack
+ Tags []string `json:"-"`
}
// ToStackUpdateMap casts a CreateOpts struct to a map.
func (opts UpdateOpts) ToStackUpdateMap() (map[string]interface{}, error) {
- s := make(map[string]interface{})
- Files := make(map[string]string)
- if opts.TemplateOpts == nil {
- if opts.Template != "" {
- s["template"] = opts.Template
- } else if opts.TemplateURL != "" {
- s["template_url"] = opts.TemplateURL
- } else {
- err := gophercloud.ErrMissingInput{}
- err.Argument = "stacks.UpdateOpts"
- err.Info = "Either Template or TemplateURL must be provided"
- return nil, err
- }
- } else {
- if err := opts.TemplateOpts.Parse(); err != nil {
- return nil, err
- }
+ b, err := gophercloud.BuildRequestBody(opts, "")
+ if err != nil {
+ return nil, err
+ }
- if err := opts.TemplateOpts.getFileContents(opts.TemplateOpts.Parsed, ignoreIfTemplate, true); err != nil {
- return nil, err
- }
- opts.TemplateOpts.fixFileRefs()
- s["template"] = string(opts.TemplateOpts.Bin)
+ if err := opts.TemplateOpts.Parse(); err != nil {
+ return nil, err
+ }
- for k, v := range opts.TemplateOpts.Files {
- Files[k] = v
- }
+ if err := opts.TemplateOpts.getFileContents(opts.TemplateOpts.Parsed, ignoreIfTemplate, true); err != nil {
+ return nil, err
+ }
+ opts.TemplateOpts.fixFileRefs()
+ b["template"] = string(opts.TemplateOpts.Bin)
+
+ files := make(map[string]string)
+ for k, v := range opts.TemplateOpts.Files {
+ files[k] = v
}
if opts.EnvironmentOpts != nil {
@@ -491,54 +315,40 @@
}
opts.EnvironmentOpts.fixFileRefs()
for k, v := range opts.EnvironmentOpts.Files {
- Files[k] = v
+ files[k] = v
}
- s["environment"] = string(opts.EnvironmentOpts.Bin)
- } else if opts.Environment != "" {
- s["environment"] = opts.Environment
+ b["environment"] = string(opts.EnvironmentOpts.Bin)
}
- if opts.Files != nil {
- s["files"] = opts.Files
- } else {
- s["files"] = Files
- }
-
- if opts.Parameters != nil {
- s["parameters"] = opts.Parameters
- }
-
- if opts.Timeout != 0 {
- s["timeout_mins"] = opts.Timeout
+ if len(files) > 0 {
+ b["files"] = files
}
if opts.Tags != nil {
- s["tags"] = strings.Join(opts.Tags, ",")
+ b["tags"] = strings.Join(opts.Tags, ",")
}
- return s, nil
+ return b, nil
}
// Update accepts an UpdateOpts struct and updates an existing stack using the values
// provided.
func Update(c *gophercloud.ServiceClient, stackName, stackID string, opts UpdateOptsBuilder) UpdateResult {
- var res UpdateResult
-
- reqBody, err := opts.ToStackUpdateMap()
+ var r UpdateResult
+ b, err := opts.ToStackUpdateMap()
if err != nil {
- res.Err = err
- return res
+ r.Err = err
+ return r
}
-
- _, res.Err = c.Put(updateURL(c, stackName, stackID), reqBody, nil, nil)
- return res
+ _, r.Err = c.Put(updateURL(c, stackName, stackID), b, nil, nil)
+ return r
}
// Delete deletes a stack based on the stack name and stack ID.
func Delete(c *gophercloud.ServiceClient, stackName, stackID string) DeleteResult {
- var res DeleteResult
- _, res.Err = c.Delete(deleteURL(c, stackName, stackID), nil)
- return res
+ var r DeleteResult
+ _, r.Err = c.Delete(deleteURL(c, stackName, stackID), nil)
+ return r
}
// PreviewOptsBuilder is the interface options structs have to satisfy in order
@@ -550,90 +360,43 @@
// PreviewOpts contains the common options struct used in this package's Preview
// operation.
type PreviewOpts struct {
- // (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
- // (REQUIRED) A structure that contains either the template file or url. Call the
+ // The name of the stack. It must start with an alphabetic character.
+ Name string `json:"stack_name" required:"true"`
+ // The timeout for stack creation in minutes.
+ Timeout int `json:"timeout_mins" required:"true"`
+ // A structure that contains either the template file or url. Call the
// associated methods to extract the information relevant to send in a create request.
- TemplateOpts *Template
- // (DEPRECATED): Please use TemplateOpts for providing the template. If
- // TemplateOpts is provided, TemplateURL will be ignored
- // (OPTIONAL; REQUIRED IF Template IS EMPTY) The URL of the template to instantiate.
- // This value is ignored if Template is supplied inline.
- TemplateURL string
- // (DEPRECATED): Please use TemplateOpts for providing the template. If
- // TemplateOpts is provided, Template will be ignored
- // (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
+ TemplateOpts *Template `json:"-" required:"true"`
+ // 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 Rollback
- // (OPTIONAL) A structure that contains details for the environment of the stack.
- EnvironmentOpts *Environment
- // (DEPRECATED): Please use EnvironmentOpts to provide Environment data
- // (OPTIONAL) A stringified JSON environment for the stack.
- Environment string
- // (DEPRECATED): Files is automatically determined
- // by parsing the template and environment passed as TemplateOpts and
- // EnvironmentOpts respectively.
- // (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
+ DisableRollback *bool `json:"disable_rollback,omitempty"`
+ // A structure that contains details for the environment of the stack.
+ EnvironmentOpts *Environment `json:"-"`
+ // User-defined parameters to pass to the template.
+ Parameters map[string]string `json:"parameters,omitempty"`
}
// ToStackPreviewMap casts a PreviewOpts struct to a map.
func (opts PreviewOpts) ToStackPreviewMap() (map[string]interface{}, error) {
- s := make(map[string]interface{})
-
- if opts.Name == "" {
- err := gophercloud.ErrMissingInput{}
- err.Argument = "stacks.PreviewOpts.Name"
+ b, err := gophercloud.BuildRequestBody(opts, "")
+ if err != nil {
return nil, err
}
- s["stack_name"] = opts.Name
- Files := make(map[string]string)
- if opts.TemplateOpts == nil {
- if opts.Template != "" {
- s["template"] = opts.Template
- } else if opts.TemplateURL != "" {
- s["template_url"] = opts.TemplateURL
- } else {
- err := gophercloud.ErrMissingInput{}
- err.Argument = "stacks.PreviewOpts"
- err.Info = "Either Template or TemplateURL must be provided"
- return nil, err
- }
- } else {
- if err := opts.TemplateOpts.Parse(); err != nil {
- return nil, err
- }
- if err := opts.TemplateOpts.getFileContents(opts.TemplateOpts.Parsed, ignoreIfTemplate, true); err != nil {
- return nil, err
- }
- opts.TemplateOpts.fixFileRefs()
- s["template"] = string(opts.TemplateOpts.Bin)
-
- for k, v := range opts.TemplateOpts.Files {
- Files[k] = v
- }
+ if err := opts.TemplateOpts.Parse(); err != nil {
+ return nil, err
}
- if opts.DisableRollback != nil {
- s["disable_rollback"] = &opts.DisableRollback
+
+ if err := opts.TemplateOpts.getFileContents(opts.TemplateOpts.Parsed, ignoreIfTemplate, true); err != nil {
+ return nil, err
+ }
+ opts.TemplateOpts.fixFileRefs()
+ b["template"] = string(opts.TemplateOpts.Bin)
+
+ files := make(map[string]string)
+ for k, v := range opts.TemplateOpts.Files {
+ files[k] = v
}
if opts.EnvironmentOpts != nil {
@@ -645,55 +408,40 @@
}
opts.EnvironmentOpts.fixFileRefs()
for k, v := range opts.EnvironmentOpts.Files {
- Files[k] = v
+ files[k] = v
}
- s["environment"] = string(opts.EnvironmentOpts.Bin)
- } else if opts.Environment != "" {
- s["environment"] = opts.Environment
+ b["environment"] = string(opts.EnvironmentOpts.Bin)
}
- if opts.Files != nil {
- s["files"] = opts.Files
- } else {
- s["files"] = Files
+ if len(files) > 0 {
+ b["files"] = files
}
- if opts.Parameters != nil {
- s["parameters"] = opts.Parameters
- }
-
- if opts.Timeout != 0 {
- s["timeout_mins"] = opts.Timeout
- }
-
- return s, nil
+ return b, nil
}
// Preview accepts a PreviewOptsBuilder interface and creates a preview of a stack using the values
// provided.
func Preview(c *gophercloud.ServiceClient, opts PreviewOptsBuilder) PreviewResult {
- var res PreviewResult
-
- reqBody, err := opts.ToStackPreviewMap()
+ var r PreviewResult
+ b, err := opts.ToStackPreviewMap()
if err != nil {
- res.Err = err
- return res
+ r.Err = err
+ return r
}
-
- // Send request to API
- _, res.Err = c.Post(previewURL(c), reqBody, &res.Body, &gophercloud.RequestOpts{
+ _, r.Err = c.Post(previewURL(c), b, &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{200},
})
- return res
+ return r
}
// Abandon deletes the stack with the provided stackName and stackID, but leaves its
// resources intact, and returns data describing the stack and its resources.
func Abandon(c *gophercloud.ServiceClient, stackName, stackID string) AbandonResult {
- var res AbandonResult
- _, res.Err = c.Delete(abandonURL(c, stackName, stackID), &gophercloud.RequestOpts{
- JSONResponse: &res.Body,
+ var r AbandonResult
+ _, r.Err = c.Delete(abandonURL(c, stackName, stackID), &gophercloud.RequestOpts{
+ JSONResponse: &r.Body,
OkCodes: []int{200},
})
- return res
+ return r
}
diff --git a/openstack/orchestration/v1/stacks/template.go b/openstack/orchestration/v1/stacks/template.go
index c08ac30..4cf5aae 100644
--- a/openstack/orchestration/v1/stacks/template.go
+++ b/openstack/orchestration/v1/stacks/template.go
@@ -2,9 +2,10 @@
import (
"fmt"
- "github.com/gophercloud/gophercloud"
"reflect"
"strings"
+
+ "github.com/gophercloud/gophercloud"
)
// Template is a structure that represents OpenStack Heat templates
@@ -27,12 +28,14 @@
return err
}
}
+ var invalid string
for key := range t.Parsed {
if _, ok := TemplateFormatVersions[key]; ok {
return nil
}
+ invalid = key
}
- return fmt.Errorf("Template format version not found.")
+ return ErrInvalidTemplateFormatVersion{Version: invalid}
}
// GetFileContents recursively parses a template to search for urls. These urls
@@ -114,8 +117,7 @@
case string, bool, float64, nil, int:
return nil
default:
- return fmt.Errorf("%v: Unrecognized type", reflect.TypeOf(te))
-
+ return gophercloud.ErrUnexpectedType{Actual: fmt.Sprintf("%v", reflect.TypeOf(te))}
}
return nil
}
diff --git a/openstack/orchestration/v1/stacks/utils.go b/openstack/orchestration/v1/stacks/utils.go
index c210b0d..71d9e35 100644
--- a/openstack/orchestration/v1/stacks/utils.go
+++ b/openstack/orchestration/v1/stacks/utils.go
@@ -113,7 +113,7 @@
}
if jerr := json.Unmarshal(t.Bin, &t.Parsed); jerr != nil {
if yerr := yaml.Unmarshal(t.Bin, &t.Parsed); yerr != nil {
- return fmt.Errorf("Data in neither json nor yaml format.")
+ return ErrInvalidDataFormat{}
}
}
return t.Validate()
@@ -142,8 +142,7 @@
}
return typedMap, nil
default:
- return nil, fmt.Errorf("Expected a map of type map[string]interface{} or map[interface{}]interface{}, actual type: %v", reflect.TypeOf(m))
-
+ return nil, gophercloud.ErrUnexpectedType{Expected: "map[string]interface{}/map[interface{}]interface{}", Actual: fmt.Sprintf("%v", reflect.TypeOf(m))}
}
}
diff --git a/openstack/orchestration/v1/stacktemplates/requests.go b/openstack/orchestration/v1/stacktemplates/requests.go
index d307fad..9337cae 100644
--- a/openstack/orchestration/v1/stacktemplates/requests.go
+++ b/openstack/orchestration/v1/stacktemplates/requests.go
@@ -4,11 +4,9 @@
// Get retreives data for the given stack template.
func Get(c *gophercloud.ServiceClient, stackName, stackID string) GetResult {
- var res GetResult
- _, res.Err = c.Request("GET", getURL(c, stackName, stackID), &gophercloud.RequestOpts{
- JSONResponse: &res.Body,
- })
- return res
+ var r GetResult
+ _, r.Err = c.Get(getURL(c, stackName, stackID), &r.Body, nil)
+ return r
}
// ValidateOptsBuilder describes struct types that can be accepted by the Validate call.
@@ -19,39 +17,25 @@
// ValidateOpts specifies the template validation parameters.
type ValidateOpts struct {
- Template string
- TemplateURL string
+ Template string `json:"template" or:"TemplateURL"`
+ TemplateURL string `json:"template_url" or:"Template"`
}
// ToStackTemplateValidateMap assembles a request body based on the contents of a ValidateOpts.
func (opts ValidateOpts) ToStackTemplateValidateMap() (map[string]interface{}, error) {
- vo := make(map[string]interface{})
- if opts.Template != "" {
- vo["template"] = opts.Template
- return vo, nil
- }
- if opts.TemplateURL != "" {
- vo["template_url"] = opts.TemplateURL
- return vo, nil
- }
- err := gophercloud.ErrMissingInput{}
- err.Argument = "stacktemplates.ValidateOpts.Template/stacktemplates.ValidateOpts.TemplateURL"
- err.Info = "One of Template or TemplateURL is required."
- return nil, err
+ return gophercloud.BuildRequestBody(opts, "")
}
// Validate validates the given stack template.
func Validate(c *gophercloud.ServiceClient, opts ValidateOptsBuilder) ValidateResult {
- var res ValidateResult
-
- reqBody, err := opts.ToStackTemplateValidateMap()
+ var r ValidateResult
+ b, err := opts.ToStackTemplateValidateMap()
if err != nil {
- res.Err = err
- return res
+ r.Err = err
+ return r
}
-
- _, res.Err = c.Post(validateURL(c), reqBody, &res.Body, &gophercloud.RequestOpts{
+ _, r.Err = c.Post(validateURL(c), b, &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{200},
})
- return res
+ return r
}