Skip to content

Commit

Permalink
feat: add support for custom ax reports #550 (#552)
Browse files Browse the repository at this point in the history
* feat: add support for custom ax reports #550

* feat: adds export of custom ax #550
  • Loading branch information
srinandan authored Oct 23, 2024
1 parent fefbaaf commit f7d35f2
Show file tree
Hide file tree
Showing 9 changed files with 303 additions and 1 deletion.
57 changes: 57 additions & 0 deletions internal/client/reports/reports.go
Original file line number Diff line number Diff line change
@@ -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
}
14 changes: 13 additions & 1 deletion internal/cmd/org/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/org/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
tracecfgFileName = "_tracecfg.json"
referencesFileName = "references.json"
envFileName = "envs.json"
customReportsName = "customreports.json"

proxiesFolderName = "proxies"
sharedFlowsFolderName = "sharedflows"
Expand Down
53 changes: 53 additions & 0 deletions internal/cmd/reports/crtreport.go
Original file line number Diff line number Diff line change
@@ -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")
}
46 changes: 46 additions & 0 deletions internal/cmd/reports/delreport.go
Original file line number Diff line number Diff line change
@@ -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")
}
46 changes: 46 additions & 0 deletions internal/cmd/reports/getreport.go
Original file line number Diff line number Diff line change
@@ -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")
}
46 changes: 46 additions & 0 deletions internal/cmd/reports/listreport.go
Original file line number Diff line number Diff line change
@@ -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")
}
39 changes: 39 additions & 0 deletions internal/cmd/reports/reports.go
Original file line number Diff line number Diff line change
@@ -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(&region, "region", "r",
"", "Apigee control plane region name; default is https://apigee.googleapis.com")

Cmd.AddCommand(GetCmd)
Cmd.AddCommand(DelCmd)
Cmd.AddCommand(ListCmd)
}
2 changes: 2 additions & 0 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -197,6 +198,7 @@ func init() {
RootCmd.AddCommand(sites.Cmd)
RootCmd.AddCommand(apihub.Cmd)
RootCmd.AddCommand(tree.Cmd)
RootCmd.AddCommand(reports.Cmd)
}

func initConfig() {
Expand Down

0 comments on commit f7d35f2

Please sign in to comment.