github.com/gophercloud/gophercloud@v1.11.0/openstack/identity/v3/credentials/requests.go (about) 1 package credentials 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 ToCredentialListQuery() (string, error) 12 } 13 14 // ListOpts provides options to filter the List results. 15 type ListOpts struct { 16 // UserID filters the response by a credential user_id 17 UserID string `q:"user_id"` 18 // Type filters the response by a credential type 19 Type string `q:"type"` 20 } 21 22 // ToCredentialListQuery formats a ListOpts into a query string. 23 func (opts ListOpts) ToCredentialListQuery() (string, error) { 24 q, err := gophercloud.BuildQueryString(opts) 25 return q.String(), err 26 } 27 28 // List enumerates the Credentials to which the current token has access. 29 func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { 30 url := listURL(client) 31 if opts != nil { 32 query, err := opts.ToCredentialListQuery() 33 if err != nil { 34 return pagination.Pager{Err: err} 35 } 36 url += query 37 } 38 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 39 return CredentialPage{pagination.LinkedPageBase{PageResult: r}} 40 }) 41 } 42 43 // Get retrieves details on a single user, by ID. 44 func Get(client *gophercloud.ServiceClient, id string) (r GetResult) { 45 resp, err := client.Get(getURL(client, id), &r.Body, nil) 46 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 47 return 48 } 49 50 // CreateOptsBuilder allows extensions to add additional parameters to 51 // the Create request. 52 type CreateOptsBuilder interface { 53 ToCredentialCreateMap() (map[string]interface{}, error) 54 } 55 56 // CreateOpts provides options used to create a credential. 57 type CreateOpts struct { 58 // Serialized blob containing the credentials 59 Blob string `json:"blob" required:"true"` 60 // ID of the project. 61 ProjectID string `json:"project_id,omitempty"` 62 // The type of the credential. 63 Type string `json:"type" required:"true"` 64 // ID of the user who owns the credential. 65 UserID string `json:"user_id" required:"true"` 66 } 67 68 // ToCredentialCreateMap formats a CreateOpts into a create request. 69 func (opts CreateOpts) ToCredentialCreateMap() (map[string]interface{}, error) { 70 return gophercloud.BuildRequestBody(opts, "credential") 71 } 72 73 // Create creates a new Credential. 74 func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 75 b, err := opts.ToCredentialCreateMap() 76 if err != nil { 77 r.Err = err 78 return 79 } 80 resp, err := client.Post(createURL(client), &b, &r.Body, &gophercloud.RequestOpts{ 81 OkCodes: []int{201}, 82 }) 83 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 84 return 85 } 86 87 // Delete deletes a credential. 88 func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) { 89 resp, err := client.Delete(deleteURL(client, id), nil) 90 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 91 return 92 } 93 94 // UpdateOptsBuilder allows extensions to add additional parameters to 95 // the Update request. 96 type UpdateOptsBuilder interface { 97 ToCredentialsUpdateMap() (map[string]interface{}, error) 98 } 99 100 // UpdateOpts represents parameters to update a credential. 101 type UpdateOpts struct { 102 // Serialized blob containing the credentials. 103 Blob string `json:"blob,omitempty"` 104 // ID of the project. 105 ProjectID string `json:"project_id,omitempty"` 106 // The type of the credential. 107 Type string `json:"type,omitempty"` 108 // ID of the user who owns the credential. 109 UserID string `json:"user_id,omitempty"` 110 } 111 112 // ToUpdateCreateMap formats a UpdateOpts into an update request. 113 func (opts UpdateOpts) ToCredentialsUpdateMap() (map[string]interface{}, error) { 114 return gophercloud.BuildRequestBody(opts, "credential") 115 } 116 117 // Update modifies the attributes of a Credential. 118 func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 119 b, err := opts.ToCredentialsUpdateMap() 120 if err != nil { 121 r.Err = err 122 return 123 } 124 resp, err := client.Patch(updateURL(client, id), b, &r.Body, &gophercloud.RequestOpts{ 125 OkCodes: []int{200}, 126 }) 127 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 128 return 129 }