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

     1  package environments
     2  
     3  import (
     4  	"github.com/chnsz/golangsdk"
     5  	"github.com/chnsz/golangsdk/pagination"
     6  )
     7  
     8  // UpdateOpts is the structure required by the Create method to create a new environment.
     9  type CreateOpts struct {
    10  	// Specified the environment name with 2 to 64 characters long.
    11  	// It consists of English letters, numbers, underscores (-), and underscores (_).
    12  	// It must start with an English letter and end with an English letter or number.
    13  	Name string `json:"name" required:"true"`
    14  	// Specifies the VPC ID.
    15  	VpcId string `json:"vpc_id" required:"true"`
    16  	// Specified the environment alias.
    17  	// The alias can contain a maximum of 96 characters.
    18  	Alias string `json:"alias,omitempty"`
    19  	// Specified the environment description.
    20  	// The description can contain a maximum of 96 characters.
    21  	Description *string `json:"description,omitempty"`
    22  	// Specified the enterprise project ID.
    23  	EnterpriseProjectId string `json:"enterprise_project_id,omitempty"`
    24  	// Specified the billing mode. The valid values are:
    25  	//   provided: provided resources are used and no fees are charged.
    26  	//   on_demanded: on-demand charging.
    27  	//   monthly: monthly subscription.
    28  	ChargeMode string `json:"charge_mode,omitempty"`
    29  	// Specifies the basic resources.
    30  	BaseResources []Resource `json:"base_resources,omitempty"`
    31  	// Specifies the optional resources.
    32  	OptionalResources []Resource `json:"optional_resources,omitempty"`
    33  }
    34  
    35  // Resource is an object specifying the basic or optional resource.
    36  type Resource struct {
    37  	// Specified the resource ID.
    38  	ID string `json:"id" required:"true"`
    39  	// Specified the resource type. the valid values are: CCE, CCI, ECS and AS.
    40  	Type string `json:"type" required:"true"`
    41  	// Specified the resource name.
    42  	Name string `json:"name,omitempty"`
    43  }
    44  
    45  var requestOpts = golangsdk.RequestOpts{
    46  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
    47  }
    48  
    49  // Create is a method to create a new ServiceStage environment using create option.
    50  // Environment is a collection of infrestructures, covering computing, storage and networks, used for application
    51  // deployment and running.
    52  func Create(c *golangsdk.ServiceClient, opts CreateOpts) (*Environment, error) {
    53  	b, err := golangsdk.BuildRequestBody(opts, "")
    54  	if err != nil {
    55  		return nil, err
    56  	}
    57  
    58  	var rst Environment
    59  	_, err = c.Post(rootURL(c), b, &rst, &golangsdk.RequestOpts{
    60  		MoreHeaders: requestOpts.MoreHeaders,
    61  	})
    62  	return &rst, err
    63  }
    64  
    65  // Get is a method to obtain the details of a specified ServiceStage environment using its ID.
    66  func Get(c *golangsdk.ServiceClient, envId string) (*Environment, error) {
    67  	var r Environment
    68  	_, err := c.Get(detailURL(c, envId), &r, &golangsdk.RequestOpts{
    69  		MoreHeaders: requestOpts.MoreHeaders,
    70  	})
    71  	return &r, err
    72  }
    73  
    74  // ListOpts allows to filter list data using given parameters.
    75  type ListOpts struct {
    76  	// Number of records to be queried.
    77  	// Value range: 0–100.
    78  	// Default value: 1000, indicating that a maximum of 1000 records can be queried and all records are displayed on
    79  	// the same page.
    80  	Limit int `q:"limit"`
    81  	// The offset number.
    82  	Offset int `q:"offset"`
    83  	// Sorting field. By default, query results are sorted by creation time.
    84  	// The following enumerated values are supported: create_time, name, and update_time.
    85  	OrderBy string `q:"order_by"`
    86  	// Descending or ascending order. Default value: desc.
    87  	Order string `q:"order"`
    88  }
    89  
    90  // List is a method to query the list of the environments using given opts.
    91  func List(c *golangsdk.ServiceClient, opts ListOpts) ([]Environment, error) {
    92  	url := rootURL(c)
    93  	query, err := golangsdk.BuildQueryString(opts)
    94  	if err != nil {
    95  		return nil, err
    96  	}
    97  	url += query.String()
    98  
    99  	pages, err := pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
   100  		p := EnvironmentPage{pagination.OffsetPageBase{PageResult: r}}
   101  		return p
   102  	}).AllPages()
   103  
   104  	if err != nil {
   105  		return nil, err
   106  	}
   107  	return ExtractEnvironments(pages)
   108  }
   109  
   110  // UpdateOpts is the structure required by the Update method to update the configuration of the specified environment.
   111  type UpdateOpts struct {
   112  	// Specified the environment description.
   113  	// The description can contain a maximum of 96 characters.
   114  	Description *string `json:"description,omitempty"`
   115  	// Specified the environment name with 2 to 64 characters long.
   116  	// It consists of English letters, numbers, underscores (-), and underscores (_).
   117  	// It must start with an English letter and end with an English letter or number.
   118  	Name string `json:"name,omitempty"`
   119  	// Specified the environment alias.
   120  	// The alias can contain a maximum of 96 characters.
   121  	Alias string `json:"alias,omitempty"`
   122  }
   123  
   124  // Update is a method to update the current dependency configuration.
   125  func Update(c *golangsdk.ServiceClient, envId string, opts UpdateOpts) (*Environment, error) {
   126  	b, err := golangsdk.BuildRequestBody(opts, "")
   127  	if err != nil {
   128  		return nil, err
   129  	}
   130  
   131  	var r Environment
   132  	_, err = c.Put(detailURL(c, envId), b, &r, &golangsdk.RequestOpts{
   133  		MoreHeaders: requestOpts.MoreHeaders,
   134  	})
   135  	return &r, err
   136  }
   137  
   138  // ResourceOpts is a structure required bu the UpdateResources method to bind or unbind the resources.
   139  type ResourceOpts struct {
   140  	// Basic resources to be added.
   141  	AddBaseResources []Resource `json:"add_base_resources,omitempty"`
   142  	// Other resources to be added.
   143  	AddOptionalResources []Resource `json:"add_optional_resources,omitempty"`
   144  	// Resources to be removed.
   145  	RemoveResources []Resource `json:"remove_resources,omitempty"`
   146  }
   147  
   148  // UpdateResources is a method to add or remove the basic resources and the optional resources.
   149  func UpdateResources(c *golangsdk.ServiceClient, envId string, opts ResourceOpts) (*Environment, error) {
   150  	b, err := golangsdk.BuildRequestBody(opts, "")
   151  	if err != nil {
   152  		return nil, err
   153  	}
   154  
   155  	var r Environment
   156  	_, err = c.Patch(resourceURL(c, envId), b, &r, &golangsdk.RequestOpts{
   157  		MoreHeaders: requestOpts.MoreHeaders,
   158  	})
   159  	return &r, err
   160  }
   161  
   162  // Delete is a method to remove an existing environment.
   163  func Delete(c *golangsdk.ServiceClient, envId string) *golangsdk.ErrResult {
   164  	var r golangsdk.ErrResult
   165  	_, r.Err = c.Delete(detailURL(c, envId), &golangsdk.RequestOpts{
   166  		MoreHeaders: requestOpts.MoreHeaders,
   167  	})
   168  	return &r
   169  }