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

122 lines
3.1 KiB
Go
Raw Normal View History

2022-12-22 17:56:47 +03:00
package compute
import (
"context"
2026-09-18 16:49:20 +03:00
"encoding/json"
2022-12-22 17:56:47 +03:00
"net/http"
"strconv"
2026-09-25 16:32:09 +03:00
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/constants"
2023-03-17 12:54:34 +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
// AffinityRuleAddRequest struct to add affinity rule
2022-12-22 17:56:47 +03:00
type AffinityRuleAddRequest struct {
// IDs of the compute instances
// Required: true
2023-03-24 17:09:30 +03:00
ComputeIDs []uint64 `url:"computeIds" json:"computeIds" validate:"min=1"`
2022-12-22 17:56:47 +03:00
// Should be one of:
// - node
// - compute
// Required: true
2023-03-24 17:09:30 +03:00
Topology string `url:"topology" json:"topology" validate:"computeTopology"`
2022-12-22 17:56:47 +03:00
// The degree of 'strictness' of this rule
// Should be one of:
// - RECOMMENDED
// - REQUIRED
// Required: true
2023-03-24 17:09:30 +03:00
Policy string `url:"policy" json:"policy" validate:"computePolicy"`
2022-12-22 17:56:47 +03:00
// The comparison mode is 'value', recorded by the specified 'key'
// Should be one of:
// - EQ
// - EN
// - ANY
// Required: true
2023-03-24 17:09:30 +03:00
Mode string `url:"mode" json:"mode" validate:"computeMode"`
2022-12-22 17:56:47 +03:00
// Key that are taken into account when analyzing this rule will be identified
// Required: true
2023-03-24 17:09:30 +03:00
Key string `url:"key" json:"key" validate:"required"`
2022-12-22 17:56:47 +03:00
// Value that must match the key to be taken into account when analyzing this rule
2025-12-23 17:39:58 +03:00
// Required: true
Value string `url:"value" json:"value" validate:"required"`
2026-09-18 16:49:20 +03:00
// Migrate compute according to the rule
// Default: false
// Required: false
Migrate bool `url:"migrate" json:"migrate"`
}
type wrapperAffinityRuleAddRequest struct {
AffinityRuleAddRequest
2026-09-25 16:32:09 +03:00
AsyncMode bool `url:"async_mode" json:"async_mode"`
2022-12-22 17:56:47 +03:00
}
2023-10-25 17:37:18 +03:00
// AffinityRuleAdd adds affinity rule
2026-09-25 16:32:09 +03:00
func (c Compute) AffinityRuleAdd(ctx context.Context, req AffinityRuleAddRequest) (AffinityRuleAddResult, error) {
2023-03-24 17:09:30 +03:00
err := validators.ValidateRequest(req)
2022-12-22 17:56:47 +03:00
if err != nil {
2026-09-25 16:32:09 +03:00
return AffinityRuleAddResult{}, validators.ValidationErrors(validators.GetErrors(err))
2022-12-22 17:56:47 +03:00
}
url := "/cloudbroker/compute/affinityRuleAdd"
2026-09-18 16:49:20 +03:00
aReq := wrapperAffinityRuleAddRequest{
AffinityRuleAddRequest: req,
AsyncMode: false,
}
2026-09-25 16:32:09 +03:00
res, err := c.client.DecortApiCallCtype(ctx, http.MethodPost, url, constants.MIMEJSON, aReq)
2022-12-22 17:56:47 +03:00
if err != nil {
2026-09-25 16:32:09 +03:00
return AffinityRuleAddResult{}, err
2022-12-22 17:56:47 +03:00
}
2026-09-25 16:32:09 +03:00
if !req.Migrate {
result, err := strconv.ParseBool(string(res))
return AffinityRuleAddResult{Ok: result}, err
}
var report MigrationReport
err = json.Unmarshal(res, &report)
2022-12-22 17:56:47 +03:00
if err != nil {
2026-09-25 16:32:09 +03:00
return AffinityRuleAddResult{}, err
2022-12-22 17:56:47 +03:00
}
2026-09-25 16:32:09 +03:00
return AffinityRuleAddResult{Ok: report.Applied, MigrationReport: &report}, nil
2022-12-22 17:56:47 +03:00
}
2026-09-18 16:49:20 +03:00
// AsyncAffinityRuleAdd add affinity rule in async mode
func (c Compute) AsyncAffinityRuleAdd(ctx context.Context, req AffinityRuleAddRequest) (string, error) {
err := validators.ValidateRequest(req)
if err != nil {
return "", validators.ValidationErrors(validators.GetErrors(err))
}
url := "/cloudbroker/compute/affinityRuleAdd"
aReq := wrapperAffinityRuleAddRequest{
AffinityRuleAddRequest: req,
AsyncMode: true,
}
2026-09-25 16:32:09 +03:00
res, err := c.client.DecortApiCallCtype(ctx, http.MethodPost, url, constants.MIMEJSON, aReq)
2026-09-18 16:49:20 +03:00
if err != nil {
return "", err
}
var resp string
err = json.Unmarshal(res, &resp)
if err != nil {
return "", err
}
return resp, nil
}