Skip to content

Commit

Permalink
Revert "Revert "Provider > Add link API between on-prem VCS and Agent…
Browse files Browse the repository at this point in the history
…-Pool ""
  • Loading branch information
penja committed Apr 20, 2023
1 parent 77c82c6 commit 75bf8ca
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 27 deletions.
6 changes: 3 additions & 3 deletions access_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestAccessTokenRead(t *testing.T) {
client := testClient(t)
ctx := context.Background()

ap, apCleanup := createAgentPool(t, client)
ap, apCleanup := createAgentPool(t, client, false)
defer apCleanup()

atTest, atTestCleanup := createAgentPoolToken(t, client, ap.ID)
Expand All @@ -40,7 +40,7 @@ func TestAccessTokenUpdate(t *testing.T) {
client := testClient(t)
ctx := context.Background()

ap, apCleanup := createAgentPool(t, client)
ap, apCleanup := createAgentPool(t, client, false)
defer apCleanup()

apt, aptCleanup := createAgentPoolToken(t, client, ap.ID)
Expand Down Expand Up @@ -75,7 +75,7 @@ func TestAccessTokenDelete(t *testing.T) {
client := testClient(t)
ctx := context.Background()

ap, apCleanup := createAgentPool(t, client)
ap, apCleanup := createAgentPool(t, client, false)
defer apCleanup()

apt, _ := createAgentPoolToken(t, client, ap.ID)
Expand Down
12 changes: 7 additions & 5 deletions agent_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ type AgentPoolList struct {

// AgentPool represents a Scalr agent pool.
type AgentPool struct {
ID string `jsonapi:"primary,agent-pools"`
Name string `jsonapi:"attr,name"`

ID string `jsonapi:"primary,agent-pools"`
Name string `jsonapi:"attr,name"`
VcsEnabled bool `jsonapi:"attr,vcs-enabled"`
// Relations

// The agent pool's scope
Expand All @@ -51,8 +51,9 @@ type AgentPool struct {

// AgentPoolCreateOptions represents the options for creating a new AgentPool.
type AgentPoolCreateOptions struct {
ID string `jsonapi:"primary,agent-pools"`
Name *string `jsonapi:"attr,name"`
ID string `jsonapi:"primary,agent-pools"`
Name *string `jsonapi:"attr,name"`
VcsEnabled *bool `jsonapi:"attr,vcs-enabled,omitempty"`

// The agent pool's scope
Account *Account `jsonapi:"relation,account"`
Expand Down Expand Up @@ -96,6 +97,7 @@ type AgentPoolListOptions struct {
Environment *string `url:"filter[environment],omitempty"`
Name string `url:"filter[name],omitempty"`
AgentPool string `url:"filter[agent-pool],omitempty"`
VcsEnabled *bool `url:"filter[vcs-enabled],omitempty"`
Include string `url:"include,omitempty"`
}

Expand Down
58 changes: 51 additions & 7 deletions agent_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ func TestAgentPoolsList(t *testing.T) {
client := testClient(t)
ctx := context.Background()

agentPoolTest1, agentPoolTest1Cleanup := createAgentPool(t, client)
agentPoolTest1, agentPoolTest1Cleanup := createAgentPool(t, client, false)
defer agentPoolTest1Cleanup()
agentPoolTest2, agentPoolTest2Cleanup := createAgentPool(t, client)
agentPoolTest2, agentPoolTest2Cleanup := createAgentPool(t, client, true)
defer agentPoolTest2Cleanup()
agentPoolTest3, agentPoolTest3Cleanup := createAgentPool(t, client, true)
defer agentPoolTest3Cleanup()

t.Run("without options", func(t *testing.T) {
apList, err := client.AgentPools.List(ctx, AgentPoolListOptions{})
Expand All @@ -27,6 +29,7 @@ func TestAgentPoolsList(t *testing.T) {
}
assert.Contains(t, apListIDs, agentPoolTest1.ID)
assert.Contains(t, apListIDs, agentPoolTest2.ID)
assert.Contains(t, apListIDs, agentPoolTest3.ID)
})
t.Run("with account filter", func(t *testing.T) {
apList, err := client.AgentPools.List(ctx, AgentPoolListOptions{Account: String(defaultAccountID)})
Expand All @@ -37,6 +40,7 @@ func TestAgentPoolsList(t *testing.T) {
}
assert.Contains(t, apListIDs, agentPoolTest1.ID)
assert.Contains(t, apListIDs, agentPoolTest2.ID)
assert.Contains(t, apListIDs, agentPoolTest3.ID)
})
t.Run("with account and name filter", func(t *testing.T) {
apList, err := client.AgentPools.List(ctx, AgentPoolListOptions{Account: String(defaultAccountID), Name: agentPoolTest1.Name})
Expand All @@ -50,6 +54,18 @@ func TestAgentPoolsList(t *testing.T) {
assert.Len(t, apList.Items, 1)
assert.Equal(t, apList.Items[0].ID, agentPoolTest2.ID)
})
t.Run("with vcs-enabled filter", func(t *testing.T) {
var vcsEnabledFiler = true
apList, err := client.AgentPools.List(ctx, AgentPoolListOptions{VcsEnabled: &vcsEnabledFiler})
require.NoError(t, err)
apListIDs := make([]string, 0)
for _, agentPool := range apList.Items {
apListIDs = append(apListIDs, agentPool.ID)
}
assert.Len(t, apListIDs, 2)
assert.Contains(t, apListIDs, agentPoolTest2.ID)
assert.Contains(t, apListIDs, agentPoolTest3.ID)
})
}

func TestAgentPoolsCreate(t *testing.T) {
Expand All @@ -58,8 +74,9 @@ func TestAgentPoolsCreate(t *testing.T) {

t.Run("when account and name are provided", func(t *testing.T) {
options := AgentPoolCreateOptions{
Account: &Account{ID: defaultAccountID},
Name: String("test-provider-pool-" + randomString(t)),
Account: &Account{ID: defaultAccountID},
Name: String("test-provider-pool-" + randomString(t)),
VcsEnabled: Bool(true),
}

agentPool, err := client.AgentPools.Create(ctx, options)
Expand All @@ -76,6 +93,31 @@ func TestAgentPoolsCreate(t *testing.T) {
assert.NotEmpty(t, item.ID)
assert.Equal(t, *options.Name, item.Name)
assert.Equal(t, options.Account, item.Account)
assert.Equal(t, *options.VcsEnabled, item.VcsEnabled)
}
err = client.AgentPools.Delete(ctx, agentPool.ID)
require.NoError(t, err)
})

t.Run("when create without vcs_enabled", func(t *testing.T) {
options := AgentPoolCreateOptions{
Account: &Account{ID: defaultAccountID},
Name: String("test-provider-pool-" + randomString(t)),
}

agentPool, err := client.AgentPools.Create(ctx, options)
require.NoError(t, err)

// Get a refreshed view from the API.
refreshed, err := client.AgentPools.Read(ctx, agentPool.ID)
require.NoError(t, err)

for _, item := range []*AgentPool{
agentPool,
refreshed,
} {
assert.NotEmpty(t, item.ID)
assert.Equal(t, item.VcsEnabled, false)
}
err = client.AgentPools.Delete(ctx, agentPool.ID)
require.NoError(t, err)
Expand All @@ -90,6 +132,7 @@ func TestAgentPoolsCreate(t *testing.T) {
Account: &Account{ID: defaultAccountID},
Environment: &Environment{ID: env.ID},
Name: String("test-provider-pool-" + randomString(t)),
VcsEnabled: Bool(false),
}

agentPool, err := client.AgentPools.Create(ctx, options)
Expand Down Expand Up @@ -123,6 +166,7 @@ func TestAgentPoolsCreate(t *testing.T) {
Environment: &Environment{ID: env.ID},
Workspaces: []*Workspace{{ID: ws.ID}},
Name: String("test-provider-pool-" + randomString(t)),
VcsEnabled: Bool(false),
}

agentPool, err := client.AgentPools.Create(ctx, options)
Expand Down Expand Up @@ -241,7 +285,7 @@ func TestAgentPoolsRead(t *testing.T) {
client := testClient(t)
ctx := context.Background()

agentPoolTest, agentPoolTestCleanup := createAgentPool(t, client)
agentPoolTest, agentPoolTestCleanup := createAgentPool(t, client, false)
defer agentPoolTestCleanup()

t.Run("when the agentPool exists", func(t *testing.T) {
Expand Down Expand Up @@ -278,7 +322,7 @@ func TestAgentPoolsUpdate(t *testing.T) {
client := testClient(t)
ctx := context.Background()

agentPoolTest, agentPoolTestCleanup := createAgentPool(t, client)
agentPoolTest, agentPoolTestCleanup := createAgentPool(t, client, false)
defer agentPoolTestCleanup()

t.Run("when updating a name", func(t *testing.T) {
Expand Down Expand Up @@ -337,7 +381,7 @@ func TestAgentPoolsDelete(t *testing.T) {
client := testClient(t)
ctx := context.Background()

pool, _ := createAgentPool(t, client)
pool, _ := createAgentPool(t, client, false)

t.Run("with valid agent pool id", func(t *testing.T) {
err := client.AgentPools.Delete(ctx, pool.ID)
Expand Down
4 changes: 2 additions & 2 deletions agent_pool_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestAgentPoolTokenList(t *testing.T) {
client := testClient(t)
ctx := context.Background()

ap, apCleanup := createAgentPool(t, client)
ap, apCleanup := createAgentPool(t, client, false)
defer apCleanup()

apt, aptCleanup := createAgentPoolToken(t, client, ap.ID)
Expand Down Expand Up @@ -41,7 +41,7 @@ func TestAgentPoolTokenCreate(t *testing.T) {
client := testClient(t)
ctx := context.Background()

ap, apCleanup := createAgentPool(t, client)
ap, apCleanup := createAgentPool(t, client, false)
defer apCleanup()

t.Run("when description is provided", func(t *testing.T) {
Expand Down
7 changes: 4 additions & 3 deletions helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ func createEnvironment(t *testing.T, client *Client) (*Environment, func()) {
}
}

func createAgentPool(t *testing.T, client *Client) (*AgentPool, func()) {
func createAgentPool(t *testing.T, client *Client, vcsEnabled bool) (*AgentPool, func()) {
ctx := context.Background()
ap, err := client.AgentPools.Create(ctx, AgentPoolCreateOptions{
Name: String("provider-tst-pool-" + randomString(t)),
Account: &Account{ID: defaultAccountID},
Name: String("provider-tst-pool-" + randomString(t)),
Account: &Account{ID: defaultAccountID},
VcsEnabled: Bool(vcsEnabled),
})
if err != nil {
t.Fatal(err)
Expand Down
10 changes: 6 additions & 4 deletions run_triggers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,16 @@ func TestRunTriggersRead(t *testing.T) {
Downstream: &Downstream{ID: wsTest1.ID},
Upstream: &Upstream{ID: wsTest2.ID},
}
created_trigger, err := client.RunTriggers.Create(ctx, options)
createdTrigger, err := client.RunTriggers.Create(ctx, options)
require.NoError(t, err)
assert.NotEmpty(t, created_trigger.ID)
assert.NotEmpty(t, createdTrigger.ID)

t.Run("get run trigger by id", func(t *testing.T) {
trigger, err := client.RunTriggers.Read(ctx, created_trigger.ID)
trigger, err := client.RunTriggers.Read(ctx, createdTrigger.ID)
require.NoError(t, err)
assert.Equal(t, created_trigger, trigger)
assert.Equal(t, createdTrigger.ID, trigger.ID)
assert.Equal(t, createdTrigger.Upstream.ID, trigger.Upstream.ID)
assert.Equal(t, createdTrigger.Downstream.ID, trigger.Downstream.ID)
})

t.Run("try to get run trigger with not valid ID", func(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions vcs_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ type VcsProvider struct {
// Relations
Environments []*Environment `jsonapi:"relation,environments"`
Account *Account `jsonapi:"relation,account"`
AgentPool *AgentPool `jsonapi:"relation,agent-pool"`
}

// VcsProvidersListOptions represents the options for listing vcs providers.
Expand All @@ -96,6 +97,7 @@ type VcsProvidersListOptions struct {
// Scope filters.
Environment *string `url:"filter[environment],omitempty"`
Account *string `url:"filter[account],omitempty"`
AgentPool *string `url:"filter[agent-pool],omitempty"`
}

// List the vcs providers.
Expand Down Expand Up @@ -128,6 +130,7 @@ type VcsProviderCreateOptions struct {
// Relations
Environments []*Environment `jsonapi:"relation,environments,omitempty"`
Account *Account `jsonapi:"relation,account,omitempty"`
AgentPool *AgentPool `jsonapi:"relation,agent-pool,omitempty"`
}

// Create is used to create a new vcs provider.
Expand Down Expand Up @@ -178,6 +181,9 @@ type VcsProviderUpdateOptions struct {
Token *string `jsonapi:"attr,token,omitempty"`
Url *string `jsonapi:"attr,url,omitempty"`
Username *string `jsonapi:"attr,username,omitempty"`

// Relations
AgentPool *AgentPool `jsonapi:"relation,agent-pool"`
}

// Update settings of an existing vcs provider.
Expand Down
21 changes: 20 additions & 1 deletion vcs_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestVcsProvidersCreate(t *testing.T) {

t.Run("with valid options", func(t *testing.T) {
options := VcsProviderCreateOptions{
Name: String("foo"),
Name: String("vcs-" + randomString(t)),
VcsType: Github,
AuthType: PersonalToken,
Token: os.Getenv("GITHUB_TOKEN"),
Expand All @@ -95,6 +95,25 @@ func TestVcsProvidersCreate(t *testing.T) {
}
})

t.Run("with agent-pool attr vcs-enabled: false", func(t *testing.T) {
ap, apCleanup := createAgentPool(t, client, false)
defer apCleanup()

options := VcsProviderCreateOptions{
Name: String("vcs-" + randomString(t)),
VcsType: Github,
AuthType: PersonalToken,
Token: os.Getenv("GITHUB_TOKEN"),

Environments: []*Environment{envTest},
Account: &Account{ID: defaultAccountID},
AgentPool: ap,
}

_, err := client.VcsProviders.Create(ctx, options)
require.Error(t, err)
})

t.Run("when options has an invalid environment", func(t *testing.T) {
_, err := client.VcsProviders.Create(ctx, VcsProviderCreateOptions{
Name: String("test-vcs"),
Expand Down
4 changes: 2 additions & 2 deletions workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestWorkspacesCreate(t *testing.T) {
envTest, envTestCleanup := createEnvironment(t, client)
defer envTestCleanup()

pool, poolCleanup := createAgentPool(t, client)
pool, poolCleanup := createAgentPool(t, client, false)
defer poolCleanup()

t.Run("with valid options", func(t *testing.T) {
Expand Down Expand Up @@ -272,7 +272,7 @@ func TestWorkspacesUpdate(t *testing.T) {
envTest, envTestCleanup := createEnvironment(t, client)
defer envTestCleanup()

pool, poolCleanup := createAgentPool(t, client)
pool, poolCleanup := createAgentPool(t, client, false)
defer poolCleanup()

wsTest, _ := createWorkspace(t, client, envTest)
Expand Down

0 comments on commit 75bf8ca

Please sign in to comment.