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