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 { |
| 28 | // The stack resource ID with which to start the listing. |
| 29 | Marker string `q:"marker"` |
| 30 | |
| 31 | // Integer value for the limit of values to return. |
| 32 | Limit int `q:"limit"` |
| 33 | |
| 34 | // Include resources from nest stacks up to Depth levels of recursion. |
| 35 | Depth int `q:"nested_depth"` |
| 36 | } |
| 37 | |
| 38 | // ToStackResourceListQuery formats a ListOpts into a query string. |
| 39 | func (opts ListOpts) ToStackResourceListQuery() (string, error) { |
| 40 | q, err := gophercloud.BuildQueryString(opts) |
| 41 | if err != nil { |
| 42 | return "", err |
| 43 | } |
| 44 | return q.String(), nil |
| 45 | } |
| 46 | |
| 47 | // List makes a request against the API to list resources for the given stack. |
| 48 | func List(client *gophercloud.ServiceClient, stackName, stackID string, opts ListOptsBuilder) pagination.Pager { |
| 49 | url := listURL(client, stackName, stackID) |
| 50 | |
| 51 | if opts != nil { |
| 52 | query, err := opts.ToStackResourceListQuery() |
| 53 | if err != nil { |
| 54 | return pagination.Pager{Err: err} |
| 55 | } |
| 56 | url += query |
| 57 | } |
| 58 | |
| 59 | createPageFn := func(r pagination.PageResult) pagination.Page { |
Jon Perritt | 64f594d | 2015-02-08 21:24:33 -0700 | [diff] [blame] | 60 | p := ResourcePage{pagination.MarkerPageBase{PageResult: r}} |
| 61 | p.MarkerPageBase.Owner = p |
| 62 | return p |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | return pagination.NewPager(client, url, createPageFn) |
| 66 | } |
| 67 | |
| 68 | // Get retreives data for the given stack resource. |
| 69 | func Get(c *gophercloud.ServiceClient, stackName, stackID, resourceName string) GetResult { |
| 70 | var res GetResult |
| 71 | |
| 72 | // Send request to API |
Jamie Hannaford | 1d27afa | 2015-03-24 16:20:45 +0100 | [diff] [blame] | 73 | _, res.Err = c.Get(getURL(c, stackName, stackID, resourceName), &res.Body, &gophercloud.RequestOpts{ |
| 74 | OkCodes: []int{200}, |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 75 | }) |
| 76 | return res |
| 77 | } |
| 78 | |
| 79 | // Metadata retreives the metadata for the given stack resource. |
| 80 | func Metadata(c *gophercloud.ServiceClient, stackName, stackID, resourceName string) MetadataResult { |
| 81 | var res MetadataResult |
| 82 | |
| 83 | // Send request to API |
Jamie Hannaford | 1d27afa | 2015-03-24 16:20:45 +0100 | [diff] [blame] | 84 | _, res.Err = c.Get(metadataURL(c, stackName, stackID, resourceName), &res.Body, &gophercloud.RequestOpts{ |
| 85 | OkCodes: []int{200}, |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 86 | }) |
| 87 | return res |
| 88 | } |
Jon Perritt | a065da1 | 2015-02-06 10:20:16 -0700 | [diff] [blame] | 89 | |
| 90 | // ListTypes makes a request against the API to list resource types. |
| 91 | func ListTypes(client *gophercloud.ServiceClient) pagination.Pager { |
| 92 | url := listTypesURL(client) |
| 93 | |
| 94 | createPageFn := func(r pagination.PageResult) pagination.Page { |
| 95 | return ResourceTypePage{pagination.SinglePageBase(r)} |
| 96 | } |
| 97 | |
| 98 | return pagination.NewPager(client, url, createPageFn) |
| 99 | } |
Jon Perritt | 1d4aca0 | 2015-02-06 12:29:16 -0700 | [diff] [blame] | 100 | |
| 101 | // Schema retreives the schema for the given resource type. |
| 102 | func Schema(c *gophercloud.ServiceClient, resourceType string) SchemaResult { |
| 103 | var res SchemaResult |
| 104 | |
| 105 | // Send request to API |
Jamie Hannaford | 1d27afa | 2015-03-24 16:20:45 +0100 | [diff] [blame] | 106 | _, res.Err = c.Get(schemaURL(c, resourceType), &res.Body, &gophercloud.RequestOpts{ |
| 107 | OkCodes: []int{200}, |
Jon Perritt | 1d4aca0 | 2015-02-06 12:29:16 -0700 | [diff] [blame] | 108 | }) |
| 109 | return res |
| 110 | } |
Jon Perritt | b1e303a | 2015-02-06 22:15:44 -0700 | [diff] [blame] | 111 | |
| 112 | // Template retreives the template representation for the given resource type. |
| 113 | func Template(c *gophercloud.ServiceClient, resourceType string) TemplateResult { |
| 114 | var res TemplateResult |
| 115 | |
| 116 | // Send request to API |
Jamie Hannaford | 1d27afa | 2015-03-24 16:20:45 +0100 | [diff] [blame] | 117 | _, res.Err = c.Get(templateURL(c, resourceType), &res.Body, &gophercloud.RequestOpts{ |
| 118 | OkCodes: []int{200}, |
Jon Perritt | b1e303a | 2015-02-06 22:15:44 -0700 | [diff] [blame] | 119 | }) |
| 120 | return res |
| 121 | } |