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

     1  package containers
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"strconv"
     7  	"strings"
     8  	"time"
     9  
    10  	"github.com/huaweicloud/golangsdk"
    11  	"github.com/huaweicloud/golangsdk/pagination"
    12  )
    13  
    14  // Container represents a container resource.
    15  type Container struct {
    16  	// The total number of bytes stored in the container.
    17  	Bytes int64 `json:"bytes"`
    18  
    19  	// The total number of objects stored in the container.
    20  	Count int64 `json:"count"`
    21  
    22  	// The name of the container.
    23  	Name string `json:"name"`
    24  }
    25  
    26  // ContainerPage is the page returned by a pager when traversing over a
    27  // collection of containers.
    28  type ContainerPage struct {
    29  	pagination.MarkerPageBase
    30  }
    31  
    32  //IsEmpty returns true if a ListResult contains no container names.
    33  func (r ContainerPage) IsEmpty() (bool, error) {
    34  	names, err := ExtractNames(r)
    35  	return len(names) == 0, err
    36  }
    37  
    38  // LastMarker returns the last container name in a ListResult.
    39  func (r ContainerPage) LastMarker() (string, error) {
    40  	names, err := ExtractNames(r)
    41  	if err != nil {
    42  		return "", err
    43  	}
    44  	if len(names) == 0 {
    45  		return "", nil
    46  	}
    47  	return names[len(names)-1], nil
    48  }
    49  
    50  // ExtractInfo is a function that takes a ListResult and returns the
    51  // containers' information.
    52  func ExtractInfo(r pagination.Page) ([]Container, error) {
    53  	var s []Container
    54  	err := (r.(ContainerPage)).ExtractInto(&s)
    55  	return s, err
    56  }
    57  
    58  // ExtractNames is a function that takes a ListResult and returns the
    59  // containers' names.
    60  func ExtractNames(page pagination.Page) ([]string, error) {
    61  	casted := page.(ContainerPage)
    62  	ct := casted.Header.Get("Content-Type")
    63  
    64  	switch {
    65  	case strings.HasPrefix(ct, "application/json"):
    66  		parsed, err := ExtractInfo(page)
    67  		if err != nil {
    68  			return nil, err
    69  		}
    70  
    71  		names := make([]string, 0, len(parsed))
    72  		for _, container := range parsed {
    73  			names = append(names, container.Name)
    74  		}
    75  		return names, nil
    76  	case strings.HasPrefix(ct, "text/plain"):
    77  		names := make([]string, 0, 50)
    78  
    79  		body := string(page.(ContainerPage).Body.([]uint8))
    80  		for _, name := range strings.Split(body, "\n") {
    81  			if len(name) > 0 {
    82  				names = append(names, name)
    83  			}
    84  		}
    85  
    86  		return names, nil
    87  	default:
    88  		return nil, fmt.Errorf("Cannot extract names from response with content-type: [%s]", ct)
    89  	}
    90  }
    91  
    92  // GetHeader represents the headers returned in the response from a Get request.
    93  type GetHeader struct {
    94  	AcceptRanges     string    `json:"Accept-Ranges"`
    95  	BytesUsed        int64     `json:"-"`
    96  	ContentLength    int64     `json:"-"`
    97  	ContentType      string    `json:"Content-Type"`
    98  	Date             time.Time `json:"-"`
    99  	ObjectCount      int64     `json:"-"`
   100  	Read             []string  `json:"-"`
   101  	TransID          string    `json:"X-Trans-Id"`
   102  	VersionsLocation string    `json:"X-Versions-Location"`
   103  	Write            []string  `json:"-"`
   104  	StoragePolicy    string    `json:"X-Storage-Policy"`
   105  }
   106  
   107  func (r *GetHeader) UnmarshalJSON(b []byte) error {
   108  	type tmp GetHeader
   109  	var s struct {
   110  		tmp
   111  		BytesUsed     string                `json:"X-Container-Bytes-Used"`
   112  		ContentLength string                `json:"Content-Length"`
   113  		ObjectCount   string                `json:"X-Container-Object-Count"`
   114  		Write         string                `json:"X-Container-Write"`
   115  		Read          string                `json:"X-Container-Read"`
   116  		Date          golangsdk.JSONRFC1123 `json:"Date"`
   117  	}
   118  	err := json.Unmarshal(b, &s)
   119  	if err != nil {
   120  		return err
   121  	}
   122  
   123  	*r = GetHeader(s.tmp)
   124  
   125  	switch s.BytesUsed {
   126  	case "":
   127  		r.BytesUsed = 0
   128  	default:
   129  		r.BytesUsed, err = strconv.ParseInt(s.BytesUsed, 10, 64)
   130  		if err != nil {
   131  			return err
   132  		}
   133  	}
   134  
   135  	switch s.ContentLength {
   136  	case "":
   137  		r.ContentLength = 0
   138  	default:
   139  		r.ContentLength, err = strconv.ParseInt(s.ContentLength, 10, 64)
   140  		if err != nil {
   141  			return err
   142  		}
   143  	}
   144  
   145  	switch s.ObjectCount {
   146  	case "":
   147  		r.ObjectCount = 0
   148  	default:
   149  		r.ObjectCount, err = strconv.ParseInt(s.ObjectCount, 10, 64)
   150  		if err != nil {
   151  			return err
   152  		}
   153  	}
   154  
   155  	r.Read = strings.Split(s.Read, ",")
   156  	r.Write = strings.Split(s.Write, ",")
   157  
   158  	r.Date = time.Time(s.Date)
   159  
   160  	return err
   161  }
   162  
   163  // GetResult represents the result of a get operation.
   164  type GetResult struct {
   165  	golangsdk.HeaderResult
   166  }
   167  
   168  // Extract will return a struct of headers returned from a call to Get.
   169  func (r GetResult) Extract() (*GetHeader, error) {
   170  	var s *GetHeader
   171  	err := r.ExtractInto(&s)
   172  	return s, err
   173  }
   174  
   175  // ExtractMetadata is a function that takes a GetResult (of type *http.Response)
   176  // and returns the custom metadata associated with the container.
   177  func (r GetResult) ExtractMetadata() (map[string]string, error) {
   178  	if r.Err != nil {
   179  		return nil, r.Err
   180  	}
   181  	metadata := make(map[string]string)
   182  	for k, v := range r.Header {
   183  		if strings.HasPrefix(k, "X-Container-Meta-") {
   184  			key := strings.TrimPrefix(k, "X-Container-Meta-")
   185  			metadata[key] = v[0]
   186  		}
   187  	}
   188  	return metadata, nil
   189  }
   190  
   191  // CreateHeader represents the headers returned in the response from a Create
   192  // request.
   193  type CreateHeader struct {
   194  	ContentLength int64     `json:"-"`
   195  	ContentType   string    `json:"Content-Type"`
   196  	Date          time.Time `json:"-"`
   197  	TransID       string    `json:"X-Trans-Id"`
   198  }
   199  
   200  func (r *CreateHeader) UnmarshalJSON(b []byte) error {
   201  	type tmp CreateHeader
   202  	var s struct {
   203  		tmp
   204  		ContentLength string                `json:"Content-Length"`
   205  		Date          golangsdk.JSONRFC1123 `json:"Date"`
   206  	}
   207  	err := json.Unmarshal(b, &s)
   208  	if err != nil {
   209  		return err
   210  	}
   211  
   212  	*r = CreateHeader(s.tmp)
   213  
   214  	switch s.ContentLength {
   215  	case "":
   216  		r.ContentLength = 0
   217  	default:
   218  		r.ContentLength, err = strconv.ParseInt(s.ContentLength, 10, 64)
   219  		if err != nil {
   220  			return err
   221  		}
   222  	}
   223  
   224  	r.Date = time.Time(s.Date)
   225  
   226  	return err
   227  }
   228  
   229  // CreateResult represents the result of a create operation. To extract the
   230  // the headers from the HTTP response, call its Extract method.
   231  type CreateResult struct {
   232  	golangsdk.HeaderResult
   233  }
   234  
   235  // Extract will return a struct of headers returned from a call to Create.
   236  // To extract the headers from the HTTP response, call its Extract method.
   237  func (r CreateResult) Extract() (*CreateHeader, error) {
   238  	var s *CreateHeader
   239  	err := r.ExtractInto(&s)
   240  	return s, err
   241  }
   242  
   243  // UpdateHeader represents the headers returned in the response from a Update
   244  // request.
   245  type UpdateHeader struct {
   246  	ContentLength int64     `json:"-"`
   247  	ContentType   string    `json:"Content-Type"`
   248  	Date          time.Time `json:"-"`
   249  	TransID       string    `json:"X-Trans-Id"`
   250  }
   251  
   252  func (r *UpdateHeader) UnmarshalJSON(b []byte) error {
   253  	type tmp UpdateHeader
   254  	var s struct {
   255  		tmp
   256  		ContentLength string                `json:"Content-Length"`
   257  		Date          golangsdk.JSONRFC1123 `json:"Date"`
   258  	}
   259  	err := json.Unmarshal(b, &s)
   260  	if err != nil {
   261  		return err
   262  	}
   263  
   264  	*r = UpdateHeader(s.tmp)
   265  
   266  	switch s.ContentLength {
   267  	case "":
   268  		r.ContentLength = 0
   269  	default:
   270  		r.ContentLength, err = strconv.ParseInt(s.ContentLength, 10, 64)
   271  		if err != nil {
   272  			return err
   273  		}
   274  	}
   275  
   276  	r.Date = time.Time(s.Date)
   277  
   278  	return err
   279  }
   280  
   281  // UpdateResult represents the result of an update operation. To extract the
   282  // the headers from the HTTP response, call its Extract method.
   283  type UpdateResult struct {
   284  	golangsdk.HeaderResult
   285  }
   286  
   287  // Extract will return a struct of headers returned from a call to Update.
   288  func (r UpdateResult) Extract() (*UpdateHeader, error) {
   289  	var s *UpdateHeader
   290  	err := r.ExtractInto(&s)
   291  	return s, err
   292  }
   293  
   294  // DeleteHeader represents the headers returned in the response from a Delete
   295  // request.
   296  type DeleteHeader struct {
   297  	ContentLength int64     `json:"-"`
   298  	ContentType   string    `json:"Content-Type"`
   299  	Date          time.Time `json:"-"`
   300  	TransID       string    `json:"X-Trans-Id"`
   301  }
   302  
   303  func (r *DeleteHeader) UnmarshalJSON(b []byte) error {
   304  	type tmp DeleteHeader
   305  	var s struct {
   306  		tmp
   307  		ContentLength string                `json:"Content-Length"`
   308  		Date          golangsdk.JSONRFC1123 `json:"Date"`
   309  	}
   310  	err := json.Unmarshal(b, &s)
   311  	if err != nil {
   312  		return err
   313  	}
   314  
   315  	*r = DeleteHeader(s.tmp)
   316  
   317  	switch s.ContentLength {
   318  	case "":
   319  		r.ContentLength = 0
   320  	default:
   321  		r.ContentLength, err = strconv.ParseInt(s.ContentLength, 10, 64)
   322  		if err != nil {
   323  			return err
   324  		}
   325  	}
   326  
   327  	r.Date = time.Time(s.Date)
   328  
   329  	return err
   330  }
   331  
   332  // DeleteResult represents the result of a delete operation. To extract the
   333  // the headers from the HTTP response, call its Extract method.
   334  type DeleteResult struct {
   335  	golangsdk.HeaderResult
   336  }
   337  
   338  // Extract will return a struct of headers returned from a call to Delete.
   339  func (r DeleteResult) Extract() (*DeleteHeader, error) {
   340  	var s *DeleteHeader
   341  	err := r.ExtractInto(&s)
   342  	return s, err
   343  }