Refactoring
This commit is contained in:
45
pkg/cloudapi/flipgroup/compute_add.go
Normal file
45
pkg/cloudapi/flipgroup/compute_add.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package flipgroup
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"github.com/rudecs/decort-sdk/opts"
|
||||
"github.com/rudecs/decort-sdk/typed"
|
||||
)
|
||||
|
||||
type ComputeAddRequest struct {
|
||||
FlipGroupID uint64 `url:"flipgroupId"`
|
||||
ComputeID uint64 `url:"computeId"`
|
||||
}
|
||||
|
||||
func (frq ComputeAddRequest) Validate() error {
|
||||
if frq.FlipGroupID == 0 {
|
||||
return errors.New("field FlipGroupID can not be empty or equal to 0")
|
||||
}
|
||||
if frq.ComputeID == 0 {
|
||||
return errors.New("field ComputeID can not be empty or equal to 0")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f FlipGroup) ComputeAdd(ctx context.Context, req ComputeAddRequest, options ...opts.DecortOpts) (bool, error) {
|
||||
if err := req.Validate(); err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
url := "/cloudapi/flipgroup/computeAdd"
|
||||
res, err := f.client.DecortApiCall(ctx, typed.POST, url, req)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
result, err := strconv.ParseBool(string(res))
|
||||
if err != nil {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
45
pkg/cloudapi/flipgroup/compute_remove.go
Normal file
45
pkg/cloudapi/flipgroup/compute_remove.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package flipgroup
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"github.com/rudecs/decort-sdk/opts"
|
||||
"github.com/rudecs/decort-sdk/typed"
|
||||
)
|
||||
|
||||
type ComputeRemoveRequest struct {
|
||||
FlipGroupID uint64 `url:"flipgroupId"`
|
||||
ComputeID uint64 `url:"computeId"`
|
||||
}
|
||||
|
||||
func (frq ComputeRemoveRequest) Validate() error {
|
||||
if frq.FlipGroupID == 0 {
|
||||
return errors.New("field FlipGroupID can not be empty or equal to 0")
|
||||
}
|
||||
if frq.ComputeID == 0 {
|
||||
return errors.New("field ComputeID can not be empty or equal to 0")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f FlipGroup) ComputeRemove(ctx context.Context, req ComputeRemoveRequest, options ...opts.DecortOpts) (bool, error) {
|
||||
if err := req.Validate(); err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
url := "/cloudapi/flipgroup/computeRemove"
|
||||
res, err := f.client.DecortApiCall(ctx, typed.POST, url, req)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
result, err := strconv.ParseBool(string(res))
|
||||
if err != nil {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
63
pkg/cloudapi/flipgroup/create.go
Normal file
63
pkg/cloudapi/flipgroup/create.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package flipgroup
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"github.com/rudecs/decort-sdk/internal/validators"
|
||||
"github.com/rudecs/decort-sdk/opts"
|
||||
"github.com/rudecs/decort-sdk/typed"
|
||||
)
|
||||
|
||||
type CreateRequest struct {
|
||||
AccountID uint64 `url:"accountId"`
|
||||
Name string `url:"name"`
|
||||
NetType string `url:"netType"`
|
||||
NetID uint64 `url:"netId"`
|
||||
ClientType string `url:"clientType"`
|
||||
IP string `url:"ip,omitempty"`
|
||||
Description string `url:"desc,omitempty"`
|
||||
}
|
||||
|
||||
func (frq CreateRequest) Validate() error {
|
||||
if frq.AccountID == 0 {
|
||||
return errors.New("field AccountID can not be empty or equal to 0")
|
||||
}
|
||||
if frq.NetID == 0 {
|
||||
return errors.New("field NetID can not be empty or equal to 0")
|
||||
}
|
||||
if frq.Name == "" {
|
||||
return errors.New("field Name can not be empty")
|
||||
}
|
||||
|
||||
validator := validators.StringInSlice(frq.NetType, []string{"EXTNET", "VINS"})
|
||||
if !validator {
|
||||
return errors.New("field Name can be only EXTNET or VINS")
|
||||
}
|
||||
validator = validators.StringInSlice(frq.ClientType, []string{"compute", "node"})
|
||||
if !validator {
|
||||
return errors.New("field Name can be only compute or node")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f FlipGroup) Create(ctx context.Context, req CreateRequest, options ...opts.DecortOpts) (*FlipGroupRecord, error) {
|
||||
if err := req.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
url := "/cloudapi/flipgroup/create"
|
||||
fgRaw, err := f.client.DecortApiCall(ctx, typed.POST, url, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fg := &FlipGroupRecord{}
|
||||
if err := json.Unmarshal(fgRaw, fg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return fg, nil
|
||||
}
|
||||
41
pkg/cloudapi/flipgroup/delete.go
Normal file
41
pkg/cloudapi/flipgroup/delete.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package flipgroup
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"github.com/rudecs/decort-sdk/opts"
|
||||
"github.com/rudecs/decort-sdk/typed"
|
||||
)
|
||||
|
||||
type DeleteRequest struct {
|
||||
FlipGroupID uint64 `url:"flipgroupId"`
|
||||
}
|
||||
|
||||
func (frq DeleteRequest) Validate() error {
|
||||
if frq.FlipGroupID == 0 {
|
||||
return errors.New("field FlipGroupID can not be empty or equal to 0")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f FlipGroup) Delete(ctx context.Context, req DeleteRequest, options ...opts.DecortOpts) (bool, error) {
|
||||
if err := req.Validate(); err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
url := "/cloudapi/flipgroup/delete"
|
||||
res, err := f.client.DecortApiCall(ctx, typed.POST, url, req)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
result, err := strconv.ParseBool(string(res))
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
43
pkg/cloudapi/flipgroup/edit.go
Normal file
43
pkg/cloudapi/flipgroup/edit.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package flipgroup
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"github.com/rudecs/decort-sdk/opts"
|
||||
"github.com/rudecs/decort-sdk/typed"
|
||||
)
|
||||
|
||||
type EditRequest struct {
|
||||
FlipGroupID uint64 `url:"flipgroupId"`
|
||||
Name string `url:"name,omitempty"`
|
||||
Description string `url:"desc,omitempty"`
|
||||
}
|
||||
|
||||
func (frq EditRequest) Validate() error {
|
||||
if frq.FlipGroupID == 0 {
|
||||
return errors.New("field FlipGroupID can not be empty or equal to 0")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f FlipGroup) Edit(ctx context.Context, req EditRequest, options ...opts.DecortOpts) (bool, error) {
|
||||
if err := req.Validate(); err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
url := "/cloudapi/flipgroup/edit"
|
||||
res, err := f.client.DecortApiCall(ctx, typed.POST, url, req)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
result, err := strconv.ParseBool(string(res))
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
15
pkg/cloudapi/flipgroup/flipgroup.go
Normal file
15
pkg/cloudapi/flipgroup/flipgroup.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package flipgroup
|
||||
|
||||
import (
|
||||
"github.com/rudecs/decort-sdk/interfaces"
|
||||
)
|
||||
|
||||
type FlipGroup struct {
|
||||
client interfaces.Caller
|
||||
}
|
||||
|
||||
func New(client interfaces.Caller) *FlipGroup {
|
||||
return &FlipGroup{
|
||||
client,
|
||||
}
|
||||
}
|
||||
42
pkg/cloudapi/flipgroup/get.go
Normal file
42
pkg/cloudapi/flipgroup/get.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package flipgroup
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"github.com/rudecs/decort-sdk/opts"
|
||||
"github.com/rudecs/decort-sdk/typed"
|
||||
)
|
||||
|
||||
type GetRequest struct {
|
||||
FlipGroupID uint64 `url:"flipgroupId"`
|
||||
}
|
||||
|
||||
func (frq GetRequest) Validate() error {
|
||||
if frq.FlipGroupID == 0 {
|
||||
return errors.New("field FlipGroupID can not be empty or equal to 0")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f FlipGroup) Get(ctx context.Context, req GetRequest, options ...opts.DecortOpts) (*FlipGroupItem, error) {
|
||||
if err := req.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
url := "/cloudapi/flipgroup/get"
|
||||
res, err := f.client.DecortApiCall(ctx, typed.POST, url, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fg := &FlipGroupItem{}
|
||||
err = json.Unmarshal(res, fg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return fg, nil
|
||||
}
|
||||
29
pkg/cloudapi/flipgroup/list.go
Normal file
29
pkg/cloudapi/flipgroup/list.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package flipgroup
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/rudecs/decort-sdk/opts"
|
||||
"github.com/rudecs/decort-sdk/typed"
|
||||
)
|
||||
|
||||
type ListRequest struct {
|
||||
Page uint64 `url:"page,omitempty"`
|
||||
Size uint64 `url:"size,omitempty"`
|
||||
}
|
||||
|
||||
func (f FlipGroup) List(ctx context.Context, req ListRequest, options ...opts.DecortOpts) (FlipGroupList, error) {
|
||||
url := "/cloudapi/flipgroup/list"
|
||||
fgListRaw, err := f.client.DecortApiCall(ctx, typed.POST, url, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fgList := FlipGroupList{}
|
||||
if err := json.Unmarshal(fgListRaw, &fgList); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return fgList, nil
|
||||
}
|
||||
40
pkg/cloudapi/flipgroup/models.go
Normal file
40
pkg/cloudapi/flipgroup/models.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package flipgroup
|
||||
|
||||
type FlipGroupRecord struct {
|
||||
DefaultGW string `json:"defaultGW"`
|
||||
ID uint64 `json:"id"`
|
||||
IP string `json:"ip"`
|
||||
Name string `json:"name"`
|
||||
Netmask uint64 `json:"netmask"`
|
||||
}
|
||||
|
||||
type FlipGroupItem struct {
|
||||
AccountID uint64 `json:"accountId"`
|
||||
AccountName string `json:"accountName"`
|
||||
ClientIds []uint64 `json:"clientIds"`
|
||||
ClientType string `json:"clientType"`
|
||||
ConnID uint64 `json:"connId"`
|
||||
ConnType string `json:"connType"`
|
||||
CreatedBy string `json:"createdBy"`
|
||||
CreatedTime uint64 `json:"createdTime"`
|
||||
DefaultGW string `json:"defaultGW"`
|
||||
DeletedBy string `json:"deletedBy"`
|
||||
DeletedTime uint64 `json:"deletedTime"`
|
||||
Description string `json:"desc"`
|
||||
GID uint64 `json:"gid"`
|
||||
GUID uint64 `json:"guid"`
|
||||
ID uint64 `json:"id"`
|
||||
IP string `json:"ip"`
|
||||
Milestones uint64 `json:"milestones"`
|
||||
Name string `json:"name"`
|
||||
NetID uint64 `json:"netId"`
|
||||
NetType string `json:"netType"`
|
||||
Network string `json:"network"`
|
||||
RGID uint64 `json:"rgId"`
|
||||
RGName string `json:"rgName"`
|
||||
Status string `json:"status"`
|
||||
UpdatedBy string `json:"updatedBy"`
|
||||
UpdatedTime uint64 `json:"updatedTime"`
|
||||
}
|
||||
|
||||
type FlipGroupList []FlipGroupItem
|
||||
Reference in New Issue
Block a user