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

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