1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-16 22:50:10 +03:00

F 4021: [GOCA] Enable VM pool filtering

* Mutualize some code parts
* make InfoExtended prototype consistent with other pool methods

Signed-off-by: Pierre Lafievre <pierre.lafievre@iguanesolutions.com>
(cherry picked from commit 0d5f15f34789787fddc75be8c66383c440a5aff4)
This commit is contained in:
Pierre Lafievre 2019-12-21 00:00:01 +01:00 committed by Ruben S. Montero
parent 147416fba8
commit c8780da017
No known key found for this signature in database
GPG Key ID: A0CEA6FA880A1D87
12 changed files with 245 additions and 172 deletions

View File

@ -74,26 +74,14 @@ func (dc *DocumentsController) ByName(name string, args ...int) (int, error) {
// Info returns a document pool. A connection to OpenNebula is
// performed.
func (dc *DocumentsController) Info(args ...int) (*document.Pool, error) {
var who, start, end int
switch len(args) {
case 0:
who = parameters.PoolWhoMine
start = -1
end = -1
case 1:
who = args[0]
start = -1
end = -1
case 3:
who = args[0]
start = args[1]
end = args[2]
default:
return nil, errors.New("Wrong number of arguments")
fArgs, err := handleArgs(args)
if err != nil {
return nil, err
}
fArgs = append(fArgs, dc.dType)
response, err := dc.c.Client.Call("one.documentpool.info", who, start, end, dc.dType)
response, err := dc.c.Client.Call("one.documentpool.info", fArgs...)
if err != nil {
return nil, err
}

View File

@ -360,13 +360,12 @@ func (t *Template) AddVector(key string) *Vector {
return vector
}
// AddPair adds a new pair to a Template objects
func (t *Template) AddPair(key string, v interface{}) error {
func MakePair(key string, v interface{}) (*Pair, error) {
var val string
switch v := v.(type) {
default:
return fmt.Errorf("AddPair: Unexpected type")
return nil, fmt.Errorf("MakePair: Unexpected type")
case float32, float64:
val = fmt.Sprintf("%f", v)
case int, uint:
@ -374,8 +373,18 @@ func (t *Template) AddPair(key string, v interface{}) error {
case string:
val = v
}
pair := &Pair{XMLName: xml.Name{Local: strings.ToUpper(key)}, Value: val}
return pair, nil
}
// AddPair adds a new pair to a Template objects
func (t *Template) AddPair(key string, v interface{}) error {
pair, err := MakePair(key, v)
if err != nil {
return fmt.Errorf("AddPair: %s", err)
}
t.Elements = append(t.Elements, pair)
return nil
@ -383,21 +392,12 @@ func (t *Template) AddPair(key string, v interface{}) error {
// AddPair adds a new pair to a Template
func (t *Vector) AddPair(key string, v interface{}) error {
var val string
switch v := v.(type) {
default:
return fmt.Errorf("AddPair: Unexpected type")
case float32, float64:
val = fmt.Sprintf("%f", v)
case int, uint:
val = fmt.Sprintf("%d", v)
case string:
val = v
pair, err := MakePair(key, v)
if err != nil {
return fmt.Errorf("AddPair: %s", err)
}
pair := Pair{XMLName: xml.Name{Local: strings.ToUpper(key)}, Value: val}
t.Pairs = append(t.Pairs, pair)
t.Pairs = append(t.Pairs, *pair)
return nil
}

View File

@ -0,0 +1,134 @@
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2019, OpenNebula Project, OpenNebula Systems */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
/* not use this file except in compliance with the License. You may obtain */
/* a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/*--------------------------------------------------------------------------- */
package goca
import (
"errors"
"fmt"
dyn "github.com/OpenNebula/one/src/oca/go/src/goca/dynamic"
"github.com/OpenNebula/one/src/oca/go/src/goca/parameters"
param "github.com/OpenNebula/one/src/oca/go/src/goca/parameters"
)
// VMFilter groups filtering criterias for VMs
type VMFilter struct {
Who int
StartID int
EndID int
State int
// Pair is optional. Format: "KEY=VALUE"
Pair string
}
// NewVMFilterDefault return a VM filter configured by default
func NewVMFilterDefault() *VMFilter {
return &VMFilter{
Who: param.PoolWhoMine,
StartID: -1,
EndID: -1,
State: -1,
}
}
// NewVMFilter return a VM filter
func NewVMFilter(who, start, end, state int) *VMFilter {
filter := &VMFilter{
Who: who,
StartID: start,
EndID: end,
State: state,
}
return filter
}
// SetPair set the optional argument pair to the filter
func (f *VMFilter) SetPair(key string, value interface{}) error {
pair, err := dyn.MakePair(key, value)
if err != nil {
return err
}
// don't work with String method defined on Pair
f.Pair = fmt.Sprintf("%s=%s", pair.Key(), pair.Value)
return nil
}
func (f *VMFilter) toArgs() []interface{} {
if len(f.Pair) > 0 {
return []interface{}{f.Who, f.StartID, f.EndID, f.State, f.Pair}
}
return []interface{}{f.Who, f.StartID, f.EndID, f.State}
}
func handleArgs(args []int) ([]interface{}, error) {
var who, start, end int
switch len(args) {
case 0:
who = parameters.PoolWhoMine
start = -1
end = -1
case 1:
who = args[0]
start = -1
end = -1
case 2:
return nil, errors.New("Info method: wrong number of arguments, provide the end of the range ID")
case 3:
who = args[0]
start = args[1]
end = args[2]
default:
return nil, errors.New("Info method: too many arguments")
}
return []interface{}{who, start, end}, nil
}
func handleVMArgs(args []int) ([]interface{}, error) {
if len(args) > 4 {
return nil, errors.New("Info method: too many arguments")
}
var min int
if len(args) <= 3 {
min = len(args)
} else {
min = 3
}
vmArgs, err := handleArgs(args[:min])
if err != nil {
return nil, err
}
state := -1
if len(args) == 4 {
state = args[3]
}
vmArgs = append(vmArgs, state)
return vmArgs, nil
}

View File

@ -69,8 +69,14 @@ func (c *HooksController) ByName(name string) (int, error) {
// Info returns a hook pool. A connection to OpenNebula is
// performed
func (hc *HooksController) Info() (*hook.Pool, error) {
response, err := hc.c.Client.Call("one.hookpool.info", -1, -1, -1)
func (hc *HooksController) Info(args ...int) (*hook.Pool, error) {
fArgs, err := handleArgs(args)
if err != nil {
return nil, err
}
response, err := hc.c.Client.Call("one.hookpool.info", fArgs...)
if err != nil {
return nil, err
}

View File

@ -78,22 +78,13 @@ func (c *ImagesController) ByName(name string, args ...int) (int, error) {
// Info returns a new image pool. It accepts the scope of the query.
func (ic *ImagesController) Info(args ...int) (*image.Pool, error) {
var who, start, end int
switch len(args) {
case 0:
who = parameters.PoolWhoMine
start = -1
end = -1
case 3:
who = args[0]
start = args[1]
end = args[2]
default:
return nil, errors.New("Wrong number of arguments")
fArgs, err := handleArgs(args)
if err != nil {
return nil, err
}
response, err := ic.c.Client.Call("one.imagepool.info", who, start, end)
response, err := ic.c.Client.Call("one.imagepool.info", fArgs...)
if err != nil {
return nil, err
}

View File

@ -72,26 +72,13 @@ func (c *MarketPlacesController) ByName(name string) (int, error) {
// Info returns a marketplace pool. A connection to OpenNebula is
// performed.
func (mc *MarketPlacesController) Info(args ...int) (*marketplace.Pool, error) {
var who, start, end int
switch len(args) {
case 0:
who = parameters.PoolWhoMine
start = -1
end = -1
case 1:
who = args[0]
start = -1
end = -1
case 3:
who = args[0]
start = args[1]
end = args[2]
default:
return nil, errors.New("Wrong number of arguments")
fArgs, err := handleArgs(args)
if err != nil {
return nil, err
}
response, err := mc.c.Client.Call("one.marketpool.info", who, start, end)
response, err := mc.c.Client.Call("one.marketpool.info", fArgs...)
if err != nil {
return nil, err
}

View File

@ -71,26 +71,13 @@ func (c *MarketPlaceAppsController) ByName(name string, args ...int) (int, error
// Info returns a marketplace app pool. A connection to OpenNebula is
// performed.
func (mc *MarketPlaceAppsController) Info(args ...int) (*marketplaceapp.Pool, error) {
var who, start, end int
switch len(args) {
case 0:
who = parameters.PoolWhoMine
start = -1
end = -1
case 1:
who = args[0]
start = -1
end = -1
case 3:
who = args[0]
start = args[1]
end = args[2]
default:
return nil, errors.New("Wrong number of arguments")
fArgs, err := handleArgs(args)
if err != nil {
return nil, err
}
response, err := mc.c.Client.Call("one.marketapppool.info", who, start, end)
response, err := mc.c.Client.Call("one.marketapppool.info", fArgs...)
if err != nil {
return nil, err
}

View File

@ -71,26 +71,13 @@ func (c *SecurityGroupsController) ByName(name string, args ...int) (int, error)
// Info returns a security group pool. A connection to OpenNebula is
// performed.
func (sc *SecurityGroupsController) Info(args ...int) (*securitygroup.Pool, error) {
var who, start, end int
switch len(args) {
case 0:
who = parameters.PoolWhoMine
start = -1
end = -1
case 1:
who = args[0]
start = -1
end = -1
case 3:
who = args[0]
start = args[1]
end = args[2]
default:
return nil, errors.New("Wrong number of arguments")
fArgs, err := handleArgs(args)
if err != nil {
return nil, err
}
response, err := sc.c.Client.Call("one.secgrouppool.info", who, start, end)
response, err := sc.c.Client.Call("one.secgrouppool.info", fArgs...)
if err != nil {
return nil, err
}

View File

@ -71,22 +71,13 @@ func (c *TemplatesController) ByName(name string, args ...int) (int, error) {
// Info returns a template pool. A connection to OpenNebula is
// performed.
func (tc *TemplatesController) Info(args ...int) (*template.Pool, error) {
var who, start, end int
switch len(args) {
case 0:
who = parameters.PoolWhoMine
start = -1
end = -1
case 3:
who = args[0]
start = args[1]
end = args[2]
default:
return nil, errors.New("Wrong number of arguments")
fArgs, err := handleArgs(args)
if err != nil {
return nil, err
}
response, err := tc.c.Client.Call("one.templatepool.info", who, start, end)
response, err := tc.c.Client.Call("one.templatepool.info", fArgs...)
if err != nil {
return nil, err
}

View File

@ -19,6 +19,7 @@ package goca
import (
"encoding/xml"
"errors"
"fmt"
"github.com/OpenNebula/one/src/oca/go/src/goca/parameters"
"github.com/OpenNebula/one/src/oca/go/src/goca/schemas/shared"
@ -78,34 +79,13 @@ func (c *VMsController) ByName(name string, args ...int) (int, error) {
// Info returns a new VM pool. It accepts the scope of the query.
func (vc *VMsController) Info(args ...int) (*vm.Pool, error) {
var who, start, end, state int
switch len(args) {
case 0:
who = parameters.PoolWhoMine
start = -1
end = -1
state = -1
case 1:
who = args[0]
start = -1
end = -1
state = -1
case 3:
who = args[0]
start = args[1]
end = args[2]
state = -1
case 4:
who = args[0]
start = args[1]
end = args[2]
state = args[3]
default:
return nil, errors.New("Wrong number of arguments")
fArgs, err := handleVMArgs(args)
if err != nil {
return nil, err
}
response, err := vc.c.Client.Call("one.vmpool.info", who, start, end, state)
response, err := vc.c.Client.Call("one.vmpool.info", fArgs...)
if err != nil {
return nil, err
}
@ -120,9 +100,54 @@ func (vc *VMsController) Info(args ...int) (*vm.Pool, error) {
}
// InfoExtended connects to OpenNebula and fetches the whole VM_POOL information
func (vc *VMsController) InfoExtended(filterFlag, startID, endID, state int) (*vm.Pool, error) {
response, err := vc.c.Client.Call("one.vmpool.infoextended", filterFlag,
startID, endID, state)
func (vc *VMsController) InfoExtended(args ...int) (*vm.Pool, error) {
fArgs, err := handleVMArgs(args)
if err != nil {
return nil, err
}
response, err := vc.c.Client.Call("one.vmpool.infoextended", fArgs...)
if err != nil {
return nil, err
}
vmPool := &vm.Pool{}
err = xml.Unmarshal([]byte(response.Body()), vmPool)
if err != nil {
return nil, err
}
return vmPool, nil
}
// InfoFilter returns a new VM pool. It accepts the scope of the query.
func (vc *VMsController) InfoFilter(f *VMFilter) (*vm.Pool, error) {
if f == nil {
return nil, fmt.Errorf("InfoFilter: nil parameter passed.")
}
response, err := vc.c.Client.Call("one.vmpool.info", f.toArgs()...)
if err != nil {
return nil, err
}
vmPool := &vm.Pool{}
err = xml.Unmarshal([]byte(response.Body()), vmPool)
if err != nil {
return nil, err
}
return vmPool, nil
}
// InfoExtendedFilter connects to OpenNebula and fetches the whole VM_POOL information
func (vc *VMsController) InfoExtendedFilter(f *VMFilter) (*vm.Pool, error) {
if f == nil {
return nil, fmt.Errorf("InfoFilter: nil parameter passed.")
}
response, err := vc.c.Client.Call("one.vmpool.info", f.toArgs()...)
if err != nil {
return nil, err
}

View File

@ -20,7 +20,6 @@ import (
"encoding/xml"
"errors"
"github.com/OpenNebula/one/src/oca/go/src/goca/parameters"
"github.com/OpenNebula/one/src/oca/go/src/goca/schemas/shared"
"github.com/OpenNebula/one/src/oca/go/src/goca/schemas/vmgroup"
)
@ -71,26 +70,13 @@ func (c *VMGroupsController) ByName(name string, args ...int) (int, error) {
// Info returns a vm group pool. A connection to OpenNebula is
// performed.
func (vc *VMGroupsController) Info(args ...int) (*vmgroup.Pool, error) {
var who, start, end int
switch len(args) {
case 0:
who = parameters.PoolWhoMine
start = -1
end = -1
case 1:
who = args[0]
start = -1
end = -1
case 3:
who = args[0]
start = args[1]
end = args[2]
default:
return nil, errors.New("Wrong number of arguments")
fArgs, err := handleArgs(args)
if err != nil {
return nil, err
}
response, err := vc.c.Client.Call("one.vmgrouppool.info", who, start, end)
response, err := vc.c.Client.Call("one.vmgrouppool.info", fArgs...)
if err != nil {
return nil, err
}

View File

@ -73,22 +73,13 @@ func (c *VNTemplatesController) ByName(name string) (int, error) {
// Info returns a vntemplate pool. A connection to OpenNebula is
// performed.
func (vc *VNTemplatesController) Info(args ...int) (*vntemplate.Pool, error) {
var who, start, end int
switch len(args) {
case 0:
who = parameters.PoolWhoMine
start = -1
end = -1
case 3:
who = args[0]
start = args[1]
end = args[2]
default:
return nil, errors.New("Wrong number of arguments")
fArgs, err := handleArgs(args)
if err != nil {
return nil, err
}
response, err := vc.c.Client.Call("one.vntemplatepool.info", who, start, end)
response, err := vc.c.Client.Call("one.vntemplatepool.info", fArgs...)
if err != nil {
return nil, err
}