blob: 74a9b15934ef7719cf3ed3ba5fb8dd103fbd8a4b [file] [log] [blame]
Joe Topjianf61691c2016-11-05 12:34:59 -06001package projects
2
3import (
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
10type ListOptsBuilder interface {
11 ToProjectListQuery() (string, error)
12}
13
14// ListOpts allows you to query the List method.
15type 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.
34func (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.
40func 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 Topjian1c236d32017-01-09 15:33:32 -070053
54// GetOptsBuilder allows extensions to add additional parameters to
55// the Get request.
56type GetOptsBuilder interface {
57 ToProjectGetQuery() (string, error)
58}
59
60// GetOpts allows you to modify the details included in the Get request.
61type GetOpts struct{}
62
63// ToProjectGetQuery formats a GetOpts into a query string.
64func (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.
70func 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 Topjian8ad602c2017-01-11 21:01:47 -070086
87// CreateOptsBuilder allows extensions to add additional parameters to
88// the Create request.
89type CreateOptsBuilder interface {
90 ToProjectCreateMap() (map[string]interface{}, error)
91}
92
93// CreateOpts allows you to modify the details included in the Create request.
94type 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.
115func (opts CreateOpts) ToProjectCreateMap() (map[string]interface{}, error) {
116 return gophercloud.BuildRequestBody(opts, "project")
117}
118
119// Create creates a new Project.
120func 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 Topjiand131fb82017-01-11 21:41:44 -0700129
130// Delete deletes a project.
131func Delete(client *gophercloud.ServiceClient, projectID string) (r DeleteResult) {
132 _, r.Err = client.Delete(deleteURL(client, projectID), nil)
133 return
134}
Joe Topjian8baf47a2017-01-11 21:50:24 -0700135
136// UpdateOptsBuilder allows extensions to add additional parameters to
137// the Update request.
138type UpdateOptsBuilder interface {
139 ToProjectUpdateMap() (map[string]interface{}, error)
140}
141
142// UpdateOpts allows you to modify the details included in the Update request.
143type 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.
164func (opts UpdateOpts) ToProjectUpdateMap() (map[string]interface{}, error) {
165 return gophercloud.BuildRequestBody(opts, "project")
166}
167
168// Update modifies the attributes of a project.
169func 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}