Skip to content

Commit

Permalink
feat: adds support to import apidocs into new orgs #357
Browse files Browse the repository at this point in the history
  • Loading branch information
srinandan committed Dec 18, 2023
1 parent 87d6434 commit 0f7829f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
25 changes: 20 additions & 5 deletions cmd/apidocs/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,40 @@ import (
"github.com/spf13/cobra"
)

/**
How the useNewSiteID flag works:
Assume data is exported from org1 which contains siteid site1. The fles are exported as
site_org1-site1.json and apidocs_org1-site1_00000.json
When importing this data into a new org, the siteid changes (since it is a combination of
org name and siteid).
Now import the data to org2 as
apigeecli apidocs import -o org2 -s site1 --source-f . --use-new-siteid=true -t $token
*/

// ImpCmd to import products
var ImpCmd = &cobra.Command{
Use: "import",
Short: "Import from a folder containing apidocs",
Long: "Import from a folder containing apidocs",
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 apidocs.Import(siteid, folder)
return apiclient.SetApigeeOrg(org)
},
RunE: func(cmd *cobra.Command, args []string) error {
return apidocs.Import(siteid, useNewSiteID, folder)
},
}

var useNewSiteID bool

func init() {
ImpCmd.Flags().StringVarP(&folder, "folder", "f",
"", "Folder containing site_<siteid>.json and apidocs_<siteid>_<id>.json files")

ImpCmd.Flags().BoolVarP(&useNewSiteID, "use-new-siteid", "",
false, "Use new siteid when importing; useful whem importing data between two orgs")
_ = ImpCmd.MarkFlagRequired("folder")
}
32 changes: 29 additions & 3 deletions internal/client/apidocs/apidocs.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,30 @@ func Export(folder string) (err error) {
return nil
}

func Import(siteid string, folder string) (err error) {
/**
How the useNewSiteID flag works:
Assume data is exported from org1 which contains siteid site1. The fles are exported as
site_org1-site1.json and apidocs_org1-site1_00000.json
When importing this data into a new org, the siteid changes (since it is a combination of
org name and siteid).
Now import the data to org2 as
apigeecli apidocs import -o org2 -s site1 --source-f . --use-new-siteid=true -t $token
*/

// Import
func Import(siteid string, useNewSiteID bool, folder string) (err error) {
var errs []string
var respBody []byte
docsList, err := readAPIDocsDataFile(path.Join(folder, "site_"+siteid+".json"))
var docsList []data

if useNewSiteID {
apiDocsName := fmt.Sprintf("site_%s-%s.json", apiclient.GetApigeeOrg(), siteid)
docsList, err = readAPIDocsDataFile(path.Join(folder, apiDocsName))
} else {
docsList, err = readAPIDocsDataFile(path.Join(folder, "site_"+siteid+".json"))
}
if err != nil {
return err
}
Expand All @@ -363,7 +383,13 @@ func Import(siteid string, folder string) (err error) {
}

// 2. find the documentation associated with this site
documentationFileName := path.Join(folder, "apidocs_"+siteid+"_"+doc.ID+".json")
var documentationFileName string
if useNewSiteID {
apiDocsName := fmt.Sprintf("apidocs_%s-%s_%s.json", apiclient.GetApigeeOrg(), siteid, doc.ID)
documentationFileName = path.Join(folder, apiDocsName)
} else {
documentationFileName = path.Join(folder, "apidocs_"+siteid+"_"+doc.ID+".json")
}
apidocument, err := readAPIDocumentationFile(documentationFileName)
if err != nil {
errs = append(errs, err.Error())
Expand Down

0 comments on commit 0f7829f

Please sign in to comment.