-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added cloud provider CRUD - Refactored secrets
- Loading branch information
1 parent
d659a35
commit 7aa4759
Showing
25 changed files
with
1,494 additions
and
220 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.