github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/identity/v3/domains/requests.go (about)

     1  package domains
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  	"github.com/huaweicloud/golangsdk/pagination"
     6  )
     7  
     8  // ListOptsBuilder allows extensions to add additional parameters to
     9  // the List request
    10  type ListOptsBuilder interface {
    11  	ToDomainListQuery() (string, error)
    12  }
    13  
    14  // ListOpts provides options to filter the List results.
    15  type ListOpts struct {
    16  	// Enabled filters the response by enabled domains.
    17  	Enabled *bool `q:"enabled"`
    18  
    19  	// Name filters the response by domain name.
    20  	Name string `q:"name"`
    21  }
    22  
    23  // ToDomainListQuery formats a ListOpts into a query string.
    24  func (opts ListOpts) ToDomainListQuery() (string, error) {
    25  	q, err := golangsdk.BuildQueryString(opts)
    26  	return q.String(), err
    27  }
    28  
    29  // List enumerates the domains to which the current token has access.
    30  func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    31  	url := listURL(client)
    32  	if opts != nil {
    33  		query, err := opts.ToDomainListQuery()
    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 DomainPage{pagination.LinkedPageBase{PageResult: r}}
    41  	})
    42  }
    43  
    44  // Get retrieves details on a single domain, by ID.
    45  func Get(client *golangsdk.ServiceClient, id string) (r GetResult) {
    46  	_, r.Err = client.Get(getURL(client, id), &r.Body, nil)
    47  	return
    48  }
    49  
    50  // CreateOptsBuilder allows extensions to add additional parameters to
    51  // the Create request.
    52  type CreateOptsBuilder interface {
    53  	ToDomainCreateMap() (map[string]interface{}, error)
    54  }
    55  
    56  // CreateOpts provides options used to create a domain.
    57  type CreateOpts struct {
    58  	// Name is the name of the new domain.
    59  	Name string `json:"name" required:"true"`
    60  
    61  	// Description is a description of the domain.
    62  	Description string `json:"description,omitempty"`
    63  
    64  	// Enabled sets the domain status to enabled or disabled.
    65  	Enabled *bool `json:"enabled,omitempty"`
    66  }
    67  
    68  // ToDomainCreateMap formats a CreateOpts into a create request.
    69  func (opts CreateOpts) ToDomainCreateMap() (map[string]interface{}, error) {
    70  	return golangsdk.BuildRequestBody(opts, "domain")
    71  }
    72  
    73  // Create creates a new Domain.
    74  func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    75  	b, err := opts.ToDomainCreateMap()
    76  	if err != nil {
    77  		r.Err = err
    78  		return
    79  	}
    80  	_, r.Err = client.Post(createURL(client), &b, &r.Body, &golangsdk.RequestOpts{
    81  		OkCodes: []int{201},
    82  	})
    83  	return
    84  }
    85  
    86  // Delete deletes a domain.
    87  func Delete(client *golangsdk.ServiceClient, domainID string) (r DeleteResult) {
    88  	_, r.Err = client.Delete(deleteURL(client, domainID), nil)
    89  	return
    90  }
    91  
    92  // UpdateOptsBuilder allows extensions to add additional parameters to
    93  // the Update request.
    94  type UpdateOptsBuilder interface {
    95  	ToDomainUpdateMap() (map[string]interface{}, error)
    96  }
    97  
    98  // UpdateOpts represents parameters to update a domain.
    99  type UpdateOpts struct {
   100  	// Name is the name of the domain.
   101  	Name string `json:"name,omitempty"`
   102  
   103  	// Description is the description of the domain.
   104  	Description string `json:"description,omitempty"`
   105  
   106  	// Enabled sets the domain status to enabled or disabled.
   107  	Enabled *bool `json:"enabled,omitempty"`
   108  }
   109  
   110  // ToUpdateCreateMap formats a UpdateOpts into an update request.
   111  func (opts UpdateOpts) ToDomainUpdateMap() (map[string]interface{}, error) {
   112  	return golangsdk.BuildRequestBody(opts, "domain")
   113  }
   114  
   115  // Update modifies the attributes of a domain.
   116  func Update(client *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   117  	b, err := opts.ToDomainUpdateMap()
   118  	if err != nil {
   119  		r.Err = err
   120  		return
   121  	}
   122  	_, r.Err = client.Patch(updateURL(client, id), b, &r.Body, &golangsdk.RequestOpts{
   123  		OkCodes: []int{200},
   124  	})
   125  	return
   126  }