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

     1  package containers
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  	"github.com/huaweicloud/golangsdk/pagination"
     6  )
     7  
     8  // ListOptsBuilder allows extensions to add additional parameters to the List
     9  // request.
    10  type ListOptsBuilder interface {
    11  	ToContainerListParams() (bool, string, error)
    12  }
    13  
    14  // ListOpts is a structure that holds options for listing containers.
    15  type ListOpts struct {
    16  	Full      bool
    17  	Limit     int    `q:"limit"`
    18  	Marker    string `q:"marker"`
    19  	EndMarker string `q:"end_marker"`
    20  	Format    string `q:"format"`
    21  	Prefix    string `q:"prefix"`
    22  	Delimiter string `q:"delimiter"`
    23  }
    24  
    25  // ToContainerListParams formats a ListOpts into a query string and boolean
    26  // representing whether to list complete information for each container.
    27  func (opts ListOpts) ToContainerListParams() (bool, string, error) {
    28  	q, err := golangsdk.BuildQueryString(opts)
    29  	return opts.Full, q.String(), err
    30  }
    31  
    32  // List is a function that retrieves containers associated with the account as
    33  // well as account metadata. It returns a pager which can be iterated with the
    34  // EachPage function.
    35  func List(c *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    36  	headers := map[string]string{"Accept": "text/plain", "Content-Type": "text/plain"}
    37  
    38  	url := listURL(c)
    39  	if opts != nil {
    40  		full, query, err := opts.ToContainerListParams()
    41  		if err != nil {
    42  			return pagination.Pager{Err: err}
    43  		}
    44  		url += query
    45  
    46  		if full {
    47  			headers = map[string]string{"Accept": "application/json", "Content-Type": "application/json"}
    48  		}
    49  	}
    50  
    51  	pager := pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
    52  		p := ContainerPage{pagination.MarkerPageBase{PageResult: r}}
    53  		p.MarkerPageBase.Owner = p
    54  		return p
    55  	})
    56  	pager.Headers = headers
    57  	return pager
    58  }
    59  
    60  // CreateOptsBuilder allows extensions to add additional parameters to the
    61  // Create request.
    62  type CreateOptsBuilder interface {
    63  	ToContainerCreateMap() (map[string]string, error)
    64  }
    65  
    66  // CreateOpts is a structure that holds parameters for creating a container.
    67  type CreateOpts struct {
    68  	Metadata          map[string]string
    69  	ContainerRead     string `h:"X-Container-Read"`
    70  	ContainerSyncTo   string `h:"X-Container-Sync-To"`
    71  	ContainerSyncKey  string `h:"X-Container-Sync-Key"`
    72  	ContainerWrite    string `h:"X-Container-Write"`
    73  	ContentType       string `h:"Content-Type"`
    74  	DetectContentType bool   `h:"X-Detect-Content-Type"`
    75  	IfNoneMatch       string `h:"If-None-Match"`
    76  	VersionsLocation  string `h:"X-Versions-Location"`
    77  }
    78  
    79  // ToContainerCreateMap formats a CreateOpts into a map of headers.
    80  func (opts CreateOpts) ToContainerCreateMap() (map[string]string, error) {
    81  	h, err := golangsdk.BuildHeaders(opts)
    82  	if err != nil {
    83  		return nil, err
    84  	}
    85  	for k, v := range opts.Metadata {
    86  		h["X-Container-Meta-"+k] = v
    87  	}
    88  	return h, nil
    89  }
    90  
    91  // Create is a function that creates a new container.
    92  func Create(c *golangsdk.ServiceClient, containerName string, opts CreateOptsBuilder) (r CreateResult) {
    93  	h := make(map[string]string)
    94  	if opts != nil {
    95  		headers, err := opts.ToContainerCreateMap()
    96  		if err != nil {
    97  			r.Err = err
    98  			return
    99  		}
   100  		for k, v := range headers {
   101  			h[k] = v
   102  		}
   103  	}
   104  	resp, err := c.Request("PUT", createURL(c, containerName), &golangsdk.RequestOpts{
   105  		MoreHeaders: h,
   106  		OkCodes:     []int{201, 202, 204},
   107  	})
   108  	if resp != nil {
   109  		r.Header = resp.Header
   110  		resp.Body.Close()
   111  	}
   112  	r.Err = err
   113  	return
   114  }
   115  
   116  // Delete is a function that deletes a container.
   117  func Delete(c *golangsdk.ServiceClient, containerName string) (r DeleteResult) {
   118  	_, r.Err = c.Delete(deleteURL(c, containerName), nil)
   119  	return
   120  }
   121  
   122  // UpdateOptsBuilder allows extensions to add additional parameters to the
   123  // Update request.
   124  type UpdateOptsBuilder interface {
   125  	ToContainerUpdateMap() (map[string]string, error)
   126  }
   127  
   128  // UpdateOpts is a structure that holds parameters for updating, creating, or
   129  // deleting a container's metadata.
   130  type UpdateOpts struct {
   131  	Metadata               map[string]string
   132  	ContainerRead          string `h:"X-Container-Read"`
   133  	ContainerSyncTo        string `h:"X-Container-Sync-To"`
   134  	ContainerSyncKey       string `h:"X-Container-Sync-Key"`
   135  	ContainerWrite         string `h:"X-Container-Write"`
   136  	ContentType            string `h:"Content-Type"`
   137  	DetectContentType      bool   `h:"X-Detect-Content-Type"`
   138  	RemoveVersionsLocation string `h:"X-Remove-Versions-Location"`
   139  	VersionsLocation       string `h:"X-Versions-Location"`
   140  }
   141  
   142  // ToContainerUpdateMap formats a UpdateOpts into a map of headers.
   143  func (opts UpdateOpts) ToContainerUpdateMap() (map[string]string, error) {
   144  	h, err := golangsdk.BuildHeaders(opts)
   145  	if err != nil {
   146  		return nil, err
   147  	}
   148  	for k, v := range opts.Metadata {
   149  		h["X-Container-Meta-"+k] = v
   150  	}
   151  	return h, nil
   152  }
   153  
   154  // Update is a function that creates, updates, or deletes a container's
   155  // metadata.
   156  func Update(c *golangsdk.ServiceClient, containerName string, opts UpdateOptsBuilder) (r UpdateResult) {
   157  	h := make(map[string]string)
   158  	if opts != nil {
   159  		headers, err := opts.ToContainerUpdateMap()
   160  		if err != nil {
   161  			r.Err = err
   162  			return
   163  		}
   164  
   165  		for k, v := range headers {
   166  			h[k] = v
   167  		}
   168  	}
   169  	resp, err := c.Request("POST", updateURL(c, containerName), &golangsdk.RequestOpts{
   170  		MoreHeaders: h,
   171  		OkCodes:     []int{201, 202, 204},
   172  	})
   173  	if resp != nil {
   174  		r.Header = resp.Header
   175  	}
   176  	r.Err = err
   177  	return
   178  }
   179  
   180  // GetOptsBuilder allows extensions to add additional parameters to the Get
   181  // request.
   182  type GetOptsBuilder interface {
   183  	ToContainerGetMap() (map[string]string, error)
   184  }
   185  
   186  // GetOpts is a structure that holds options for listing containers.
   187  type GetOpts struct {
   188  	Newest bool `h:"X-Newest"`
   189  }
   190  
   191  // ToContainerGetMap formats a GetOpts into a map of headers.
   192  func (opts GetOpts) ToContainerGetMap() (map[string]string, error) {
   193  	return golangsdk.BuildHeaders(opts)
   194  }
   195  
   196  // Get is a function that retrieves the metadata of a container. To extract just
   197  // the custom metadata, pass the GetResult response to the ExtractMetadata
   198  // function.
   199  func Get(c *golangsdk.ServiceClient, containerName string, opts GetOptsBuilder) (r GetResult) {
   200  	h := make(map[string]string)
   201  	if opts != nil {
   202  		headers, err := opts.ToContainerGetMap()
   203  		if err != nil {
   204  			r.Err = err
   205  			return
   206  		}
   207  
   208  		for k, v := range headers {
   209  			h[k] = v
   210  		}
   211  	}
   212  	resp, err := c.Request("HEAD", getURL(c, containerName), &golangsdk.RequestOpts{
   213  		MoreHeaders: h,
   214  		OkCodes:     []int{200, 204},
   215  	})
   216  	if resp != nil {
   217  		r.Header = resp.Header
   218  	}
   219  	r.Err = err
   220  	return
   221  }