Skip to content

Commit

Permalink
retrieved mapping config parser changes
Browse files Browse the repository at this point in the history
Signed-off-by: Pappu Kumar <pappu.kumar@progress.com>
  • Loading branch information
Pappu Kumar authored and Dmaddu committed Sep 2, 2023
1 parent e1419f1 commit 4ed8761
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 8 deletions.
58 changes: 50 additions & 8 deletions components/automate-cli/pkg/verifyserver/models/batchcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@ import (
"strings"

"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/sirupsen/logrus"

"github.com/chef/automate/lib/config"
"github.com/gofiber/fiber/v2"
)

const (
AWS_S3 = "s3"
GCP_CLOUD_STORAGE = "gcs"
)

type BatchCheckRequest struct {
Checks []string `json:"checks"`
Config *Config `json:"config"`
Expand Down Expand Up @@ -271,15 +278,50 @@ func (c *Config) populateCommonConfig(haConfig *config.HaDeployConfig) error {
}

func (c *Config) populateObjectStorageConfig(haConfig *config.HaDeployConfig) {
if haConfig == nil {
logrus.Errorln("haConfig cannot be nil")
return
}

objectStorageConfig := haConfig.GetObjectStorageConfig()
c.Backup = &Backup{
ObjectStorage: &ObjectStorage{
BucketName: objectStorageConfig.BucketName,
AWSRegion: objectStorageConfig.Region,
AccessKey: objectStorageConfig.AccessKey,
SecretKey: objectStorageConfig.SecretKey,
Endpoint: objectStorageConfig.Endpoint,
},
if objectStorageConfig.Location == "" {
logrus.Errorln("object storage location cannot be empty")
return
}

if objectStorageConfig.Location == AWS_S3 {
c.Backup = &Backup{
ObjectStorage: &ObjectStorage{
BucketName: objectStorageConfig.BucketName,
AWSRegion: objectStorageConfig.Region,
AccessKey: objectStorageConfig.AccessKey,
SecretKey: objectStorageConfig.SecretKey,
Endpoint: objectStorageConfig.Endpoint,
},
}
} else if objectStorageConfig.Location == GCP_CLOUD_STORAGE {
c.Backup = &Backup{
ObjectStorage: &ObjectStorage{
BucketName: objectStorageConfig.BucketName,
GoogleServiceAccountFile: objectStorageConfig.GoogleServiceAccountFile,
GcpServiceAccount: &GcpServiceAccount{
Type: objectStorageConfig.GcpServiceAccount.Type,
ProjectID: objectStorageConfig.GcpServiceAccount.ProjectID,
PrivateKeyID: objectStorageConfig.GcpServiceAccount.PrivateKeyID,
PrivateKey: objectStorageConfig.GcpServiceAccount.PrivateKey,
ClientEmail: objectStorageConfig.GcpServiceAccount.ClientEmail,
ClientID: objectStorageConfig.GcpServiceAccount.ClientID,
AuthURI: objectStorageConfig.GcpServiceAccount.AuthURI,
TokenURI: objectStorageConfig.GcpServiceAccount.TokenURI,
AuthProviderX509CertURL: objectStorageConfig.GcpServiceAccount.AuthProviderX509CertURL,
ClientX509CertURL: objectStorageConfig.GcpServiceAccount.ClientX509CertURL,
UniverseDomain: objectStorageConfig.GcpServiceAccount.UniverseDomain,
},
},
}
} else {
logrus.Errorf("invalid location: %s", objectStorageConfig.Location)
return
}
}

Expand Down
138 changes: 138 additions & 0 deletions components/automate-cli/pkg/verifyserver/models/batchcheck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/chef/automate/lib/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var (
Expand Down Expand Up @@ -632,3 +633,140 @@ func TestPopulateWith(t *testing.T) {
})
}
}

func TestPopulateObjectStorageConfig(t *testing.T) {
t.Run("Nil Config", func(t *testing.T) {
mockConfig := &Config{}
mockConfig.populateObjectStorageConfig(nil)
require.Empty(t, mockConfig)
})
t.Run("Empty Location", func(t *testing.T) {
mockHaDeployConfig := &config.HaDeployConfig{
ObjectStorage: &config.ObjectStorage{
Config: &config.ConfigObjectStorage{
BucketName: "dummybucket",
AccessKey: "access",
SecretKey: "secret",
Endpoint: "endpoint",
Region: "Somewhere",
},
},
Architecture: &config.Architecture{
Aws: &config.ConfigInitials{
SSHUser: "ec2-user",
},
ExistingInfra: &config.ConfigInitials{
SSHUser: "ec2-user",
},
},
}
mockConfig := &Config{}
mockConfig.populateObjectStorageConfig(mockHaDeployConfig)
require.Empty(t, mockConfig)
})
t.Run("Not Supported Location", func(t *testing.T) {
mockHaDeployConfig := &config.HaDeployConfig{
ObjectStorage: &config.ObjectStorage{
Config: &config.ConfigObjectStorage{
Location: "Azure",
BucketName: "dummybucket",
AccessKey: "access",
SecretKey: "secret",
Endpoint: "endpoint",
Region: "Somewhere",
},
},
Architecture: &config.Architecture{
Aws: &config.ConfigInitials{
SSHUser: "ec2-user",
},
ExistingInfra: &config.ConfigInitials{
SSHUser: "ec2-user",
},
},
}
mockConfig := &Config{}
mockConfig.populateObjectStorageConfig(mockHaDeployConfig)
require.Empty(t, mockConfig)
})

t.Run("Valid Config S3", func(t *testing.T) {
mockHaDeployConfig := &config.HaDeployConfig{
ObjectStorage: &config.ObjectStorage{
Config: &config.ConfigObjectStorage{
Location: AWS_S3,
BucketName: "dummybucket",
AccessKey: "access",
SecretKey: "secret",
Endpoint: "endpoint",
Region: "Somewhere",
},
},
Architecture: &config.Architecture{
Aws: &config.ConfigInitials{
SSHUser: "ec2-user",
},
ExistingInfra: &config.ConfigInitials{
SSHUser: "ec2-user",
},
},
}

mockConfig := &Config{}
mockConfig.populateObjectStorageConfig(mockHaDeployConfig)
require.NotNil(t, mockConfig)
require.Equal(t, mockConfig.Backup.ObjectStorage.AWSRegion, "Somewhere")
require.Equal(t, mockConfig.Backup.ObjectStorage.BucketName, "dummybucket")
require.Equal(t, mockConfig.Backup.ObjectStorage.AccessKey, "access")
require.Equal(t, mockConfig.Backup.ObjectStorage.SecretKey, "secret")
require.Equal(t, mockConfig.Backup.ObjectStorage.Endpoint, "endpoint")
})

t.Run("Valid Config GCS", func(t *testing.T) {
mockHaDeployConfig := &config.HaDeployConfig{
ObjectStorage: &config.ObjectStorage{
Config: &config.ConfigObjectStorage{
Location: GCP_CLOUD_STORAGE,
BucketName: "dummybucket",
GcpServiceAccount: &config.GcpServiceAccount{
Type: "GcpServiceAccount.Type",
ProjectID: "GcpServiceAccount.ProjectID",
PrivateKeyID: "GcpServiceAccount.PrivateKeyID",
PrivateKey: "GcpServiceAccount.PrivateKey",
ClientEmail: "GcpServiceAccount.ClientEmail",
ClientID: "GcpServiceAccount.ClientID",
AuthURI: "GcpServiceAccount.AuthURI",
TokenURI: "GcpServiceAccount.TokenURI",
AuthProviderX509CertURL: "GcpServiceAccount.AuthProviderX509CertURL",
ClientX509CertURL: "GcpServiceAccount.ClientX509CertURL",
UniverseDomain: "GcpServiceAccount.UniverseDomain",
},
},
},
Architecture: &config.Architecture{
Aws: &config.ConfigInitials{
SSHUser: "ec2-user",
},
ExistingInfra: &config.ConfigInitials{
SSHUser: "ec2-user",
},
},
}

mockConfig := &Config{}
mockConfig.populateObjectStorageConfig(mockHaDeployConfig)
require.NotNil(t, mockConfig)
require.Equal(t, mockConfig.Backup.ObjectStorage.BucketName, "dummybucket")
require.Equal(t, mockConfig.Backup.ObjectStorage.GcpServiceAccount.Type, "GcpServiceAccount.Type")
require.Equal(t, mockConfig.Backup.ObjectStorage.GcpServiceAccount.ProjectID, "GcpServiceAccount.ProjectID")
require.Equal(t, mockConfig.Backup.ObjectStorage.GcpServiceAccount.PrivateKeyID, "GcpServiceAccount.PrivateKeyID")
require.Equal(t, mockConfig.Backup.ObjectStorage.GcpServiceAccount.PrivateKey, "GcpServiceAccount.PrivateKey")
require.Equal(t, mockConfig.Backup.ObjectStorage.GcpServiceAccount.ClientEmail, "GcpServiceAccount.ClientEmail")
require.Equal(t, mockConfig.Backup.ObjectStorage.GcpServiceAccount.ClientID, "GcpServiceAccount.ClientID")
require.Equal(t, mockConfig.Backup.ObjectStorage.GcpServiceAccount.AuthURI, "GcpServiceAccount.AuthURI")
require.Equal(t, mockConfig.Backup.ObjectStorage.GcpServiceAccount.TokenURI, "GcpServiceAccount.TokenURI")
require.Equal(t, mockConfig.Backup.ObjectStorage.GcpServiceAccount.AuthProviderX509CertURL, "GcpServiceAccount.AuthProviderX509CertURL")
require.Equal(t, mockConfig.Backup.ObjectStorage.GcpServiceAccount.ClientX509CertURL, "GcpServiceAccount.ClientX509CertURL")
require.Equal(t, mockConfig.Backup.ObjectStorage.GcpServiceAccount.UniverseDomain, "GcpServiceAccount.UniverseDomain")
})
}

0 comments on commit 4ed8761

Please sign in to comment.