github.com/gophercloud/gophercloud@v1.11.0/openstack/objectstorage/v1/accounts/results.go (about)

     1  package accounts
     2  
     3  import (
     4  	"encoding/json"
     5  	"strings"
     6  	"time"
     7  
     8  	"github.com/gophercloud/gophercloud"
     9  )
    10  
    11  // UpdateResult is returned from a call to the Update function.
    12  type UpdateResult struct {
    13  	gophercloud.HeaderResult
    14  }
    15  
    16  // UpdateHeader represents the headers returned in the response from an Update
    17  // request.
    18  type UpdateHeader struct {
    19  	ContentLength int64     `json:"Content-Length,string"`
    20  	ContentType   string    `json:"Content-Type"`
    21  	TransID       string    `json:"X-Trans-Id"`
    22  	Date          time.Time `json:"-"`
    23  }
    24  
    25  func (r *UpdateHeader) UnmarshalJSON(b []byte) error {
    26  	type tmp UpdateHeader
    27  	var s struct {
    28  		tmp
    29  		Date gophercloud.JSONRFC1123 `json:"Date"`
    30  	}
    31  	err := json.Unmarshal(b, &s)
    32  	if err != nil {
    33  		return err
    34  	}
    35  
    36  	*r = UpdateHeader(s.tmp)
    37  
    38  	r.Date = time.Time(s.Date)
    39  
    40  	return err
    41  }
    42  
    43  // Extract will return a struct of headers returned from a call to Get. To
    44  // obtain a map of headers, call the Extract method on the GetResult.
    45  func (r UpdateResult) Extract() (*UpdateHeader, error) {
    46  	var s UpdateHeader
    47  	err := r.ExtractInto(&s)
    48  	return &s, err
    49  }
    50  
    51  // GetHeader represents the headers returned in the response from a Get request.
    52  type GetHeader struct {
    53  	BytesUsed      int64     `json:"X-Account-Bytes-Used,string"`
    54  	QuotaBytes     *int64    `json:"X-Account-Meta-Quota-Bytes,string"`
    55  	ContainerCount int64     `json:"X-Account-Container-Count,string"`
    56  	ContentLength  int64     `json:"Content-Length,string"`
    57  	ObjectCount    int64     `json:"X-Account-Object-Count,string"`
    58  	ContentType    string    `json:"Content-Type"`
    59  	TransID        string    `json:"X-Trans-Id"`
    60  	TempURLKey     string    `json:"X-Account-Meta-Temp-URL-Key"`
    61  	TempURLKey2    string    `json:"X-Account-Meta-Temp-URL-Key-2"`
    62  	Date           time.Time `json:"-"`
    63  }
    64  
    65  func (r *GetHeader) UnmarshalJSON(b []byte) error {
    66  	type tmp GetHeader
    67  	var s struct {
    68  		tmp
    69  		Date string `json:"Date"`
    70  	}
    71  	err := json.Unmarshal(b, &s)
    72  	if err != nil {
    73  		return err
    74  	}
    75  
    76  	*r = GetHeader(s.tmp)
    77  
    78  	if s.Date != "" {
    79  		r.Date, err = time.Parse(time.RFC1123, s.Date)
    80  	}
    81  
    82  	return err
    83  }
    84  
    85  // GetResult is returned from a call to the Get function.
    86  type GetResult struct {
    87  	gophercloud.HeaderResult
    88  }
    89  
    90  // Extract will return a struct of headers returned from a call to Get.
    91  func (r GetResult) Extract() (*GetHeader, error) {
    92  	var s GetHeader
    93  	err := r.ExtractInto(&s)
    94  	return &s, err
    95  }
    96  
    97  // ExtractMetadata is a function that takes a GetResult (of type *http.Response)
    98  // and returns the custom metatdata associated with the account.
    99  func (r GetResult) ExtractMetadata() (map[string]string, error) {
   100  	if r.Err != nil {
   101  		return nil, r.Err
   102  	}
   103  
   104  	metadata := make(map[string]string)
   105  	for k, v := range r.Header {
   106  		if strings.HasPrefix(k, "X-Account-Meta-") {
   107  			key := strings.TrimPrefix(k, "X-Account-Meta-")
   108  			metadata[key] = v[0]
   109  		}
   110  	}
   111  	return metadata, nil
   112  }