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