github.com/gophercloud/gophercloud@v1.11.0/openstack/identity/v2/tenants/requests.go (about)

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