Files
decort-golang-sdk/pkg/cloudapi/vins/nat_rule_add.go

62 lines
1.5 KiB
Go
Raw Normal View History

2022-10-03 16:56:47 +03:00
package vins
import (
"context"
"net/http"
"strconv"
2023-03-24 17:09:30 +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
// NATRuleAddRequest struct to create NAT rules
2022-12-22 17:56:47 +03:00
type NATRuleAddRequest struct {
// VINS ID
// Required: true
2023-03-24 17:09:30 +03:00
VINSID uint64 `url:"vinsId" json:"vinsId" validate:"required"`
2022-12-22 17:56:47 +03:00
// Internal IP address to apply this rule to
// Required: true
2023-03-24 17:09:30 +03:00
IntIP string `url:"intIp" json:"intIp" validate:"required"`
2022-12-22 17:56:47 +03:00
// External IP start port to use for this rule
// Required: true
2023-03-24 17:09:30 +03:00
ExtPortStart uint `url:"extPortStart" json:"extPortStart" validate:"required"`
2022-12-22 17:56:47 +03:00
2024-04-16 14:26:06 +03:00
// Internal IP port number to use for this rule
// Required: false
IntPort uint `url:"intPort,omitempty" json:"intPort,omitempty"`
2022-12-22 17:56:47 +03:00
// External IP end port to use for this rule
// Required: false
2023-03-01 19:05:53 +03:00
ExtPortEnd uint `url:"extPortEnd,omitempty" json:"extPortEnd,omitempty"`
2022-12-22 17:56:47 +03:00
// IP protocol type
// Should be one of:
// - "tcp"
// - "udp"
// Required: false
2023-03-24 17:09:30 +03:00
Proto string `url:"proto,omitempty" json:"proto,omitempty" validate:"omitempty,proto"`
2022-10-03 16:56:47 +03:00
}
2022-12-22 17:56:47 +03:00
// NATRuleAdd create NAT (port forwarding) rule on VINS
2023-06-23 15:13:22 +03:00
func (v VINS) NATRuleAdd(ctx context.Context, req NATRuleAddRequest) (uint64, error) {
2023-03-24 17:09:30 +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 0, validators.ValidationErrors(validators.GetErrors(err))
2022-10-03 16:56:47 +03:00
}
url := "/cloudapi/vins/natRuleAdd"
res, err := v.client.DecortApiCall(ctx, http.MethodPost, url, req)
if err != nil {
2023-06-23 15:13:22 +03:00
return 0, err
2022-10-03 16:56:47 +03:00
}
2023-06-23 15:13:22 +03:00
result, err := strconv.ParseUint(string(res), 10, 64)
2022-10-03 16:56:47 +03:00
if err != nil {
2023-06-23 15:13:22 +03:00
return 0, err
2022-10-03 16:56:47 +03:00
}
return result, nil
}