command

package
v2.8.3+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 26, 2015 License: BSD-3-Clause Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Status codes for os.Exit.
	EXIT_SUCCESS              int = 0
	EXIT_SUCCESS_ERROR        int = 1
	EXIT_AUTHENTICATION_ERROR int = 2
	EXIT_FLAG_ERROR           int = 3
)
View Source
const (
	DEFAULT_FLAG_VALUE string = `!>dUmmy<!`
)

Variables

View Source
var AlertActions = []*Command{
	{
		Name:      "list",
		UsageLine: "alert list [--filter]",
		Long: `
Get all alerts from CoScale Api.

The flags for list alert action are:

Optional:
	--filter
		List actions filtered by unresolved or by unacknowledged.		
`,
		Run: func(cmd *Command, args []string) {
			var filter string
			cmd.Flag.Usage = func() { cmd.PrintUsage() }
			cmd.Flag.StringVar(&filter, "filter", DEFAULT_FLAG_VALUE, "List actions filtered by unresolved or by unacknowledged.")
			cmd.ParseArgs(args)

			switch filter {
			case DEFAULT_FLAG_VALUE:
				cmd.PrintResult(cmd.Capi.GetObjects(alertObjectName))
			case "unresolved":
				cmd.PrintResult(cmd.Capi.GetAlertsBy("selectByResolved"))
			case "unacknowledged":
				cmd.PrintResult(cmd.Capi.GetAlertsBy("selectByAcknowledged"))
			default:
				cmd.PrintUsage()
				os.Exit(EXIT_FLAG_ERROR)
			}
		},
	},
	{
		Name:      "acknowledge",
		UsageLine: "alert acknowledge (--id | --name)",
		Long: `
Acknowledge an alert.

The flags for acknowledge alert action are:
Mandatory:
	--id
		The id of the alert.
	or
	--name
		The name of the alert.
`,
		Run: func(cmd *Command, args []string) {
			var id int64
			cmd.Flag.Usage = func() { cmd.PrintUsage() }
			cmd.Flag.Int64Var(&id, "id", -1, "The id of the alert.")
			cmd.ParseArgs(args)
			var alert = &api.Alert{}
			var err error
			if id == -1 {
				cmd.PrintUsage()
				os.Exit(EXIT_FLAG_ERROR)
			}
			if err = cmd.Capi.GetObjectRef("alert", id, alert); err != nil {
				cmd.PrintResult("", err)
			}
			cmd.PrintResult(cmd.Capi.AlertSolution(alert, "acknowledge"))
		},
	},
	{
		Name:      "resolve",
		UsageLine: "alert resolve (--id | --name)",
		Long: `
Resolve an alert.

The flags for resolve alert action are:
Mandatory:
	--id
		The id of the alert.
	or 
	--name
		The name of the alert.
`,
		Run: func(cmd *Command, args []string) {
			var id int64
			cmd.Flag.Usage = func() { cmd.PrintUsage() }
			cmd.Flag.Int64Var(&id, "id", -1, "The id of the alert.")
			cmd.ParseArgs(args)
			var alert = &api.Alert{}
			var err error
			if id == -1 {
				cmd.PrintUsage()
				os.Exit(EXIT_FLAG_ERROR)
			}
			if err = cmd.Capi.GetObjectRef("alert", id, alert); err != nil {
				cmd.PrintResult("", err)
			}
			cmd.PrintResult(cmd.Capi.AlertSolution(alert, "resolve"))
		},
	},
}
View Source
var AlertObject = NewCommand(alertObjectName, "alert <action> [--<field>='<data>']", AlertActions)
View Source
var CheckObject = &Command{
	Name:      CheckObjectName,
	UsageLine: `check-config is used to check to api configuration file`,
	Run: func(cmd *Command, args []string) {

		file, err := GetConfigPath()
		if err != nil {
			cmd.PrintResult("", err)
		}

		if _, err := os.Stat(file); err != nil {
			cmd.PrintResult("", err)
		}

		config, err := api.ReadApiConfiguration(file)
		if err != nil {
			cmd.PrintResult("", err)
		}

		api := api.NewApi(config.BaseUrl, config.AccessToken, config.AppId, false)
		err = api.Login()
		if err != nil {
			cmd.PrintResult("", err)
		}
		cmd.PrintResult(`{"msg":"Configuration successfuly checked!"}`, nil)
	},
}

CheckObject is the check-config subcommand and is used to check to api configuration

View Source
var CheckObjectName = "check-config"

CheckObjectName is the name of the check-config subcommand

View Source
var DataActions = []*Command{
	{
		Name:      "get",
		UsageLine: `data get (--id --subjectIds) [--start --stop --aggregator --aggregateSubjects]`,
		Long: `
Retrieve a batch of data from the datastore.

The flags for get data action are:
Mandatory:
	--id
		Metric id.
	--subjectIds 
		The subject string eg. s1 for server 1, g2 for servergroup 2, a for application.
Optional:		
	--start
		The start timestamp in seconds ago(negative values) or unix timestamp (positive values). [default: 0]
	--stop
		The stop timestamp in seconds ago(negative values) or unix timestamp (positive values). [default: 0]
	--aggregator
		The data aggregator (AVG, MIN, MAX). [default: AVG]
	--aggregateSubjects
		Boolean that indicates if the aggregated value over all subjectIds should be returned. [default: false]
`,
		Run: func(cmd *Command, args []string) {
			var subjectIds, aggregator string
			var aggregateSubjects bool
			var start, stop int
			var id int64
			cmd.Flag.Usage = func() { cmd.PrintUsage() }
			cmd.Flag.Int64Var(&id, "id", -1, "Unique identifier for metric.")
			cmd.Flag.IntVar(&start, "start", 0, "The start timestamp in seconds ago.")
			cmd.Flag.IntVar(&stop, "stop", 0, "The stop timestamp in seconds ago.")
			cmd.Flag.StringVar(&subjectIds, "subjectIds", DEFAULT_FLAG_VALUE, "The subject string")
			cmd.Flag.StringVar(&aggregator, "aggregator", "AVG", "The data aggregator (AVG, MIN, MAX).")
			cmd.Flag.BoolVar(&aggregateSubjects, "aggregateSubjects", false, "Boolean that indicates if the aggregated value over all subjectIds should be returned.")
			cmd.ParseArgs(args)
			if subjectIds == DEFAULT_FLAG_VALUE || id == -1 {
				cmd.PrintUsage()
				os.Exit(EXIT_FLAG_ERROR)
			}
			cmd.PrintResult(cmd.Capi.GetData(start, stop, id, subjectIds, aggregator, aggregateSubjects))
		},
	},
	{
		Name:      "insert",
		UsageLine: `data insert (--datapoint)`,
		Long: `
Create new EventData for a given event.

The flags for data event action are:
Mandatory:
	--datapoint
		Data format is:"M<metric id>:<subject Id>:<seconds ago>:<value/s>" eg:"M1:S100:120:1,2"
`,
		Run: func(cmd *Command, args []string) {
			var datapoint string
			cmd.Flag.Usage = func() { cmd.PrintUsage() }
			cmd.Flag.StringVar(&datapoint, "datapoint", DEFAULT_FLAG_VALUE, "")
			cmd.ParseArgs(args)
			if datapoint == DEFAULT_FLAG_VALUE {
				cmd.PrintUsage()
				os.Exit(EXIT_FLAG_ERROR)
			}
			data, err := api.ParseDataPoint(datapoint)
			if err != nil {
				cmd.PrintUsage()
				os.Exit(EXIT_FLAG_ERROR)
			}
			cmd.PrintResult(cmd.Capi.InsertData(data))
		},
	},
}
View Source
var DataObject = NewCommand(dataObjectName, "data <action> [--<field>='<data>']", DataActions)
View Source
var EventActions = []*Command{
	ListCmd(eventObjectName),
	GetCmd(eventObjectName),
	DeleteCmd(eventObjectName, &api.Event{}),
	{
		Name:      "new",
		UsageLine: "event new (--name) [--description --attributeDescriptions --source]",
		Long: `
Create new event category.

The flags for new event action are:

Mandatory:
	--name 
		specify name of the event.
Optional:
	--description
		specify the description of the event.
	--attributeDescriptions
		JSON string describing what items the "attribute" of an EventData instance belonging to this Event must have.  [default: "[]"]
	--source
		Describes who added the event. Can be chosen by the user. [default: "cli"]
`,
		Run: func(cmd *Command, args []string) {
			var name, description, attributeDescriptions, source string
			cmd.Flag.Usage = func() { cmd.PrintUsage() }
			cmd.Flag.StringVar(&name, "name", DEFAULT_FLAG_VALUE, "specify the event name of the event")
			cmd.Flag.StringVar(&description, "description", "", "specify the description of the event")
			cmd.Flag.StringVar(&attributeDescriptions, "attributeDescriptions", "[]", "")
			cmd.Flag.StringVar(&source, "source", "cli", "Describes who added the event")
			cmd.ParseArgs(args)

			if name == DEFAULT_FLAG_VALUE {
				cmd.PrintUsage()
				os.Exit(EXIT_FLAG_ERROR)
			}
			cmd.PrintResult(cmd.Capi.CreateEvent(name, description, attributeDescriptions, source, ""))
		},
	},
	{
		Name:      "update",
		UsageLine: "event update (--name | --id) [--description --attributeDescriptions --source]",
		Long: `
Update a CoScale event object.

The flags for update event action are:
The name or id should be specified
	--id
		Unique identifier, if we want to update the name of the event, this become mandatory
	--name 
		specify the event name of the event.
	--description
		specify the description of the event.
	--attributeDescriptions
		JSON string describing what items the "attribute" of an EventData instance belonging to this Event must have.
	--source
		Describes who added the event. Can be chosen by the user. [default: "cli"]
`,
		Run: func(cmd *Command, args []string) {
			var name, description, attributeDescriptions, source string
			var id int64
			cmd.Flag.Usage = func() { cmd.PrintUsage() }
			cmd.Flag.StringVar(&name, "name", DEFAULT_FLAG_VALUE, "specify the event name of the event")
			cmd.Flag.StringVar(&description, "description", DEFAULT_FLAG_VALUE, "specify the description of the event")
			cmd.Flag.StringVar(&attributeDescriptions, "attributeDescriptions", DEFAULT_FLAG_VALUE, "")
			cmd.Flag.StringVar(&source, "source", DEFAULT_FLAG_VALUE, "Describes who added the event")
			cmd.Flag.Int64Var(&id, "id", -1, "Unique identifier")
			cmd.ParseArgs(args)

			var eventObj = &api.Event{}
			var err error
			if id != -1 {
				err = cmd.Capi.GetObjectRef("event", id, eventObj)
			} else if name != DEFAULT_FLAG_VALUE {
				err = cmd.Capi.GetObejctRefByName("event", name, eventObj)
			} else {
				cmd.PrintUsage()
				os.Exit(EXIT_FLAG_ERROR)
			}
			if err != nil {
				cmd.PrintResult("", err)
			}

			if name != DEFAULT_FLAG_VALUE {
				eventObj.Name = name
			}
			if description != DEFAULT_FLAG_VALUE {
				eventObj.Description = description
			}
			if attributeDescriptions != DEFAULT_FLAG_VALUE {
				eventObj.AttributeDescriptions = attributeDescriptions
			}
			if source != DEFAULT_FLAG_VALUE {
				eventObj.Source = source
			}

			cmd.PrintResult(cmd.Capi.UpdateEvent(eventObj))
		},
	},
	{
		Name:      "data",
		UsageLine: "event data (--name --id --message --subject) [--attribute --timestamp --stopTime]",
		Long: `
Insert event data.

The flags for data event action are:
Mandatory:
	--name 
		specify the event name.
	--id
		specify the event id.
	Only one from id/name is neccessary.
		
	--message
		The message for the event data.	
	--subject
		The subject for the event data. The subject is structured as follows:
		s<serverId> for a server, g<servergroupId> for a server group, a for the application.
Optional:	
	--attribute
		JSON String detailing the progress of the event.
	--timestamp
		Timestamp in seconds ago(negative values) or unix timestamp(positive values). [default: 0]
	--stopTime
		The time at which the EventData stopped in seconds ago(negative values) or unix timestamp(positive values). [default: 0]
		`,
		Run: func(cmd *Command, args []string) {
			var id, timestamp, stopTime int64
			var name, message, subject, attribute string
			cmd.Flag.Usage = func() { cmd.PrintUsage() }
			cmd.Flag.StringVar(&name, "name", DEFAULT_FLAG_VALUE, "event name")
			cmd.Flag.StringVar(&message, "message", DEFAULT_FLAG_VALUE, "message for the event data")
			cmd.Flag.StringVar(&subject, "subject", DEFAULT_FLAG_VALUE, "subject for the event data")
			cmd.Flag.StringVar(&attribute, "attribute", "{}", "JSON String detailing the progress of the event")
			cmd.Flag.Int64Var(&id, "id", -1, "Unique identifier")
			cmd.Flag.Int64Var(&timestamp, "timestamp", 0, "Timestamp in seconds ago")
			cmd.Flag.Int64Var(&stopTime, "stopTime", 0, "The time at which the EventData stopped in seconds ago")
			cmd.ParseArgs(args)

			var eventObj = &api.Event{}
			var err error

			flagErr := message == DEFAULT_FLAG_VALUE || subject == DEFAULT_FLAG_VALUE
			if !flagErr {
				if id != -1 {
					err = cmd.Capi.GetObjectRef("event", id, eventObj)
				} else if name != DEFAULT_FLAG_VALUE {
					err = cmd.Capi.GetObejctRefByName("event", name, eventObj)
				} else {
					flagErr = true
				}
			}
			if flagErr {
				cmd.PrintUsage()
				os.Exit(EXIT_FLAG_ERROR)
			}
			if err != nil {
				cmd.PrintResult("", err)
			}
			cmd.PrintResult(cmd.Capi.InsertEventData(eventObj.ID, message, subject, attribute, timestamp, stopTime))
		},
	},
}
View Source
var EventObject = NewCommand(eventObjectName, "event <action> [--<field>='<data>']", EventActions)
View Source
var MetricActions = []*Command{
	ListCmd("metric"),
	{
		Name:      "listbygroup",
		UsageLine: `metric listbygroup (--id | --name)`,
		Long: `
Get all metrics from a metric group

The flags for listbygroup metric action are:

Mandatory:
	--id
		Unique identifier for a metricgroup
	or 
	--name 
		specify the name of the metrigroup.
`,
		Run: func(cmd *Command, args []string) {
			var id int64
			var name string
			cmd.Flag.Usage = func() { cmd.PrintUsage() }
			cmd.Flag.Int64Var(&id, "id", -1, "Unique identifier")
			cmd.Flag.StringVar(&name, "name", DEFAULT_FLAG_VALUE, "Name for the metric group.")

			cmd.ParseArgs(args)

			var metricGroupObj = &api.MetricGroup{}
			var err error
			if id != -1 {
				err = cmd.Capi.GetObjectRef("metricgroup", id, metricGroupObj)
			} else if name != DEFAULT_FLAG_VALUE {
				err = cmd.Capi.GetObejctRefByName("metricgroup", name, metricGroupObj)
			} else {
				cmd.PrintUsage()
				os.Exit(EXIT_FLAG_ERROR)
			}
			if err != nil {
				cmd.PrintResult("", err)
			}

			cmd.PrintResult(cmd.Capi.GetMetricsByGroup(metricGroupObj))
		},
	},
	{
		Name:      "new",
		UsageLine: `metric new (--name --dataType --subject) [--period --description --unit --attachTo --source]`,
		Long: `
Create a new CoScale metric object.

The flags for new metric action are:

Mandatory:
	--name 
		specify the name of the metric.
	--dataType
		The following data types are defined: "LONG", "DOUBLE", "HISTOGRAM".
	--subject
		A metric is defined on either a "SERVER", "GROUP" or "APPLICATION". This allows for metric per server, per server group or on the whole application.
Optional:
	--period
		The amount of time (in seconds) between 2 data points. [default: 60]
	--description
		Description for the metric. [default: ""]
	--unit
		The unit for the metric. This is shown on the axis in the widgets. [default: ""]
	--attachTo
		Describes what the relation of this Metric is. Options are SERVER, SERVERGROUP, APPLICATION, REQUEST, DATABASE, QUERY and ANALYSIS.
	--source
		Describes who added the metric. Can be chosen by the user. [default: "cli"]
`,
		Run: func(cmd *Command, args []string) {
			var name, description, dataType, subject, unit, attachTo, source string
			var period int
			cmd.Flag.Usage = func() { cmd.PrintUsage() }
			cmd.Flag.StringVar(&name, "name", DEFAULT_FLAG_VALUE, "Name for the metric.")
			cmd.Flag.StringVar(&description, "description", "", "Description for the metric.")
			cmd.Flag.StringVar(&dataType, "dataType", DEFAULT_FLAG_VALUE, `The following data types are defined: "LONG", "DOUBLE", "HISTOGRAM".`)
			cmd.Flag.StringVar(&subject, "subject", DEFAULT_FLAG_VALUE, `A metric is defined on either a "SERVER", "GROUP" or "APPLICATION".`)
			cmd.Flag.StringVar(&unit, "unit", "", "The unit for the metric.")
			cmd.Flag.StringVar(&attachTo, "attachTo", "", "Describes what the relation of this Metric is.")
			cmd.Flag.StringVar(&source, "source", "cli", "Describes who added the metric.")
			cmd.Flag.IntVar(&period, "period", 60, "The amount of time (in seconds) between 2 data points.")
			cmd.ParseArgs(args)

			if name == DEFAULT_FLAG_VALUE || dataType == DEFAULT_FLAG_VALUE || subject == DEFAULT_FLAG_VALUE {
				cmd.PrintUsage()
				os.Exit(EXIT_FLAG_ERROR)
			}
			cmd.PrintResult(cmd.Capi.CreateMetric(name, description, dataType, unit, subject, source, period))
		},
	},
	{
		Name:      "update",
		UsageLine: `metric update (--name | --id) [--description --dataType --subject --unit --period --attachTo --source]`,
		Long: `
Update a CoScale metric object.

The flags for new event action are:

Mandatory:
	--name 
		specify the event name of the event.
Optional:
	--id
		Unique identifier, if we want to update the name of a event, this become mandatory
	--description
			Description for the metric.
	--dataType
			The following data types are defined: "LONG", "DOUBLE", "HISTOGRAM".
	--subject
			A metric is defined on either a "SERVER", "GROUP" or "APPLICATION". This allows for metric per server, per server group or on the whole application.
	--unit
			The unit for the metric. This is shown on the axis in the widgets. [default: ""]
	--attachTo
			Describes what the relation of this Metric is. Options are SERVER, SERVERGROUP, APPLICATION, REQUEST, DATABASE, QUERY and ANALYSIS.
	--source
			Describes who added the event. Can be chosen by the user. [default: "cli"]
	--period
			The amount of time (in seconds) between 2 data points. [default: 60]
`,
		Run: func(cmd *Command, args []string) {
			var name, description, dataType, subject, unit, attachTo, source string
			var period int
			var id int64
			cmd.Flag.Usage = func() { cmd.PrintUsage() }
			cmd.Flag.Int64Var(&id, "id", -1, "Unique identifier")
			cmd.Flag.StringVar(&name, "name", DEFAULT_FLAG_VALUE, "Name for the metric.")
			cmd.Flag.StringVar(&description, "description", DEFAULT_FLAG_VALUE, "Description for the metric.")
			cmd.Flag.StringVar(&dataType, "dataType", DEFAULT_FLAG_VALUE, `The following data types are defined: "LONG", "DOUBLE", "HISTOGRAM".`)
			cmd.Flag.StringVar(&subject, "subject", "", `A metric is defined on either a "SERVER", "GROUP" or "APPLICATION".`)
			cmd.Flag.StringVar(&unit, "unit", DEFAULT_FLAG_VALUE, "The unit for the metric.")
			cmd.Flag.StringVar(&attachTo, "attachTo", "", "Describes what the relation of this Metric is.")
			cmd.Flag.StringVar(&source, "source", DEFAULT_FLAG_VALUE, "Describes who added the metric.")
			cmd.Flag.IntVar(&period, "period", -1, "The amount of time (in seconds) between 2 data points.")
			cmd.ParseArgs(args)

			var metricObj = &api.Metric{}
			var err error
			if name == DEFAULT_FLAG_VALUE {
				cmd.PrintUsage()
				os.Exit(EXIT_FLAG_ERROR)
			}
			if id != -1 {
				err = cmd.Capi.GetObjectRef("metric", id, metricObj)
			} else {
				err = cmd.Capi.GetObejctRefByName("metric", name, metricObj)
			}
			if err != nil {
				cmd.PrintResult("", err)
			}

			if name != DEFAULT_FLAG_VALUE {
				metricObj.Name = name
			}
			if dataType != DEFAULT_FLAG_VALUE {
				metricObj.DataType = dataType
			}
			if description != DEFAULT_FLAG_VALUE {
				metricObj.Description = description
			}
			if period != -1 {
				metricObj.Period = period
			}
			if source != DEFAULT_FLAG_VALUE {
				metricObj.Source = source
			}
			if unit != DEFAULT_FLAG_VALUE {
				metricObj.Unit = unit
			}

			cmd.PrintResult(cmd.Capi.UpdateMetric(metricObj))
		},
	},
}
View Source
var MetricGroupActions = []*Command{
	ListCmd("metricgroup"),
	GetCmd("metricgroup"),
	AddObjToGroupCmd("metric", &api.Metric{}, &api.MetricGroup{}),
	DeleteObjFromGroupCmd("metric", &api.Metric{}, &api.MetricGroup{}),
	{
		Name:      "new",
		UsageLine: `servergroup new (--name --subject) [--description --state --source]`,
		Long: `
Create a new CoScale metricgroup object.

The flags for new metricgroup action are:

Mandatory:
	--name 
		Name for the metric group.
	--subject
		The subject type of the metric group. "APPLICATION", "SERVERGROUP" or "SERVER".
Optional:
	--description
		Description for the metric group.
	--type
		Describes the type of metric group.
	--state
		"ENABLED": capturing data, "INACTIVE": not capturing data, "DISABLED": not capturing data and not shown on the dashboard.
	--source
		Describes who added the metric group. Can be chosen by the user. [default: "cli"]
`,
		Run: func(cmd *Command, args []string) {
			var name, description, Type, state, source, subject string
			cmd.Flag.Usage = func() { cmd.PrintUsage() }
			cmd.Flag.StringVar(&name, "name", DEFAULT_FLAG_VALUE, "Name for the metric group.")
			cmd.Flag.StringVar(&description, "description", "", "Description for the metric group.")
			cmd.Flag.StringVar(&Type, "type", "", "Describes the type of metric group.")
			cmd.Flag.StringVar(&subject, "subject", DEFAULT_FLAG_VALUE, `The subject type of the metric group. "APPLICATION", "SERVERGROUP" or "SERVER".`)
			cmd.Flag.StringVar(&state, "state", "ENABLED", `"ENABLED": capturing data, "INACTIVE": not capturing data, "DISABLED": not capturing data and not shown on the dashboard.`)
			cmd.Flag.StringVar(&source, "source", "cli", "Describes who added the metric group.")
			cmd.ParseArgs(args)

			if name == DEFAULT_FLAG_VALUE || subject == DEFAULT_FLAG_VALUE {
				cmd.PrintUsage()
				os.Exit(EXIT_FLAG_ERROR)
			}
			cmd.PrintResult(cmd.Capi.CreateMetricGroup(name, description, Type, state, subject, source))
		},
	},
	{
		Name:      "update",
		UsageLine: `metricgroup update (--name | --id) [--description --type --state --source]`,
		Long: `
Update a CoScale metricgroup object.

The flags for update metricgroup action are:

Mandatory:
	--name 
		Specify the name of the metricgroup.
Optional:
	--id
		Unique identifier, if we want to update the name of the metricgroup, this become mandatory.
	--description
		Description for the metric group.
	--type
		Describes the type of metric group.
	--state
		"ENABLED": capturing data, "INACTIVE": not capturing data, "DISABLED": not capturing data and not shown on the dashboard.
	--source
		Describes who added the metric group. Can be chosen by the user. [default: "cli"]
`,
		Run: func(cmd *Command, args []string) {
			var id int64
			var name, description, Type, state, source string
			cmd.Flag.Usage = func() { cmd.PrintUsage() }
			cmd.Flag.Int64Var(&id, "id", -1, "Unique identifier")
			cmd.Flag.StringVar(&name, "name", DEFAULT_FLAG_VALUE, "Name for the metric group.")
			cmd.Flag.StringVar(&description, "description", DEFAULT_FLAG_VALUE, "Description for the metric group.")
			cmd.Flag.StringVar(&Type, "type", DEFAULT_FLAG_VALUE, "Describes the type of metric group.")
			cmd.Flag.StringVar(&state, "state", DEFAULT_FLAG_VALUE, `"ENABLED": capturing data, "INACTIVE": not capturing data, "DISABLED": not capturing data and not shown on the dashboard.`)
			cmd.Flag.StringVar(&source, "source", DEFAULT_FLAG_VALUE, "Describes who added the metric group.")
			cmd.ParseArgs(args)

			if name == DEFAULT_FLAG_VALUE {
				cmd.PrintUsage()
				os.Exit(EXIT_FLAG_ERROR)
			}

			var metricGroupObj = &api.MetricGroup{}
			var err error
			if name == "" {
				cmd.PrintUsage()
				os.Exit(EXIT_FLAG_ERROR)
			}
			if id != -1 {
				err = cmd.Capi.GetObjectRef("metricgroup", id, metricGroupObj)
			} else {
				err = cmd.Capi.GetObejctRefByName("metricgroup", name, metricGroupObj)
			}
			if err != nil {
				cmd.PrintResult("", err)
			}

			if name != DEFAULT_FLAG_VALUE {
				metricGroupObj.Name = name
			}
			if description != DEFAULT_FLAG_VALUE {
				metricGroupObj.Description = description
			}
			if Type != DEFAULT_FLAG_VALUE {
				metricGroupObj.Type = Type
			}
			if source != DEFAULT_FLAG_VALUE {
				metricGroupObj.Source = source
			}
			if state != DEFAULT_FLAG_VALUE {
				metricGroupObj.State = state
			}
			cmd.PrintResult(cmd.Capi.UpdateMetricGroup(metricGroupObj))
		},
	},
}
View Source
var MetricGroupObject = NewCommand("metricgroup", "metricgroup <action> [--<field>='<data>']", MetricGroupActions)
View Source
var MetricObject = NewCommand("metric", "metric <action> [--<field>='<data>']", MetricActions)
View Source
var ServerActions = []*Command{
	ListCmd("server"),
	GetCmd("server"),
	DeleteCmd("server", &api.Server{}),
	{
		Name:      "new",
		UsageLine: "server new (--name) [--description --serverType --source]",
		Long: `
Create a new CoScale server object.

The flags for new server action are:

Mandatory:
	--name 
		Name for the server.
Optional:
	--description
		Description for the server.
	--serverType
		Describes the type of server.
	--source
		Describes who added the server. Can be chosen by the user. [default: "cli"]
`,
		Run: func(cmd *Command, args []string) {
			var name, description, serverType, source string
			cmd.Flag.Usage = func() { cmd.PrintUsage() }
			cmd.Flag.StringVar(&name, "name", DEFAULT_FLAG_VALUE, "Name for the server.")
			cmd.Flag.StringVar(&description, "description", "", "Description for the server.")
			cmd.Flag.StringVar(&serverType, "serverType", "", "Describes the type of server.")
			cmd.Flag.StringVar(&source, "source", "cli", "Describes who added the server.")
			cmd.ParseArgs(args)

			if name == DEFAULT_FLAG_VALUE {
				cmd.PrintUsage()
				os.Exit(EXIT_FLAG_ERROR)
			}
			cmd.PrintResult(cmd.Capi.CreateServer(name, description, serverType, source))
		},
	},
	{
		Name:      "update",
		UsageLine: "server update (--name | --id) [--description --serverType --state --source]",
		Long: `
Update a CoScale server object.

The flags for update server action are:
The name or id should be specified
	--id
		Unique identifier, if we want to update the name of the server, this become mandatory
	--name 
		specify the name of the server.
	--description
		Description for the server.
	--serverType
		Describes the type of server.
	--source
		Describes who added the server. Can be chosen by the user. [default: "cli"]
	--state
	 	"ENABLED": capturing data, "INACTIVE": not capturing data, "DISABLED": not capturing data and not shown on the dashboard.
`,
		Run: func(cmd *Command, args []string) {
			var name, description, Type, source, state string
			var id int64
			cmd.Flag.Usage = func() { cmd.PrintUsage() }
			cmd.Flag.StringVar(&name, "name", DEFAULT_FLAG_VALUE, "Name for the server.")
			cmd.Flag.StringVar(&description, "description", DEFAULT_FLAG_VALUE, "Description for the server.")
			cmd.Flag.StringVar(&Type, "type", DEFAULT_FLAG_VALUE, "Describes the type of server.")
			cmd.Flag.StringVar(&source, "source", DEFAULT_FLAG_VALUE, "Describes who added the server.")
			cmd.Flag.StringVar(&state, "state", DEFAULT_FLAG_VALUE, `"ENABLED": capturing data, "INACTIVE": not capturing data, "DISABLED": not capturing data and not shown on the dashboard.`)
			cmd.Flag.Int64Var(&id, "id", -1, "Unique identifier")
			cmd.ParseArgs(args)

			var serverObj = &api.Server{}
			var err error
			if id != -1 {
				err = cmd.Capi.GetObjectRef("server", id, serverObj)
			} else if name != DEFAULT_FLAG_VALUE {
				err = cmd.Capi.GetObejctRefByName("server", name, serverObj)
			} else {
				cmd.PrintUsage()
				os.Exit(EXIT_FLAG_ERROR)
			}
			if err != nil {
				cmd.PrintResult("", err)
			}

			if name != DEFAULT_FLAG_VALUE {
				serverObj.Name = name
			}
			if description != DEFAULT_FLAG_VALUE {
				serverObj.Description = description
			}
			if Type != DEFAULT_FLAG_VALUE {
				serverObj.Type = Type
			}
			if source != DEFAULT_FLAG_VALUE {
				serverObj.Source = source
			}
			if state != DEFAULT_FLAG_VALUE {
				serverObj.State = state
			}

			cmd.PrintResult(cmd.Capi.UpdateServer(serverObj))
		},
	},
}
View Source
var ServerGroupActions = []*Command{
	ListCmd("servergroup"),
	GetCmd("servergroup"),
	DeleteCmd("servergroup", &api.ServerGroup{}),
	{
		Name:      "new",
		UsageLine: `servergroup new (--name) [--description --type --state --source]`,
		Long: `
Create a new CoScale servergroup object.

The flags for new servergroup action are:

Mandatory:
	--name 
		Name for the server group.
Optional:
	--description
		Description for the server group.
	--type
		Describes the type of server group.
	--state
		"ENABLED": capturing data, "INACTIVE": not capturing data, "DISABLED": not capturing data and not shown on the dashboard.	
	--source
		Describes who added the server group. Can be chosen by the user. [default: "cli"]
`,
		Run: func(cmd *Command, args []string) {
			var name, description, Type, state, source string
			cmd.Flag.Usage = func() { cmd.PrintUsage() }
			cmd.Flag.StringVar(&name, "name", DEFAULT_FLAG_VALUE, "Name for the server group.")
			cmd.Flag.StringVar(&description, "description", "", "Description for the server group.")
			cmd.Flag.StringVar(&Type, "type", "", "Describes the type of server group.")
			cmd.Flag.StringVar(&state, "state", "", `"ENABLED": capturing data, "INACTIVE": not capturing data, "DISABLED": not capturing data and not shown on the dashboard.`)
			cmd.Flag.StringVar(&source, "source", "cli", "Describes who added the server group.")
			cmd.ParseArgs(args)

			if name == DEFAULT_FLAG_VALUE {
				cmd.PrintUsage()
				os.Exit(EXIT_FLAG_ERROR)
			}
			cmd.PrintResult(cmd.Capi.CreateServerGroup(name, description, Type, state, source))
		},
	},
	{
		Name:      "update",
		UsageLine: `servergroup update (--name | --id) [--description --type --state --source]`,
		Long: `
Update a CoScale servergroup object.

The flags for update servergroup action are:
The name or id should be specified
	--id
		Unique identifier, if we want to update the name of the servergroup, this become mandatory
	--name 
		Name for the server group.
	--description
		Description for the server group.
	--type
		Describes the type of server group.
	--state
		"ENABLED": capturing data, "INACTIVE": not capturing data, "DISABLED": not capturing data and not shown on the dashboard.	
	--source
		Describes who added the server group. Can be chosen by the user. [default: "cli"]
`,
		Run: func(cmd *Command, args []string) {
			var name, description, Type, source, state string
			var id int64
			cmd.Flag.Usage = func() { cmd.PrintUsage() }
			cmd.Flag.Int64Var(&id, "id", -1, "Unique identifier.")
			cmd.Flag.StringVar(&name, "name", DEFAULT_FLAG_VALUE, "Name for the server group.")
			cmd.Flag.StringVar(&description, "description", DEFAULT_FLAG_VALUE, "Description for the server group.")
			cmd.Flag.StringVar(&Type, "type", DEFAULT_FLAG_VALUE, "Describes the type of server group.")
			cmd.Flag.StringVar(&state, "state", DEFAULT_FLAG_VALUE, `"ENABLED": capturing data, "INACTIVE": not capturing data, "DISABLED": not capturing data and not shown on the dashboard.`)
			cmd.Flag.StringVar(&source, "source", DEFAULT_FLAG_VALUE, "Describes who added the server. Can be chosen by the user.")
			cmd.ParseArgs(args)

			var serverGroupObj = &api.ServerGroup{}
			var err error
			if id != -1 {
				err = cmd.Capi.GetObjectRef("servergroup", id, serverGroupObj)
			} else if name != DEFAULT_FLAG_VALUE {
				err = cmd.Capi.GetObejctRefByName("servergroup", name, serverGroupObj)
			} else {
				cmd.PrintUsage()
				os.Exit(EXIT_FLAG_ERROR)
			}
			if err != nil {
				cmd.PrintResult("", err)
			}

			if name != DEFAULT_FLAG_VALUE {
				serverGroupObj.Name = name
			}
			if description != DEFAULT_FLAG_VALUE {
				serverGroupObj.Description = description
			}
			if Type != DEFAULT_FLAG_VALUE {
				serverGroupObj.Type = Type
			}
			if source != DEFAULT_FLAG_VALUE {
				serverGroupObj.Source = source
			}
			if state != DEFAULT_FLAG_VALUE {
				serverGroupObj.State = state
			}

			cmd.PrintResult(cmd.Capi.UpdateServerGroup(serverGroupObj))
		},
	},
	AddObjToGroupCmd("server", &api.Server{}, &api.ServerGroup{}),
	DeleteObjFromGroupCmd("server", &api.Server{}, &api.ServerGroup{}),
}
View Source
var ServerGroupObject = NewCommand("servergroup", "servergroup <action> [--<field>='<data>']", ServerGroupActions)
View Source
var ServerObject = NewCommand("server", "server <action> [--<field>='<data>']", ServerActions)

Functions

func GetCommandOutput

func GetCommandOutput(command string, timeout time.Duration, arg ...string) ([]byte, error)

GetCommandOutput returns stdout of command as a string

func GetConfigPath

func GetConfigPath() (c string, e error)

GetConfigPath is used to return the absolut path of the api configuration file

func GetErrorJson

func GetErrorJson(err error) string

GetErrorJson return only the json string from a error message from api

Types

type Command

type Command struct {
	Name        string
	UsageLine   string
	Long        string
	SubCommands []*Command
	Capi        *api.Api //api connector
	Flag        flag.FlagSet
	Run         func(cmd *Command, args []string)
}

func AddObjToGroupCmd

func AddObjToGroupCmd(objectName string, object api.Object, group api.Object) *Command

func DeleteCmd

func DeleteCmd(objectName string, object api.Object) *Command

func DeleteObjFromGroupCmd

func DeleteObjFromGroupCmd(objectName string, object api.Object, group api.Object) *Command

func GetCmd

func GetCmd(objectName string) *Command

func ListCmd

func ListCmd(objectName string) *Command

func NewCommand

func NewCommand(name, usage string, subCommands []*Command) *Command

func (*Command) GetAllSubCommands

func (c *Command) GetAllSubCommands() []*Command

func (*Command) GetApi

func (c *Command) GetApi(baseUrl, accessToken, appId string, rawOutput bool) *api.Api

GetApi returns a Api object

func (*Command) GetSubCommand

func (c *Command) GetSubCommand(args []string) *Command

func (*Command) ParseArgs

func (c *Command) ParseArgs(args []string)

func (*Command) PrintResult

func (c *Command) PrintResult(result string, err error)

func (*Command) PrintUsage

func (c *Command) PrintUsage()

func (*Command) Runnable

func (c *Command) Runnable() bool

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL