results.go 4.0 KB

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