diff --git a/internal/client/reports/reports.go b/internal/client/reports/reports.go new file mode 100644 index 000000000..a83d27ecb --- /dev/null +++ b/internal/client/reports/reports.go @@ -0,0 +1,57 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package reports + +import ( + "internal/apiclient" + "net/url" + "path" + "strconv" +) + +// Create a report +func Create(contents []byte) (respBody []byte, err error) { + u, _ := url.Parse(apiclient.GetApigeeBaseURL()) + u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "reports") + respBody, err = apiclient.HttpClient(u.String(), string(contents)) + return respBody, err +} + +// Get a report +func Get(name string) (respBody []byte, err error) { + u, _ := url.Parse(apiclient.GetApigeeBaseURL()) + u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "reports", name) + respBody, err = apiclient.HttpClient(u.String()) + return respBody, err +} + +// Delete a report +func Delete(name string) (respBody []byte, err error) { + u, _ := url.Parse(apiclient.GetApigeeBaseURL()) + u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "reports", name) + respBody, err = apiclient.HttpClient(u.String(), "", "DELETE") + return respBody, err +} + +// List reports +func List(expand bool) (respBody []byte, err error) { + u, _ := url.Parse(apiclient.GetApigeeBaseURL()) + u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "reports") + q := u.Query() + q.Set("expand", strconv.FormatBool(expand)) + u.RawQuery = q.Encode() + respBody, err = apiclient.HttpClient(u.String()) + return respBody, err +} diff --git a/internal/cmd/org/export.go b/internal/cmd/org/export.go index 5a0f16d16..fd878f2f8 100644 --- a/internal/cmd/org/export.go +++ b/internal/cmd/org/export.go @@ -31,6 +31,7 @@ import ( "internal/client/orgs" "internal/client/products" "internal/client/references" + "internal/client/reports" "internal/client/securityprofiles" "internal/client/sharedflows" "internal/client/sync" @@ -59,7 +60,7 @@ var ExportCmd = &cobra.Command{ cmd.SilenceUsage = true var productResponse, appsResponse, targetServerResponse, referencesResponse, appGroupAppsResponse [][]byte - var respBody, listKVMBytes, appGroupsRespBody []byte + var respBody, listKVMBytes, appGroupsRespBody, custReports []byte apiclient.DisableCmdPrintHttpResponse() @@ -207,6 +208,17 @@ var ExportCmd = &cobra.Command{ } } + //export custom reports + if custReports, err = reports.List(true); proceedOnError(err) != nil { + return err + } + clilog.Info.Println("Exporting analytics custom reports...") + if err = apiclient.WriteByteArrayToFile( + customReportsName, + false, custReports); proceedOnError(err) != nil { + return err + } + var _, envDetailsRespBody []byte clilog.Info.Println("Exporting list of environments...") apiclient.DisableCmdPrintHttpResponse() diff --git a/internal/cmd/org/variables.go b/internal/cmd/org/variables.go index 384aa5375..ac87ba8ea 100644 --- a/internal/cmd/org/variables.go +++ b/internal/cmd/org/variables.go @@ -30,6 +30,7 @@ const ( tracecfgFileName = "_tracecfg.json" referencesFileName = "references.json" envFileName = "envs.json" + customReportsName = "customreports.json" proxiesFolderName = "proxies" sharedFlowsFolderName = "sharedflows" diff --git a/internal/cmd/reports/crtreport.go b/internal/cmd/reports/crtreport.go new file mode 100644 index 000000000..5b9d868cf --- /dev/null +++ b/internal/cmd/reports/crtreport.go @@ -0,0 +1,53 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package reports + +import ( + "internal/apiclient" + "internal/client/reports" + "internal/cmd/utils" + + "github.com/spf13/cobra" +) + +// CrtCmd to get a resource +var CrtCmd = &cobra.Command{ + Use: "create", + Short: "Create a new custom report", + Long: "Create a new custom report", + Args: func(cmd *cobra.Command, args []string) (err error) { + apiclient.SetRegion(region) + return apiclient.SetApigeeOrg(org) + }, + RunE: func(cmd *cobra.Command, args []string) (err error) { + cmd.SilenceUsage = true + + contents, err := utils.ReadFile(contentPath) + if err != nil { + return err + } + _, err = reports.Create(contents) + return err + }, +} + +var contentPath string + +func init() { + CrtCmd.Flags().StringVarP(&contentPath, "file", "f", + "", "Path to a file containing the custom report content") + + _ = CrtCmd.MarkFlagRequired("file") +} diff --git a/internal/cmd/reports/delreport.go b/internal/cmd/reports/delreport.go new file mode 100644 index 000000000..7b1f74826 --- /dev/null +++ b/internal/cmd/reports/delreport.go @@ -0,0 +1,46 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package reports + +import ( + "internal/apiclient" + "internal/client/reports" + + "github.com/spf13/cobra" +) + +// DelCmd to get a resource +var DelCmd = &cobra.Command{ + Use: "delete", + Short: "Delete a custom report", + Long: "Delete a custom report", + Args: func(cmd *cobra.Command, args []string) (err error) { + apiclient.SetRegion(region) + return apiclient.SetApigeeOrg(org) + }, + RunE: func(cmd *cobra.Command, args []string) (err error) { + cmd.SilenceUsage = true + + _, err = reports.Delete(name) + return err + }, +} + +func init() { + DelCmd.Flags().StringVarP(&name, "name", "n", + "", "Name of the custom report") + + _ = DelCmd.MarkFlagRequired("name") +} diff --git a/internal/cmd/reports/getreport.go b/internal/cmd/reports/getreport.go new file mode 100644 index 000000000..07d5c1ac8 --- /dev/null +++ b/internal/cmd/reports/getreport.go @@ -0,0 +1,46 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package reports + +import ( + "internal/apiclient" + "internal/client/reports" + + "github.com/spf13/cobra" +) + +// GetCmd to get a resource +var GetCmd = &cobra.Command{ + Use: "get", + Short: "Get details for a custom report", + Long: "Get details for a custom report", + Args: func(cmd *cobra.Command, args []string) (err error) { + apiclient.SetRegion(region) + return apiclient.SetApigeeOrg(org) + }, + RunE: func(cmd *cobra.Command, args []string) (err error) { + cmd.SilenceUsage = true + + _, err = reports.Get(name) + return err + }, +} + +func init() { + GetCmd.Flags().StringVarP(&name, "name", "n", + "", "Name of the custom report") + + _ = GetCmd.MarkFlagRequired("name") +} diff --git a/internal/cmd/reports/listreport.go b/internal/cmd/reports/listreport.go new file mode 100644 index 000000000..d777de491 --- /dev/null +++ b/internal/cmd/reports/listreport.go @@ -0,0 +1,46 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package reports + +import ( + "internal/apiclient" + "internal/client/reports" + + "github.com/spf13/cobra" +) + +// ListCmd to get a resource +var ListCmd = &cobra.Command{ + Use: "list", + Short: "List all custom reports in the org", + Long: "List all custom reports in the org", + Args: func(cmd *cobra.Command, args []string) (err error) { + apiclient.SetRegion(region) + return apiclient.SetApigeeOrg(org) + }, + RunE: func(cmd *cobra.Command, args []string) (err error) { + cmd.SilenceUsage = true + + _, err = reports.List(expand) + return + }, +} + +var expand bool + +func init() { + ListCmd.Flags().BoolVarP(&expand, "expand", "x", + false, "Set to 'true' to get expanded details about each custom report") +} diff --git a/internal/cmd/reports/reports.go b/internal/cmd/reports/reports.go new file mode 100644 index 000000000..3e0416e12 --- /dev/null +++ b/internal/cmd/reports/reports.go @@ -0,0 +1,39 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package reports + +import ( + "github.com/spf13/cobra" +) + +// Cmd to manage identities +var Cmd = &cobra.Command{ + Use: "reports", + Short: "Manage Analytics custom reports", + Long: "Manage analytics custom reports", +} + +var org, name, region string + +func init() { + Cmd.PersistentFlags().StringVarP(&org, "org", "o", + "", "Apigee organization name") + Cmd.PersistentFlags().StringVarP(®ion, "region", "r", + "", "Apigee control plane region name; default is https://apigee.googleapis.com") + + Cmd.AddCommand(GetCmd) + Cmd.AddCommand(DelCmd) + Cmd.AddCommand(ListCmd) +} diff --git a/internal/cmd/root.go b/internal/cmd/root.go index dcd482a6c..654bc3be5 100644 --- a/internal/cmd/root.go +++ b/internal/cmd/root.go @@ -43,6 +43,7 @@ import ( "internal/cmd/products" "internal/cmd/projects" "internal/cmd/references" + "internal/cmd/reports" "internal/cmd/securityprofiles" "internal/cmd/sharedflows" "internal/cmd/sites" @@ -197,6 +198,7 @@ func init() { RootCmd.AddCommand(sites.Cmd) RootCmd.AddCommand(apihub.Cmd) RootCmd.AddCommand(tree.Cmd) + RootCmd.AddCommand(reports.Cmd) } func initConfig() {