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/cloudapi/vins/ip_reserve.go

73 lines
1.8 KiB

package vins
import (
"context"
"errors"
"net/http"
3 years ago
3 years ago
"repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators"
)
3 years ago
// Request struct for IP reserve
type IPReserveRequest struct {
3 years ago
// VINS ID
// Required: true
3 years ago
VINSID uint64 `url:"vinsId" json:"vinsId"`
3 years ago
// Type of the reservation
// Should be one of:
// - DHCP
// - VIP
// - EXCLUDE
// Required: true
3 years ago
Type string `url:"type" json:"type"`
3 years ago
// IP address to use. Non-empty string is required for type "EXCLUDE".
// Ignored for types "DHCP" and "VIP".
// Required: false
3 years ago
IPAddr string `url:"ipAddr,omitempty" json:"ipAddr,omitempty"`
3 years ago
// MAC address to associate with IP reservation.
// Ignored for type "EXCLUDE",
// non-empty string is required for "DHCP" and "VIP"
// Required: false
3 years ago
MAC string `url:"mac,omitempty" json:"mac,omitempty"`
3 years ago
// ID of the compute, associated with this reservation of type "DHCP".
// Ignored for other types
// Required: false
3 years ago
ComputeID uint64 `url:"computeId,omitempty" json:"computeId,omitempty"`
}
3 years ago
func (vrq IPReserveRequest) validate() error {
if vrq.VINSID == 0 {
return errors.New("validation-error: field VINSID can not be empty or equal to 0")
}
if vrq.Type == "" {
return errors.New("validation-error: field Type can not be empty")
}
3 years ago
validate := validators.StringInSlice(vrq.Type, []string{"DHCP", "VIP", "EXCLUDED"})
if !validate {
return errors.New("'type' should be 'DHCP', 'VIP' or 'EXCLUDED'")
}
return nil
}
3 years ago
// IPReserve creates reservation on ViNS DHCP
func (v VINS) IPReserve(ctx context.Context, req IPReserveRequest) (string, error) {
3 years ago
err := req.validate()
if err != nil {
return "", err
}
url := "/cloudapi/vins/ipReserve"
res, err := v.client.DecortApiCall(ctx, http.MethodPost, url, req)
if err != nil {
return "", err
}
return string(res), nil
}