github.com/gophercloud/gophercloud@v1.11.0/openstack/identity/v3/applicationcredentials/requests.go (about) 1 package applicationcredentials 2 3 import ( 4 "time" 5 6 "github.com/gophercloud/gophercloud" 7 "github.com/gophercloud/gophercloud/pagination" 8 ) 9 10 // ListOptsBuilder allows extensions to add additional parameters to 11 // the List request 12 type ListOptsBuilder interface { 13 ToApplicationCredentialListQuery() (string, error) 14 } 15 16 // ListOpts provides options to filter the List results. 17 type ListOpts struct { 18 // Name filters the response by an application credential name 19 Name string `q:"name"` 20 } 21 22 // ToApplicationCredentialListQuery formats a ListOpts into a query string. 23 func (opts ListOpts) ToApplicationCredentialListQuery() (string, error) { 24 q, err := gophercloud.BuildQueryString(opts) 25 return q.String(), err 26 } 27 28 // List enumerates the ApplicationCredentials to which the current token has access. 29 func List(client *gophercloud.ServiceClient, userID string, opts ListOptsBuilder) pagination.Pager { 30 url := listURL(client, userID) 31 if opts != nil { 32 query, err := opts.ToApplicationCredentialListQuery() 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 ApplicationCredentialPage{pagination.LinkedPageBase{PageResult: r}} 40 }) 41 } 42 43 // Get retrieves details on a single user, by ID. 44 func Get(client *gophercloud.ServiceClient, userID string, id string) (r GetResult) { 45 resp, err := client.Get(getURL(client, userID, 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 ToApplicationCredentialCreateMap() (map[string]interface{}, error) 54 } 55 56 // CreateOpts provides options used to create an application credential. 57 type CreateOpts struct { 58 // The name of the application credential. 59 Name string `json:"name,omitempty" required:"true"` 60 // A description of the application credential’s purpose. 61 Description string `json:"description,omitempty"` 62 // A flag indicating whether the application credential may be used for creation or destruction of other application credentials or trusts. 63 // Defaults to false 64 Unrestricted bool `json:"unrestricted"` 65 // The secret for the application credential, either generated by the server or provided by the user. 66 // This is only ever shown once in the response to a create request. It is not stored nor ever shown again. 67 // If the secret is lost, a new application credential must be created. 68 Secret string `json:"secret,omitempty"` 69 // A list of one or more roles that this application credential has associated with its project. 70 // A token using this application credential will have these same roles. 71 Roles []Role `json:"roles,omitempty"` 72 // A list of access rules objects. 73 AccessRules []AccessRule `json:"access_rules,omitempty"` 74 // The expiration time of the application credential, if one was specified. 75 ExpiresAt *time.Time `json:"-"` 76 } 77 78 // ToApplicationCredentialCreateMap formats a CreateOpts into a create request. 79 func (opts CreateOpts) ToApplicationCredentialCreateMap() (map[string]interface{}, error) { 80 parent := "application_credential" 81 b, err := gophercloud.BuildRequestBody(opts, parent) 82 if err != nil { 83 return nil, err 84 } 85 86 if opts.ExpiresAt != nil { 87 if v, ok := b[parent].(map[string]interface{}); ok { 88 v["expires_at"] = opts.ExpiresAt.Format(gophercloud.RFC3339MilliNoZ) 89 } 90 } 91 92 return b, nil 93 } 94 95 // Create creates a new ApplicationCredential. 96 func Create(client *gophercloud.ServiceClient, userID string, opts CreateOptsBuilder) (r CreateResult) { 97 b, err := opts.ToApplicationCredentialCreateMap() 98 if err != nil { 99 r.Err = err 100 return 101 } 102 resp, err := client.Post(createURL(client, userID), &b, &r.Body, &gophercloud.RequestOpts{ 103 OkCodes: []int{201}, 104 }) 105 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 106 return 107 } 108 109 // Delete deletes an application credential. 110 func Delete(client *gophercloud.ServiceClient, userID string, id string) (r DeleteResult) { 111 resp, err := client.Delete(deleteURL(client, userID, id), nil) 112 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 113 return 114 } 115 116 // ListAccessRules enumerates the AccessRules to which the current user has access. 117 func ListAccessRules(client *gophercloud.ServiceClient, userID string) pagination.Pager { 118 url := listAccessRulesURL(client, userID) 119 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 120 return AccessRulePage{pagination.LinkedPageBase{PageResult: r}} 121 }) 122 } 123 124 // GetAccessRule retrieves details on a single access rule by ID. 125 func GetAccessRule(client *gophercloud.ServiceClient, userID string, id string) (r GetAccessRuleResult) { 126 resp, err := client.Get(getAccessRuleURL(client, userID, id), &r.Body, nil) 127 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 128 return 129 } 130 131 // DeleteAccessRule deletes an access rule. 132 func DeleteAccessRule(client *gophercloud.ServiceClient, userID string, id string) (r DeleteResult) { 133 resp, err := client.Delete(deleteAccessRuleURL(client, userID, id), nil) 134 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 135 return 136 }