From c78b1348e3c345b05b2b5888665ac8838949064f Mon Sep 17 00:00:00 2001 From: Nikita Sorokin Date: Mon, 24 Jul 2023 15:13:04 +0300 Subject: [PATCH] v1.5.0 --- pkg/cloudapi/kvmppc/create.go | 24 ++++++++++++------- pkg/cloudapi/kvmppc/create_blank.go | 26 +++++++++++++------- pkg/cloudapi/kvmx86/create.go | 22 +++++++++++------ pkg/cloudapi/kvmx86/create_blank.go | 24 ++++++++++++------- pkg/cloudbroker/kvmppc/create.go | 24 ++++++++++++------- pkg/cloudbroker/kvmppc/create_blank.go | 24 ++++++++++++------- pkg/cloudbroker/kvmppc/mass_create.go | 33 ++++++++++++++++++++++++-- pkg/cloudbroker/kvmx86/create.go | 24 ++++++++++++------- pkg/cloudbroker/kvmx86/create_blank.go | 24 ++++++++++++------- pkg/cloudbroker/kvmx86/mass_create.go | 33 ++++++++++++++++++++++++-- 10 files changed, 190 insertions(+), 68 deletions(-) diff --git a/pkg/cloudapi/kvmppc/create.go b/pkg/cloudapi/kvmppc/create.go index d385319..70ea036 100644 --- a/pkg/cloudapi/kvmppc/create.go +++ b/pkg/cloudapi/kvmppc/create.go @@ -64,8 +64,10 @@ type CreateRequest struct { Pool string `url:"pool,omitempty" json:"pool,omitempty"` // Slice of structs with net interface description. + // If not specified, compute will be created with default interface from RG. + // To create compute without interfaces, pass initialized empty slice . // Required: false - Interfaces []Interface `url:"-" json:"interfaces,omitempty" validate:"omitempty,min=1,dive"` + Interfaces []Interface `url:"-" json:"interfaces,omitempty" validate:"omitempty,dive"` // Input data for cloud-init facility // Required: false @@ -102,15 +104,21 @@ func (k KVMPPC) Create(ctx context.Context, req CreateRequest) (uint64, error) { } } - interfaces := make([]string, 0, len(req.Interfaces)) + var interfaces []string - for i := range req.Interfaces { - b, err := json.Marshal(req.Interfaces[i]) - if err != nil { - return 0, err - } + if req.Interfaces != nil && len(req.Interfaces) != 0 { + interfaces = make([]string, 0, len(req.Interfaces)) + + for i := range req.Interfaces { + b, err := json.Marshal(req.Interfaces[i]) + if err != nil { + return 0, err + } - interfaces = append(interfaces, string(b)) + interfaces = append(interfaces, string(b)) + } + } else if req.Interfaces != nil && len(req.Interfaces) == 0 { + interfaces = []string{"[]"} } reqWrapped := wrapperCreateRequest{ diff --git a/pkg/cloudapi/kvmppc/create_blank.go b/pkg/cloudapi/kvmppc/create_blank.go index 6367189..2967e6e 100644 --- a/pkg/cloudapi/kvmppc/create_blank.go +++ b/pkg/cloudapi/kvmppc/create_blank.go @@ -41,9 +41,11 @@ type CreateBlankRequest struct { // Required: true Pool string `url:"pool" json:"pool" validate:"required"` - // Slice of structs with net interface description + // Slice of structs with net interface description. + // If not specified, compute will be created with default interface from RG. + // To create compute without interfaces, pass initialized empty slice . // Required: false - Interfaces []Interface `url:"-" json:"interfaces,omitempty" validate:"omitempty,min=1,dive"` + Interfaces []Interface `url:"-" json:"interfaces,omitempty" validate:"omitempty,dive"` // Text description of this VM // Required: false @@ -64,15 +66,21 @@ func (k KVMPPC) CreateBlank(ctx context.Context, req CreateBlankRequest) (uint64 } } - interfaces := make([]string, 0, len(req.Interfaces)) + var interfaces []string - for i := range req.Interfaces { - b, err := json.Marshal(req.Interfaces[i]) - if err != nil { - return 0, err - } + if req.Interfaces != nil && len(req.Interfaces) != 0 { + interfaces = make([]string, 0, len(req.Interfaces)) + + for i := range req.Interfaces { + b, err := json.Marshal(req.Interfaces[i]) + if err != nil { + return 0, err + } - interfaces = append(interfaces, string(b)) + interfaces = append(interfaces, string(b)) + } + } else if req.Interfaces != nil && len(req.Interfaces) == 0 { + interfaces = []string{"[]"} } reqWrapped := wrapperCreateBlankRequest{ diff --git a/pkg/cloudapi/kvmx86/create.go b/pkg/cloudapi/kvmx86/create.go index 60b651a..7c999f8 100644 --- a/pkg/cloudapi/kvmx86/create.go +++ b/pkg/cloudapi/kvmx86/create.go @@ -64,6 +64,8 @@ type CreateRequest struct { Pool string `url:"pool,omitempty" json:"pool,omitempty"` // Slice of structs with net interface description. + // If not specified, compute will be created with default interface from RG. + // To create compute without interfaces, pass initialized empty slice . // Required: false Interfaces []Interface `url:"-" json:"interfaces,omitempty" validate:"omitempty,dive"` @@ -110,15 +112,21 @@ func (k KVMX86) Create(ctx context.Context, req CreateRequest) (uint64, error) { } } - interfaces := make([]string, 0, len(req.Interfaces)) + var interfaces []string - for i := range req.Interfaces { - b, err := json.Marshal(req.Interfaces[i]) - if err != nil { - return 0, err - } + if req.Interfaces != nil && len(req.Interfaces) != 0 { + interfaces = make([]string, 0, len(req.Interfaces)) + + for i := range req.Interfaces { + b, err := json.Marshal(req.Interfaces[i]) + if err != nil { + return 0, err + } - interfaces = append(interfaces, string(b)) + interfaces = append(interfaces, string(b)) + } + } else if req.Interfaces != nil && len(req.Interfaces) == 0 { + interfaces = []string{"[]"} } reqWrapped := wrapperCreateRequest{ diff --git a/pkg/cloudapi/kvmx86/create_blank.go b/pkg/cloudapi/kvmx86/create_blank.go index 97df11e..6a71133 100644 --- a/pkg/cloudapi/kvmx86/create_blank.go +++ b/pkg/cloudapi/kvmx86/create_blank.go @@ -42,8 +42,10 @@ type CreateBlankRequest struct { Pool string `url:"pool" json:"pool" validate:"required"` // Slice of structs with net interface description. + // If not specified, compute will be created with default interface from RG. + // To create compute without interfaces, pass initialized empty slice . // Required: false - Interfaces []Interface `url:"-" json:"interfaces,omitempty" validate:"omitempty,min=1,dive"` + Interfaces []Interface `url:"-" json:"interfaces,omitempty" validate:"omitempty,dive"` // Type of compute Stateful (KVM_X86) or Stateless (SVA_KVM_X86) // Required: false @@ -68,15 +70,21 @@ func (k KVMX86) CreateBlank(ctx context.Context, req CreateBlankRequest) (uint64 } } - interfaces := make([]string, 0, len(req.Interfaces)) + var interfaces []string - for i := range req.Interfaces { - b, err := json.Marshal(req.Interfaces[i]) - if err != nil { - return 0, err - } + if req.Interfaces != nil && len(req.Interfaces) != 0 { + interfaces = make([]string, 0, len(req.Interfaces)) + + for i := range req.Interfaces { + b, err := json.Marshal(req.Interfaces[i]) + if err != nil { + return 0, err + } - interfaces = append(interfaces, string(b)) + interfaces = append(interfaces, string(b)) + } + } else if req.Interfaces != nil && len(req.Interfaces) == 0 { + interfaces = []string{"[]"} } reqWrapped := wrapperCreateBlankRequest{ diff --git a/pkg/cloudbroker/kvmppc/create.go b/pkg/cloudbroker/kvmppc/create.go index bdd2bcc..15915c7 100644 --- a/pkg/cloudbroker/kvmppc/create.go +++ b/pkg/cloudbroker/kvmppc/create.go @@ -64,8 +64,10 @@ type CreateRequest struct { Pool string `url:"pool,omitempty" json:"pool,omitempty"` // Slice of structs with net interface description. + // If not specified, compute will be created with default interface from RG. + // To create compute without interfaces, pass initialized empty slice . // Required: false - Interfaces []Interface `url:"-" json:"interfaces,omitempty" validate:"omitempty,min=1,dive"` + Interfaces []Interface `url:"-" json:"interfaces,omitempty" validate:"omitempty,dive"` // Input data for cloud-init facility // Required: false @@ -102,15 +104,21 @@ func (k KVMPPC) Create(ctx context.Context, req CreateRequest) (uint64, error) { } } - interfaces := make([]string, 0, len(req.Interfaces)) + var interfaces []string - for i := range req.Interfaces { - b, err := json.Marshal(req.Interfaces[i]) - if err != nil { - return 0, err - } + if req.Interfaces != nil && len(req.Interfaces) != 0 { + interfaces = make([]string, 0, len(req.Interfaces)) + + for i := range req.Interfaces { + b, err := json.Marshal(req.Interfaces[i]) + if err != nil { + return 0, err + } - interfaces = append(interfaces, string(b)) + interfaces = append(interfaces, string(b)) + } + } else if req.Interfaces != nil && len(req.Interfaces) == 0 { + interfaces = []string{"[]"} } reqWrapped := wrapperCreateRequest{ diff --git a/pkg/cloudbroker/kvmppc/create_blank.go b/pkg/cloudbroker/kvmppc/create_blank.go index 352ee2a..401cb19 100644 --- a/pkg/cloudbroker/kvmppc/create_blank.go +++ b/pkg/cloudbroker/kvmppc/create_blank.go @@ -42,8 +42,10 @@ type CreateBlankRequest struct { Pool string `url:"pool" json:"pool" validate:"required"` // Slice of structs with net interface description. + // If not specified, compute will be created with default interface from RG. + // To create compute without interfaces, pass initialized empty slice . // Required: false - Interfaces []Interface `url:"-" json:"interfaces,omitempty" validate:"omitempty,min=1,dive"` + Interfaces []Interface `url:"-" json:"interfaces,omitempty" validate:"omitempty,dive"` // Text description of this VM // Required: false @@ -64,15 +66,21 @@ func (k KVMPPC) CreateBlank(ctx context.Context, req CreateBlankRequest) (uint64 } } - interfaces := make([]string, 0, len(req.Interfaces)) + var interfaces []string - for i := range req.Interfaces { - b, err := json.Marshal(req.Interfaces[i]) - if err != nil { - return 0, err - } + if req.Interfaces != nil && len(req.Interfaces) != 0 { + interfaces = make([]string, 0, len(req.Interfaces)) + + for i := range req.Interfaces { + b, err := json.Marshal(req.Interfaces[i]) + if err != nil { + return 0, err + } - interfaces = append(interfaces, string(b)) + interfaces = append(interfaces, string(b)) + } + } else if req.Interfaces != nil && len(req.Interfaces) == 0 { + interfaces = []string{"[]"} } reqWrapped := wrapperCreateBlankRequest{ diff --git a/pkg/cloudbroker/kvmppc/mass_create.go b/pkg/cloudbroker/kvmppc/mass_create.go index 6601bc4..41ff703 100644 --- a/pkg/cloudbroker/kvmppc/mass_create.go +++ b/pkg/cloudbroker/kvmppc/mass_create.go @@ -49,8 +49,10 @@ type MassCreateRequest struct { Pool string `url:"pool,omitempty" json:"pool,omitempty"` // Slice of structs with net interface description. + // If not specified, compute will be created with default interface from RG. + // To create compute without interfaces, pass initialized empty slice. // Required: false - Interfaces []Interface `url:"interfaces,omitempty" json:"interfaces,omitempty" validate:"omitempty,min=1,dive"` + Interfaces []Interface `url:"-" json:"interfaces,omitempty" validate:"omitempty,dive"` // Input data for cloud-init facility // Required: false @@ -69,6 +71,11 @@ type MassCreateRequest struct { Reason string `url:"reason,omitempty" json:"reason,omitempty"` } +type wrapperMassCreateRequest struct { + MassCreateRequest + Interfaces []string `url:"interfaces,omitempty"` +} + // MassCreate creates KVM PPC computes based on specified OS image func (k KVMPPC) MassCreate(ctx context.Context, req MassCreateRequest) ([]uint64, error) { err := validators.ValidateRequest(req) @@ -78,9 +85,31 @@ func (k KVMPPC) MassCreate(ctx context.Context, req MassCreateRequest) ([]uint64 } } + var interfaces []string + + if req.Interfaces != nil && len(req.Interfaces) != 0 { + interfaces = make([]string, 0, len(req.Interfaces)) + + for i := range req.Interfaces { + b, err := json.Marshal(req.Interfaces[i]) + if err != nil { + return nil, err + } + + interfaces = append(interfaces, string(b)) + } + } else if req.Interfaces != nil && len(req.Interfaces) == 0 { + interfaces = []string{"[]"} + } + + reqWrapped := wrapperMassCreateRequest{ + MassCreateRequest: req, + Interfaces: interfaces, + } + url := "/cloudbroker/kvmppc/massCreate" - res, err := k.client.DecortApiCall(ctx, http.MethodPost, url, req) + res, err := k.client.DecortApiCall(ctx, http.MethodPost, url, reqWrapped) if err != nil { return nil, err } diff --git a/pkg/cloudbroker/kvmx86/create.go b/pkg/cloudbroker/kvmx86/create.go index e5be33e..987721b 100644 --- a/pkg/cloudbroker/kvmx86/create.go +++ b/pkg/cloudbroker/kvmx86/create.go @@ -64,8 +64,10 @@ type CreateRequest struct { Pool string `url:"pool,omitempty" json:"pool,omitempty"` // Slice of structs with net interface description. + // If not specified, compute will be created with default interface from RG. + // To create compute without interfaces, pass initialized empty slice . // Required: false - Interfaces []Interface `url:"-" json:"interfaces,omitempty" validate:"omitempty,min=1,dive"` + Interfaces []Interface `url:"-" json:"interfaces,omitempty" validate:"omitempty,dive"` // Input data for cloud-init facility // Required: false @@ -114,15 +116,21 @@ func (k KVMX86) Create(ctx context.Context, req CreateRequest) (uint64, error) { } } - interfaces := make([]string, 0, len(req.Interfaces)) + var interfaces []string - for i := range req.Interfaces { - b, err := json.Marshal(req.Interfaces[i]) - if err != nil { - return 0, err - } + if req.Interfaces != nil && len(req.Interfaces) != 0 { + interfaces = make([]string, 0, len(req.Interfaces)) + + for i := range req.Interfaces { + b, err := json.Marshal(req.Interfaces[i]) + if err != nil { + return 0, err + } - interfaces = append(interfaces, string(b)) + interfaces = append(interfaces, string(b)) + } + } else if req.Interfaces != nil && len(req.Interfaces) == 0 { + interfaces = []string{"[]"} } reqWrapped := wrapperCreateRequest{ diff --git a/pkg/cloudbroker/kvmx86/create_blank.go b/pkg/cloudbroker/kvmx86/create_blank.go index 52e808e..62d7515 100644 --- a/pkg/cloudbroker/kvmx86/create_blank.go +++ b/pkg/cloudbroker/kvmx86/create_blank.go @@ -42,8 +42,10 @@ type CreateBlankRequest struct { Pool string `url:"pool" json:"pool" validate:"required"` // Slice of structs with net interface description. + // If not specified, compute will be created with default interface from RG. + // To create compute without interfaces, pass initialized empty slice . // Required: false - Interfaces []Interface `url:"-" json:"interfaces,omitempty" validate:"omitempty,min=1,dive"` + Interfaces []Interface `url:"-" json:"interfaces,omitempty" validate:"omitempty,dive"` // Text description of this VM // Required: false @@ -64,15 +66,21 @@ func (k KVMX86) CreateBlank(ctx context.Context, req CreateBlankRequest) (uint64 } } - interfaces := make([]string, 0, len(req.Interfaces)) + var interfaces []string - for i := range req.Interfaces { - b, err := json.Marshal(req.Interfaces[i]) - if err != nil { - return 0, err - } + if req.Interfaces != nil && len(req.Interfaces) != 0 { + interfaces = make([]string, 0, len(req.Interfaces)) + + for i := range req.Interfaces { + b, err := json.Marshal(req.Interfaces[i]) + if err != nil { + return 0, err + } - interfaces = append(interfaces, string(b)) + interfaces = append(interfaces, string(b)) + } + } else if req.Interfaces != nil && len(req.Interfaces) == 0 { + interfaces = []string{"[]"} } reqWrapped := wrapperCreateBlankRequest{ diff --git a/pkg/cloudbroker/kvmx86/mass_create.go b/pkg/cloudbroker/kvmx86/mass_create.go index 834ceae..30d8671 100644 --- a/pkg/cloudbroker/kvmx86/mass_create.go +++ b/pkg/cloudbroker/kvmx86/mass_create.go @@ -49,8 +49,10 @@ type MassCreateRequest struct { Pool string `url:"pool,omitempty" json:"pool,omitempty"` // Slice of structs with net interface description. + // If not specified, compute will be created with default interface from RG. + // To create compute without interfaces, pass initialized empty slice . // Required: false - Interfaces []Interface `url:"interfaces,omitempty" json:"interfaces,omitempty" validate:"omitempty,min=1,dive"` + Interfaces []Interface `url:"-" json:"interfaces,omitempty" validate:"omitempty,dive"` // Input data for cloud-init facility // Required: false @@ -69,6 +71,11 @@ type MassCreateRequest struct { Reason string `url:"reason,omitempty" json:"reason,omitempty"` } +type wrapperMassCreateRequest struct { + MassCreateRequest + Interfaces []string `url:"interfaces,omitempty"` +} + // MassCreate creates KVM x86 computes based on specified OS image func (k KVMX86) MassCreate(ctx context.Context, req MassCreateRequest) ([]uint64, error) { err := validators.ValidateRequest(req) @@ -78,9 +85,31 @@ func (k KVMX86) MassCreate(ctx context.Context, req MassCreateRequest) ([]uint64 } } + var interfaces []string + + if req.Interfaces != nil && len(req.Interfaces) != 0 { + interfaces = make([]string, 0, len(req.Interfaces)) + + for i := range req.Interfaces { + b, err := json.Marshal(req.Interfaces[i]) + if err != nil { + return nil, err + } + + interfaces = append(interfaces, string(b)) + } + } else if req.Interfaces != nil && len(req.Interfaces) == 0 { + interfaces = []string{"[]"} + } + + reqWrapped := wrapperMassCreateRequest{ + MassCreateRequest: req, + Interfaces: interfaces, + } + url := "/cloudbroker/kvmx86/massCreate" - res, err := k.client.DecortApiCall(ctx, http.MethodPost, url, req) + res, err := k.client.DecortApiCall(ctx, http.MethodPost, url, reqWrapped) if err != nil { return nil, err }