github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/objectstorage/v1/accounts/requests.go (about)

     1  package accounts
     2  
     3  import "github.com/huaweicloud/golangsdk"
     4  
     5  // GetOptsBuilder allows extensions to add additional headers to the Get
     6  // request.
     7  type GetOptsBuilder interface {
     8  	ToAccountGetMap() (map[string]string, error)
     9  }
    10  
    11  // GetOpts is a structure that contains parameters for getting an account's
    12  // metadata.
    13  type GetOpts struct {
    14  	Newest bool `h:"X-Newest"`
    15  }
    16  
    17  // ToAccountGetMap formats a GetOpts into a map[string]string of headers.
    18  func (opts GetOpts) ToAccountGetMap() (map[string]string, error) {
    19  	return golangsdk.BuildHeaders(opts)
    20  }
    21  
    22  // Get is a function that retrieves an account's metadata. To extract just the
    23  // custom metadata, call the ExtractMetadata method on the GetResult. To extract
    24  // all the headers that are returned (including the metadata), call the
    25  // Extract method on the GetResult.
    26  func Get(c *golangsdk.ServiceClient, opts GetOptsBuilder) (r GetResult) {
    27  	h := make(map[string]string)
    28  	if opts != nil {
    29  		headers, err := opts.ToAccountGetMap()
    30  		if err != nil {
    31  			r.Err = err
    32  			return
    33  		}
    34  		for k, v := range headers {
    35  			h[k] = v
    36  		}
    37  	}
    38  	resp, err := c.Request("HEAD", getURL(c), &golangsdk.RequestOpts{
    39  		MoreHeaders: h,
    40  		OkCodes:     []int{204},
    41  	})
    42  	if resp != nil {
    43  		r.Header = resp.Header
    44  	}
    45  	r.Err = err
    46  	return
    47  }
    48  
    49  // UpdateOptsBuilder allows extensions to add additional headers to the Update
    50  // request.
    51  type UpdateOptsBuilder interface {
    52  	ToAccountUpdateMap() (map[string]string, error)
    53  }
    54  
    55  // UpdateOpts is a structure that contains parameters for updating, creating, or
    56  // deleting an account's metadata.
    57  type UpdateOpts struct {
    58  	Metadata          map[string]string
    59  	ContentType       string `h:"Content-Type"`
    60  	DetectContentType bool   `h:"X-Detect-Content-Type"`
    61  	TempURLKey        string `h:"X-Account-Meta-Temp-URL-Key"`
    62  	TempURLKey2       string `h:"X-Account-Meta-Temp-URL-Key-2"`
    63  }
    64  
    65  // ToAccountUpdateMap formats an UpdateOpts into a map[string]string of headers.
    66  func (opts UpdateOpts) ToAccountUpdateMap() (map[string]string, error) {
    67  	headers, err := golangsdk.BuildHeaders(opts)
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  	for k, v := range opts.Metadata {
    72  		headers["X-Account-Meta-"+k] = v
    73  	}
    74  	return headers, err
    75  }
    76  
    77  // Update is a function that creates, updates, or deletes an account's metadata.
    78  // To extract the headers returned, call the Extract method on the UpdateResult.
    79  func Update(c *golangsdk.ServiceClient, opts UpdateOptsBuilder) (r UpdateResult) {
    80  	h := make(map[string]string)
    81  	if opts != nil {
    82  		headers, err := opts.ToAccountUpdateMap()
    83  		if err != nil {
    84  			r.Err = err
    85  			return
    86  		}
    87  		for k, v := range headers {
    88  			h[k] = v
    89  		}
    90  	}
    91  	resp, err := c.Request("POST", updateURL(c), &golangsdk.RequestOpts{
    92  		MoreHeaders: h,
    93  		OkCodes:     []int{201, 202, 204},
    94  	})
    95  	if resp != nil {
    96  		r.Header = resp.Header
    97  	}
    98  	r.Err = err
    99  	return
   100  }