Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: export and import apicategories #353

Merged
merged 4 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"editor.insertSpaces": true,
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": true
"source.organizeImports": "explicit"
},
"editor.defaultFormatter": "golang.go"
},
Expand Down
2 changes: 2 additions & 0 deletions cmd/apicategories/apicategories.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ func init() {
Cmd.AddCommand(GetCmd)
Cmd.AddCommand(DelCmd)
Cmd.AddCommand(CreateCmd)
Cmd.AddCommand(ExpCmd)
Cmd.AddCommand(ImpCmd)

_ = Cmd.MarkFlagRequired("org")
_ = Cmd.MarkFlagRequired("siteid")
Expand Down
52 changes: 52 additions & 0 deletions cmd/apicategories/export.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2023 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 apicategories

import (
"os"

"internal/apiclient"

"internal/client/apicategories"

"github.com/spf13/cobra"
)

// ExpCmd to export apidocs
var ExpCmd = &cobra.Command{
Use: "export",
Short: "Export API Categories across all sites",
Long: "Export API Categories across all sites",
Args: func(cmd *cobra.Command, args []string) (err error) {
return apiclient.SetApigeeOrg(org)
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
if folder == "" {
folder, _ = os.Getwd()
}
if err = apiclient.FolderExists(folder); err != nil {
return err
}
apiclient.DisableCmdPrintHttpResponse()
return apicategories.Export(folder)
},
}

var folder string

func init() {
ExpCmd.Flags().StringVarP(&folder, "folder", "f",
"", "folder to export API Docs")
}
50 changes: 50 additions & 0 deletions cmd/apicategories/import.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2023 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 apicategories

import (
"fmt"

"internal/apiclient"

"internal/client/apicategories"

"github.com/spf13/cobra"
)

// ImpCmd to import products
var ImpCmd = &cobra.Command{
Use: "import",
Short: "Import from a folder containing apicategories",
Long: "Import from a folder containing apicategories",
Args: func(cmd *cobra.Command, args []string) (err error) {
return apiclient.SetApigeeOrg(org)
},
RunE: func(cmd *cobra.Command, args []string) error {
if siteid == "" {
return fmt.Errorf("siteid is a mandatory parameter")
}
return apicategories.Import(siteid, apicategoryFile)
},
}

var apicategoryFile string

func init() {
ImpCmd.Flags().StringVarP(&apicategoryFile, "file", "f",
"", "A file containing apicategories")

_ = ImpCmd.MarkFlagRequired("file")
}
2 changes: 1 addition & 1 deletion cmd/apidocs/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ func init() {
ImpCmd.Flags().StringVarP(&folder, "folder", "f",
"", "Folder containing site_<siteid>.json and apidocs_<siteid>_<id>.json files")

_ = ImpCmd.MarkFlagRequired("file")
_ = ImpCmd.MarkFlagRequired("folder")
}
6 changes: 6 additions & 0 deletions cmd/org/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"internal/clilog"

"internal/client/apicategories"
"internal/client/apidocs"
"internal/client/apis"
"internal/client/appgroups"
Expand Down Expand Up @@ -186,6 +187,11 @@ var ExportCmd = &cobra.Command{
return err
}

clilog.Info.Println("Exporting API Portal apicategories Configuration...")
if err = apicategories.Export(portalsFolderName); proceedOnError(err) != nil {
return err
}

if runtimeType == "HYBRID" {
clilog.Info.Println("Exporting Sync Authorization Identities...")
if respBody, err = sync.Get(); err != nil {
Expand Down
90 changes: 90 additions & 0 deletions internal/client/apicategories/apicategories.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,36 @@
package apicategories

import (
"encoding/json"
"errors"
"fmt"
"io"
"net/url"
"os"
"path"
"strings"

"internal/apiclient"
"internal/client/sites"
"internal/clilog"
)

type listapicategories struct {
Status string `json:"status,omitempty"`
Message string `json:"message,omitempty"`
RequestID string `json:"requestId,omitempty"`
ErrorCode string `json:"errorCode,omitempty"`
Data []data `json:"data,omitempty"`
NextPageToken string `json:"nextPageToken,omitempty"`
}

type data struct {
SiteID string `json:"siteId,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
UpdateTime string `json:"updateTime,omitempty"`
}

// Create
func Create(siteid string, name string) (respBody []byte, err error) {
apicategories := []string{}
Expand Down Expand Up @@ -69,3 +92,70 @@ func Update(siteid string, name string) (respBody []byte, err error) {
respBody, err = apiclient.HttpClient(u.String(), payload, "PATCH")
return respBody, err
}

// Export
func Export(folder string) (err error) {
apiclient.ClientPrintHttpResponse.Set(false)
defer apiclient.ClientPrintHttpResponse.Set(apiclient.GetCmdPrintHttpResponseSetting())

siteids, err := sites.GetSiteIDs()
if err != nil {
return err
}

for _, siteid := range siteids {
listRespBytes, err := List(siteid)
if err != nil {
return fmt.Errorf("failed to fetch apicategories: %w", err)
}

docFileName := fmt.Sprintf("apicategory_%s.json", siteid)
if err = apiclient.WriteByteArrayToFile(path.Join(folder, docFileName), false, listRespBytes); err != nil {
return err
}
}
return nil
}

// Import
func Import(siteid string, apicateogyFile string) (err error) {
errs := []string{}
l, err := readAPICategoriesFile(apicateogyFile)
if err != nil {
return err
}
if len(l.Data) < 1 {
clilog.Warning.Println("No categories found for the siteid")
return nil
}

for _, category := range l.Data {
_, err = Create(siteid, category.Name)
if err != nil {
errs = append(errs, err.Error())
}
}
if len(errs) > 0 {
return errors.New(strings.Join(errs, "\n"))
}
return nil
}

func readAPICategoriesFile(fileName string) (l listapicategories, err error) {
jsonFile, err := os.Open(fileName)
if err != nil {
return l, err
}

defer jsonFile.Close()

content, err := io.ReadAll(jsonFile)
if err != nil {
return l, err
}
err = json.Unmarshal(content, &l)
if err != nil {
return l, err
}
return l, nil
}