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

F #4298: Improve GOCA services support (#4918)

This commit is contained in:
Christian González 2020-06-11 11:51:27 +02:00 committed by GitHub
parent 525ca47189
commit 770cb0d7f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 65 additions and 34 deletions

View File

@ -23,6 +23,7 @@ import (
"log"
"net/http"
"strings"
"errors"
)
// RESTClient
@ -44,7 +45,7 @@ type HTTPAuth struct {
// newHTTPResponse Creates Response from http response
func newHTTPResponse(r *http.Response, err error) (*Response, error) {
if err != nil {
return &Response{}, err
return nil, err
}
status := true
@ -76,7 +77,7 @@ func (c *RESTClient) HTTPMethod(method string, url string, args ...interface{})
case "PUT":
r, err = c.put(string(url), args[0].(map[string]interface{}))
case "":
return &Response{}, err
return nil, errors.New("Unsupported method.")
}
return r, err

View File

@ -20,6 +20,7 @@ import (
"fmt"
"strconv"
"encoding/json"
"errors"
"github.com/OpenNebula/one/src/oca/go/src/goca/schemas/service"
"github.com/OpenNebula/one/src/oca/go/src/goca/schemas/shared"
@ -55,6 +56,9 @@ func (sc *ServicesController) Info() (*service.Pool, error) {
if err != nil {
return nil, err
}
if !response.status {
return nil, errors.New(response.body)
}
servicepool := &service.Pool{}
pool_str, err := json.Marshal(response.BodyMap()["DOCUMENT_POOL"])
if err != nil {
@ -75,6 +79,9 @@ func (sc *ServiceController) Info() (*service.Service, error) {
if err != nil {
return nil, err
}
if !response.status {
return nil, errors.New(response.body)
}
service := &service.Service{}
service_str, err := json.Marshal(response.BodyMap()["DOCUMENT"])
@ -90,14 +97,14 @@ func (sc *ServiceController) Info() (*service.Service, error) {
}
// Delete the service resource identified by <id>
func (sc *ServiceController) Delete() (bool, error) {
func (sc *ServiceController) Delete() error {
url := urlService(sc.ID)
return sc.c.boolResponse("DELETE", url, nil)
}
// Recover existing service if delete de service is recover and deleted
func (sc *ServiceController) Recover(delete bool) (bool, error) {
func (sc *ServiceController) Recover(delete bool) error {
action := make(map[string]interface{})
action["action"] = map[string]interface{}{
@ -113,7 +120,7 @@ func (sc *ServiceController) Recover(delete bool) (bool, error) {
// Permissions operations
// Chgrp service
func (sc *ServiceController) Chgrp(gid int) (bool, error) {
func (sc *ServiceController) Chgrp(gid int) error {
action := make(map[string]interface{})
action["action"] = map[string]interface{}{
@ -127,7 +134,7 @@ func (sc *ServiceController) Chgrp(gid int) (bool, error) {
}
// Chown service
func (sc *ServiceController) Chown(uid, gid int) (bool, error) {
func (sc *ServiceController) Chown(uid, gid int) error {
action := make(map[string]interface{})
action["action"] = map[string]interface{}{
@ -142,7 +149,7 @@ func (sc *ServiceController) Chown(uid, gid int) (bool, error) {
}
// Chmod service
func (sc *ServiceController) Chmod(perm shared.Permissions) (bool, error) {
func (sc *ServiceController) Chmod(perm shared.Permissions) error {
action := make(map[string]interface{})
action["action"] = map[string]interface{}{
@ -156,7 +163,7 @@ func (sc *ServiceController) Chmod(perm shared.Permissions) (bool, error) {
}
// Rename service
func (sc *ServiceController) Rename(new_name string) (bool, error) {
func (sc *ServiceController) Rename(new_name string) error {
action := make(map[string]interface{})
action["action"] = map[string]interface{}{
@ -172,7 +179,7 @@ func (sc *ServiceController) Rename(new_name string) (bool, error) {
// Role level actions
// Scale the cardinality of a service role
func (sc *ServiceController) Scale(role string, cardinality int, force bool) (bool, error) {
func (sc *ServiceController) Scale(role string, cardinality int, force bool) error {
url := fmt.Sprintf("%s/scale", urlService(sc.ID))
body := make(map[string]interface{})
@ -192,7 +199,7 @@ func (sc *ServiceController) Scale(role string, cardinality int, force bool) (bo
// "number": 2,
// },
// TODO: enforce only available actions
func (sc *ServiceController) VMAction(role, action string, params map[string]interface{}) (bool, error) {
func (sc *ServiceController) VMAction(role, action string, params map[string]interface{}) error {
url := fmt.Sprintf("%s/action", urlRole(sc.ID, role))
body := make(map[string]interface{})
@ -220,18 +227,22 @@ func urlService(id int) string {
}
// Action handler for existing flow services. Requires the action body.
func (sc *ServiceController) action(action map[string]interface{}) (bool, error) {
func (sc *ServiceController) action(action map[string]interface{}) error {
url := urlServiceAction(sc.ID)
return sc.c.boolResponse("POST", url, action)
}
func (c *Controller) boolResponse(method string, url string, body map[string]interface{}) (bool, error) {
func (c *Controller) boolResponse(method string, url string, body map[string]interface{}) error {
response, err := c.ClientFlow.HTTPMethod(method, url, body)
if err != nil {
return false, err
return err
}
return response.status, nil
if !response.status {
return errors.New(response.body)
}
return nil
}

View File

@ -20,6 +20,7 @@ import (
"fmt"
"strconv"
"encoding/json"
"errors"
srv_tmpl "github.com/OpenNebula/one/src/oca/go/src/goca/schemas/service_template"
"github.com/OpenNebula/one/src/oca/go/src/goca/schemas/service"
@ -56,6 +57,9 @@ func (stc *STemplatesController) Info() (*srv_tmpl.Pool, error) {
if err != nil {
return nil, err
}
if !response.status {
return nil, errors.New(response.body)
}
stemplatepool := &srv_tmpl.Pool{}
pool_str, err := json.Marshal(response.BodyMap()["DOCUMENT_POOL"])
@ -74,6 +78,9 @@ func (tc *STemplateController) Info() (*srv_tmpl.ServiceTemplate, error) {
if err != nil {
return nil, err
}
if !response.status {
return nil, errors.New(response.body)
}
stemplate := &srv_tmpl.ServiceTemplate{}
stemplate_str, err := json.Marshal(response.BodyMap()["DOCUMENT"])
@ -87,37 +94,40 @@ func (tc *STemplateController) Info() (*srv_tmpl.ServiceTemplate, error) {
// Allocate a service template
// st will be filled with the new ServiceTemplate information
func (tc *STemplatesController) Create(st *srv_tmpl.ServiceTemplate) (bool, error) {
func (tc *STemplatesController) Create(st *srv_tmpl.ServiceTemplate) error {
body := make(map[string]interface{})
// Get Template.Body as map
tmpl_byte, err := json.Marshal(st.Template.Body)
if err != nil {
return false, err
return err
}
json.Unmarshal(tmpl_byte, &body)
// Get response
response, err := tc.c.ClientFlow.HTTPMethod("POST", endpointFTemplate, body)
if err != nil {
return false, err
return err
}
if !response.status {
return errors.New(response.body)
}
// Update current ServiceTemplate with new values
stemplate_str, err := json.Marshal(response.BodyMap()["DOCUMENT"])
if err != nil {
return false, err
return err
}
err = json.Unmarshal(stemplate_str, st)
if err != nil {
return false, err
return err
}
return true, nil
return nil
}
// Delete the service resource identified by <id>
func (tc *STemplateController) Delete() (bool, error) {
func (tc *STemplateController) Delete() error {
url := urlTemplate(tc.ID)
return tc.c.boolResponse("DELETE", url, nil)
@ -150,6 +160,9 @@ func (tc *STemplateController) Instantiate(extra_tmpl string) (*service.Service,
if err != nil {
return nil, err
}
if !response.status {
return nil, errors.New(response.body)
}
//Build Service from response
service := &service.Service{}
@ -166,10 +179,10 @@ func (tc *STemplateController) Instantiate(extra_tmpl string) (*service.Service,
}
// Update service template
func (tc *STemplateController) Update(st *srv_tmpl.ServiceTemplate, append bool) (bool, error) {
func (tc *STemplateController) Update(st *srv_tmpl.ServiceTemplate, append bool) error {
tmpl_byte, err := json.Marshal(st.Template.Body)
if err != nil {
return false, err
return err
}
action := make(map[string]interface{})
@ -185,7 +198,7 @@ func (tc *STemplateController) Update(st *srv_tmpl.ServiceTemplate, append bool)
}
// Rename service template
func (tc *STemplateController) Rename(new_name string) (bool, error) {
func (tc *STemplateController) Rename(new_name string) error {
action := make(map[string]interface{})
action["action"] = map[string]interface{}{
@ -216,6 +229,9 @@ func (tc *STemplateController) Clone(clone_name string, recursive bool) (*srv_tm
if err != nil {
return nil, err
}
if !response.status {
return nil, errors.New(response.body)
}
//Build Service from response
stemplate := &srv_tmpl.ServiceTemplate{}
@ -234,7 +250,7 @@ func (tc *STemplateController) Clone(clone_name string, recursive bool) (*srv_tm
// Permissions operations
// Chgrp template
func (tc *STemplateController) Chgrp(gid int) (bool, error) {
func (tc *STemplateController) Chgrp(gid int) error {
action := make(map[string]interface{})
action["action"] = map[string]interface{}{
@ -248,7 +264,7 @@ func (tc *STemplateController) Chgrp(gid int) (bool, error) {
}
// Chown template
func (tc *STemplateController) Chown(uid, gid int) (bool, error) {
func (tc *STemplateController) Chown(uid, gid int) error {
action := make(map[string]interface{})
action["action"] = map[string]interface{}{
@ -263,7 +279,7 @@ func (tc *STemplateController) Chown(uid, gid int) (bool, error) {
}
// Chmod template
func (tc *STemplateController) Chmod(perm shared.Permissions) (bool, error) {
func (tc *STemplateController) Chmod(perm shared.Permissions) error {
action := make(map[string]interface{})
action["action"] = map[string]interface{}{
@ -278,7 +294,7 @@ func (tc *STemplateController) Chmod(perm shared.Permissions) (bool, error) {
// Helpers
// Action handler for service_templates identified by <id>
func (tc *STemplateController) action(action map[string]interface{}) (bool, error) {
func (tc *STemplateController) action(action map[string]interface{}) error {
url := urlTemplateAction(tc.ID)
return tc.c.boolResponse("POST", url, action)

View File

@ -43,7 +43,7 @@ func createServiceTemplate(t *testing.T) (*srv_tmpl.ServiceTemplate, int) {
},
}
_, err := testCtrl.STemplates().Create(&tmpl)
err := testCtrl.STemplates().Create(&tmpl)
if err != nil {
t.Fatal(err)
}
@ -120,7 +120,7 @@ func TestServiceTemplate(t *testing.T) {
},
},
}
_, err = tmpl_ctrl.Update(&tmpl_update, true)
err = tmpl_ctrl.Update(&tmpl_update, true)
if err != nil {
t.Fatal(err)
}
@ -133,7 +133,7 @@ func TestServiceTemplate(t *testing.T) {
}
// Check the Service is correctly deleted
_, err = tmpl_ctrl.Delete()
err = tmpl_ctrl.Delete()
if err != nil {
t.Errorf("Failure deleting the service template.")
}

View File

@ -44,7 +44,7 @@ func createService(t *testing.T) (*sv.Service, int) {
},
}
_, err := testCtrl.STemplates().Create(&tmpl)
err := testCtrl.STemplates().Create(&tmpl)
if err != nil {
t.Fatal(err)
}
@ -115,8 +115,11 @@ func TestService(t *testing.T) {
}
// Check the Service is correctly deleted
_, err = service_ctrl.Delete()
err = service_ctrl.Delete()
if err != nil {
t.Errorf("Failure deleting the service.")
err = service_ctrl.Recover(true)
if err != nil {
t.Errorf(err.Error())
}
}
}