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