This commit is contained in:
2023-04-20 11:17:35 +03:00
parent 84b64b7d80
commit 7d6cda7119
40 changed files with 1337 additions and 126 deletions

View File

@@ -0,0 +1,56 @@
package pcidevice
import (
"context"
"net/http"
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
"strconv"
)
// Request struct for creating PCI device
type CreateRequest struct {
// StackID
// Required: true
StackID uint64 `url:"stackId" json:"stackId" validate:"required"`
// Resource group ID
// Required: true
RGID uint64 `url:"rgId" json:"rgId" validate:"required"`
// Name of device
// Required: true
Name string `url:"name" json:"name" validate:"required"`
// PCI address of the device
// Must be in format 0000:1f:2b.0
// Required: true
HWPath string `url:"hwPath" json:"hwPath" validate:"required,hwPath"`
// Description, just for information
// Required: false
Description string `url:"description,omitempty" json:"description,omitempty"`
}
// Create creates PCI Device
func (p PCIDevice) Create(ctx context.Context, req CreateRequest) (uint64, error) {
err := validators.ValidateRequest(req)
if err != nil {
for _, validationError := range validators.GetErrors(err) {
return 0, validators.ValidationError(validationError)
}
}
url := "/cloudbroker/pcidevice/create"
res, err := p.client.DecortApiCall(ctx, http.MethodPost, url, req)
if err != nil {
return 0, err
}
result, err := strconv.ParseUint(string(res), 10, 64)
if err != nil {
return 0, err
}
return result, nil
}

View File

@@ -0,0 +1,43 @@
package pcidevice
import (
"context"
"net/http"
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
"strconv"
)
// Request struct for deleting PCI device
type DeleteRequest struct {
// PCI device ID
// Required: true
DeviceID uint64 `url:"deviceId" json:"deviceId" validate:"required"`
// Force delete
// Required: false
Force bool `url:"force,omitempty" json:"force,omitempty"`
}
// Delete PCI device
func (p PCIDevice) Delete(ctx context.Context, req DeleteRequest) (bool, error) {
err := validators.ValidateRequest(req)
if err != nil {
for _, validationError := range validators.GetErrors(err) {
return false, validators.ValidationError(validationError)
}
}
url := "/cloudbroker/pcidevice/delete"
res, err := p.client.DecortApiCall(ctx, http.MethodPost, url, req)
if err != nil {
return false, err
}
result, err := strconv.ParseBool(string(res))
if err != nil {
return false, err
}
return result, nil
}

View File

@@ -0,0 +1,43 @@
package pcidevice
import (
"context"
"net/http"
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
"strconv"
)
// Request struct for disabling PCI device
type DisableRequest struct {
// PCI device ID
// Required: true
DeviceID uint64 `url:"deviceId" json:"deviceId" validate:"required"`
// Force delete
// Required: false
Force bool `url:"force,omitempty" json:"force,omitempty"`
}
// Disable PCI device
func (p PCIDevice) Disable(ctx context.Context, req DisableRequest) (bool, error) {
err := validators.ValidateRequest(req)
if err != nil {
for _, validationError := range validators.GetErrors(err) {
return false, validators.ValidationError(validationError)
}
}
url := "/cloudbroker/pcidevice/disable"
res, err := p.client.DecortApiCall(ctx, http.MethodPost, url, req)
if err != nil {
return false, err
}
result, err := strconv.ParseBool(string(res))
if err != nil {
return false, err
}
return result, nil
}

View File

@@ -0,0 +1,39 @@
package pcidevice
import (
"context"
"net/http"
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
"strconv"
)
// Request struct for enabling PCI device
type EnableRequest struct {
// PCI device ID
// Required: true
DeviceID uint64 `url:"deviceId" json:"deviceId" validate:"required"`
}
// Enable PCI device
func (p PCIDevice) Enable(ctx context.Context, req EnableRequest) (bool, error) {
err := validators.ValidateRequest(req)
if err != nil {
for _, validationError := range validators.GetErrors(err) {
return false, validators.ValidationError(validationError)
}
}
url := "/cloudbroker/pcidevice/enable"
res, err := p.client.DecortApiCall(ctx, http.MethodPost, url, req)
if err != nil {
return false, err
}
result, err := strconv.ParseBool(string(res))
if err != nil {
return false, err
}
return result, nil
}

View File

@@ -0,0 +1,26 @@
package pcidevice
import (
"context"
"encoding/json"
"net/http"
)
// List gets list all pci devices
func (p PCIDevice) List(ctx context.Context) (ListPCIDevices, error) {
url := "/cloudbroker/pcidevice/list"
res, err := p.client.DecortApiCall(ctx, http.MethodPost, url, nil)
if err != nil {
return nil, err
}
list := ListPCIDevices{}
err = json.Unmarshal(res, &list)
if err != nil {
return nil, err
}
return list, nil
}

View File

@@ -0,0 +1,43 @@
package pcidevice
// Main information about PCI device
type ItemPCIDevice struct {
// CKey
CKey string `json:"_ckey"`
// Meta
Meta []interface{} `json:"_meta"`
// Compute ID
ComputeID uint64 `json:"computeId"`
// Description
Description string `json:"description"`
// GUID
GUID uint64 `json:"guid"`
// HwPath
HwPath string `json:"hwPath"`
// ID
ID uint64 `json:"id"`
// Name
Name string `json:"name"`
// Resource group ID
RGID uint64 `json:"rgId"`
// Stack ID
StackID uint64 `json:"stackId"`
// Status
Status string `json:"status"`
// System name
SystemName string `json:"systemName"`
}
// List PCI devices
type ListPCIDevices []ItemPCIDevice

View File

@@ -0,0 +1,15 @@
package pcidevice
import "repository.basistech.ru/BASIS/decort-golang-sdk/interfaces"
// Structure for creating request to PCI device
type PCIDevice struct {
client interfaces.Caller
}
// Builder for PCI device endpoints
func New(client interfaces.Caller) *PCIDevice {
return &PCIDevice{
client: client,
}
}

View File

@@ -0,0 +1,42 @@
package pcidevice
import (
"encoding/json"
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/serialization"
)
// Serialize returns JSON-serialized []byte. Used as a wrapper over json.Marshal and json.MarshalIndent functions.
//
// In order to serialize with indent make sure to follow these guidelines:
// - First argument -> prefix
// - Second argument -> indent
func (l ListPCIDevices) Serialize(params ...string) (serialization.Serialized, error) {
if len(l) == 0 {
return []byte{}, nil
}
if len(params) > 1 {
prefix := params[0]
indent := params[1]
return json.MarshalIndent(l, prefix, indent)
}
return json.Marshal(l)
}
// Serialize returns JSON-serialized []byte. Used as a wrapper over json.Marshal and json.MarshalIndent functions.
//
// In order to serialize with indent make sure to follow these guidelines:
// - First argument -> prefix
// - Second argument -> indent
func (i ItemPCIDevice) Serialize(params ...string) (serialization.Serialized, error) {
if len(params) > 1 {
prefix := params[0]
indent := params[1]
return json.MarshalIndent(i, prefix, indent)
}
return json.Marshal(i)
}