Skip to content

Commit

Permalink
Added get server group by path (#14)
Browse files Browse the repository at this point in the history
* added get server group by path
  • Loading branch information
cristianciutea authored and fryckbos committed Jun 11, 2018
1 parent 7c23937 commit 66cf741
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 51 deletions.
45 changes: 36 additions & 9 deletions src/coscale/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,21 +129,25 @@ type Api struct {
// AccessToken is a UUID giving access permissions on the application.
AccessToken string
// AppID is a UUID defining the application.
AppID string
rawOutput bool
token string
AppID string
rawOutput bool
token string
// Set aditional query parameters.
query string
validConfig bool
// Print aditional util information.
verbose bool
}

// NewApi creates a new Api connector using an email and a password.
func NewApi(baseUrl string, accessToken string, appID string, rawOutput bool) *Api {
api := &Api{baseUrl, accessToken, appID, rawOutput, "", true}
func NewApi(baseUrl string, accessToken string, appID string, rawOutput, verbose bool) *Api {
api := &Api{baseUrl, accessToken, appID, rawOutput, "", "", true, verbose}
return api
}

// NewFakeApi creates a new Api connector using an email and a password.
func NewFakeApi() *Api {
api := &Api{"", "", "", true, "", false}
api := &Api{"", "", "", true, "", "", false, false}
return api
}

Expand Down Expand Up @@ -185,9 +189,21 @@ type RequestErrorResponse struct {

// Do an http request.
func (api *Api) doHttpRequest(method string, uri string, token string, data map[string][]string, timeout time.Duration) ([]byte, error) {
if method == "GET" && len(api.query) > 0 {
if strings.ContainsAny(uri, "?") {
uri = fmt.Sprintf("%s&%s", uri, api.query)
} else {
uri = fmt.Sprintf("%s?%s", uri, api.query)
}
}
requestBody := url.Values(data).Encode()
req, err := http.NewRequest(method, uri, strings.NewReader(requestBody))

// Print the requested url.
if api.verbose {
fmt.Println(method, uri)
}

if err != nil {
return nil, err
}
Expand Down Expand Up @@ -236,6 +252,12 @@ func (api *Api) doHttpRequest(method string, uri string, token string, data map[
return body, nil
}

// SetQueryString set aditional query parameters only for the next call.
func (api *Api) SetQueryString(query string) {
// URL Encoded.
api.query = query
}

// LoginData contains the required fields for the login API function.
type LoginData struct {
// Token should contain the AccessToken.
Expand Down Expand Up @@ -300,22 +322,27 @@ func (api *Api) makeCall(method string, uri string, data map[string][]string, js
if err != nil {
return err
}
return api.HandleResponse(b, jsonOut, target)
}

// HandleResponse is used the Handle the response from the API.
func (api *Api) HandleResponse(response []byte, jsonOut bool, target interface{}) error {
if target != nil {
if jsonOut {
//if the output will be json, check if we need to format it or no
var result string
if api.rawOutput {
result = string(b)
result = string(response)
} else {
var out bytes.Buffer
json.Indent(&out, b, "", " ")
json.Indent(&out, response, "", " ")
result = out.String()
}
if t, ok := target.(*string); ok {
*t = result
}
} else {
return json.Unmarshal(b, target)
return json.Unmarshal(response, target)
}
}
return nil
Expand Down
8 changes: 4 additions & 4 deletions src/coscale/api/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ func (api *Api) GetObjectByName(objectName string, name string) (string, error)
return result, nil
}

// GetObejctRefByName will put in result a reference to the oject specified by objectName and name
func (api *Api) GetObejctRefByName(objectName string, name string, result Object) error {
// GetObjectRefByName will put in result a reference to the oject specified by objectName and name
func (api *Api) GetObjectRefByName(objectName string, name string, result Object) error {
// URL Encoded.
name = url.QueryEscape(name)

Expand All @@ -79,8 +79,8 @@ func (api *Api) GetObejctRefByName(objectName string, name string, result Object
return nil
}

// GetObejctRefByNameFromGroup will return the object specified by objectName from objectGroup that have a certain name
func (api *Api) GetObejctRefByNameFromGroup(objectGroup, objectName string, groupID int64, name string, result Object) error {
// GetObjectRefByNameFromGroup will return the object specified by objectName from objectGroup that have a certain name
func (api *Api) GetObjectRefByNameFromGroup(objectGroup, objectName string, groupID int64, name string, result Object) error {
// URL Encoded.
name = url.QueryEscape(name)

Expand Down
43 changes: 43 additions & 0 deletions src/coscale/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"fmt"
"strconv"
"strings"
)

// Server describes the server object on the API.
Expand Down Expand Up @@ -83,6 +84,48 @@ func (api *Api) UpdateServer(server *Server) (string, error) {
return api.GetObject("server", server.ID)
}

// GetServerGroupByPath returns a server group by the hierarchy.
func (api *Api) GetServerGroupByPath(path string) (string, error) {

var serverGroup *ServerGroup

groupNames := strings.Split(path, "/")
for i, groupName := range groupNames {

var query string

if i == 0 {
query = "selectByRoot=true"
} else if serverGroup != nil && serverGroup.ID > 0 {
query = fmt.Sprintf("selectByParent_id=%d", serverGroup.ID)
} else {
return "[]", nil
}

api.SetQueryString(query)
result, err := api.GetObjectByName("servergroup", groupName)
if err != nil {
return "", err
}
// For the last group just return it.
if i == len(groupNames)-1 {
return result, nil
}

tmp := make([]*ServerGroup, 1)
err = api.HandleResponse([]byte(result), false, &tmp)
if err != nil {
return "", err
}
if len(tmp) != 1 {
return "[]", nil
}
serverGroup = tmp[0]
}

return "[]", nil
}

// CreateServerGroup creates a new ServerGroup using the API.
func (api *Api) CreateServerGroup(name, description, Type, state string, parentID int64) (string, error) {
data := map[string][]string{
Expand Down
26 changes: 13 additions & 13 deletions src/coscale/command/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ Optional:
if id != -1 {
err = cmd.Capi.GetObjectRef("alerttype", id, alertTypeObj)
} else if name != DEFAULT_STRING_FLAG_VALUE {
err = cmd.Capi.GetObejctRefByName("alerttype", name, alertTypeObj)
err = cmd.Capi.GetObjectRefByName("alerttype", name, alertTypeObj)
} else {
cmd.PrintUsage()
os.Exit(EXIT_FLAG_ERROR)
Expand Down Expand Up @@ -357,7 +357,7 @@ Mandatory:
if id != -1 {
cmd.PrintResult(cmd.Capi.GetTriggers(id))
} else if name != DEFAULT_STRING_FLAG_VALUE {
err = cmd.Capi.GetObejctRefByName("alerttype", name, alertTypeObj)
err = cmd.Capi.GetObjectRefByName("alerttype", name, alertTypeObj)
} else {
cmd.PrintUsage()
os.Exit(EXIT_FLAG_ERROR)
Expand Down Expand Up @@ -449,7 +449,7 @@ Optional:
var metricObj = &api.Metric{}
var err error
if metricID == -1 {
err = cmd.Capi.GetObejctRefByName("metric", metric, metricObj)
err = cmd.Capi.GetObjectRefByName("metric", metric, metricObj)
if err != nil {
cmd.PrintResult("", err)
}
Expand All @@ -460,7 +460,7 @@ Optional:
// Get the server id
var serverObj = &api.Server{}
if serverID == -1 && server != DEFAULT_STRING_FLAG_VALUE {
err = cmd.Capi.GetObejctRefByName("server", server, serverObj)
err = cmd.Capi.GetObjectRefByName("server", server, serverObj)
if err != nil {
cmd.PrintResult("", err)
}
Expand All @@ -471,7 +471,7 @@ Optional:
// Get the servergroup id
var serverGroupObj = &api.ServerGroup{}
if serverGroupID == -1 && serverGroup != DEFAULT_STRING_FLAG_VALUE {
err = cmd.Capi.GetObejctRefByName("servergroup", serverGroup, serverGroupObj)
err = cmd.Capi.GetObjectRefByName("servergroup", serverGroup, serverGroupObj)
if err != nil {
cmd.PrintResult("", err)
}
Expand All @@ -485,7 +485,7 @@ Optional:
// get the alert type for the trigger
var alertTypeObj = &api.AlertType{}
if typeID == -1 {
err = cmd.Capi.GetObejctRefByName("alerttype", typeName, alertTypeObj)
err = cmd.Capi.GetObjectRefByName("alerttype", typeName, alertTypeObj)
if err != nil {
cmd.PrintResult("", err)
}
Expand Down Expand Up @@ -580,7 +580,7 @@ Optional:
var metricObj = &api.Metric{}
var err error
if metricID == -1 && metric != DEFAULT_STRING_FLAG_VALUE {
err = cmd.Capi.GetObejctRefByName("metric", metric, metricObj)
err = cmd.Capi.GetObjectRefByName("metric", metric, metricObj)
if err != nil {
cmd.PrintResult("", err)
}
Expand All @@ -591,7 +591,7 @@ Optional:
// Get the server id
var serverObj = &api.Server{}
if serverID == -1 && server != DEFAULT_STRING_FLAG_VALUE {
err = cmd.Capi.GetObejctRefByName("server", server, serverObj)
err = cmd.Capi.GetObjectRefByName("server", server, serverObj)
if err != nil {
cmd.PrintResult("", err)
}
Expand All @@ -602,7 +602,7 @@ Optional:
// Get the servergroup id
var serverGroupObj = &api.ServerGroup{}
if serverGroupID == -1 && serverGroup != DEFAULT_STRING_FLAG_VALUE {
err = cmd.Capi.GetObejctRefByName("servergroup", serverGroup, serverGroupObj)
err = cmd.Capi.GetObjectRefByName("servergroup", serverGroup, serverGroupObj)
if err != nil {
cmd.PrintResult("", err)
}
Expand All @@ -613,7 +613,7 @@ Optional:
// get the alert type for the trigger
var alertTypeObj = &api.AlertType{}
if typeID == -1 {
err = cmd.Capi.GetObejctRefByName("alerttype", typeName, alertTypeObj)
err = cmd.Capi.GetObjectRefByName("alerttype", typeName, alertTypeObj)
if err != nil {
cmd.PrintUsage()
os.Exit(EXIT_FLAG_ERROR)
Expand All @@ -627,7 +627,7 @@ Optional:
if id != -1 {
err = cmd.Capi.GetObjectRefFromGroup("alerttype", "trigger", typeID, id, alertTriggerObj)
} else if name != DEFAULT_STRING_FLAG_VALUE {
err = cmd.Capi.GetObejctRefByNameFromGroup("alerttype", "trigger", typeID, name, alertTriggerObj)
err = cmd.Capi.GetObjectRefByNameFromGroup("alerttype", "trigger", typeID, name, alertTriggerObj)
} else {
cmd.PrintUsage()
os.Exit(EXIT_FLAG_ERROR)
Expand Down Expand Up @@ -713,7 +713,7 @@ Mandatory:
// get the alert type for the trigger
var alertTypeObj = &api.AlertType{}
if typeID == -1 {
err = cmd.Capi.GetObejctRefByName("alerttype", Type, alertTypeObj)
err = cmd.Capi.GetObjectRefByName("alerttype", Type, alertTypeObj)
if err != nil {
cmd.PrintResult("", err)
}
Expand All @@ -724,7 +724,7 @@ Mandatory:
// Get the existing trigger.
var alertTriggerObj = &api.AlertTrigger{}
if id == -1 {
err = cmd.Capi.GetObejctRefByNameFromGroup("alerttype", "trigger", typeID, name, alertTriggerObj)
err = cmd.Capi.GetObjectRefByNameFromGroup("alerttype", "trigger", typeID, name, alertTriggerObj)
if err != nil {
cmd.PrintResult("", err)
}
Expand Down
2 changes: 1 addition & 1 deletion src/coscale/command/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var CheckObject = &Command{
cmd.PrintResult("", err)
}
// check if we can loggin with this configuration
api := api.NewApi(config.BaseUrl, config.AccessToken, config.AppId, false)
api := api.NewApi(config.BaseUrl, config.AccessToken, config.AppId, false, false)
err = api.Login()
if err != nil {
cmd.PrintResult("", err)
Expand Down
16 changes: 11 additions & 5 deletions src/coscale/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (c *Command) PrintFullUsage() {
}

// GetApi returns a Api object
func (c *Command) GetApi(baseUrl, accessToken, appId string, rawOutput bool) *api.Api {
func (c *Command) GetApi(baseUrl, accessToken, appId string, rawOutput, verbose bool) *api.Api {
if accessToken == "" || appId == "" {
configPath, err := GetConfigPath()
if err != nil {
Expand All @@ -145,25 +145,27 @@ func (c *Command) GetApi(baseUrl, accessToken, appId string, rawOutput bool) *ap
accessToken = config.AccessToken
appId = config.AppId
}
return api.NewApi(baseUrl, accessToken, appId, rawOutput)
return api.NewApi(baseUrl, accessToken, appId, rawOutput, verbose)
}

// ParseArgs takes the API configuration from the args and stores them in the Command.
func (c *Command) ParseArgs(args []string) {
//add the flags for the api configuration
var baseUrl, accessToken, appId string
var rawOutput bool
var rawOutput, verbose bool
c.Flag.StringVar(&baseUrl, "api-url", "https://api.coscale.com", "Base url for the api.")
c.Flag.StringVar(&appId, "app-id", "", "The application id.")
c.Flag.StringVar(&accessToken, "access-token", "", "A valid access token for the given application.")
c.Flag.BoolVar(&rawOutput, "rawOutput", false, "The returned json objects are returned formatted by default.")
c.Flag.BoolVar(&verbose, "verbose", false, "Print the URLs of the API calls.")

c.Flag.Parse(args)
unknownArgs := c.Flag.Args()
if len(unknownArgs) > 0 && unknownArgs[0] != "help" {
fmt.Fprintf(os.Stderr, "Unknown field %s\n", unknownArgs[0])
os.Exit(EXIT_FLAG_ERROR)
}
c.Capi = c.GetApi(strings.Trim(baseUrl, "/"), accessToken, appId, rawOutput)
c.Capi = c.GetApi(strings.Trim(baseUrl, "/"), accessToken, appId, rawOutput, verbose)
}

// PrintResult formats the result or error and exits the process with the appropriate exit code.
Expand Down Expand Up @@ -206,14 +208,18 @@ the credentials can also be provided on the command line using:
The application id.
--access-token
A valid access token for the given application.
--verbose
Print the URLs of the API calls.
`

var usageTemplate = `coscale-cli a tool for CoScale Api.
Usage:
{{.UsageLine}}
{{if .Runnable}}
{{.Name | printf "Action \"%s\" usage:"}}
{{.Name | printf "Action \"%s\" usage:"}}
{{.Long | trim}}{{else}}
{{.Name | printf "Actions for command \"%s\":"}}
Expand Down
Loading

0 comments on commit 66cf741

Please sign in to comment.