Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
added more detailed errors
Browse files Browse the repository at this point in the history
  • Loading branch information
candysmurf committed Mar 31, 2017
1 parent 92adf0d commit 630463c
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 61 deletions.
100 changes: 100 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,40 @@ get get details on a single metric
help, h Shows a list of commands or help for one command
```

#### task
```
$ snaptel task command [command options] [arguments...]
```
```
create There are two ways to create a task.
1) Use a task manifest with [--task-manifest, t]
2) Provide a workflow manifest and schedule details [--workflow-manifest, -w]
--task-manifest value, -t value File path for task manifest to use for task creation.
--workflow-manifest value, -w value File path for workflow manifest to use for task creation
--interval value, -i value Interval for the task schedule [ex (simple schedule): 250ms, 1s, 30m (cron schedule): "0 * * * * *"]
--count value The count of runs for the task schedule [defaults to 0 what means no limit, e.g. set to 1 determines a single run task]
--start-date value Start date for the task schedule [defaults to today]
--start-time value Start time for the task schedule [defaults to now]
--stop-date value Stop date for the task schedule [defaults to today]
--stop-time value Start time for the task schedule [defaults to now]
--name value, -n value Optional requirement for giving task names
--duration value, -d value The amount of time to run the task [appends to start or creates a start time before a stop]
--no-start Do not start task on creation [normally started on creation]
--deadline value The deadline for the task to be killed after started if the task runs too long (All tasks default to 5s)
--max-failures value The number of consecutive failures before Snap disables the task
* Note: Start and stop date/time are optional.
list list
start start <task_id>
stop stop <task_id>
remove remove <task_id>
export export <task_id>
watch watch <task_id>
enable enable <task_id>
help, h Shows a list of commands or help for one command
```

Example Usage
-------------

Expand All @@ -76,6 +110,68 @@ In one terminal window, run snapteld (log level is set to 1 and signing is turne
$ snapteld --log-level 1 --log-path '' --plugin-trust 0
```

prepare a task manifest file, for example, task.json with following content:

```json
{
"version": 1,
"name": "sample",
"schedule": {
"type": "simple",
"interval": "15s"
},
"workflow": {
"collect": {
"metrics": {
"/intel/mock/foo": {},
"/intel/mock/bar": {},
"/intel/mock/*/baz": {}
},
"config": {
"/intel/mock": {
"user": "root",
"password": "secret"
}
},
"process": null,
"publish": [
{
"plugin_name": "file",
"config": {
"file": "/tmp/collected_swagger"
}
}
]
}
}
}
```

prepare a workflow manifest file, for example, workflow.json with the following content:
```json
{
"collect": {
"metrics": {
"/intel/mock/foo": {}
},
"config": {
"/intel/mock/foo": {
"password": "testval"
}
},
"process": [],
"publish": [
{
"plugin_name": "file",
"config": {
"file": "/tmp/rest.test"
}
}
]
}
}
```

and then in another terminal:

1. load a collector plugin
Expand All @@ -99,6 +195,10 @@ $ snaptel metric list --verbose
$ snaptel metric get -m /intel/mock/foo
$ snaptel metric get -m /intel/mock/foo -v <version>
$ snaptel plugin config get collector:mock:<version>
$ snaptel task create -t mock-file.json
$ snaptel task create -w workflow.json -i 1s
$ snaptel task list
$ snaptel task watch <task_id>
$ snaptel plugin unload processor passthru <version>
$ snaptel plugin unload publisher publisher <version>
```
Expand Down
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (

"github.com/golang/glog"
"github.com/intelsdi-x/snap-cli/snaptel"
"github.com/intelsdi-x/snap-client-go/snap"
"github.com/intelsdi-x/snap-client-go/client"
"github.com/urfave/cli"
)

Expand Down Expand Up @@ -66,7 +66,8 @@ func beforeAction(ctx *cli.Context) error {
glog.Fatal(err)
}

snaptel.SetClient(snap.New(snap.ClientParams{URL: u.Host, APIVer: snaptel.FlAPIVer.Value, Scheme: u.Scheme}))
c := client.NewHTTPClientWithConfig(nil, &client.TransportConfig{Host: u.Host, BasePath: snaptel.FlAPIVer.Value, Schemes: []string{u.Scheme}})
snaptel.SetClient(c)

return nil
}
Expand Down
2 changes: 1 addition & 1 deletion snaptel/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
http://www.apache.org/licenses/LICENSE-2.0.txt
Copyright 2015-2017 Intel Corporation
Copyright 2017 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
54 changes: 50 additions & 4 deletions snaptel/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@ package snaptel
import (
"fmt"

"github.com/intelsdi-x/snap-cli/snap"
snapClient "github.com/intelsdi-x/snap-client-go/client"
"github.com/intelsdi-x/snap-client-go/client/snap"
"github.com/urfave/cli"
)

const (
errNoClient = "Error: no Client is created"
)

var snapClient *snap.Client
var client *snapClient.Snap

type UsageError struct {
s string
Expand All @@ -50,8 +51,8 @@ func newUsageError(s string, ctx *cli.Context) UsageError {
}

// SetClient provides a way to set the private snapClient in this package.
func SetClient(c *snap.Client) {
snapClient = c
func SetClient(cl *snapClient.Snap) {
client = cl
}

// GetFirstChar gets the first character of a giving string.
Expand All @@ -63,3 +64,48 @@ func GetFirstChar(s string) string {
}
return firstChar
}

func getErrorDetail(err error) error {
switch err.(type) {
case *snap.GetMetricsNotFound:
return fmt.Errorf("Error getting metrics: %v", err.(*snap.GetMetricsNotFound).Payload.Message)
case *snap.GetMetricsInternalServerError:
return fmt.Errorf("Error getting metrics: %v", err.(*snap.GetMetricsInternalServerError).Payload.Message)
case *snap.LoadPluginBadRequest:
return fmt.Errorf("Error loading plugin: %v", err.(*snap.LoadPluginBadRequest).Payload.Message)
case *snap.LoadPluginConflict:
return fmt.Errorf("Error loading plugin: %v", err.(*snap.LoadPluginConflict).Payload.Message)
case *snap.LoadPluginInternalServerError:
return fmt.Errorf("Error loading plugin: %v", err.(*snap.LoadPluginInternalServerError).Payload.Message)
case *snap.LoadPluginUnsupportedMediaType:
return fmt.Errorf("Error loading plugin: %v", err.(*snap.LoadPluginUnsupportedMediaType).Payload.Message)
case *snap.UnloadPluginBadRequest:
return fmt.Errorf("Error unloading plugin: %v", err.(*snap.UnloadPluginBadRequest).Payload.Message)
case *snap.UnloadPluginConflict:
return fmt.Errorf("Error unloading plugin: %v", err.(*snap.UnloadPluginConflict).Payload.Message)
case *snap.UnloadPluginInternalServerError:
return fmt.Errorf("Error unloading plugin: %v", err.(*snap.UnloadPluginInternalServerError).Payload.Message)
case *snap.UnloadPluginNotFound:
return fmt.Errorf("Error unloading plugin: %v", err.(*snap.UnloadPluginNotFound).Payload.Message)
case *snap.GetPluginBadRequest:
return fmt.Errorf("Error getting plugin: %v", err.(*snap.GetPluginBadRequest).Payload.Message)
case *snap.GetPluginInternalServerError:
return fmt.Errorf("Error getting plugin: %v", err.(*snap.GetPluginInternalServerError).Payload.Message)
case *snap.GetPluginNotFound:
return fmt.Errorf("Error getting plugin: %v", err.(*snap.GetPluginNotFound).Payload.Message)
case *snap.GetPluginConfigItemBadRequest:
return fmt.Errorf("Error getting plugin config item: %v", err.(*snap.GetPluginConfigItemBadRequest).Payload.Message)
case *snap.GetTaskNotFound:
return fmt.Errorf("Error getting task: %v", err.(*snap.GetTaskNotFound).Payload.Message)
case *snap.AddTaskInternalServerError:
return fmt.Errorf("Error creating task: %v", err.(*snap.AddTaskInternalServerError).Payload.Message)
case *snap.UpdateTaskStateBadRequest:
return fmt.Errorf("Error updating task: %v", err.(*snap.UpdateTaskStateBadRequest).Payload.Message)
case *snap.UpdateTaskStateConflict:
return fmt.Errorf("Error updating task: %v", err.(*snap.UpdateTaskStateConflict).Payload.Message)
case *snap.UpdateTaskStateInternalServerError:
return fmt.Errorf("Error updating task: %v", err.(*snap.UpdateTaskStateInternalServerError).Payload.Message)
default:
return fmt.Errorf("Error: %v", err)
}
}
6 changes: 3 additions & 3 deletions snaptel/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
http://www.apache.org/licenses/LICENSE-2.0.txt
Copyright 2015-2017 Intel Corporation
Copyright 2017 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -27,7 +27,7 @@ import (
"strconv"
"text/tabwriter"

"github.com/intelsdi-x/snap-cli/snap"
"github.com/intelsdi-x/snap-client-go/client/snap"
"github.com/urfave/cli"
)

Expand Down Expand Up @@ -68,7 +68,7 @@ func getConfig(ctx *cli.Context) error {
params.SetPname(pname)
params.SetPversion(int64(pver))

resp, err := snapClient.GetPluginConfigItem(params)
resp, err := client.Snap.GetPluginConfigItem(params)
if err != nil {
return fmt.Errorf("Error requesting plugin config %v", err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion snaptel/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
http://www.apache.org/licenses/LICENSE-2.0.txt
Copyright 2015-2017 Intel Corporation
Copyright 2017 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
30 changes: 8 additions & 22 deletions snaptel/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
http://www.apache.org/licenses/LICENSE-2.0.txt
Copyright 2015-2017 Intel Corporation
Copyright 2017 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -28,32 +28,18 @@ import (
"text/tabwriter"
"time"

"github.com/intelsdi-x/snap-cli/models"
"github.com/intelsdi-x/snap-cli/snap"
"github.com/intelsdi-x/snap-client-go/client/snap"
"github.com/intelsdi-x/snap-client-go/models"
"github.com/urfave/cli"
)

func listMetrics(ctx *cli.Context) error {
ns := ctx.String("metric-namespace")
verbose := ctx.Bool("verbose")

if ns != "" {
//if the user doesn't provide '/*' we fix it
if ns[len(ns)-2:] != "/*" {
if ns[len(ns)-1:] == "/" {
ns = ns + "*"
} else {
ns = ns + "/*"
}
}
} else {
ns = "/*"
}

params := snap.NewGetMetricsParams()
resp, err := snapClient.GetMetrics(params)
resp, err := client.Snap.GetMetrics(params)
if err != nil {
return fmt.Errorf("Error getting metrics: %v", err)
return getErrorDetail(err)
}
if len(resp.Payload.Metrics) == 0 {
fmt.Println("No metrics found. Have you loaded any collectors yet?")
Expand Down Expand Up @@ -166,9 +152,9 @@ func getMetric(ctx *cli.Context) error {
ver64 := int64(ver)
params.SetVer(&ver64)

resp, err := snapClient.GetMetrics(params)
resp, err := client.Snap.GetMetrics(params)
if err != nil {
return fmt.Errorf("Error getting metrics: %v", err)
return getErrorDetail(err)
}

metrics := resp.Payload.Metrics
Expand All @@ -182,7 +168,7 @@ func getMetric(ctx *cli.Context) error {
}
}
} else {
return fmt.Errorf("Unexpected response type %v", metrics)
return fmt.Errorf("Error getting metrics: No metric found the giving namespace %s, version %d", ns, ver)
}
return nil
}
Expand Down
Loading

0 comments on commit 630463c

Please sign in to comment.