github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/identity/v3/credentials/requests.go (about)

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