You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
decort-golang-sdk/pkg/cloudbroker/compute/migrate.go

82 lines
1.9 KiB

3 years ago
package compute
import (
"context"
4 months ago
"encoding/json"
3 years ago
"net/http"
"strconv"
3 years ago
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
3 years ago
)
2 years ago
// MigrateRequest struct to migrate compute
3 years ago
type MigrateRequest struct {
// ID of compute instance
// Required: true
3 years ago
ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"`
3 years ago
// Particular Stack ID to migrate this compute to
// Required: false
3 years ago
TargetStackID uint64 `url:"targetStackId,omitempty" json:"targetStackId,omitempty"`
3 years ago
// If live migration fails, destroy compute
// on source node and recreate on the target
// Required: false
3 years ago
Force bool `url:"force,omitempty" json:"force,omitempty"`
3 years ago
}
3 months ago
type AsyncWrapperMigrateRequest struct {
4 months ago
MigrateRequest
SyncMode bool `url:"sync"`
}
3 years ago
// Migrate migrates compute to another stack
func (c Compute) Migrate(ctx context.Context, req MigrateRequest) (bool, error) {
3 years ago
err := validators.ValidateRequest(req)
3 years ago
if err != nil {
2 years ago
return false, validators.ValidationErrors(validators.GetErrors(err))
3 years ago
}
url := "/cloudbroker/compute/migrate"
3 months ago
syncReq := AsyncWrapperMigrateRequest{MigrateRequest: req, SyncMode: true}
4 months ago
res, err := c.client.DecortApiCall(ctx, http.MethodPost, url, syncReq)
3 years ago
if err != nil {
return false, err
}
result, err := strconv.ParseBool(string(res))
if err != nil {
return false, err
}
return result, nil
}
4 months ago
// AsyncMigrate migrates compute to another stack in async mode
func (c Compute) AsyncMigrate(ctx context.Context, req MigrateRequest) (string, error) {
err := validators.ValidateRequest(req)
if err != nil {
return "", validators.ValidationErrors(validators.GetErrors(err))
}
url := "/cloudbroker/compute/migrate"
3 months ago
asyncReq := AsyncWrapperMigrateRequest{MigrateRequest: req, SyncMode: false}
4 months ago
res, err := c.client.DecortApiCall(ctx, http.MethodPost, url, asyncReq)
if err != nil {
return " ", err
}
var taskID string
err = json.Unmarshal(res, &taskID)
if err != nil {
return "", err
}
return taskID, nil
}