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

     1  package accounts
     2  
     3  import (
     4  	"encoding/json"
     5  	"strconv"
     6  	"strings"
     7  	"time"
     8  
     9  	"github.com/huaweicloud/golangsdk"
    10  )
    11  
    12  // UpdateResult is returned from a call to the Update function.
    13  type UpdateResult struct {
    14  	golangsdk.HeaderResult
    15  }
    16  
    17  // UpdateHeader represents the headers returned in the response from an Update
    18  // request.
    19  type UpdateHeader struct {
    20  	ContentLength int64     `json:"-"`
    21  	ContentType   string    `json:"Content-Type"`
    22  	TransID       string    `json:"X-Trans-Id"`
    23  	Date          time.Time `json:"-"`
    24  }
    25  
    26  func (r *UpdateHeader) UnmarshalJSON(b []byte) error {
    27  	type tmp UpdateHeader
    28  	var s struct {
    29  		tmp
    30  		ContentLength string                `json:"Content-Length"`
    31  		Date          golangsdk.JSONRFC1123 `json:"Date"`
    32  	}
    33  	err := json.Unmarshal(b, &s)
    34  	if err != nil {
    35  		return err
    36  	}
    37  
    38  	*r = UpdateHeader(s.tmp)
    39  
    40  	switch s.ContentLength {
    41  	case "":
    42  		r.ContentLength = 0
    43  	default:
    44  		r.ContentLength, err = strconv.ParseInt(s.ContentLength, 10, 64)
    45  		if err != nil {
    46  			return err
    47  		}
    48  	}
    49  
    50  	r.Date = time.Time(s.Date)
    51  
    52  	return err
    53  }
    54  
    55  // Extract will return a struct of headers returned from a call to Get. To
    56  // obtain a map of headers, call the Extract method on the GetResult.
    57  func (r UpdateResult) Extract() (*UpdateHeader, error) {
    58  	var s *UpdateHeader
    59  	err := r.ExtractInto(&s)
    60  	return s, err
    61  }
    62  
    63  // GetHeader represents the headers returned in the response from a Get request.
    64  type GetHeader struct {
    65  	BytesUsed      int64     `json:"-"`
    66  	QuotaBytes     *int64    `json:"-"`
    67  	ContainerCount int64     `json:"-"`
    68  	ContentLength  int64     `json:"-"`
    69  	ObjectCount    int64     `json:"-"`
    70  	ContentType    string    `json:"Content-Type"`
    71  	TransID        string    `json:"X-Trans-Id"`
    72  	TempURLKey     string    `json:"X-Account-Meta-Temp-URL-Key"`
    73  	TempURLKey2    string    `json:"X-Account-Meta-Temp-URL-Key-2"`
    74  	Date           time.Time `json:"-"`
    75  }
    76  
    77  func (r *GetHeader) UnmarshalJSON(b []byte) error {
    78  	type tmp GetHeader
    79  	var s struct {
    80  		tmp
    81  		BytesUsed      string `json:"X-Account-Bytes-Used"`
    82  		QuotaBytes     string `json:"X-Account-Meta-Quota-Bytes"`
    83  		ContentLength  string `json:"Content-Length"`
    84  		ContainerCount string `json:"X-Account-Container-Count"`
    85  		ObjectCount    string `json:"X-Account-Object-Count"`
    86  		Date           string `json:"Date"`
    87  	}
    88  	err := json.Unmarshal(b, &s)
    89  	if err != nil {
    90  		return err
    91  	}
    92  
    93  	*r = GetHeader(s.tmp)
    94  
    95  	switch s.BytesUsed {
    96  	case "":
    97  		r.BytesUsed = 0
    98  	default:
    99  		r.BytesUsed, err = strconv.ParseInt(s.BytesUsed, 10, 64)
   100  		if err != nil {
   101  			return err
   102  		}
   103  	}
   104  
   105  	switch s.QuotaBytes {
   106  	case "":
   107  		r.QuotaBytes = nil
   108  	default:
   109  		v, err := strconv.ParseInt(s.QuotaBytes, 10, 64)
   110  		if err != nil {
   111  			return err
   112  		}
   113  		r.QuotaBytes = &v
   114  	}
   115  
   116  	switch s.ContentLength {
   117  	case "":
   118  		r.ContentLength = 0
   119  	default:
   120  		r.ContentLength, err = strconv.ParseInt(s.ContentLength, 10, 64)
   121  		if err != nil {
   122  			return err
   123  		}
   124  	}
   125  
   126  	switch s.ObjectCount {
   127  	case "":
   128  		r.ObjectCount = 0
   129  	default:
   130  		r.ObjectCount, err = strconv.ParseInt(s.ObjectCount, 10, 64)
   131  		if err != nil {
   132  			return err
   133  		}
   134  	}
   135  
   136  	switch s.ContainerCount {
   137  	case "":
   138  		r.ContainerCount = 0
   139  	default:
   140  		r.ContainerCount, err = strconv.ParseInt(s.ContainerCount, 10, 64)
   141  		if err != nil {
   142  			return err
   143  		}
   144  	}
   145  
   146  	if s.Date != "" {
   147  		r.Date, err = time.Parse(time.RFC1123, s.Date)
   148  	}
   149  
   150  	return err
   151  }
   152  
   153  // GetResult is returned from a call to the Get function.
   154  type GetResult struct {
   155  	golangsdk.HeaderResult
   156  }
   157  
   158  // Extract will return a struct of headers returned from a call to Get.
   159  func (r GetResult) Extract() (*GetHeader, error) {
   160  	var s *GetHeader
   161  	err := r.ExtractInto(&s)
   162  	return s, err
   163  }
   164  
   165  // ExtractMetadata is a function that takes a GetResult (of type *http.Response)
   166  // and returns the custom metatdata associated with the account.
   167  func (r GetResult) ExtractMetadata() (map[string]string, error) {
   168  	if r.Err != nil {
   169  		return nil, r.Err
   170  	}
   171  
   172  	metadata := make(map[string]string)
   173  	for k, v := range r.Header {
   174  		if strings.HasPrefix(k, "X-Account-Meta-") {
   175  			key := strings.TrimPrefix(k, "X-Account-Meta-")
   176  			metadata[key] = v[0]
   177  		}
   178  	}
   179  	return metadata, nil
   180  }