github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/swr/v2/domains/requests.go (about)

     1  package domains
     2  
     3  import (
     4  	"github.com/chnsz/golangsdk"
     5  	"github.com/chnsz/golangsdk/pagination"
     6  )
     7  
     8  type CreateOptsBuilder interface {
     9  	ToAccessDomainCreateMap() (map[string]interface{}, error)
    10  }
    11  
    12  type CreateOpts struct {
    13  	AccessDomain string `json:"access_domain" required:"true"`
    14  	// Currently, only the `read` permission is supported.
    15  	Permit string `json:"permit" required:"true"`
    16  	// End date of image sharing (UTC). When the value is set to `forever`,
    17  	// the image will be permanently available for the domain.
    18  	// The validity period is calculated by day. The shared images expire at 00:00:00 on the day after the end date.
    19  	Deadline    string `json:"deadline" required:"true"`
    20  	Description string `json:"description,omitempty"`
    21  }
    22  
    23  func (opts CreateOpts) ToAccessDomainCreateMap() (map[string]interface{}, error) {
    24  	return golangsdk.BuildRequestBody(opts, "")
    25  }
    26  
    27  func Create(client *golangsdk.ServiceClient, namespace, repository string, opts CreateOptsBuilder) (r CreateResult) {
    28  	url := rootURL(client, namespace, repository)
    29  	b, err := opts.ToAccessDomainCreateMap()
    30  	if err != nil {
    31  		r.Err = err
    32  		return
    33  	}
    34  	_, r.Err = client.Post(url, b, &r.Body, nil)
    35  	return
    36  }
    37  
    38  func Get(client *golangsdk.ServiceClient, namespace, repository, domain string) (r GetResult) {
    39  	_, r.Err = client.Get(resourceURL(client, namespace, repository, domain), &r.Body, nil)
    40  	return
    41  }
    42  
    43  type UpdateOptsBuilder interface {
    44  	ToAccessDomainUpdateMap() (map[string]interface{}, error)
    45  }
    46  
    47  // UpdateOpts used for update operations
    48  // For argument details see CreateOpts
    49  type UpdateOpts struct {
    50  	Permit      string  `json:"permit" required:"true"`
    51  	Deadline    string  `json:"deadline" required:"true"`
    52  	Description *string `json:"description,omitempty"`
    53  }
    54  
    55  func (opts UpdateOpts) ToAccessDomainUpdateMap() (map[string]interface{}, error) {
    56  	return golangsdk.BuildRequestBody(opts, "")
    57  }
    58  
    59  func Update(client *golangsdk.ServiceClient, namespace, repository, domain string, opts UpdateOptsBuilder) (r UpdateResult) {
    60  	b, err := opts.ToAccessDomainUpdateMap()
    61  	if err != nil {
    62  		r.Err = err
    63  		return
    64  	}
    65  
    66  	_, r.Err = client.Patch(resourceURL(client, namespace, repository, domain), b, &r.Body, &golangsdk.RequestOpts{
    67  		OkCodes: []int{201},
    68  	})
    69  	return
    70  }
    71  
    72  func Delete(client *golangsdk.ServiceClient, namespace, repository, domain string) (r DeleteResult) {
    73  	_, r.Err = client.Delete(resourceURL(client, namespace, repository, domain), nil)
    74  	return
    75  }
    76  
    77  func List(client *golangsdk.ServiceClient, namespace, repository string) (p pagination.Pager) {
    78  	return pagination.NewPager(client, rootURL(client, namespace, repository), func(r pagination.PageResult) pagination.Page {
    79  		return AccessDomainPage{SinglePageBase: pagination.SinglePageBase(r)}
    80  	})
    81  }