diff --git a/README.md b/README.md index 091f12f9..e616d242 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,7 @@ ___ * [apis](#apis) * [apps](#apps) +* [cache](#cache) * [developers](#devs) * [envs](#env) * [flowhooks](#flow) @@ -100,6 +101,7 @@ ___ * [export](#expapis) * [list](#listorgs) * [listdeploy](#listdeploy) +* [trace](#trace) * [undeploy](#undepapi) ### create @@ -234,6 +236,62 @@ The following parameters are supported. See Common Reference for a list of addit * `--expand -x` (optional) Returns an expanded list of proxies for the organization. * `--count -c` (optional) Number of apis to return +### trace + +Manage debug sessions/trace for API Proxy revisions + +* [create](#crttrcapi) +* [get](#gettrcapi) +* [list](#listtrcapi) + +#### create + +Create a new trace/debug session + +``` +apigeecli apis trace create -o org -e env -n name -v 1 -f "name1=value1,name2=value2" +``` +Parameters +The following parameters are supported. See Common Reference for a list of additional parameters. + +* `--org -o` (required) Apigee organization name +* `--env -e` (required) Apigee environment name +* `--name -n` (required) API proxy name +* `--rev -v` (required) API proxy revision +* `--filter -f` (optional) Trace filter; format is: name1=value1,name2=value2 + +#### get + +Get details for trace/debug session + +``` +apigeecli apis trace create -o org -e env -n name -v 1 -s uuid +``` +Parameters +The following parameters are supported. See Common Reference for a list of additional parameters. + +* `--org -o` (required) Apigee organization name +* `--env -e` (required) Apigee environment name +* `--name -n` (required) API proxy name +* `--rev -v` (required) API proxy revision +* `--ses -s` (required) Trace session ID +* `--msg -m` (optional) Message ID + +#### list + +List all trace/debug session for a proxy revision in the last 24 hours + +``` +apigeecli apis trace create -o org -e env -n name -v 1 +``` +Parameters +The following parameters are supported. See Common Reference for a list of additional parameters. + +* `--org -o` (required) Apigee organization name +* `--env -e` (required) Apigee environment name +* `--name -n` (required) API proxy name +* `--rev -v` (required) API proxy revision + ### undeploy Undeploys a revision of an existing API proxy to an environment in an organization. @@ -378,6 +436,40 @@ The following parameters are supported. See Common Reference for a list of addit --- +## cache + +* [delete](#delcache) +* [list](#listlist) + +### delete + +Delete a cache resource from an environment + +``` +apigeecli cache delete -o org -e env -n name +``` +Parameters +The following parameters are supported. See Common Reference for a list of additional parameters. + +* `--org -o` (required) Apigee organization name +* `--env -e` (required) Apigee environment name +* `--name -n` (required) Cache name + +### lsit + +List cache resources in an environment + +``` +apigeecli cache list -o org -e env +``` +Parameters +The following parameters are supported. See Common Reference for a list of additional parameters. + +* `--org -o` (required) Apigee organization name +* `--env -e` (required) Apigee environment name + +--- + ## developers Supported alias `developers` diff --git a/cmd/apis/apis.go b/cmd/apis/apis.go index 650c8811..70dbe778 100644 --- a/cmd/apis/apis.go +++ b/cmd/apis/apis.go @@ -11,6 +11,7 @@ import ( impapis "github.com/srinandan/apigeecli/cmd/apis/impapis" "github.com/srinandan/apigeecli/cmd/apis/listapis" "github.com/srinandan/apigeecli/cmd/apis/listdeploy" + traceapi "github.com/srinandan/apigeecli/cmd/apis/traceapi" "github.com/srinandan/apigeecli/cmd/apis/undepapi" "github.com/srinandan/apigeecli/cmd/shared" ) @@ -39,4 +40,5 @@ func init() { Cmd.AddCommand(getapi.Cmd) Cmd.AddCommand(impapis.Cmd) Cmd.AddCommand(undepapi.Cmd) + Cmd.AddCommand(traceapi.Cmd) } diff --git a/cmd/apis/fetchapi/fetchapi.go b/cmd/apis/fetchapi/fetchapi.go index 75453680..155c102c 100644 --- a/cmd/apis/fetchapi/fetchapi.go +++ b/cmd/apis/fetchapi/fetchapi.go @@ -25,5 +25,5 @@ func init() { "", "API Proxy revision") _ = Cmd.MarkFlagRequired("name") - _ = Cmd.MarkFlagRequired("revision") + _ = Cmd.MarkFlagRequired("rev") } diff --git a/cmd/apis/traceapi/crttrcapi/crttrcapi.go b/cmd/apis/traceapi/crttrcapi/crttrcapi.go new file mode 100644 index 00000000..404ae364 --- /dev/null +++ b/cmd/apis/traceapi/crttrcapi/crttrcapi.go @@ -0,0 +1,60 @@ +package crttrcapi + +import ( + "bytes" + "fmt" + "net/url" + "path" + + "github.com/spf13/cobra" + "github.com/srinandan/apigeecli/cmd/shared" +) + +//Cmd to manage tracing of apis +var Cmd = &cobra.Command{ + Use: "create", + Short: "Create a new debug session for an API proxy", + Long: "Create a new debug session for Apigee API proxy revision deployed in an environment", + RunE: func(cmd *cobra.Command, args []string) (err error) { + u, _ := url.Parse(shared.BaseURL) + u.Path = path.Join(u.Path, shared.RootArgs.Org, "environments", shared.RootArgs.Env, "apis", name, "revisions", revision, "debugsessions") + q := u.Query() + q.Set("timeout", "567") + u.RawQuery = q.Encode() + + var payload = "" + if len(filter) != 0 { + payload = "{\"filter\":" + getFilterStr() + "}" + } else { + payload = "{}" + } + _, err = shared.HttpClient(true, u.String(), payload) + return + + }, +} + +var name, revision string +var filter map[string]string + +func init() { + + Cmd.Flags().StringVarP(&name, "name", "n", + "", "API proxy name") + Cmd.Flags().StringVarP(&revision, "rev", "v", + "", "API Proxy revision") + Cmd.Flags().StringToStringVar(&filter, "filter", + nil, "Filter Conditions; format is name1=value1,name2=value2...") + + _ = Cmd.MarkFlagRequired("name") + _ = Cmd.MarkFlagRequired("rev") + +} + +func getFilterStr() string { + b := new(bytes.Buffer) + for key, value := range filter { + fmt.Fprintf(b, "%s=\"%s\"\n", key, value) + } + return b.String() +} diff --git a/cmd/apis/traceapi/gettrcapi/gettrcapi.go b/cmd/apis/traceapi/gettrcapi/gettrcapi.go new file mode 100644 index 00000000..dc3547f2 --- /dev/null +++ b/cmd/apis/traceapi/gettrcapi/gettrcapi.go @@ -0,0 +1,50 @@ +package gettrcapi + +import ( + "net/url" + "path" + + "github.com/spf13/cobra" + "github.com/srinandan/apigeecli/cmd/shared" +) + +//Cmd to manage tracing of apis +var Cmd = &cobra.Command{ + Use: "get", + Short: "Get a debug session for an API proxy revision", + Long: "Get a debug session for an API proxy revision deployed in an environment", + RunE: func(cmd *cobra.Command, args []string) (err error) { + u, _ := url.Parse(shared.BaseURL) + if messageID == "" { + u.Path = path.Join(u.Path, shared.RootArgs.Org, "environments", shared.RootArgs.Env, "apis", name, "revisions", revision, "debugsessions", sessionID, "data") + q := u.Query() + q.Set("limit", "20") + u.RawQuery = q.Encode() + } else { + u.Path = path.Join(u.Path, shared.RootArgs.Org, "environments", shared.RootArgs.Env, "apis", name, "revisions", revision, "debugsessions", sessionID, "data", messageID) + } + + _, err = shared.HttpClient(true, u.String()) + return + + }, +} + +var name, revision, sessionID, messageID string + +func init() { + + Cmd.Flags().StringVarP(&name, "name", "n", + "", "API proxy name") + Cmd.Flags().StringVarP(&revision, "rev", "v", + "", "API Proxy revision") + Cmd.Flags().StringVarP(&sessionID, "ses", "s", + "", "Debug session Id") + Cmd.Flags().StringVarP(&messageID, "msg", "m", + "", "Debug session Id") + + _ = Cmd.MarkFlagRequired("name") + _ = Cmd.MarkFlagRequired("rev") + _ = Cmd.MarkFlagRequired("ses") + +} diff --git a/cmd/apis/traceapi/listtrcapi/listtrcapi.go b/cmd/apis/traceapi/listtrcapi/listtrcapi.go new file mode 100644 index 00000000..e9b631f2 --- /dev/null +++ b/cmd/apis/traceapi/listtrcapi/listtrcapi.go @@ -0,0 +1,37 @@ +package listtrcapi + +import ( + "net/url" + "path" + + "github.com/spf13/cobra" + "github.com/srinandan/apigeecli/cmd/shared" +) + +//Cmd to manage tracing of apis +var Cmd = &cobra.Command{ + Use: "list", + Short: "List all debug sessions for an API proxy revision", + Long: "List all debug sessions for an API proxy revision deployed in an environment", + RunE: func(cmd *cobra.Command, args []string) (err error) { + u, _ := url.Parse(shared.BaseURL) + u.Path = path.Join(u.Path, shared.RootArgs.Org, "environments", shared.RootArgs.Env, "apis", name, "revisions", revision, "debugsessions") + _, err = shared.HttpClient(true, u.String()) + return + + }, +} + +var name, revision string + +func init() { + + Cmd.Flags().StringVarP(&name, "name", "n", + "", "API proxy name") + Cmd.Flags().StringVarP(&revision, "rev", "v", + "", "API Proxy revision") + + _ = Cmd.MarkFlagRequired("name") + _ = Cmd.MarkFlagRequired("rev") + +} diff --git a/cmd/apis/traceapi/traceapi.go b/cmd/apis/traceapi/traceapi.go new file mode 100644 index 00000000..aabf42f2 --- /dev/null +++ b/cmd/apis/traceapi/traceapi.go @@ -0,0 +1,28 @@ +package apis + +import ( + "github.com/spf13/cobra" + "github.com/srinandan/apigeecli/cmd/apis/traceapi/crttrcapi" + "github.com/srinandan/apigeecli/cmd/apis/traceapi/gettrcapi" + "github.com/srinandan/apigeecli/cmd/apis/traceapi/listtrcapi" + "github.com/srinandan/apigeecli/cmd/shared" +) + +//Cmd to manage tracing of apis +var Cmd = &cobra.Command{ + Use: "trace", + Short: "Manage debugging/tracing of Apigee API proxies", + Long: "Manage debugging/tracing of Apigee API proxy revisions deployed in an environment", +} + +func init() { + + Cmd.PersistentFlags().StringVarP(&shared.RootArgs.Env, "env", "e", + "", "Apigee environment name") + + _ = Cmd.MarkPersistentFlagRequired("env") + + Cmd.AddCommand(crttrcapi.Cmd) + Cmd.AddCommand(listtrcapi.Cmd) + Cmd.AddCommand(gettrcapi.Cmd) +} diff --git a/cmd/cache/cache.go b/cmd/cache/cache.go new file mode 100644 index 00000000..1d96bf51 --- /dev/null +++ b/cmd/cache/cache.go @@ -0,0 +1,30 @@ +package cache + +import ( + "github.com/spf13/cobra" + "github.com/srinandan/apigeecli/cmd/cache/delcache" + "github.com/srinandan/apigeecli/cmd/cache/listcache" + "github.com/srinandan/apigeecli/cmd/shared" +) + +//Cmd to manage tracing of apis +var Cmd = &cobra.Command{ + Use: "cache", + Short: "Manage caches within an Apigee environment", + Long: "Manage caches within an Apigee environment", +} + +func init() { + + Cmd.PersistentFlags().StringVarP(&shared.RootArgs.Org, "org", "o", + "", "Apigee organization name") + + Cmd.PersistentFlags().StringVarP(&shared.RootArgs.Env, "env", "e", + "", "Apigee environment name") + + _ = Cmd.MarkPersistentFlagRequired("org") + _ = Cmd.MarkPersistentFlagRequired("env") + + Cmd.AddCommand(listcache.Cmd) + Cmd.AddCommand(delcache.Cmd) +} diff --git a/cmd/cache/delcache/delcache.go b/cmd/cache/delcache/delcache.go new file mode 100644 index 00000000..a9f98e11 --- /dev/null +++ b/cmd/cache/delcache/delcache.go @@ -0,0 +1,34 @@ +package delcache + +import ( + "net/url" + "path" + + "github.com/spf13/cobra" + "github.com/srinandan/apigeecli/cmd/shared" +) + +//Cmd to delete cache +var Cmd = &cobra.Command{ + Use: "delete", + Short: "Delete a cache resource from the environment", + Long: "Delete a cache resource from the environment", + RunE: func(cmd *cobra.Command, args []string) (err error) { + u, _ := url.Parse(shared.BaseURL) + u.Path = path.Join(u.Path, shared.RootArgs.Org, "environments", shared.RootArgs.Env, "caches", name) + _, err = shared.HttpClient(true, u.String()) + return + + }, +} + +var name string + +func init() { + + Cmd.Flags().StringVarP(&name, "name", "n", + "", "API proxy name") + + _ = Cmd.MarkFlagRequired("name") + +} diff --git a/cmd/cache/listcache/listcache.go b/cmd/cache/listcache/listcache.go new file mode 100644 index 00000000..b4dcd283 --- /dev/null +++ b/cmd/cache/listcache/listcache.go @@ -0,0 +1,27 @@ +package listcache + +import ( + "net/url" + "path" + + "github.com/spf13/cobra" + "github.com/srinandan/apigeecli/cmd/shared" +) + +//Cmd to list caches +var Cmd = &cobra.Command{ + Use: "list", + Short: "List all caches in your environment", + Long: "List all caches in your environment", + RunE: func(cmd *cobra.Command, args []string) (err error) { + u, _ := url.Parse(shared.BaseURL) + u.Path = path.Join(u.Path, shared.RootArgs.Org, "environments", shared.RootArgs.Env, "caches") + _, err = shared.HttpClient(true, u.String()) + return + + }, +} + +func init() { + +} diff --git a/cmd/res/getres/getres.go b/cmd/res/getres/getres.go new file mode 100644 index 00000000..350ce147 --- /dev/null +++ b/cmd/res/getres/getres.go @@ -0,0 +1,44 @@ +package getres + +import ( + "fmt" + "net/url" + "path" + + "github.com/spf13/cobra" + "github.com/srinandan/apigeecli/cmd/shared" + "github.com/srinandan/apigeecli/cmd/types" +) + +//Cmd to get a resource +var Cmd = &cobra.Command{ + Use: "get", + Short: "Get a resource file", + Long: "Get a resource file", + PreRunE: func(cmd *cobra.Command, args []string) (err error) { + if !types.IsValidResource(resType) { + return fmt.Errorf("Invalid resource type") + } + return err + }, + RunE: func(cmd *cobra.Command, args []string) (err error) { + u, _ := url.Parse(shared.BaseURL) + u.Path = path.Join(u.Path, shared.RootArgs.Org, "environments", shared.RootArgs.Env, "resourcefiles", resType, name) + _, err = shared.DownloadResource(u.String(), name, resType) + return + }, +} + +var name, resType string + +func init() { + + Cmd.Flags().StringVarP(&name, "name", "n", + "", "Name of the resource file") + Cmd.Flags().StringVarP(&resType, "type", "p", + "", "Resource type") + + _ = Cmd.MarkFlagRequired("name") + _ = Cmd.MarkFlagRequired("type") + +} diff --git a/cmd/res/listres/listres.go b/cmd/res/listres/listres.go new file mode 100644 index 00000000..9cd85a2a --- /dev/null +++ b/cmd/res/listres/listres.go @@ -0,0 +1,46 @@ +package listres + +import ( + "fmt" + "net/url" + "path" + + "github.com/spf13/cobra" + "github.com/srinandan/apigeecli/cmd/shared" + "github.com/srinandan/apigeecli/cmd/types" +) + +//Cmd to list resources +var Cmd = &cobra.Command{ + Use: "list", + Short: "List all resources in your environment", + Long: "List all resources in your environment", + PreRunE: func(cmd *cobra.Command, args []string) (err error) { + if !types.IsValidResource(resType) { + return fmt.Errorf("Invalid resource type") + } + return err + }, + RunE: func(cmd *cobra.Command, args []string) (err error) { + u, _ := url.Parse(shared.BaseURL) + u.Path = path.Join(u.Path, shared.RootArgs.Org, "environments", shared.RootArgs.Env, "resourcefiles") + + if resType != "" { + q := u.Query() + q.Set("type", resType) + u.RawQuery = q.Encode() + } + _, err = shared.HttpClient(true, u.String()) + return + + }, +} + +var resType string + +func init() { + + Cmd.Flags().StringVarP(&resType, "type", "p", + "", "Resource type") + +} diff --git a/cmd/res/res.go b/cmd/res/res.go new file mode 100644 index 00000000..0cd18d09 --- /dev/null +++ b/cmd/res/res.go @@ -0,0 +1,29 @@ +package res + +import ( + "github.com/spf13/cobra" + "github.com/srinandan/apigeecli/cmd/shared" + "github.com/srinandan/apigeecli/cmd/res/listres" +) + +//Cmd to manage resources +var Cmd = &cobra.Command{ + Use: "resources", + Aliases: []string{"res"}, + Short: "Manage resources within an Apigee environment", + Long: "Manage resources within an Apigee environment", +} + +func init() { + + Cmd.PersistentFlags().StringVarP(&shared.RootArgs.Org, "org", "o", + "", "Apigee organization name") + + Cmd.PersistentFlags().StringVarP(&shared.RootArgs.Env, "env", "e", + "", "Apigee environment name") + + _ = Cmd.MarkPersistentFlagRequired("org") + _ = Cmd.MarkPersistentFlagRequired("env") + + Cmd.AddCommand(listres.Cmd) +} diff --git a/cmd/root.go b/cmd/root.go index 75497fa3..c23d45b5 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -5,6 +5,7 @@ import ( "github.com/spf13/viper" "github.com/srinandan/apigeecli/cmd/apis" "github.com/srinandan/apigeecli/cmd/apps" + cache "github.com/srinandan/apigeecli/cmd/cache" "github.com/srinandan/apigeecli/cmd/developers" "github.com/srinandan/apigeecli/cmd/env" flowhooks "github.com/srinandan/apigeecli/cmd/flowhooks" @@ -13,6 +14,7 @@ import ( "github.com/srinandan/apigeecli/cmd/kvm" "github.com/srinandan/apigeecli/cmd/org" "github.com/srinandan/apigeecli/cmd/products" + res "github.com/srinandan/apigeecli/cmd/res" "github.com/srinandan/apigeecli/cmd/shared" "github.com/srinandan/apigeecli/cmd/sharedflows" "github.com/srinandan/apigeecli/cmd/sync" @@ -66,6 +68,8 @@ func init() { RootCmd.AddCommand(token.Cmd) RootCmd.AddCommand(keystores.Cmd) RootCmd.AddCommand(keyaliases.Cmd) + RootCmd.AddCommand(cache.Cmd) + RootCmd.AddCommand(res.Cmd) } func initConfig() { diff --git a/cmd/shared/shared.go b/cmd/shared/shared.go index b4e4fdae..93ae1ad2 100644 --- a/cmd/shared/shared.go +++ b/cmd/shared/shared.go @@ -132,9 +132,9 @@ func PostHttpOctet(print bool, url string, proxyName string) (respBody []byte, e } //DownloadResource method is used to download resources, proxy bundles, sharedflows -func DownloadResource(url string, name string) error { +func DownloadResource(url string, name string, resType string) error { - out, err := os.Create(name + ".zip") + out, err := os.Create(name + resType) if err != nil { Error.Fatalln("error creating file: ", err) return err @@ -170,7 +170,7 @@ func DownloadResource(url string, name string) error { return err } - fmt.Println("Proxy bundle " + name + ".zip completed") + fmt.Println("Resource " + name + resType + " completed") return nil } @@ -567,7 +567,7 @@ func FetchBundle(entityType string, name string, revision string) error { u.RawQuery = q.Encode() u.Path = path.Join(u.Path, RootArgs.Org, entityType, name, "revisions", revision) - err := DownloadResource(u.String(), name) + err := DownloadResource(u.String(), name, ".zip") if err != nil { Error.Fatalf("error with entity: %s", name) Error.Fatalln(err) diff --git a/cmd/types/types.go b/cmd/types/types.go index 680a0e55..445dfe09 100644 --- a/cmd/types/types.go +++ b/cmd/types/types.go @@ -25,8 +25,22 @@ type OAuthAccessToken struct { TokenType string `json:"token_type,omitempty"` } +//KeyAlias hold the name of the key alias type KeyAliasName string func (a KeyAliasName) String() string { return string(a) } + +//ResourceTypes contains a list of valid resources +var resourceTypes = [7]string{"js", "jsc", "properties", "java", "wsdl", "xsd", "py"} + +//IsValidResource returns true is the resource type is valid +func IsValidResource(resType string) bool { + for _, n := range resourceTypes { + if n == resType { + return true + } + } + return false +}