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

112 lines
2.7 KiB
Go
Raw Normal View History

2022-10-03 16:56:47 +03:00
package compute
import (
"context"
2026-09-18 16:49:20 +03:00
"encoding/json"
2022-10-03 16:56:47 +03:00
"net/http"
"strconv"
2023-03-17 12:54:34 +03:00
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
2022-10-03 16:56:47 +03:00
)
2023-10-25 17:37:18 +03:00
// AffinityRuleAddRequest struct to add affinity rule
2022-10-03 16:56:47 +03:00
type AffinityRuleAddRequest struct {
2022-12-22 17:56:47 +03:00
// ID of the compute instance
// Required: true
2023-03-17 12:54:34 +03:00
ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"`
2022-12-22 17:56:47 +03:00
// Compute or node, for whom rule applies
// Required: true
2023-03-17 12:54:34 +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-17 12:54:34 +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-17 12:54:34 +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-17 12:54:34 +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
AsyncMode bool `url:"async_mode"`
2022-10-03 16:56:47 +03:00
}
2022-12-22 17:56:47 +03:00
// AffinityRuleAdd add affinity rule
2022-10-03 16:56:47 +03:00
func (c Compute) AffinityRuleAdd(ctx context.Context, req AffinityRuleAddRequest) (bool, error) {
2023-03-17 12:54:34 +03:00
err := validators.ValidateRequest(req)
2022-10-03 16:56:47 +03:00
if err != nil {
2023-10-25 17:37:18 +03:00
return false, validators.ValidationErrors(validators.GetErrors(err))
2022-10-03 16:56:47 +03:00
}
url := "/cloudapi/compute/affinityRuleAdd"
2026-09-18 16:49:20 +03:00
aReq := wrapperAffinityRuleAddRequest{
AffinityRuleAddRequest: req,
AsyncMode: false,
}
res, err := c.client.DecortApiCall(ctx, http.MethodPost, url, aReq)
2022-10-03 16: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
}
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 := "/cloudapi/compute/affinityRuleAdd"
aReq := wrapperAffinityRuleAddRequest{
AffinityRuleAddRequest: req,
AsyncMode: true,
}
res, err := c.client.DecortApiCall(ctx, http.MethodPost, url, aReq)
if err != nil {
return "", err
}
var resp string
err = json.Unmarshal(res, &resp)
if err != nil {
return "", err
}
return resp, nil
}