Skip to content

Commit

Permalink
Merge pull request #228 from GoogleCloudPlatform/issue227
Browse files Browse the repository at this point in the history
bug: handles entity names with underscore #227
  • Loading branch information
srinandan authored Mar 20, 2024
2 parents 6955573 + d6a1dc9 commit e686e59
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 21 deletions.
4 changes: 1 addition & 3 deletions internal/apiclient/bundles.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ func ClearEntityPayloadList() {
}

func ExtractTgz(gcsURL string) (folder string, err error) {

ctx := context.Background()

folder, err = os.MkdirTemp("", "integration")
Expand Down Expand Up @@ -206,7 +205,7 @@ func ExtractTgz(gcsURL string) (folder string, err error) {
switch header.Typeflag {
case tar.TypeDir:
// Create directory
if err := os.Mkdir(path.Join(folder, header.Name), 0755); err != nil {
if err := os.Mkdir(path.Join(folder, header.Name), 0o755); err != nil {
return "", fmt.Errorf("Error creating directory:", err)
}
case tar.TypeReg:
Expand Down Expand Up @@ -265,7 +264,6 @@ func GetCloudDeployGCSLocations(pipeline string, release string) (skaffoldConfig
}

func WriteResultsFile(deployOutputGCS string, status string) (err error) {

contents := fmt.Sprintf("{\"resultStatus\": \"%s\"}", status)
filename := "results.json"

Expand Down
1 change: 0 additions & 1 deletion internal/client/connections/connectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,6 @@ func create(name string, content []byte, serviceAccountName string, serviceAccou
} else {
*c.ConnectorVersion = fmt.Sprintf("projects/%s/locations/global/providers/%s/connectors/%s/versions/%d",
apiclient.GetProjectID(), c.ConnectorDetails.Provider, c.ConnectorDetails.Name, *c.ConnectorDetails.Version)

}

// remove the element
Expand Down
3 changes: 2 additions & 1 deletion internal/client/connections/custom-connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,8 @@ func IsCustomConnector(contents []byte) bool {
}

func CreateCustomWithVersion(name string, version string, contents []byte,
serviceAccount string, serviceAccountProject string) (err error) {
serviceAccount string, serviceAccountProject string,
) (err error) {
c := customConnectorOverrides{}
err = json.Unmarshal(contents, &c)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion internal/client/connections/eventsubscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ package connections

import (
"encoding/json"
"internal/apiclient"
"net/url"
"path"
"strconv"

"internal/apiclient"
)

type eventRequest struct {
Expand Down
3 changes: 2 additions & 1 deletion internal/client/integrations/integrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -1453,7 +1453,8 @@ func getIntegrationCustomConnection(connectionVersion eventparameter) integratio
}

func getIntegrationConnection(connectionName eventparameter,
connectionVersion eventparameter, configParams []parameterConfig) integrationConnection {
connectionVersion eventparameter, configParams []parameterConfig,
) integrationConnection {
ic := integrationConnection{}

// determine connection name.
Expand Down
2 changes: 0 additions & 2 deletions internal/client/integrations/overrides.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,6 @@ func handleGenericConnectorTask(taskConfig taskconfig, taskOverrides *overrides,
co.Parameters.ConnectionName = connName

}

} else if (eventparameter{}) != cparams && ok {
if cparams.Value.JsonValue != nil {
cd, err := getConnectionDetails(*cparams.Value.JsonValue)
Expand Down Expand Up @@ -530,7 +529,6 @@ func stringifyValue(cd connectiondetails) (string, error) {

// getConnectionStringFromConnectionName
func getConnectionStringFromConnectionName(connectionName string, iconfigParam []parameterConfig) (connection string, err error) {

var name string
if strings.HasPrefix(connectionName, "$`CONFIG_") {
for _, param := range iconfigParam {
Expand Down
6 changes: 4 additions & 2 deletions internal/cmd/connectors/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ var PatchCmd = &cobra.Command{
}

if len(updateMask) == 0 {
updateMask = []string{"destinationConfigs", "description",
updateMask = []string{
"destinationConfigs", "description",
"nodeConfig", "labels", "connectorVersion",
"configVariables", "authConfig", "logConfig", "sslConfig", "eventingEnablementType", "eventingConfig"}
"configVariables", "authConfig", "logConfig", "sslConfig", "eventingEnablementType", "eventingConfig",
}
}

_, err = connections.Patch(name, content, updateMask)
Expand Down
24 changes: 19 additions & 5 deletions internal/cmd/integrations/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ var ApplyCmd = &cobra.Command{
return apiclient.SetProjectID(cmdProject.Value.String())
},
RunE: func(cmd *cobra.Command, args []string) (err error) {

var skaffoldConfigUri string

if folder == "" {
Expand Down Expand Up @@ -174,7 +173,8 @@ func init() {
false, "Waits for the connector to finish, with success or error; default is false")
ApplyCmd.Flags().BoolVarP(&skipConnectors, "skip-connectors", "",
false, "Skip applying connector configuration; default is false")

ApplyCmd.Flags().BoolVarP(&useUnderscore, "use-underscore", "",
false, "Use underscore as a file splitter; default is __")
}

func getFilenameWithoutExtension(filname string) string {
Expand Down Expand Up @@ -381,18 +381,25 @@ func processConnectors(connectorsFolder string, grantPermission bool, createSecr

func processCustomConnectors(customConnectorsFolder string) (err error) {
var stat fs.FileInfo
var fileSplitter string
rJSONFiles := regexp.MustCompile(`(\S*)\.json`)

if useUnderscore {
fileSplitter = utils.LegacyFileSplitter
} else {
fileSplitter = utils.DefaultFileSplitter
}

if stat, err = os.Stat(customConnectorsFolder); err == nil && stat.IsDir() {
//create any custom connectors
// create any custom connectors
err = filepath.Walk(customConnectorsFolder, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
customConnectionFile := filepath.Base(path)
if rJSONFiles.MatchString(customConnectionFile) {
customConnectionDetails := strings.Split(strings.TrimSuffix(customConnectionFile, filepath.Ext(customConnectionFile)), "-")
customConnectionDetails := strings.Split(strings.TrimSuffix(customConnectionFile, filepath.Ext(customConnectionFile)), fileSplitter)
// the file format is name-version.json
if len(customConnectionDetails) == 2 {
clilog.Info.Printf("Found configuration for custom connection: %v\n", customConnectionFile)
Expand Down Expand Up @@ -463,9 +470,16 @@ func processSfdcInstances(sfdcinstancesFolder string) (err error) {

func processSfdcChannels(sfdcchannelsFolder string) (err error) {
var stat fs.FileInfo
var fileSplitter string
rJSONFiles := regexp.MustCompile(`(\S*)\.json`)
const sfdcNamingConvention = 2 // when file is split with _, the result must be 2

if useUnderscore {
fileSplitter = utils.LegacyFileSplitter
} else {
fileSplitter = utils.DefaultFileSplitter
}

if stat, err = os.Stat(sfdcchannelsFolder); err == nil && stat.IsDir() {
// create any sfdc channels
err = filepath.Walk(sfdcchannelsFolder, func(path string, info os.FileInfo, err error) error {
Expand All @@ -476,7 +490,7 @@ func processSfdcChannels(sfdcchannelsFolder string) (err error) {
channelFile := filepath.Base(path)
if rJSONFiles.MatchString(channelFile) {
clilog.Info.Printf("Found configuration for sfdc channel: %s\n", channelFile)
sfdcNames := strings.Split(getFilenameWithoutExtension(channelFile), "_")
sfdcNames := strings.Split(getFilenameWithoutExtension(channelFile), fileSplitter)
if len(sfdcNames) != sfdcNamingConvention {
clilog.Warning.Printf("sfdc chanel file %s does not follow the naming "+
"convention instanceName_channelName.json\n", channelFile)
Expand Down
19 changes: 14 additions & 5 deletions internal/cmd/integrations/scaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,19 @@ var ScaffoldCmd = &cobra.Command{
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
const jsonExt = ".json"
var fileSplitter string
var integrationBody, overridesBody []byte
version := cmd.Flag("ver").Value.String()
name := cmd.Flag("name").Value.String()

apiclient.DisableCmdPrintHttpResponse()

if useUnderscore {
fileSplitter = utils.LegacyFileSplitter
} else {
fileSplitter = utils.DefaultFileSplitter
}

if folder != "" {
if stat, err := os.Stat(folder); err != nil || !stat.IsDir() {
return fmt.Errorf("problem with supplied path, %w", err)
Expand Down Expand Up @@ -201,7 +208,7 @@ var ScaffoldCmd = &cobra.Command{
if err = generateFolder(path.Join(folder, "connectors")); err != nil {
return err
}
//check for custom connectors
// check for custom connectors
for _, connector := range connectors {
if connector.CustomConnection {
if err = generateFolder(path.Join(folder, "custom-connectors")); err != nil {
Expand All @@ -222,7 +229,7 @@ var ScaffoldCmd = &cobra.Command{
return err
}
if err = apiclient.WriteByteArrayToFile(
path.Join(folder, "custom-connectors", connector.Name+"-"+connector.Version+jsonExt),
path.Join(folder, "custom-connectors", connector.Name+fileSplitter+connector.Version+jsonExt),
false,
customConnectionResp); err != nil {
return err
Expand Down Expand Up @@ -282,7 +289,7 @@ var ScaffoldCmd = &cobra.Command{
}
clilog.Info.Printf("Storing sfdcchannel %s\n", channelName)
if err = apiclient.WriteByteArrayToFile(
path.Join(folder, "sfdcchannels", instanceName+"_"+channelName+jsonExt),
path.Join(folder, "sfdcchannels", instanceName+fileSplitter+channelName+jsonExt),
false,
channelBytes); err != nil {
return err
Expand Down Expand Up @@ -322,8 +329,8 @@ var ScaffoldCmd = &cobra.Command{
}

var (
cloudBuild, cloudDeploy, skipConnectors bool
env string
cloudBuild, cloudDeploy, skipConnectors, useUnderscore bool
env string
)

func init() {
Expand All @@ -347,6 +354,8 @@ func init() {
"", "Environment name for the scaffolding")
ScaffoldCmd.Flags().BoolVarP(&skipConnectors, "skip-connectors", "",
false, "Exclude connectors from scaffold")
ScaffoldCmd.Flags().BoolVarP(&useUnderscore, "use-underscore", "",
false, "Use underscore as a file splitter; default is __")

_ = ScaffoldCmd.MarkFlagRequired("name")
}
Expand Down
5 changes: 5 additions & 0 deletions internal/cmd/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ import (
"os"
)

const (
DefaultFileSplitter = "__"
LegacyFileSplitter = "_"
)

const cloudBuild = `# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down

0 comments on commit e686e59

Please sign in to comment.