github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/swr/v2/repositories/requests.go (about)

     1  package repositories
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/chnsz/golangsdk"
     7  	"github.com/chnsz/golangsdk/pagination"
     8  )
     9  
    10  type CreateOptsBuilder interface {
    11  	ToRepositoryCreateMap() (map[string]interface{}, error)
    12  }
    13  
    14  type CreateOpts struct {
    15  	// Name of the image repository
    16  	Repository string `json:"repository" required:"true"`
    17  	// Type of the image repository
    18  	// The value can be app_server, linux, framework_app, database, lang, other, windows, arm
    19  	Category string `json:"category,omitempty"`
    20  	// Description of the image repository
    21  	Description string `json:"description,omitempty"`
    22  	// Whether the image repository is public
    23  	IsPublic bool `json:"is_public"`
    24  }
    25  
    26  func (opts CreateOpts) ToRepositoryCreateMap() (map[string]interface{}, error) {
    27  	return golangsdk.BuildRequestBody(opts, "")
    28  }
    29  
    30  // Create new repository in the namespace(organization)
    31  func Create(client *golangsdk.ServiceClient, namespace string, opts CreateOptsBuilder) (r CreateResult) {
    32  	b, err := opts.ToRepositoryCreateMap()
    33  	if err != nil {
    34  		r.Err = err
    35  		return
    36  	}
    37  	_, r.Err = client.Post(rootURL(client, namespace), b, &r.Body, nil)
    38  	return
    39  }
    40  
    41  type ListOptsBuilder interface {
    42  	ToRepositoryListQuery() (string, error)
    43  }
    44  
    45  type ListOpts struct {
    46  	// Namespace(Organization) name
    47  	Namespace string `q:"namespace"`
    48  	// Image repository name
    49  	Name string `q:"name"`
    50  	// Image repository category.
    51  	// The value can be app_server, linux, framework_app, database, lang, other, windows, arm
    52  	Category string `q:"category"`
    53  	// Sorting by column.
    54  	// You can set this parameter to name, updated_time, and tag_count.
    55  	// The parameters OrderColumn and OrderType should always be used together.
    56  	OrderColumn string `q:"order_column"`
    57  	// Sorting type.
    58  	// You can set this parameter to desc(descending sort) and asc(ascending sort).
    59  	OrderType string `q:"order_type"`
    60  	// offset 0 is a valid value
    61  	Offset *int `q:"offset,omitempty"`
    62  	Limit  int  `q:"limit,omitempty"`
    63  }
    64  
    65  const defaultLimit = 25
    66  
    67  func (opts ListOpts) ToRepositoryListQuery() (string, error) {
    68  	if opts.Limit == 0 && opts.Offset != nil {
    69  		opts.Limit = defaultLimit
    70  	}
    71  	if opts.Limit != 0 && opts.Offset == nil {
    72  		return "", fmt.Errorf("offset has to be defined if the limit is set")
    73  	}
    74  	if (opts.OrderColumn != "" && opts.OrderType == "") || (opts.OrderColumn == "" && opts.OrderType != "") {
    75  		return "", fmt.Errorf("`OrderColumn` and `OrderType` should always be used together")
    76  	}
    77  	q, err := golangsdk.BuildQueryString(opts)
    78  	if err != nil {
    79  		return "", err
    80  	}
    81  	return q.String(), nil
    82  }
    83  
    84  func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) (p pagination.Pager) {
    85  	url := listURL(client)
    86  	if opts != nil {
    87  		q, err := opts.ToRepositoryListQuery()
    88  		if err != nil {
    89  			return pagination.Pager{Err: err}
    90  		}
    91  		url += q
    92  	}
    93  
    94  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
    95  		return RepositoryPage{pagination.LinkedPageBase{PageResult: r}}
    96  	})
    97  }
    98  
    99  func Get(client *golangsdk.ServiceClient, namespace, repository string) (r GetResult) {
   100  	_, r.Err = client.Get(resourceURL(client, namespace, repository), &r.Body, nil)
   101  	return
   102  }
   103  
   104  type UpdateOptsBuilder interface {
   105  	ToRepositoryUpdateMap() (map[string]interface{}, error)
   106  }
   107  
   108  type UpdateOpts struct {
   109  	Category    string `json:"category,omitempty"`
   110  	Description string `json:"description,omitempty"`
   111  	IsPublic    bool   `json:"is_public"`
   112  }
   113  
   114  func (opts UpdateOpts) ToRepositoryUpdateMap() (map[string]interface{}, error) {
   115  	return golangsdk.BuildRequestBody(opts, "")
   116  }
   117  
   118  func Update(client *golangsdk.ServiceClient, namespace, repository string, opts UpdateOptsBuilder) (r UpdateResult) {
   119  	b, err := opts.ToRepositoryUpdateMap()
   120  	if err != nil {
   121  		r.Err = err
   122  		return
   123  	}
   124  	_, r.Err = client.Patch(resourceURL(client, namespace, repository), b, &r.Body, &golangsdk.RequestOpts{
   125  		OkCodes: []int{201},
   126  	})
   127  	return
   128  }
   129  
   130  func Delete(client *golangsdk.ServiceClient, namespace, repository string) (r DeleteResult) {
   131  	_, r.Err = client.Delete(resourceURL(client, namespace, repository), nil)
   132  	return
   133  }