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

     1  package tenants
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/vnpaycloud-console/gophercloud/v2"
     7  	"github.com/vnpaycloud-console/gophercloud/v2/pagination"
     8  )
     9  
    10  // ListOpts filters the Tenants that are returned by the List call.
    11  type ListOpts struct {
    12  	// Marker is the ID of the last Tenant on the previous page.
    13  	Marker string `q:"marker"`
    14  
    15  	// Limit specifies the page size.
    16  	Limit int `q:"limit"`
    17  }
    18  
    19  // List enumerates the Tenants to which the current token has access.
    20  func List(client *gophercloud.ServiceClient, opts *ListOpts) pagination.Pager {
    21  	url := listURL(client)
    22  	if opts != nil {
    23  		q, err := gophercloud.BuildQueryString(opts)
    24  		if err != nil {
    25  			return pagination.Pager{Err: err}
    26  		}
    27  		url += q.String()
    28  	}
    29  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
    30  		return TenantPage{pagination.LinkedPageBase{PageResult: r}}
    31  	})
    32  }
    33  
    34  // CreateOpts represents the options needed when creating new tenant.
    35  type CreateOpts struct {
    36  	// Name is the name of the tenant.
    37  	Name string `json:"name" required:"true"`
    38  
    39  	// Description is the description of the tenant.
    40  	Description string `json:"description,omitempty"`
    41  
    42  	// Enabled sets the tenant status to enabled or disabled.
    43  	Enabled *bool `json:"enabled,omitempty"`
    44  }
    45  
    46  // CreateOptsBuilder enables extensions to add additional parameters to the
    47  // Create request.
    48  type CreateOptsBuilder interface {
    49  	ToTenantCreateMap() (map[string]any, error)
    50  }
    51  
    52  // ToTenantCreateMap assembles a request body based on the contents of
    53  // a CreateOpts.
    54  func (opts CreateOpts) ToTenantCreateMap() (map[string]any, error) {
    55  	return gophercloud.BuildRequestBody(opts, "tenant")
    56  }
    57  
    58  // Create is the operation responsible for creating new tenant.
    59  func Create(ctx context.Context, client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    60  	b, err := opts.ToTenantCreateMap()
    61  	if err != nil {
    62  		r.Err = err
    63  		return
    64  	}
    65  	resp, err := client.Post(ctx, createURL(client), b, &r.Body, &gophercloud.RequestOpts{
    66  		OkCodes: []int{200, 201},
    67  	})
    68  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    69  	return
    70  }
    71  
    72  // Get requests details on a single tenant by ID.
    73  func Get(ctx context.Context, client *gophercloud.ServiceClient, id string) (r GetResult) {
    74  	resp, err := client.Get(ctx, getURL(client, id), &r.Body, nil)
    75  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    76  	return
    77  }
    78  
    79  // UpdateOptsBuilder allows extensions to add additional parameters to the
    80  // Update request.
    81  type UpdateOptsBuilder interface {
    82  	ToTenantUpdateMap() (map[string]any, error)
    83  }
    84  
    85  // UpdateOpts specifies the base attributes that may be updated on an existing
    86  // tenant.
    87  type UpdateOpts struct {
    88  	// Name is the name of the tenant.
    89  	Name string `json:"name,omitempty"`
    90  
    91  	// Description is the description of the tenant.
    92  	Description *string `json:"description,omitempty"`
    93  
    94  	// Enabled sets the tenant status to enabled or disabled.
    95  	Enabled *bool `json:"enabled,omitempty"`
    96  }
    97  
    98  // ToTenantUpdateMap formats an UpdateOpts structure into a request body.
    99  func (opts UpdateOpts) ToTenantUpdateMap() (map[string]any, error) {
   100  	return gophercloud.BuildRequestBody(opts, "tenant")
   101  }
   102  
   103  // Update is the operation responsible for updating exist tenants by their TenantID.
   104  func Update(ctx context.Context, client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   105  	b, err := opts.ToTenantUpdateMap()
   106  	if err != nil {
   107  		r.Err = err
   108  		return
   109  	}
   110  	resp, err := client.Put(ctx, updateURL(client, id), &b, &r.Body, &gophercloud.RequestOpts{
   111  		OkCodes: []int{200},
   112  	})
   113  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   114  	return
   115  }
   116  
   117  // Delete is the operation responsible for permanently deleting a tenant.
   118  func Delete(ctx context.Context, client *gophercloud.ServiceClient, id string) (r DeleteResult) {
   119  	resp, err := client.Delete(ctx, deleteURL(client, id), nil)
   120  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   121  	return
   122  }