Files
decort-golang-sdk/pkg/cloudbroker/compute/migrate.go

82 lines
1.9 KiB
Go
Raw Normal View History

2022-12-22 17:56:47 +03:00
package compute
import (
"context"
2025-07-15 17:39:18 +03:00
"encoding/json"
2022-12-22 17:56:47 +03:00
"net/http"
"strconv"
2023-03-24 17:09:30 +03:00
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
2022-12-22 17:56:47 +03:00
)
2023-10-25 17:37:18 +03:00
// MigrateRequest struct to migrate compute
2022-12-22 17:56:47 +03:00
type MigrateRequest struct {
// ID of compute instance
// Required: true
2023-03-24 17:09:30 +03:00
ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"`
2022-12-22 17:56:47 +03:00
2025-12-08 16:16:35 +03:00
// Particular Node ID to migrate this compute to
2022-12-22 17:56:47 +03:00
// Required: false
2025-12-08 16:16:35 +03:00
TargetNodeID uint64 `url:"targetNodeId,omitempty" json:"targetNodeId,omitempty"`
2022-12-22 17:56:47 +03:00
// If live migration fails, destroy compute
// on source node and recreate on the target
// Required: false
2023-03-01 19:05:53 +03:00
Force bool `url:"force,omitempty" json:"force,omitempty"`
2022-12-22 17:56:47 +03:00
}
2025-08-29 12:51:25 +03:00
type AsyncWrapperMigrateRequest struct {
2025-07-15 17:39:18 +03:00
MigrateRequest
SyncMode bool `url:"sync"`
}
2025-12-08 16:16:35 +03:00
// Migrate migrates compute to another node
2022-12-22 17:56:47 +03:00
func (c Compute) Migrate(ctx context.Context, req MigrateRequest) (bool, error) {
2023-03-24 17:09:30 +03:00
err := validators.ValidateRequest(req)
2022-12-22 17:56:47 +03:00
if err != nil {
2023-10-25 17:37:18 +03:00
return false, validators.ValidationErrors(validators.GetErrors(err))
2022-12-22 17:56:47 +03:00
}
url := "/cloudbroker/compute/migrate"
2025-08-29 12:51:25 +03:00
syncReq := AsyncWrapperMigrateRequest{MigrateRequest: req, SyncMode: true}
2025-07-15 17:39:18 +03:00
res, err := c.client.DecortApiCall(ctx, http.MethodPost, url, syncReq)
2022-12-22 17:56:47 +03:00
if err != nil {
return false, err
}
result, err := strconv.ParseBool(string(res))
if err != nil {
return false, err
}
return result, nil
}
2025-07-15 17:39:18 +03:00
2025-12-08 16:16:35 +03:00
// AsyncMigrate migrates compute to another node in async mode
2025-07-15 17:39:18 +03:00
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"
2025-08-29 12:51:25 +03:00
asyncReq := AsyncWrapperMigrateRequest{MigrateRequest: req, SyncMode: false}
2025-07-15 17:39:18 +03:00
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
}