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

     1  package resources
     2  
     3  import (
     4  	"github.com/chnsz/golangsdk"
     5  	"github.com/chnsz/golangsdk/openstack/common/tags"
     6  )
     7  
     8  // CreateGroupAndUploadOpts is a structure which allows to create a new resource group and upload package file to the
     9  // group using given parameters.
    10  type CreateGroupAndUploadOpts struct {
    11  	// List of OBS object paths. The OBS object path refers to the OBS object URL.
    12  	Paths []string `json:"paths" required:"true"`
    13  	// File type of a package group.
    14  	//   jar: JAR file
    15  	//   pyFile: User Python file
    16  	//   file: User file
    17  	//   modelFile: User AI model file
    18  	// NOTE: If the same group of packages to be uploaded contains different file types, select file as the type of the
    19  	// file to be uploaded.
    20  	Kind string `json:"kind" required:"true"`
    21  	// Name of the group to be created.
    22  	Group string `json:"group,omitempty"`
    23  	// Whether to upload resource packages in asynchronous mode.
    24  	// The default value is false, indicating that the asynchronous mode is not used.
    25  	// You are advised to upload resource packages in asynchronous mode.
    26  	IsAsync bool `json:"is_async,omitempty"`
    27  	// Resource tag.
    28  	Tags []tags.ResourceTag `json:"tags,omitempty"`
    29  }
    30  
    31  // CreateGroupAndUpload is a method to create a new resource group and upload package from the specified OBS bucket.
    32  func CreateGroupAndUpload(c *golangsdk.ServiceClient, opts CreateGroupAndUploadOpts) (*Group, error) {
    33  	b, err := golangsdk.BuildRequestBody(opts, "")
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  
    38  	var rst golangsdk.Result
    39  	_, err = c.Post(rootURL(c), b, &rst.Body, nil)
    40  	if err == nil {
    41  		var r Group
    42  		rst.ExtractInto(&r)
    43  		return &r, nil
    44  	}
    45  	return nil, err
    46  }
    47  
    48  // UploadOpts is a stucture which allows to upload resource package to the specified group using given parameters.
    49  type UploadOpts struct {
    50  	// List of OBS object paths. The OBS object path refers to the OBS object URL.
    51  	Paths []string `json:"paths" required:"true"`
    52  	// Name of a package group.
    53  	Group string `json:"group" required:"true"`
    54  }
    55  
    56  // Upload is a method to upload resource package to the specified group.
    57  func Upload(c *golangsdk.ServiceClient, typePath string, opts UploadOpts) (*Group, error) {
    58  	b, err := golangsdk.BuildRequestBody(opts, "")
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  
    63  	var rst golangsdk.Result
    64  	_, err = c.Post(resourceURL(c, typePath), b, &rst.Body, nil)
    65  	if err == nil {
    66  		var r Group
    67  		rst.ExtractInto(&r)
    68  		return &r, nil
    69  	}
    70  	return nil, err
    71  }
    72  
    73  // ResourceLocatedOpts is a structure which specify the resource package located.
    74  type ResourceLocatedOpts struct {
    75  	// Name of the package group returned when the resource package is uploaded.
    76  	Group string `q:"group,omitempty"`
    77  }
    78  
    79  // Get is a method to obtain the resource (packages) from the specified group.
    80  func Get(c *golangsdk.ServiceClient, resourceName string, opts ResourceLocatedOpts) (*Resource, error) {
    81  	url := resourceURL(c, resourceName)
    82  	query, err := golangsdk.BuildQueryString(opts)
    83  	if err != nil {
    84  		return nil, err
    85  	}
    86  	url += query.String()
    87  
    88  	var rst golangsdk.Result
    89  	_, err = c.Get(url, &rst.Body, nil)
    90  	if err == nil {
    91  		var r Resource
    92  		rst.ExtractInto(&r)
    93  		return &r, nil
    94  	}
    95  	return nil, err
    96  }
    97  
    98  // ListOpts is a structure which allows to obtain resources groups using given parameters.
    99  type ListOpts struct {
   100  	// Specifies the file type.
   101  	Kind string `q:"kind"`
   102  	// Specifies a label for filtering.
   103  	Tags string `q:"tags"`
   104  }
   105  
   106  // List is a method to obtain a list of the groups and resources.
   107  func List(c *golangsdk.ServiceClient, opts ListOpts) (*ListResp, error) {
   108  	url := rootURL(c)
   109  	query, err := golangsdk.BuildQueryString(opts)
   110  	if err != nil {
   111  		return nil, err
   112  	}
   113  	url += query.String()
   114  
   115  	var rst golangsdk.Result
   116  	_, err = c.Get(url, &rst.Body, nil)
   117  	if err == nil {
   118  		var r ListResp
   119  		rst.ExtractInto(&r)
   120  		return &r, nil
   121  	}
   122  	return nil, err
   123  }
   124  
   125  // UpdateOpts is a structure which allows to update package owner or group owner using given parameters.
   126  type UpdateOpts struct {
   127  	// New username. The name contains 5 to 32 characters, including only digits, letters, underscores (_), and
   128  	// hyphens (-). It cannot start with a digit.
   129  	NewOwner string `json:"new_owner,omitempty"`
   130  	// Group name. The name contains a maximum of 64 characters. Only digits, letters, periods (.), underscores (_),
   131  	// and hyphens (-) are allowed.
   132  	GroupName string `json:"group_name,omitempty"`
   133  	// Package name. The name can contain only digits, letters, underscores (_), exclamation marks (!), hyphens (-),
   134  	// and periods (.), but cannot start with a period.
   135  	// The length (including the file name extension) cannot exceed 128 characters.
   136  	ResourceName string `json:"resource_name,omitempty"`
   137  }
   138  
   139  // UpdateOwner is a method to update package owner or group owner.
   140  func UpdateOwner(c *golangsdk.ServiceClient, opts UpdateOpts) (*UpdateResp, error) {
   141  	b, err := golangsdk.BuildRequestBody(opts, "")
   142  	if err != nil {
   143  		return nil, err
   144  	}
   145  
   146  	var rst golangsdk.Result
   147  	_, err = c.Put(resourceURL(c, "owner"), b, &rst.Body, nil)
   148  	if err == nil {
   149  		var r UpdateResp
   150  		rst.ExtractInto(&r)
   151  		return &r, nil
   152  	}
   153  	return nil, err
   154  }
   155  
   156  // Delete is a method to remove resource package from the specified group.
   157  func Delete(c *golangsdk.ServiceClient, resourceName string, opts ResourceLocatedOpts) *golangsdk.ErrResult {
   158  	var rst golangsdk.ErrResult
   159  	url := resourceURL(c, resourceName)
   160  	query, err := golangsdk.BuildQueryString(opts)
   161  	if err != nil {
   162  		rst.Err = err
   163  		return &rst
   164  	}
   165  	url += query.String()
   166  
   167  	_, rst.Err = c.Delete(url, nil)
   168  	if err == nil {
   169  		return &rst
   170  	}
   171  	return nil
   172  }