Skip to content

Commit

Permalink
CloudProvider functionality (#22)
Browse files Browse the repository at this point in the history
- Added cloud provider CRUD
- Refactored secrets
  • Loading branch information
micahlmartin authored Jun 29, 2021
1 parent d659a35 commit 7aa4759
Show file tree
Hide file tree
Showing 25 changed files with 1,494 additions and 220 deletions.
42 changes: 42 additions & 0 deletions harness/api/api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package api

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestEnvVarsAreSet(t *testing.T) {
assert.NotEmpty(t, TestEnvVars.AwsAccessKeyId.Get())
assert.NotEmpty(t, TestEnvVars.AwsSecretAccessKey.Get())
assert.NotEmpty(t, TestEnvVars.AzureClientId.Get())
assert.NotEmpty(t, TestEnvVars.AzureClientSecret.Get())
assert.NotEmpty(t, TestEnvVars.AzureTenantId.Get())
assert.NotEmpty(t, TestEnvVars.SpotInstAccountId.Get())
assert.NotEmpty(t, TestEnvVars.SpotInstToken.Get())
}

type EnvVar string

var TestEnvVars = struct {
AwsAccessKeyId EnvVar
AwsSecretAccessKey EnvVar
AzureClientId EnvVar
AzureClientSecret EnvVar
AzureTenantId EnvVar
SpotInstAccountId EnvVar
SpotInstToken EnvVar
}{
AwsAccessKeyId: "HARNESS_TEST_AWS_ACCESS_KEY_ID",
AwsSecretAccessKey: "HARNESS_TEST_AWS_SECRET_ACCESS_KEY",
AzureClientId: "HARNESS_TEST_AZURE_CLIENT_ID",
AzureClientSecret: "HARNESS_TEST_AZURE_CLIENT_SECRET",
AzureTenantId: "HARNESS_TEST_AZURE_TENANT_ID",
SpotInstAccountId: "HARNESS_TEST_SPOT_ACCT_ID",
SpotInstToken: "HARNESS_TEST_SPOT_TOKEN",
}

func (e EnvVar) Get() string {
return os.Getenv(string(e))
}
51 changes: 51 additions & 0 deletions harness/api/aws_cloudprovider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package api

import (
"fmt"

"github.com/harness-io/harness-go-sdk/harness/api/graphql"
)

func (c *CloudProviderClient) GetAwsCloudProviderById(Id string) (*graphql.AwsCloudProvider, error) {
cp := &graphql.AwsCloudProvider{}
err := c.getCloudProviderById(Id, getAwsCloudProviderFields(), &cp)
if err != nil {
return nil, err
}

return cp, nil
}

func (c *CloudProviderClient) GetAwsCloudProviderByName(name string) (*graphql.AwsCloudProvider, error) {
cp := &graphql.AwsCloudProvider{}
err := c.getCloudProviderByName(name, getAwsCloudProviderFields(), &cp)
if err != nil {
return nil, err
}

return cp, nil
}

func (c *CloudProviderClient) CreateAwsCloudProvider(provider *graphql.AwsCloudProvider) (*graphql.AwsCloudProvider, error) {
input := &graphql.CreateCloudProviderInput{
CloudProviderType: graphql.CloudProviderTypes.Aws,
AwsCloudProvider: provider,
}

resp := &graphql.AwsCloudProvider{}
err := c.createCloudProvider(input, getAwsCloudProviderFields(), resp)
if err != nil {
return nil, err
}

return resp, nil
}

func getAwsCloudProviderFields() string {
return fmt.Sprintf(`
%[1]s
... on AwsCloudProvider {
%[2]s
}
`, commonCloudProviderFields, ceHealthStatusFields)
}
76 changes: 76 additions & 0 deletions harness/api/aws_cloudprovider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package api

import (
"fmt"
"testing"

"github.com/harness-io/harness-go-sdk/harness/api/graphql"
"github.com/harness-io/harness-go-sdk/harness/utils"
"github.com/stretchr/testify/require"
)

func TestGetAwsCloudProviderById(t *testing.T) {
c := getClient()
expectedName := fmt.Sprintf("%s_%s", t.Name(), utils.RandStringBytes(4))

cp, err := createAwsCloudProvider(expectedName)
require.NoError(t, err)

foundCP, err := c.CloudProviders().GetAwsCloudProviderById(cp.Id)
require.NoError(t, err)
require.NotNil(t, foundCP)
require.Equal(t, cp.Id, foundCP.Id)

err = c.CloudProviders().DeleteCloudProvider(cp.Id)
require.NoError(t, err)
}

func TestGetAwsCloudProviderByName(t *testing.T) {
c := getClient()
expectedName := fmt.Sprintf("%s_%s", t.Name(), utils.RandStringBytes(4))

cp, err := createAwsCloudProvider(expectedName)
require.NoError(t, err)

foundCP, err := c.CloudProviders().GetAwsCloudProviderByName(expectedName)
require.NoError(t, err)
require.NotNil(t, foundCP)
require.Equal(t, expectedName, foundCP.Name)

err = c.CloudProviders().DeleteCloudProvider(cp.Id)
require.NoError(t, err)
}

func TestCreateAwsCloudProvider(t *testing.T) {
c := getClient()
expectedName := fmt.Sprintf("%s_%s", t.Name(), utils.RandStringBytes(4))

cp, err := createAwsCloudProvider(expectedName)
require.NoError(t, err)
require.NotNil(t, cp)
require.Equal(t, expectedName, cp.Name)

err = c.CloudProviders().DeleteCloudProvider(cp.Id)
require.NoError(t, err)
}

func createAwsCloudProvider(name string) (*graphql.AwsCloudProvider, error) {

c := getClient()
expectedName := name

secret, err := createEncryptedTextSecret(expectedName, TestEnvVars.AwsSecretAccessKey.Get())
if err != nil {
return nil, err
}

input := &graphql.AwsCloudProvider{}
input.Name = expectedName
input.CredentialsType = graphql.AwsCredentialsTypes.Manual
input.ManualCredentials = &graphql.AwsManualCredentials{
AccessKey: TestEnvVars.AwsAccessKeyId.Get(),
SecretKeySecretId: secret.Id,
}

return c.CloudProviders().CreateAwsCloudProvider(input)
}
48 changes: 48 additions & 0 deletions harness/api/azure_cloudprovider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package api

import (
"fmt"

"github.com/harness-io/harness-go-sdk/harness/api/graphql"
)

func (c *CloudProviderClient) GetAzureCloudProviderById(Id string) (*graphql.AzureCloudProvider, error) {
cp := &graphql.AzureCloudProvider{}
err := c.getCloudProviderById(Id, getAzureCloudProviderFields(), &cp)
if err != nil {
return nil, err
}

return cp, nil
}

func (c *CloudProviderClient) GetAzureCloudProviderByName(name string) (*graphql.AzureCloudProvider, error) {
cp := &graphql.AzureCloudProvider{}
err := c.getCloudProviderByName(name, getAzureCloudProviderFields(), &cp)
if err != nil {
return nil, err
}

return cp, nil
}

func (c *CloudProviderClient) CreateAzureCloudProvider(provider *graphql.AzureCloudProvider) (*graphql.AzureCloudProvider, error) {
input := &graphql.CreateCloudProviderInput{
CloudProviderType: graphql.CloudProviderTypes.Azure,
AzureCloudProvider: provider,
}

resp := &graphql.AzureCloudProvider{}
err := c.createCloudProvider(input, getAzureCloudProviderFields(), resp)
if err != nil {
return nil, err
}

return resp, nil
}

func getAzureCloudProviderFields() string {
return fmt.Sprintf(`
%[1]s
`, commonCloudProviderFields)
}
79 changes: 79 additions & 0 deletions harness/api/azure_cloudprovider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package api

import (
"fmt"
"testing"

"github.com/harness-io/harness-go-sdk/harness/api/graphql"
"github.com/harness-io/harness-go-sdk/harness/utils"
"github.com/stretchr/testify/require"
)

func TestGetAzureCloudProviderById(t *testing.T) {
c := getClient()
expectedName := fmt.Sprintf("%s_%s", t.Name(), utils.RandStringBytes(4))

cp, err := createAzureCloudProvider(expectedName)
require.NoError(t, err)

foundCP, err := c.CloudProviders().GetAzureCloudProviderById(cp.Id)
require.NoError(t, err)
require.NotNil(t, foundCP)
require.Equal(t, cp.Id, foundCP.Id)

err = c.CloudProviders().DeleteCloudProvider(cp.Id)
require.NoError(t, err)
}

func TestGetAzureCloudProviderByName(t *testing.T) {
c := getClient()
expectedName := fmt.Sprintf("%s_%s", t.Name(), utils.RandStringBytes(4))

cp, err := createAzureCloudProvider(expectedName)
require.NoError(t, err)

foundCP, err := c.CloudProviders().GetAzureCloudProviderByName(expectedName)
require.NoError(t, err)
require.NotNil(t, foundCP)
require.Equal(t, expectedName, foundCP.Name)

err = c.CloudProviders().DeleteCloudProvider(cp.Id)
require.NoError(t, err)
}

func TestCreateAzureCloudProvider(t *testing.T) {
c := getClient()

expectedName := fmt.Sprintf("%s_%s", t.Name(), utils.RandStringBytes(4))

cp, err := createAzureCloudProvider(expectedName)
require.NoError(t, err)
require.NotNil(t, cp)
require.Equal(t, expectedName, cp.Name)

err = c.CloudProviders().DeleteCloudProvider(cp.Id)
require.NoError(t, err)

secret, err := c.Secrets().GetEncryptedTextByName(expectedName)
require.NoError(t, err)
c.Secrets().DeleteSecret(secret.Id, secret.SecretType)
}

func createAzureCloudProvider(name string) (*graphql.AzureCloudProvider, error) {

c := getClient()
expectedName := name

secret, err := createEncryptedTextSecret(expectedName, TestEnvVars.AzureClientSecret.Get())
if err != nil {
return nil, err
}

input := &graphql.AzureCloudProvider{}
input.Name = expectedName
input.ClientId = TestEnvVars.AzureClientId.Get()
input.KeySecretId = secret.Id
input.TenantId = TestEnvVars.AzureTenantId.Get()

return c.CloudProviders().CreateAzureCloudProvider(input)
}
Loading

0 comments on commit 7aa4759

Please sign in to comment.