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

     1  package instances
     2  
     3  import (
     4  	"github.com/chnsz/golangsdk"
     5  	"github.com/chnsz/golangsdk/pagination"
     6  )
     7  
     8  // CreateOpts is the structure required by the Create method to deploy a specified component.
     9  type CreateOpts struct {
    10  	// Specified the component instance name.
    11  	// The value can contain 2 to 63 characters, including lowercase letters, digits, and hyphens (-).
    12  	// It must start with a lowercase letter and end with a lowercase letter or digit.
    13  	Name string `json:"name" required:"true"`
    14  	// Specified the environment ID.
    15  	EnvId string `json:"environment_id" required:"true"`
    16  	// Specified the number of instance replicas.
    17  	Replica int `json:"replica" required:"true"`
    18  	// Resource specifications, which can be obtained by using the API in Obtaining All Supported Flavors of Application
    19  	// Resources. If you need to customize resource specifications, the format is as follows:
    20  	//   CUSTOM-xxG:xxC-xxC:xxGi-xxGi
    21  	// The meaning of each part is:
    22  	//   xxG: storage capacity allocated to a component instance (reserved field). You can set it to a fixed number.
    23  	//   xxC-xxC: the maximum and minimum number of CPU cores allocated to a component instance.
    24  	//   xxGi-xxGi: the maximum and minimum memory allocated to a component instance.
    25  	// For example, CUSTOM-10G:0.5C-0.25C:1.6Gi-0.8Gi indicates that the maximum number of CPU cores allocated to a
    26  	// component instance is 0.5, the minimum number of CPU cores is 0.25, the maximum memory is 1.6 Gi, and the minimum
    27  	// memory is 0.8 Gi.
    28  	FlavorId string `json:"flavor_id" required:"true"`
    29  	// Deployed resources.
    30  	ReferResources []ReferResource `json:"refer_resources" required:"true"`
    31  	// Application component version that meets version semantics. Example: 1.0.0.
    32  	Version string `json:"version" required:"true"`
    33  	// Artifact. key indicates the component name. In the Docker container scenario, key indicates the container name.
    34  	// If the source parameters of a component specify the software package source, this parameter is optional, and the
    35  	// software package source of the component is inherited by default. Otherwise, this parameter is mandatory.
    36  	Artifacts map[string]Artifact `json:"artifacts,omitempty"`
    37  	// Configuration parameters, such as environment variables, deployment configurations, and O&M monitoring.
    38  	// By default, this parameter is left blank.
    39  	Configuration Configuration `json:"configuration,omitempty"`
    40  	// Description. The value can contain up to 128 characters.
    41  	Description string `json:"description,omitempty"`
    42  	// External network access.
    43  	ExternalAccesses []ExternalAccess `json:"external_accesses,omitempty"`
    44  }
    45  
    46  // ReferResource is an object that specifies the deployed basic and optional resources.
    47  type ReferResource struct {
    48  	// Resource ID.
    49  	// Note: If type is set to ecs, the value of this parameter must be Default.
    50  	ID string `json:"id" required:"true"`
    51  	// Resource type.
    52  	// Basic resources: Cloud Container Engine (CCE), Auto Scaling (AS), and Elastic Cloud Server (ECS).
    53  	// Optional resources: Relational Database Service (RDS), Distributed Cache Service (DCS),
    54  	// Elastic Load Balance (ELB), and other services.
    55  	Type string `json:"type" required:"true"`
    56  	// Application alias, which is provided only in DCS scenario. Value: distributed_session, distributed_cache, or
    57  	// distributed_session, distributed_cache. Default value: distributed_session, distributed_cache.
    58  	ReferAlias string `json:"refer_alias,omitempty"`
    59  	// Reference resource parameter.
    60  	// NOTICE:
    61  	// When type is set to cce, this parameter is mandatory. You need to specify the namespace of the cluster where the
    62  	// component is to be deployed. Example: {"namespace": "default"}.
    63  	// When type is set to ecs, this parameter is mandatory. You need to specify the hosts where the component is to be
    64  	// deployed. Example: {"hosts":["04d9f887-9860-4029-91d1-7d3102903a69", "04d9f887-9860-4029-91d1-7d3102903a70"]}}.
    65  	Parameters map[string]interface{} `json:"parameters,omitempty"`
    66  }
    67  
    68  // Artifact is an object that specifies the image storage of the software package.
    69  type Artifact struct {
    70  	// Storage mode. Value: swr, devcloud, or obs.
    71  	Storage string `json:"storage" required:"true"`
    72  	// Type. Value: package (VM-based deployment) or image (container-based deployment).
    73  	Type string `json:"type" required:"true"`
    74  	// Software package or image address.
    75  	// For a component deployed on a VM, this parameter is the software package address.
    76  	// For a component deployed based on a container, this parameter is the image address or component name:v${index}.
    77  	// The latter indicates that the component source code or the image automatically built using the software package
    78  	// will be used.
    79  	URL string `json:"url" required:"true"`
    80  	// Authentication mode. Value: iam or none. Default value: iam.
    81  	Auth string `json:"auth,omitempty"`
    82  	// Version number.
    83  	Version string `json:"version,omitempty"`
    84  	// Property information.
    85  	Properties map[string]interface{} `json:"properties,omitempty"`
    86  }
    87  
    88  // Configuration is an object that specifies the build configuration for a component instance.
    89  type Configuration struct {
    90  	// Environment variable.
    91  	EnvVariables []Variable `json:"env,omitempty"`
    92  	// Data storage configuration.
    93  	Storages []Storage `json:"storage,omitempty"`
    94  	// Upgrade policy.
    95  	Strategy *Strategy `json:"strategy,omitempty"`
    96  	// Lifecycle.
    97  	Lifecycle *Lifecycle `json:"lifecycle,omitempty"`
    98  	// Policy list of log collection.
    99  	LogCollectionPolicies []LogCollectionPolicy `json:"logs,omitempty"`
   100  	// Scheduling policy.
   101  	Scheduler *Scheduler `json:"scheduler,omitempty"`
   102  	// Health check.
   103  	Probe *Probe `json:"probes,omitempty"`
   104  }
   105  
   106  // Configuration is an object that specifies the environment variable for the component instance.
   107  type Variable struct {
   108  	// Environment variable name.
   109  	// The value contains 1 to 64 characters, including letters, digits, underscores (_), hyphens (-), and dots (.),
   110  	// and cannot start with a digit.
   111  	Name string `json:"name" required:"true"`
   112  	// Environment variable value.
   113  	Value string `json:"value" required:"true"`
   114  }
   115  
   116  // Storage is an object that specifies the data storage.
   117  type Storage struct {
   118  	// Storage type. Value:
   119  	// HostPath: host path mounting.
   120  	// EmptyDir: temporary directory mounting.
   121  	// ConfigMap: configuration item mounting.
   122  	// Secret: secret volume mounting.
   123  	// PersistentVolumeClaim: cloud storage mounting.
   124  	Type string `json:"type" required:"true"`
   125  	// Storage parameter.
   126  	Parameters *StorageParams `json:"parameters" required:"true"`
   127  	// Directory mounted to the container.
   128  	Mounts []Mount `json:"mounts" required:"true"`
   129  }
   130  
   131  // StorageParams is an extend object that specifies the storage path and name.
   132  type StorageParams struct {
   133  	// Host path. This parameter is applicable to the HostPath storage type.
   134  	Path string `json:"path,omitempty"`
   135  	// Name of a configuration item. This parameter is applicable to the ConfigMap storage type.
   136  	Name string `json:"name,omitempty"`
   137  	// PVC name. This parameter is applicable to the PersistentVolumeClaim storage type.
   138  	ClaimName string `json:"claimName,omitempty"`
   139  	// Secret name. This parameter is applicable to the Secret storage type.
   140  	SecretName string `json:"secretName,omitempty"`
   141  }
   142  
   143  // Mount is an object that specifies the directory mounted to the container.
   144  type Mount struct {
   145  	// Specifies the mounted disk path.
   146  	Path string `json:"path" required:"true"`
   147  	// Specifies the mounted disk permission is read-only or read-write.
   148  	ReadOnly *bool `json:"readOnly" required:"true"`
   149  	// Specifies the subpath of the mounted disk.
   150  	SubPath string `json:"subPath,omitempty"`
   151  }
   152  
   153  // Strategy is an object that specifies the upgrade type, including in-place upgrade and rolling upgrade.
   154  type Strategy struct {
   155  	// Upgrade policy. Value: Recreate or RollingUpdate (default).
   156  	// The former indicates in-place upgrade while the latter indicates rolling upgrade.
   157  	Upgrade string `json:"upgrade,omitempty"`
   158  }
   159  
   160  // Lifecycle is an object that specifies the lifecycle of the component deployment.
   161  type Lifecycle struct {
   162  	// Startup command.
   163  	Entrypoint *Entrypoint `json:"entrypoint,omitempty"`
   164  	// Post-start processing.
   165  	PostStart *Process `json:"post-start,omitempty"`
   166  	// Pre-stop processing.
   167  	PreStop *Process `json:"pre-stop,omitempty"`
   168  }
   169  
   170  // Entrypoint is an object that specifies the commands when launching up the deployment.
   171  type Entrypoint struct {
   172  	// Command that can be executed.
   173  	Commands []string `json:"command" required:"true"`
   174  	// Running parameters.
   175  	Args []string `json:"args" required:"true"`
   176  }
   177  
   178  // Process is an object that specifies the post-processing or stop pre-processing.
   179  type Process struct {
   180  	// Process type. The value is command or http.
   181  	// The command is to execute the command line, and http is to send an http request.
   182  	Type string `json:"type" required:"true"`
   183  	// Start post-processing or stop pre-processing parameters.
   184  	Parameters *ProcessParams `json:"parameters" required:"true"`
   185  }
   186  
   187  // ProcessParams is an object that specifies the arguments of the post-processing or stop pre-processing.
   188  type ProcessParams struct {
   189  	// Command parameters, such as ["sleep", "1"]. Applies to command type.
   190  	Commands []string `json:"command,omitempty"`
   191  	// The port number. Applies to http type.
   192  	Port int `json:"port,omitempty"`
   193  	// Request URL. Applies to http type.
   194  	Path string `json:"path,omitempty"`
   195  	// Defaults to the IP address of the POD instance. You can also specify it yourself. Applies to http type.
   196  	Host string `json:"host,omitempty"`
   197  }
   198  
   199  // LogCollectionPolicy is an object that specifies the policy of the log collection.
   200  type LogCollectionPolicy struct {
   201  	// Container mounting path.
   202  	LogPath string `json:"logPath" required:"ture"`
   203  	// Aging period.
   204  	AgingPeriod string `json:"rotate" required:"ture"`
   205  	// The extended host path, the valid values are as follows:
   206  	//	None
   207  	//	PodUID
   208  	//	PodName
   209  	//	PodUID/ContainerName
   210  	//	PodName/ContainerName
   211  	// If omited, means container mounting.
   212  	HostExtendPath string `json:"hostExtendPath,omitempty"`
   213  	// Host mounting path.
   214  	HostPath string `json:"hostPath,omitempty"`
   215  }
   216  
   217  // Scheduler is an object that specifies the scheduling policy.
   218  type Scheduler struct {
   219  	// Affinity.
   220  	Affinity *Affinity `json:"affinity,omitempty"`
   221  	// Anti-affinity.
   222  	AntiAffinity *Affinity `json:"anti-affinity,omitempty"`
   223  }
   224  
   225  // Affinity is an object that specifies the configuration details of the affinity or anti-affinity.
   226  type Affinity struct {
   227  	// AZ list.
   228  	AvailabilityZones []string `json:"az,omitempty"`
   229  	// Node private IP address list.
   230  	Nodes []string `json:"node,omitempty"`
   231  	// List of component instance names.
   232  	Applications []string `json:"application,omitempty"`
   233  }
   234  
   235  // Probe is an object that specifies which probe members we have.
   236  type Probe struct {
   237  	// Component liveness probe.
   238  	LivenessProbe *ProbeDetail `json:"livenessProbe,omitempty"`
   239  	// Component service probe.
   240  	ReadinessProbe *ProbeDetail `json:"readinessProbe,omitempty"`
   241  }
   242  
   243  // ProbeDetail is an object that specifies the configuration details of the liveness probe and service probe.
   244  type ProbeDetail struct {
   245  	// Value: http, tcp, or command.
   246  	// The check methods are HTTP request check, TCP port check, and command execution check, respectively.
   247  	Type string `json:"type" required:"true"`
   248  	// Parameters.
   249  	// If type is set to http, the object is HttpObject.
   250  	// If type is set to tcp, the object is CommandObject.
   251  	// If type is set to command, the object is TcpObject.
   252  	Parameters map[string]interface{} `json:"parameters" required:"true"`
   253  	// Interval between the startup and detection.
   254  	Delay int `json:"delay,omitempty"`
   255  	// Detection timeout interval.
   256  	Timeout int `json:"timeout,omitempty"`
   257  }
   258  
   259  // HttpObject is an object that specifies the check parameters of the HTTP probe.
   260  type HttpObject struct {
   261  	// Value: HTTP or HTTPS.
   262  	Scheme string `json:"scheme" required:"true"`
   263  	// Port number.
   264  	Port int `json:"port" required:"true"`
   265  	// Request path.
   266  	Path string `json:"path" required:"true"`
   267  	// Pod IP address (default). You can specify an IP address.
   268  	Host string `json:"host,omitempty"`
   269  }
   270  
   271  // CommandObject is an object that specifies the check parameters of the command probe.
   272  type CommandObject struct {
   273  	// Command list.
   274  	Command []string `json:"command" required:"true"`
   275  }
   276  
   277  // TcpObject is an object that specifies the check parameters of the TCP probe.
   278  type TcpObject struct {
   279  	// Port number.
   280  	Port int `json:"port" required:"true"`
   281  }
   282  
   283  // ExternalAccess is an object that specifies the configuration of the external IP access.
   284  type ExternalAccess struct {
   285  	// Protocol. Value: http or https.
   286  	Protocol string `json:"protocol,omitempty"`
   287  	// Access address.
   288  	Address string `json:"address,omitempty"`
   289  	// Port number.
   290  	ForwardPort int `json:"forward_port,omitempty"`
   291  }
   292  
   293  var requestOpts = golangsdk.RequestOpts{
   294  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
   295  }
   296  
   297  // Create is a method to create a new instance under ServiceStage application using create option.
   298  // Environment is a collection of infrestructures, covering computing, storage and networks, used for application
   299  // deployment and running.
   300  func Create(c *golangsdk.ServiceClient, appId string, componentId string, opts CreateOpts) (*JobResp, error) {
   301  	b, err := golangsdk.BuildRequestBody(opts, "")
   302  	if err != nil {
   303  		return nil, err
   304  	}
   305  
   306  	var r JobResp
   307  	_, err = c.Post(rootURL(c, appId, componentId), b, &r, &golangsdk.RequestOpts{
   308  		MoreHeaders: requestOpts.MoreHeaders,
   309  	})
   310  	return &r, err
   311  }
   312  
   313  // Get is a method to obtain the details of a specified component instance (deployment) using its ID.
   314  func Get(c *golangsdk.ServiceClient, appId, componentId, instanceId string) (*Instance, error) {
   315  	var r Instance
   316  	_, err := c.Get(resourceURL(c, appId, componentId, instanceId), &r, &golangsdk.RequestOpts{
   317  		MoreHeaders: requestOpts.MoreHeaders,
   318  	})
   319  	return &r, err
   320  }
   321  
   322  // ListOpts allows to filter list data using given parameters.
   323  type ListOpts struct {
   324  	// Number of records to be queried.
   325  	// Value range: 0–100.
   326  	// Default value: 1000, indicating that a maximum of 1000 records can be queried and all records are displayed on
   327  	// the same page.
   328  	Limit int `q:"limit"`
   329  	// The offset number.
   330  	Offset int `q:"offset"`
   331  	// Sorting field. By default, query results are sorted by creation time.
   332  	// The following enumerated values are supported: create_time, name, and update_time.
   333  	OrderBy string `q:"order_by"`
   334  	// Descending or ascending order. Default value: desc.
   335  	Order string `q:"order"`
   336  }
   337  
   338  // List is a method to query the list of the component instances (deployment) using given opts.
   339  func List(c *golangsdk.ServiceClient, appId, componentId string, opts ListOpts) ([]Instance, error) {
   340  	url := rootURL(c, appId, componentId)
   341  	query, err := golangsdk.BuildQueryString(opts)
   342  	if err != nil {
   343  		return nil, err
   344  	}
   345  	url += query.String()
   346  
   347  	pages, err := pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
   348  		p := InstancePage{pagination.OffsetPageBase{PageResult: r}}
   349  		return p
   350  	}).AllPages()
   351  
   352  	if err != nil {
   353  		return nil, err
   354  	}
   355  	return ExtractInstances(pages)
   356  }
   357  
   358  // UpdateOpts is the structure required by the Update method to update the configuration of the component instance.
   359  type UpdateOpts struct {
   360  	// Application component version that meets version semantics. Example: 1.0.0.
   361  	Version string `json:"version" required:"true"`
   362  	// Resource specifications, which can be obtained by using the API in Obtaining All Supported Flavors of Application
   363  	// Resources. If you need to customize resource specifications, the format is as follows:
   364  	//   CUSTOM-xxG:xxC-xxC:xxGi-xxGi
   365  	// The meaning of each part is:
   366  	//   xxG: storage capacity allocated to a component instance (reserved field). You can set it to a fixed number.
   367  	//   xxC-xxC: the maximum and minimum number of CPU cores allocated to a component instance.
   368  	//   xxGi-xxGi: the maximum and minimum memory allocated to a component instance.
   369  	// For example, CUSTOM-10G:0.5C-0.25C:1.6Gi-0.8Gi indicates that the maximum number of CPU cores allocated to a
   370  	// component instance is 0.5, the minimum number of CPU cores is 0.25, the maximum memory is 1.6 Gi, and the minimum
   371  	// memory is 0.8 Gi.
   372  	FlavorId string `json:"flavor_id,omitempty"`
   373  	// Artifact. key indicates the component name. In the Docker container scenario, key indicates the container name.
   374  	// If the source parameters of a component specify the software package source, this parameter is optional, and the
   375  	// software package source of the component is inherited by default. Otherwise, this parameter is mandatory.
   376  	Artifacts map[string]Artifact `json:"artifacts,omitempty"`
   377  	// Configuration parameters, such as environment variables, deployment configurations, and O&M monitoring.
   378  	// By default, this parameter is left blank.
   379  	Configuration Configuration `json:"configuration,omitempty"`
   380  	// Description. The value can contain up to 128 characters.
   381  	Description *string `json:"description,omitempty"`
   382  	// External network access.
   383  	ExternalAccesses []ExternalAccess `json:"external_accesses,omitempty"`
   384  	// Deployed resources.
   385  	ReferResources []ReferResource `json:"refer_resources" required:"true"`
   386  }
   387  
   388  // Update is a method to update the current dependency configuration.
   389  func Update(c *golangsdk.ServiceClient, appId, componentId, instanceId string, opts UpdateOpts) (*JobResp, error) {
   390  	b, err := golangsdk.BuildRequestBody(opts, "")
   391  	if err != nil {
   392  		return nil, err
   393  	}
   394  
   395  	var r JobResp
   396  	_, err = c.Put(resourceURL(c, appId, componentId, instanceId), b, &r, &golangsdk.RequestOpts{
   397  		MoreHeaders: requestOpts.MoreHeaders,
   398  	})
   399  	return &r, err
   400  }
   401  
   402  // Delete is a method to remove an existing instance.
   403  func Delete(c *golangsdk.ServiceClient, appId, componentId, instanceId string) (*JobResp, error) {
   404  	var r JobResp
   405  	_, err := c.Delete(resourceURL(c, appId, componentId, instanceId), &golangsdk.RequestOpts{
   406  		JSONResponse: &r,
   407  		MoreHeaders:  requestOpts.MoreHeaders,
   408  	})
   409  	return &r, err
   410  }
   411  
   412  // UpdateOpts is the structure required by the DoAction method to change component status or upgrade it.
   413  type ActionOpts struct {
   414  	// Specified the actions, the valid actions are: start, stop, restart, scale and rollback.
   415  	Action string `json:"action" required:"true"`
   416  	// Specified the action parameters, required if action is scale or rollback.
   417  	Parameters ActionParams `json:"parameters,omitempty"`
   418  }
   419  
   420  // ActionParams is an object that specifies the operate parameters.
   421  type ActionParams struct {
   422  	// Specified the number of replica, required if action is scale.
   423  	Replica string `json:"replica,omitempty"`
   424  	// Specified the list of ECS instance to be deployed during VM scaling, required if action is scale.
   425  	Host []string `json:"hosts,omitempty"`
   426  	// Specified the verison number, required if action is rollback.
   427  	Version string `json:"version,omitempty"`
   428  }
   429  
   430  // DoAction is a method to change component status or upgrade the instance.
   431  func DoAction(c *golangsdk.ServiceClient, appId string, componentId, instanceId string, opts CreateOpts) (*Job, error) {
   432  	b, err := golangsdk.BuildRequestBody(opts, "")
   433  	if err != nil {
   434  		return nil, err
   435  	}
   436  
   437  	var rst Job
   438  	_, err = c.Post(rootURL(c, appId, componentId), b, &rst, &golangsdk.RequestOpts{
   439  		MoreHeaders: requestOpts.MoreHeaders,
   440  	})
   441  	return &rst, err
   442  }