Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 1 | package stackresources |
| 2 | |
| 3 | import ( |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 4 | "github.com/rackspace/gophercloud" |
| 5 | "github.com/rackspace/gophercloud/pagination" |
| 6 | ) |
| 7 | |
Jon Perritt | 8bdc1a6 | 2015-02-12 09:14:48 -0700 | [diff] [blame] | 8 | // Find retrieves stack resources for the given stack name. |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 9 | func Find(c *gophercloud.ServiceClient, stackName string) FindResult { |
| 10 | var res FindResult |
| 11 | |
| 12 | // Send request to API |
Ash Wilson | decfed7 | 2015-02-13 09:14:55 -0500 | [diff] [blame] | 13 | _, res.Err = c.Request("GET", findURL(c, stackName), gophercloud.RequestOpts{ |
| 14 | JSONResponse: &res.Body, |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 15 | }) |
| 16 | return res |
| 17 | } |
| 18 | |
| 19 | // ListOptsBuilder allows extensions to add additional parameters to the |
| 20 | // List request. |
| 21 | type ListOptsBuilder interface { |
| 22 | ToStackResourceListQuery() (string, error) |
| 23 | } |
| 24 | |
| 25 | // ListOpts allows the filtering and sorting of paginated collections through |
| 26 | // the API. Marker and Limit are used for pagination. |
| 27 | type ListOpts struct { |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 28 | // Include resources from nest stacks up to Depth levels of recursion. |
| 29 | Depth int `q:"nested_depth"` |
| 30 | } |
| 31 | |
| 32 | // ToStackResourceListQuery formats a ListOpts into a query string. |
| 33 | func (opts ListOpts) ToStackResourceListQuery() (string, error) { |
| 34 | q, err := gophercloud.BuildQueryString(opts) |
| 35 | if err != nil { |
| 36 | return "", err |
| 37 | } |
| 38 | return q.String(), nil |
| 39 | } |
| 40 | |
| 41 | // List makes a request against the API to list resources for the given stack. |
| 42 | func List(client *gophercloud.ServiceClient, stackName, stackID string, opts ListOptsBuilder) pagination.Pager { |
| 43 | url := listURL(client, stackName, stackID) |
| 44 | |
| 45 | if opts != nil { |
| 46 | query, err := opts.ToStackResourceListQuery() |
| 47 | if err != nil { |
| 48 | return pagination.Pager{Err: err} |
| 49 | } |
| 50 | url += query |
| 51 | } |
| 52 | |
| 53 | createPageFn := func(r pagination.PageResult) pagination.Page { |
Pratik Mallya | 5448f58 | 2015-08-21 12:21:09 -0500 | [diff] [blame] | 54 | return ResourcePage{pagination.SinglePageBase(r)} |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | return pagination.NewPager(client, url, createPageFn) |
| 58 | } |
| 59 | |
| 60 | // Get retreives data for the given stack resource. |
| 61 | func Get(c *gophercloud.ServiceClient, stackName, stackID, resourceName string) GetResult { |
| 62 | var res GetResult |
| 63 | |
| 64 | // Send request to API |
Jamie Hannaford | 1d27afa | 2015-03-24 16:20:45 +0100 | [diff] [blame] | 65 | _, res.Err = c.Get(getURL(c, stackName, stackID, resourceName), &res.Body, &gophercloud.RequestOpts{ |
| 66 | OkCodes: []int{200}, |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 67 | }) |
| 68 | return res |
| 69 | } |
| 70 | |
| 71 | // Metadata retreives the metadata for the given stack resource. |
| 72 | func Metadata(c *gophercloud.ServiceClient, stackName, stackID, resourceName string) MetadataResult { |
| 73 | var res MetadataResult |
| 74 | |
| 75 | // Send request to API |
Jamie Hannaford | 1d27afa | 2015-03-24 16:20:45 +0100 | [diff] [blame] | 76 | _, res.Err = c.Get(metadataURL(c, stackName, stackID, resourceName), &res.Body, &gophercloud.RequestOpts{ |
| 77 | OkCodes: []int{200}, |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 78 | }) |
| 79 | return res |
| 80 | } |
Jon Perritt | a065da1 | 2015-02-06 10:20:16 -0700 | [diff] [blame] | 81 | |
| 82 | // ListTypes makes a request against the API to list resource types. |
| 83 | func ListTypes(client *gophercloud.ServiceClient) pagination.Pager { |
| 84 | url := listTypesURL(client) |
| 85 | |
| 86 | createPageFn := func(r pagination.PageResult) pagination.Page { |
| 87 | return ResourceTypePage{pagination.SinglePageBase(r)} |
| 88 | } |
| 89 | |
| 90 | return pagination.NewPager(client, url, createPageFn) |
| 91 | } |
Jon Perritt | 1d4aca0 | 2015-02-06 12:29:16 -0700 | [diff] [blame] | 92 | |
| 93 | // Schema retreives the schema for the given resource type. |
| 94 | func Schema(c *gophercloud.ServiceClient, resourceType string) SchemaResult { |
| 95 | var res SchemaResult |
| 96 | |
| 97 | // Send request to API |
Jamie Hannaford | 1d27afa | 2015-03-24 16:20:45 +0100 | [diff] [blame] | 98 | _, res.Err = c.Get(schemaURL(c, resourceType), &res.Body, &gophercloud.RequestOpts{ |
| 99 | OkCodes: []int{200}, |
Jon Perritt | 1d4aca0 | 2015-02-06 12:29:16 -0700 | [diff] [blame] | 100 | }) |
| 101 | return res |
| 102 | } |
Jon Perritt | b1e303a | 2015-02-06 22:15:44 -0700 | [diff] [blame] | 103 | |
| 104 | // Template retreives the template representation for the given resource type. |
| 105 | func Template(c *gophercloud.ServiceClient, resourceType string) TemplateResult { |
| 106 | var res TemplateResult |
| 107 | |
| 108 | // Send request to API |
Jamie Hannaford | 1d27afa | 2015-03-24 16:20:45 +0100 | [diff] [blame] | 109 | _, res.Err = c.Get(templateURL(c, resourceType), &res.Body, &gophercloud.RequestOpts{ |
| 110 | OkCodes: []int{200}, |
Jon Perritt | b1e303a | 2015-02-06 22:15:44 -0700 | [diff] [blame] | 111 | }) |
| 112 | return res |
| 113 | } |