Joe Topjian | f61691c | 2016-11-05 12:34:59 -0600 | [diff] [blame] | 1 | package projects |
| 2 | |
| 3 | import ( |
| 4 | "github.com/gophercloud/gophercloud" |
| 5 | "github.com/gophercloud/gophercloud/pagination" |
| 6 | ) |
| 7 | |
| 8 | // ListOptsBuilder allows extensions to add additional parameters to |
| 9 | // the List request |
| 10 | type ListOptsBuilder interface { |
| 11 | ToProjectListQuery() (string, error) |
| 12 | } |
| 13 | |
| 14 | // ListOpts allows you to query the List method. |
| 15 | type ListOpts struct { |
| 16 | // DomainID filters the response by a domain ID. |
| 17 | DomainID string `q:"domain_id"` |
| 18 | |
| 19 | // Enabled filters the response by enabled projects. |
| 20 | Enabled *bool `q:"enabled"` |
| 21 | |
| 22 | // IsDomain filters the response by projects that are domains. |
| 23 | // Setting this to true is effectively listing domains. |
| 24 | IsDomain *bool `q:"is_domain"` |
| 25 | |
| 26 | // Name filters the response by project name. |
| 27 | Name string `q:"name"` |
| 28 | |
| 29 | // ParentID filters the response by projects of a given parent project. |
| 30 | ParentID string `q:"parent_id"` |
| 31 | } |
| 32 | |
| 33 | // ToProjectListQuery formats a ListOpts into a query string. |
| 34 | func (opts ListOpts) ToProjectListQuery() (string, error) { |
| 35 | q, err := gophercloud.BuildQueryString(opts) |
| 36 | return q.String(), err |
| 37 | } |
| 38 | |
| 39 | // List enumerats the Projects to which the current token has access. |
| 40 | func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { |
| 41 | url := listURL(client) |
| 42 | if opts != nil { |
| 43 | query, err := opts.ToProjectListQuery() |
| 44 | if err != nil { |
| 45 | return pagination.Pager{Err: err} |
| 46 | } |
| 47 | url += query |
| 48 | } |
| 49 | return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { |
| 50 | return ProjectPage{pagination.LinkedPageBase{PageResult: r}} |
| 51 | }) |
| 52 | } |
Joe Topjian | 1c236d3 | 2017-01-09 15:33:32 -0700 | [diff] [blame] | 53 | |
| 54 | // GetOptsBuilder allows extensions to add additional parameters to |
| 55 | // the Get request. |
| 56 | type GetOptsBuilder interface { |
| 57 | ToProjectGetQuery() (string, error) |
| 58 | } |
| 59 | |
| 60 | // GetOpts allows you to modify the details included in the Get request. |
| 61 | type GetOpts struct{} |
| 62 | |
| 63 | // ToProjectGetQuery formats a GetOpts into a query string. |
| 64 | func (opts GetOpts) ToProjectGetQuery() (string, error) { |
| 65 | q, err := gophercloud.BuildQueryString(opts) |
| 66 | return q.String(), err |
| 67 | } |
| 68 | |
| 69 | // Get retrieves details on a single project, by ID. |
| 70 | func Get(client *gophercloud.ServiceClient, id string, opts GetOptsBuilder) (r GetResult) { |
| 71 | url := getURL(client, id) |
| 72 | if opts != nil { |
| 73 | query, err := opts.ToProjectGetQuery() |
| 74 | if err != nil { |
| 75 | r.Err = err |
| 76 | return |
| 77 | } |
| 78 | url += query |
| 79 | } |
| 80 | |
| 81 | _, r.Err = client.Get(url, &r.Body, &gophercloud.RequestOpts{ |
| 82 | OkCodes: []int{200}, |
| 83 | }) |
| 84 | return |
| 85 | } |
Joe Topjian | 8ad602c | 2017-01-11 21:01:47 -0700 | [diff] [blame] | 86 | |
| 87 | // CreateOptsBuilder allows extensions to add additional parameters to |
| 88 | // the Create request. |
| 89 | type CreateOptsBuilder interface { |
| 90 | ToProjectCreateMap() (map[string]interface{}, error) |
| 91 | } |
| 92 | |
| 93 | // CreateOpts allows you to modify the details included in the Create request. |
| 94 | type CreateOpts struct { |
| 95 | // DomainID is the ID this project will belong under. |
| 96 | DomainID string `json:"domain_id,omitempty"` |
| 97 | |
| 98 | // Enabled sets the project status to enabled or disabled. |
| 99 | Enabled *bool `json:"enabled,omitempty"` |
| 100 | |
| 101 | // IsDomain indicates if this project is a domain. |
| 102 | IsDomain *bool `json:"is_domain,omitempty"` |
| 103 | |
| 104 | // Name is the name of the project. |
| 105 | Name string `json:"name,required"` |
| 106 | |
| 107 | // ParentID specifies the parent project of this new project. |
| 108 | ParentID string `json:"parent_id,omitempty"` |
| 109 | |
| 110 | // Description is the description of the project. |
| 111 | Description string `json:"description,omitempty"` |
| 112 | } |
| 113 | |
| 114 | // ToProjectCreateMap formats a CreateOpts into a create request. |
| 115 | func (opts CreateOpts) ToProjectCreateMap() (map[string]interface{}, error) { |
| 116 | return gophercloud.BuildRequestBody(opts, "project") |
| 117 | } |
| 118 | |
| 119 | // Create creates a new Project. |
| 120 | func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { |
| 121 | b, err := opts.ToProjectCreateMap() |
| 122 | if err != nil { |
| 123 | r.Err = err |
| 124 | return |
| 125 | } |
| 126 | _, r.Err = client.Post(createURL(client), &b, &r.Body, nil) |
| 127 | return |
| 128 | } |
Joe Topjian | d131fb8 | 2017-01-11 21:41:44 -0700 | [diff] [blame] | 129 | |
| 130 | // Delete deletes a project. |
| 131 | func Delete(client *gophercloud.ServiceClient, projectID string) (r DeleteResult) { |
| 132 | _, r.Err = client.Delete(deleteURL(client, projectID), nil) |
| 133 | return |
| 134 | } |
Joe Topjian | 8baf47a | 2017-01-11 21:50:24 -0700 | [diff] [blame] | 135 | |
| 136 | // UpdateOptsBuilder allows extensions to add additional parameters to |
| 137 | // the Update request. |
| 138 | type UpdateOptsBuilder interface { |
| 139 | ToProjectUpdateMap() (map[string]interface{}, error) |
| 140 | } |
| 141 | |
| 142 | // UpdateOpts allows you to modify the details included in the Update request. |
| 143 | type UpdateOpts struct { |
| 144 | // DomainID is the ID this project will belong under. |
| 145 | DomainID string `json:"domain_id,omitempty"` |
| 146 | |
| 147 | // Enabled sets the project status to enabled or disabled. |
| 148 | Enabled *bool `json:"enabled,omitempty"` |
| 149 | |
| 150 | // IsDomain indicates if this project is a domain. |
| 151 | IsDomain *bool `json:"is_domain,omitempty"` |
| 152 | |
| 153 | // Name is the name of the project. |
| 154 | Name string `json:"name,omitempty"` |
| 155 | |
| 156 | // ParentID specifies the parent project of this new project. |
| 157 | ParentID string `json:"parent_id,omitempty"` |
| 158 | |
| 159 | // Description is the description of the project. |
| 160 | Description string `json:"description,omitempty"` |
| 161 | } |
| 162 | |
| 163 | // ToUpdateCreateMap formats a UpdateOpts into an update request. |
| 164 | func (opts UpdateOpts) ToProjectUpdateMap() (map[string]interface{}, error) { |
| 165 | return gophercloud.BuildRequestBody(opts, "project") |
| 166 | } |
| 167 | |
| 168 | // Update modifies the attributes of a project. |
| 169 | func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { |
| 170 | b, err := opts.ToProjectUpdateMap() |
| 171 | if err != nil { |
| 172 | r.Err = err |
| 173 | return |
| 174 | } |
| 175 | _, r.Err = client.Patch(updateURL(client, id), b, &r.Body, &gophercloud.RequestOpts{ |
| 176 | OkCodes: []int{200}, |
| 177 | }) |
| 178 | return |
| 179 | } |