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

     1  package dependencies
     2  
     3  import (
     4  	"github.com/chnsz/golangsdk"
     5  	"github.com/chnsz/golangsdk/pagination"
     6  )
     7  
     8  var requestOpts = golangsdk.RequestOpts{
     9  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
    10  }
    11  
    12  // DependOpts is the structure required by Create and Update method to create a new dependency or update an existing
    13  // dependency.
    14  type DependOpts struct {
    15  	// Import mode. Options: obs and zip.
    16  	Type string `json:"depend_type" required:"true"`
    17  	// Runtime.
    18  	// Enumeration values:
    19  	//   Java8
    20  	//   Node.js6.10
    21  	//   Node.js8.10
    22  	//   Node.js10.16
    23  	//   Node.js12.13
    24  	//   Python2.7
    25  	//   Python3.6
    26  	//   Go1.8
    27  	//   Go1.x
    28  	//   C#(.NET Core 2.0)
    29  	//   C#(.NET Core 2.1)
    30  	//   C#(.NET Core 3.1)
    31  	//   PHP 7.3
    32  	Runtime string `json:"runtime" required:"true"`
    33  	// Name of the dependency. The name can contain a maximum of 96 characters and must start with a letter and end with
    34  	// a letter or digit. Only letters, digits, underscores (_), periods (.), and hyphens (-) are allowed.
    35  	Name string `json:"name,omitempty"`
    36  	// Description of the dependency, which can contain a maximum of 512 characters.
    37  	Description *string `json:"description,omitempty"`
    38  	// When depend_type is set to zip, this parameter is required and indicates the file stream format.
    39  	File string `json:"depend_file,omitempty"`
    40  	// When depend_type is set to obs, this parameter indicates the address of the dependency stored in OBS.
    41  	Link string `json:"depend_link,omitempty"`
    42  }
    43  
    44  // Create is a method to create a new custom dependency using a ZIP file in an OBS bucket.
    45  func Create(c *golangsdk.ServiceClient, opts DependOpts) (*Dependency, error) {
    46  	b, err := golangsdk.BuildRequestBody(opts, "")
    47  	if err != nil {
    48  		return nil, err
    49  	}
    50  
    51  	var rst golangsdk.Result
    52  	_, err = c.Post(rootURL(c), b, &rst.Body, &golangsdk.RequestOpts{
    53  		MoreHeaders: requestOpts.MoreHeaders,
    54  	})
    55  	if err == nil {
    56  		var r Dependency
    57  		rst.ExtractInto(&r)
    58  		return &r, nil
    59  	}
    60  	return nil, err
    61  }
    62  
    63  // DependVersionOpts is the structure required by CreateVersion method to create a new dependency (with version info).
    64  type DependVersionOpts struct {
    65  	// Import mode.
    66  	// Valid options are obs and zip.
    67  	Type string `json:"depend_type" required:"true"`
    68  	// Runtime.
    69  	// Enumeration values:
    70  	// + Java8
    71  	// + Java11
    72  	// + Node.js6.10
    73  	// + Node.js8.10
    74  	// + Node.js10.16
    75  	// + Node.js12.13
    76  	// + Node.js14.18
    77  	// + Python2.7
    78  	// + Python3.6
    79  	// + Python3.9
    80  	// + Go1.8
    81  	// + Go1.x
    82  	// + C#(.NET Core 2.0)
    83  	// + C#(.NET Core 2.1)
    84  	// + C#(.NET Core 3.1)
    85  	// + Custom
    86  	// + PHP 7.3
    87  	// + http
    88  	Runtime string `json:"runtime" required:"true"`
    89  	// Name of the dependency. The name can contain a maximum of 96 characters and must start with a letter and end with
    90  	// a letter or digit. Only letters, digits, underscores (_), periods (.), and hyphens (-) are allowed.
    91  	Name string `json:"name" required:"true"`
    92  	// Description of the dependency, which can contain a maximum of 512 characters.
    93  	Description *string `json:"description,omitempty"`
    94  	// When depend_type is set to zip, this parameter is required and indicates the file stream format.
    95  	// Base64 encoded zip file is required.
    96  	// The maximum size of the file to be uploaded is 40MB. If the size exceeds 40MB, upload the file using OBS.
    97  	File string `json:"depend_file,omitempty"`
    98  	// When depend_type is set to obs, this parameter indicates the address of the dependency stored in OBS.
    99  	Link string `json:"depend_link,omitempty"`
   100  }
   101  
   102  // CreateVersion is a method to create a new custom dependency (with version info) using a ZIP file in an OBS bucket.
   103  func CreateVersion(c *golangsdk.ServiceClient, opts DependVersionOpts) (*DependencyVersion, error) {
   104  	b, err := golangsdk.BuildRequestBody(opts, "")
   105  	if err != nil {
   106  		return nil, err
   107  	}
   108  
   109  	var r DependencyVersion
   110  	_, err = c.Post(rootVersionURL(c), b, &r, &golangsdk.RequestOpts{
   111  		MoreHeaders: requestOpts.MoreHeaders,
   112  	})
   113  	return &r, err
   114  }
   115  
   116  // Get is a method to obtain a custom dependency detail using its ID.
   117  func Get(c *golangsdk.ServiceClient, dependId string) (*Dependency, error) {
   118  	var r Dependency
   119  	_, err := c.Get(resourceURL(c, dependId), &r, &golangsdk.RequestOpts{
   120  		MoreHeaders: requestOpts.MoreHeaders,
   121  	})
   122  	return &r, err
   123  }
   124  
   125  // GetVersion is a method to obtain a custom dependency (with version info) detail using its depend ID and specified version.
   126  func GetVersion(c *golangsdk.ServiceClient, dependId, version string) (*DependencyVersion, error) {
   127  	var r DependencyVersion
   128  	_, err := c.Get(resourceVersionURL(c, dependId, version), &r, &golangsdk.RequestOpts{
   129  		MoreHeaders: requestOpts.MoreHeaders,
   130  	})
   131  	return &r, err
   132  }
   133  
   134  // ListOpts allows to filter list data using given parameters.
   135  type ListOpts struct {
   136  	// Dependency type, which support public, private, and all, default to all.
   137  	//   public
   138  	//   private
   139  	//   all
   140  	DependencyType string `q:"dependency_type"`
   141  	// Runtime of function.
   142  	Runtime string `q:"runtime"`
   143  	// Name of the dependency.
   144  	Name string `q:"name"`
   145  	// Final record queried last time. Default value: 0.
   146  	Marker string `q:"marker"`
   147  	// Maximum number of dependencies that can be obtained in a query, default to 400.
   148  	Limit string `q:"limit"`
   149  }
   150  
   151  // ListOptsBuilder is an interface which to support request query build of
   152  // the dependent package search.
   153  type ListOptsBuilder interface {
   154  	ToListQuery() (string, error)
   155  }
   156  
   157  // ToListQuery is a method which to build a request query by the ListOpts.
   158  func (opts ListOpts) ToListQuery() (string, error) {
   159  	q, err := golangsdk.BuildQueryString(opts)
   160  	return q.String(), err
   161  }
   162  
   163  // List is a method to obtain an array of one or more dependent packages according to the query parameters.
   164  func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
   165  	url := rootURL(client)
   166  	if opts != nil {
   167  		query, err := opts.ToListQuery()
   168  		if err != nil {
   169  			return pagination.Pager{Err: err}
   170  		}
   171  		url += query
   172  	}
   173  
   174  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   175  		p := DependencyPage{pagination.MarkerPageBase{PageResult: r}}
   176  		p.MarkerPageBase.Owner = p
   177  		return p
   178  	})
   179  }
   180  
   181  // ListVersionsOpts allows to filter list data of the dependencies (with version info) using given parameters.
   182  type ListVersionsOpts struct {
   183  	DependId string `json:"-" required:"true"`
   184  	// Final record queried last time. Default value: 0.
   185  	Marker string `q:"marker"`
   186  	// Maximum number of dependencies that can be obtained in a query.
   187  	MaxItems string `q:"maxitems"`
   188  }
   189  
   190  // ListVersions is a method that used to query list of dependencies (with version info) using given parameters.
   191  func ListVersions(c *golangsdk.ServiceClient, opts ListVersionsOpts) ([]DependencyVersion, error) {
   192  	url := resourceVersionsURL(c, opts.DependId)
   193  	query, err := golangsdk.BuildQueryString(opts)
   194  	if err != nil {
   195  		return nil, err
   196  	}
   197  	url += query.String()
   198  
   199  	pager := pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
   200  		return DependencyVersionPage{pagination.SinglePageBase(r)}
   201  	})
   202  	pager.Headers = requestOpts.MoreHeaders
   203  	pages, err := pager.AllPages()
   204  	if err != nil {
   205  		return nil, err
   206  	}
   207  	return extractDependencieVersions(pages)
   208  }
   209  
   210  // Update is a method to update the current dependency configuration.
   211  func Update(c *golangsdk.ServiceClient, dependId string, opts DependOpts) (*Dependency, error) {
   212  	b, err := golangsdk.BuildRequestBody(opts, "")
   213  	if err != nil {
   214  		return nil, err
   215  	}
   216  
   217  	var r Dependency
   218  	_, err = c.Put(resourceURL(c, dependId), b, &r, &golangsdk.RequestOpts{
   219  		MoreHeaders: requestOpts.MoreHeaders,
   220  	})
   221  	return &r, err
   222  }
   223  
   224  // Delete is a method to remove the current dependency configuration using its ID.
   225  func Delete(c *golangsdk.ServiceClient, dependId string) *golangsdk.ErrResult {
   226  	var r golangsdk.ErrResult
   227  	_, r.Err = c.Delete(resourceURL(c, dependId), &golangsdk.RequestOpts{
   228  		MoreHeaders: requestOpts.MoreHeaders,
   229  	})
   230  	return &r
   231  }
   232  
   233  // DeleteVersion is a method to remove the specified dependency (with version info) using its depend ID and version.
   234  func DeleteVersion(c *golangsdk.ServiceClient, dependId, version string) error {
   235  	_, err := c.Delete(resourceVersionURL(c, dependId, version), &golangsdk.RequestOpts{
   236  		MoreHeaders: requestOpts.MoreHeaders,
   237  	})
   238  	return err
   239  }