github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/identity/v3/trusts/requests.go (about) 1 package trusts 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 // CreateOptsBuilder allows extensions to add additional parameters to 12 // the Create request. 13 type CreateOptsBuilder interface { 14 ToTrustCreateMap() (map[string]any, error) 15 } 16 17 // CreateOpts provides options used to create a new trust. 18 type CreateOpts struct { 19 // Impersonation allows the trustee to impersonate the trustor. 20 Impersonation bool `json:"impersonation"` 21 22 // TrusteeUserID is a user who is capable of consuming the trust. 23 TrusteeUserID string `json:"trustee_user_id" required:"true"` 24 25 // TrustorUserID is a user who created the trust. 26 TrustorUserID string `json:"trustor_user_id" required:"true"` 27 28 // AllowRedelegation enables redelegation of a trust. 29 AllowRedelegation bool `json:"allow_redelegation,omitempty"` 30 31 // ExpiresAt sets expiration time on trust. 32 ExpiresAt *time.Time `json:"-"` 33 34 // ProjectID identifies the project. 35 ProjectID string `json:"project_id,omitempty"` 36 37 // RedelegationCount specifies a depth of the redelegation chain. 38 RedelegationCount int `json:"redelegation_count,omitempty"` 39 40 // RemainingUses specifies how many times a trust can be used to get a token. 41 RemainingUses int `json:"remaining_uses,omitempty"` 42 43 // Roles specifies roles that need to be granted to trustee. 44 Roles []Role `json:"roles,omitempty"` 45 } 46 47 // ToTrustCreateMap formats a CreateOpts into a create request. 48 func (opts CreateOpts) ToTrustCreateMap() (map[string]any, error) { 49 parent := "trust" 50 b, err := gophercloud.BuildRequestBody(opts, parent) 51 if err != nil { 52 return nil, err 53 } 54 55 if opts.ExpiresAt != nil { 56 if v, ok := b[parent].(map[string]any); ok { 57 v["expires_at"] = opts.ExpiresAt.Format(gophercloud.RFC3339Milli) 58 } 59 } 60 61 return b, nil 62 } 63 64 type ListOptsBuilder interface { 65 ToTrustListQuery() (string, error) 66 } 67 68 // ListOpts provides options to filter the List results. 69 type ListOpts struct { 70 // TrustorUserID filters the response by a trustor user Id. 71 TrustorUserID string `q:"trustor_user_id"` 72 73 // TrusteeUserID filters the response by a trustee user Id. 74 TrusteeUserID string `q:"trustee_user_id"` 75 } 76 77 // ToTrustListQuery formats a ListOpts into a query string. 78 func (opts ListOpts) ToTrustListQuery() (string, error) { 79 q, err := gophercloud.BuildQueryString(opts) 80 return q.String(), err 81 } 82 83 // Create creates a new Trust. 84 func Create(ctx context.Context, client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 85 b, err := opts.ToTrustCreateMap() 86 if err != nil { 87 r.Err = err 88 return 89 } 90 resp, err := client.Post(ctx, createURL(client), &b, &r.Body, &gophercloud.RequestOpts{ 91 OkCodes: []int{201}, 92 }) 93 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 94 return 95 } 96 97 // Delete deletes a Trust. 98 func Delete(ctx context.Context, client *gophercloud.ServiceClient, trustID string) (r DeleteResult) { 99 resp, err := client.Delete(ctx, deleteURL(client, trustID), nil) 100 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 101 return 102 } 103 104 // List enumerates the Trust to which the current token has access. 105 func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { 106 url := listURL(client) 107 if opts != nil { 108 query, err := opts.ToTrustListQuery() 109 if err != nil { 110 return pagination.Pager{Err: err} 111 } 112 url += query 113 } 114 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 115 return TrustPage{pagination.LinkedPageBase{PageResult: r}} 116 }) 117 } 118 119 // Get retrieves details on a single Trust, by ID. 120 func Get(ctx context.Context, client *gophercloud.ServiceClient, id string) (r GetResult) { 121 resp, err := client.Get(ctx, resourceURL(client, id), &r.Body, nil) 122 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 123 return 124 } 125 126 // ListRoles lists roles delegated by a Trust. 127 func ListRoles(client *gophercloud.ServiceClient, id string) pagination.Pager { 128 url := listRolesURL(client, id) 129 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 130 return RolesPage{pagination.LinkedPageBase{PageResult: r}} 131 }) 132 } 133 134 // GetRole retrieves details on a single role delegated by a Trust. 135 func GetRole(ctx context.Context, client *gophercloud.ServiceClient, id string, roleID string) (r GetRoleResult) { 136 resp, err := client.Get(ctx, getRoleURL(client, id, roleID), &r.Body, nil) 137 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 138 return 139 } 140 141 // CheckRole checks whether a role ID is delegated by a Trust. 142 func CheckRole(ctx context.Context, client *gophercloud.ServiceClient, id string, roleID string) (r CheckRoleResult) { 143 resp, err := client.Head(ctx, getRoleURL(client, id, roleID), nil) 144 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 145 return 146 }