Skip to content

Commit

Permalink
chore: update create config function
Browse files Browse the repository at this point in the history
  • Loading branch information
Chief-Rishab committed Aug 9, 2022
1 parent 3718c32 commit b70e3ec
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 30 deletions.
27 changes: 0 additions & 27 deletions plugins/providers/gcs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,11 @@ const (

type Config struct {
ProviderConfig *domain.ProviderConfig
valid bool

crypto domain.Crypto
validator *validator.Validate
}

func (c *Config) EncryptCredentials() error {
if err := c.parseAndValidate(); err != nil {
return err
}

credentials, ok := c.ProviderConfig.Credentials.(*Credentials)
if !ok {
return ErrInvalidCredentialsType
}

if err := credentials.Encrypt(c.crypto); err != nil {
return err
}

c.ProviderConfig.Credentials = credentials
return nil
}

type Credentials struct {
ServiceAccountKey string `json:"service_account_key" mapstructure:"service_account_key" validate:"required,base64"`
ResourceName string `json:"resource_name" mapstructure:"resource_name" validate:"required"`
Expand Down Expand Up @@ -97,14 +78,7 @@ func NewConfig(pc *domain.ProviderConfig, crypto domain.Crypto) *Config {
}
}

func (c *Config) ParseAndValidate() error {
return c.parseAndValidate()
}

func (c *Config) parseAndValidate() error {
if c.valid {
return nil
}

validationError := []error{}

Expand All @@ -129,7 +103,6 @@ func (c *Config) parseAndValidate() error {
return errors.New(strings.Join(errorStrings, "\n"))
}

c.valid = true
return nil
}

Expand Down
14 changes: 12 additions & 2 deletions plugins/providers/gcs/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,21 @@ func NewProvider(typeName string, crypto Crypto) *Provider {
func (p *Provider) CreateConfig(pc *domain.ProviderConfig) error {
c := NewConfig(pc, p.crypto)

if err := c.ParseAndValidate(); err != nil {
if err := c.parseAndValidate(); err != nil {
return err
}

return c.EncryptCredentials()
credentials, ok := c.ProviderConfig.Credentials.(*Credentials)
if !ok {
return ErrInvalidCredentialsType
}

if err := credentials.Encrypt(c.crypto); err != nil {
return fmt.Errorf("encrypting creds: %w", err)
}

c.ProviderConfig.Credentials = credentials
return nil
}

func (p *Provider) GetResources(pc *domain.ProviderConfig) ([]*domain.Resource, error) {
Expand Down
2 changes: 1 addition & 1 deletion plugins/providers/gcs/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func TestCreateConfig(t *testing.T) {
crypto.On("Encrypt", `{"type":"service_account"}`).Return("", expectedError)
actualError := p.CreateConfig(pc)

assert.Equal(t, expectedError, actualError)
assert.ErrorIs(t, actualError, expectedError)
})

t.Run("should make the provider config, parse and validate the credentials and permissions and return nil error on success", func(t *testing.T) {
Expand Down

0 comments on commit b70e3ec

Please sign in to comment.