Skip to content
This repository was archived by the owner on Mar 9, 2023. It is now read-only.

Commit 0a2c175

Browse files
authored
Merge pull request #124 from srinandan/export
Export and import connections in a region
2 parents 2f043b2 + efec15f commit 0a2c175

File tree

5 files changed

+225
-2
lines changed

5 files changed

+225
-2
lines changed

client/connections/connectors.go

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ package connections
1616

1717
import (
1818
"encoding/json"
19+
"errors"
1920
"fmt"
2021
"io/ioutil"
2122
"net/url"
2223
"os"
2324
"path"
25+
"path/filepath"
2426
"strconv"
2527
"strings"
2628

@@ -30,6 +32,24 @@ import (
3032
"github.com/srinandan/integrationcli/secmgr"
3133
)
3234

35+
const maxPageSize = 1000
36+
37+
type listconnections struct {
38+
Connections []connection `json:"connections,omitempty"`
39+
NextPageToken string `json:"nextPageToken,omitempty"`
40+
}
41+
42+
type connection struct {
43+
Name *string `json:"name,omitempty"`
44+
Description string `json:"description,omitempty"`
45+
ConnectorVersion *string `json:"connectorVersion,omitempty"`
46+
ConnectorDetails *connectorDetails `json:"connectorDetails,omitempty"`
47+
ConfigVariables []configVar `json:"configVariables,omitempty"`
48+
AuthConfig authConfig `json:"authConfig,omitempty"`
49+
DestinationConfig destinationConfig `json:"destinationConfig,omitempty"`
50+
Suspended bool `json:"suspended,omitempty"`
51+
}
52+
3353
type connectionRequest struct {
3454
Labels *map[string]string `json:"labels,omitempty"`
3555
Description *string `json:"description,omitempty"`
@@ -375,3 +395,104 @@ func readSecretFile(name string) (payload []byte, err error) {
375395
}
376396
return content, nil
377397
}
398+
399+
// Import
400+
func Import(folder string) (err error) {
401+
402+
apiclient.SetPrintOutput(false)
403+
errs := []string{}
404+
405+
err = filepath.Walk(folder, func(path string, info os.FileInfo, err error) error {
406+
if err != nil {
407+
clilog.Warning.Println("connection folder not found")
408+
return nil
409+
}
410+
if info.IsDir() {
411+
return nil
412+
}
413+
if filepath.Ext(path) != ".json" {
414+
return nil
415+
}
416+
name := strings.TrimSuffix(filepath.Base(path), filepath.Ext(filepath.Base(path)))
417+
content, err := ioutil.ReadFile(path)
418+
if err != nil {
419+
return err
420+
}
421+
422+
if _, err := Get(name, ""); err != nil { //create only if connection doesn't exist
423+
_, err = Create(name, content, "", "", "", false)
424+
if err != nil {
425+
errs = append(errs, err.Error())
426+
}
427+
fmt.Printf("creating connection %s\n", name)
428+
} else {
429+
fmt.Printf("connection %s already exists, skipping creations\n", name)
430+
}
431+
432+
return nil
433+
})
434+
435+
if err != nil {
436+
return nil
437+
}
438+
439+
if len(errs) > 0 {
440+
return errors.New(strings.Join(errs, "\n"))
441+
}
442+
return nil
443+
}
444+
445+
// Export
446+
func Export(folder string) (err error) {
447+
apiclient.SetExportToFile(folder)
448+
apiclient.SetPrintOutput(false)
449+
450+
respBody, err := List(maxPageSize, "", "", "")
451+
if err != nil {
452+
return err
453+
}
454+
455+
lconnections := listconnections{}
456+
457+
if err = json.Unmarshal(respBody, &lconnections); err != nil {
458+
return err
459+
}
460+
461+
//no connections where found
462+
if len(lconnections.Connections) == 0 {
463+
return nil
464+
}
465+
466+
for _, lconnection := range lconnections.Connections {
467+
lconnection.ConnectorDetails = new(connectorDetails)
468+
lconnection.ConnectorDetails.Name = getConnectorName(*lconnection.ConnectorVersion)
469+
lconnection.ConnectorDetails.Version = getConnectorVersion(*lconnection.ConnectorVersion)
470+
lconnection.ConnectorVersion = nil
471+
fileName := getConnectionName(*lconnection.Name) + ".json"
472+
lconnection.Name = nil
473+
connectionPayload, err := json.Marshal(lconnection)
474+
if err != nil {
475+
return err
476+
}
477+
if err = apiclient.WriteByteArrayToFile(path.Join(apiclient.GetExportToFile(), fileName), false, connectionPayload); err != nil {
478+
clilog.Error.Println(err)
479+
return err
480+
}
481+
fmt.Printf("Downloaded %s\n", fileName)
482+
}
483+
484+
return nil
485+
}
486+
487+
func getConnectorName(version string) string {
488+
return strings.Split(version, "/")[7]
489+
}
490+
491+
func getConnectorVersion(version string) int {
492+
i, _ := strconv.Atoi(strings.Split(version, "/")[9])
493+
return i
494+
}
495+
496+
func getConnectionName(name string) string {
497+
return name[strings.LastIndex(name, "/")+1:]
498+
}

cmd/authconfigs/export.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"github.com/spf13/cobra"
2222
)
2323

24-
//ExportCmd to export integrations
24+
// ExportCmd to export integrations
2525
var ExportCmd = &cobra.Command{
2626
Use: "export",
2727
Short: "Export authconfigs in a region to a folder",
@@ -45,7 +45,7 @@ var folder string
4545

4646
func init() {
4747
ExportCmd.Flags().StringVarP(&folder, "folder", "f",
48-
"", "Folder to export Integration flows")
48+
"", "Folder to export authconfig")
4949

5050
_ = ExportCmd.MarkFlagRequired("folder")
5151
}

cmd/connectors/connectors.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,6 @@ func init() {
4141
Cmd.AddCommand(GetCmd)
4242
Cmd.AddCommand(IamCmd)
4343
Cmd.AddCommand(NodeCountCmd)
44+
Cmd.AddCommand(ExportCmd)
45+
Cmd.AddCommand(ImportCmd)
4446
}

cmd/connectors/export.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package connectors
16+
17+
import (
18+
"github.com/srinandan/integrationcli/apiclient"
19+
"github.com/srinandan/integrationcli/client/connections"
20+
21+
"github.com/spf13/cobra"
22+
)
23+
24+
// ExportCmd to export connections
25+
var ExportCmd = &cobra.Command{
26+
Use: "export",
27+
Short: "Export connections in a region to a folder",
28+
Long: "Export connections in a region to a folder",
29+
Args: func(cmd *cobra.Command, args []string) (err error) {
30+
if err = apiclient.SetRegion(region); err != nil {
31+
return err
32+
}
33+
return apiclient.SetProjectID(project)
34+
},
35+
RunE: func(cmd *cobra.Command, args []string) (err error) {
36+
if err = apiclient.FolderExists(folder); err != nil {
37+
return err
38+
}
39+
40+
return connections.Export(folder)
41+
},
42+
}
43+
44+
var folder string
45+
46+
func init() {
47+
ExportCmd.Flags().StringVarP(&folder, "folder", "f",
48+
"", "Folder to export connections")
49+
50+
_ = ExportCmd.MarkFlagRequired("folder")
51+
}

cmd/connectors/import.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package connectors
16+
17+
import (
18+
"github.com/srinandan/integrationcli/apiclient"
19+
"github.com/srinandan/integrationcli/client/connections"
20+
21+
"github.com/spf13/cobra"
22+
)
23+
24+
// ImportCmd to export connections
25+
var ImportCmd = &cobra.Command{
26+
Use: "import",
27+
Short: "Import connections to a region from a folder",
28+
Long: "Import connections to a region from a folder",
29+
Args: func(cmd *cobra.Command, args []string) (err error) {
30+
if err = apiclient.SetRegion(region); err != nil {
31+
return err
32+
}
33+
return apiclient.SetProjectID(project)
34+
},
35+
RunE: func(cmd *cobra.Command, args []string) (err error) {
36+
if err = apiclient.FolderExists(folder); err != nil {
37+
return err
38+
}
39+
40+
return connections.Import(folder)
41+
},
42+
}
43+
44+
func init() {
45+
ImportCmd.Flags().StringVarP(&folder, "folder", "f",
46+
"", "Folder to import connections")
47+
48+
_ = ImportCmd.MarkFlagRequired("folder")
49+
}

0 commit comments

Comments
 (0)