Skip to content

Commit

Permalink
Use consistent naming across resource methods
Browse files Browse the repository at this point in the history
This commit introduces a refactor for several methods that express an action on a relationship between two resources. By using
verbs, we can be more specific as to what action we're performing when calling these methods. An example would be
ModuleConsumers() -- from this method signature we can't necessarily tell what action this method is performing without
referring to the docs. Renaming it to ListModuleConsumers() we can now assess what the function does more conveniently.
  • Loading branch information
sebasslash committed Feb 23, 2022
1 parent efdb2c1 commit 015b037
Show file tree
Hide file tree
Showing 17 changed files with 98 additions and 98 deletions.
12 changes: 6 additions & 6 deletions agent_token.go
Expand Up @@ -19,8 +19,8 @@ type AgentTokens interface {
// List all the agent tokens of the given agent pool.
List(ctx context.Context, agentPoolID string) (*AgentTokenList, error)

// Generate a new agent token with the given options.
Generate(ctx context.Context, agentPoolID string, options AgentTokenGenerateOptions) (*AgentToken, error)
// Create a new agent token with the given options.
Create(ctx context.Context, agentPoolID string, options AgentTokenCreateOptions) (*AgentToken, error)

// Read an agent token by its ID.
Read(ctx context.Context, agentTokenID string) (*AgentToken, error)
Expand Down Expand Up @@ -70,8 +70,8 @@ func (s *agentTokens) List(ctx context.Context, agentPoolID string) (*AgentToken
return tokenList, nil
}

// AgentTokenGenerateOptions represents the options for creating an agent token.
type AgentTokenGenerateOptions struct {
// AgentTokenCreateOptions represents the options for creating an agent token.
type AgentTokenCreateOptions struct {
// Type is a public field utilized by JSON:API to
// set the resource type via the field tag.
// It is not a user-defined value and does not need to be set.
Expand All @@ -82,8 +82,8 @@ type AgentTokenGenerateOptions struct {
Description *string `jsonapi:"attr,description"`
}

// Generate a new agent token with the given options.
func (s *agentTokens) Generate(ctx context.Context, agentPoolID string, options AgentTokenGenerateOptions) (*AgentToken, error) {
// Create a new agent token with the given options.
func (s *agentTokens) Create(ctx context.Context, agentPoolID string, options AgentTokenCreateOptions) (*AgentToken, error) {
if !validStringID(&agentPoolID) {
return nil, ErrInvalidAgentPoolID
}
Expand Down
8 changes: 4 additions & 4 deletions agent_token_integration_test.go
Expand Up @@ -51,7 +51,7 @@ func TestAgentTokensList(t *testing.T) {
})
}

func TestAgentTokensGenerate(t *testing.T) {
func TestAgentTokensCreate(t *testing.T) {
skipIfEnterprise(t)
skipIfFreeOnly(t)

Expand All @@ -62,21 +62,21 @@ func TestAgentTokensGenerate(t *testing.T) {
defer apTestCleanup()

t.Run("with valid description", func(t *testing.T) {
token, err := client.AgentTokens.Generate(ctx, apTest.ID, AgentTokenGenerateOptions{
token, err := client.AgentTokens.Create(ctx, apTest.ID, AgentTokenCreateOptions{
Description: String(randomString(t)),
})
require.NoError(t, err)
require.NotEmpty(t, token.Token)
})

t.Run("without valid description", func(t *testing.T) {
at, err := client.AgentTokens.Generate(ctx, badIdentifier, AgentTokenGenerateOptions{})
at, err := client.AgentTokens.Create(ctx, badIdentifier, AgentTokenCreateOptions{})
assert.Nil(t, at)
assert.EqualError(t, err, ErrInvalidAgentPoolID.Error())
})

t.Run("without valid agent pool ID", func(t *testing.T) {
at, err := client.AgentTokens.Generate(ctx, badIdentifier, AgentTokenGenerateOptions{
at, err := client.AgentTokens.Create(ctx, badIdentifier, AgentTokenCreateOptions{
Description: String(randomString(t)),
})
assert.Nil(t, at)
Expand Down
4 changes: 2 additions & 2 deletions helper_test.go
Expand Up @@ -74,7 +74,7 @@ func createAgentToken(t *testing.T, client *Client, ap *AgentPool) (*AgentToken,
}

ctx := context.Background()
at, err := client.AgentTokens.Generate(ctx, ap.ID, AgentTokenGenerateOptions{
at, err := client.AgentTokens.Create(ctx, ap.ID, AgentTokenCreateOptions{
Description: String(randomString(t)),
})
if err != nil {
Expand Down Expand Up @@ -439,7 +439,7 @@ func createOrganizationToken(t *testing.T, client *Client, org *Organization) (*
}

ctx := context.Background()
tk, err := client.OrganizationTokens.Generate(ctx, org.Name)
tk, err := client.OrganizationTokens.Create(ctx, org.Name)
if err != nil {
t.Fatal(err)
}
Expand Down
28 changes: 14 additions & 14 deletions organization.go
Expand Up @@ -31,14 +31,14 @@ type Organizations interface {
// Delete an organization by its name.
Delete(ctx context.Context, organization string) error

// Capacity shows the current run capacity of an organization.
Capacity(ctx context.Context, organization string) (*Capacity, error)
// ReadCapacity shows the current run capacity of an organization.
ReadCapacity(ctx context.Context, organization string) (*Capacity, error)

// Entitlements shows the entitlements of an organization.
Entitlements(ctx context.Context, organization string) (*Entitlements, error)
// ReadEntitlements shows the entitlements of an organization.
ReadEntitlements(ctx context.Context, organization string) (*Entitlements, error)

// RunQueue shows the current run queue of an organization.
RunQueue(ctx context.Context, organization string, options RunQueueOptions) (*RunQueue, error)
// ReadRunQueue shows the current run queue of an organization.
ReadRunQueue(ctx context.Context, organization string, options ReadRunQueueOptions) (*RunQueue, error)
}

// organizations implements Organizations.
Expand Down Expand Up @@ -297,8 +297,8 @@ func (s *organizations) Delete(ctx context.Context, organization string) error {
return s.client.do(ctx, req, nil)
}

// Capacity shows the currently used capacity of an organization.
func (s *organizations) Capacity(ctx context.Context, organization string) (*Capacity, error) {
// ReadCapacity shows the currently used capacity of an organization.
func (s *organizations) ReadCapacity(ctx context.Context, organization string) (*Capacity, error) {
if !validStringID(&organization) {
return nil, ErrInvalidOrg
}
Expand All @@ -318,8 +318,8 @@ func (s *organizations) Capacity(ctx context.Context, organization string) (*Cap
return c, nil
}

// Entitlements shows the entitlements of an organization.
func (s *organizations) Entitlements(ctx context.Context, organization string) (*Entitlements, error) {
// ReadEntitlements shows the entitlements of an organization.
func (s *organizations) ReadEntitlements(ctx context.Context, organization string) (*Entitlements, error) {
if !validStringID(&organization) {
return nil, ErrInvalidOrg
}
Expand All @@ -339,13 +339,13 @@ func (s *organizations) Entitlements(ctx context.Context, organization string) (
return e, nil
}

// RunQueueOptions represents the options for showing the queue.
type RunQueueOptions struct {
// ReadRunQueueOptions represents the options for showing the queue.
type ReadRunQueueOptions struct {
ListOptions
}

// RunQueue shows the current run queue of an organization.
func (s *organizations) RunQueue(ctx context.Context, organization string, options RunQueueOptions) (*RunQueue, error) {
// ReadRunQueue shows the current run queue of an organization.
func (s *organizations) ReadRunQueue(ctx context.Context, organization string, options ReadRunQueueOptions) (*RunQueue, error) {
if !validStringID(&organization) {
return nil, ErrInvalidOrg
}
Expand Down
24 changes: 12 additions & 12 deletions organization_integration_test.go
Expand Up @@ -235,7 +235,7 @@ func TestOrganizationsDelete(t *testing.T) {
})
}

func TestOrganizationsCapacity(t *testing.T) {
func TestOrganizationsReadCapacity(t *testing.T) {
t.Skip("Capacity queues are not available in the API")
client := testClient(t)
ctx := context.Background()
Expand All @@ -253,7 +253,7 @@ func TestOrganizationsCapacity(t *testing.T) {
defer wTestCleanup4()

t.Run("without queued runs", func(t *testing.T) {
c, err := client.Organizations.Capacity(ctx, orgTest.Name)
c, err := client.Organizations.ReadCapacity(ctx, orgTest.Name)
require.NoError(t, err)
assert.Equal(t, 0, c.Pending)
assert.Equal(t, 0, c.Running)
Expand All @@ -271,7 +271,7 @@ func TestOrganizationsCapacity(t *testing.T) {
_, runCleanup4 := createRun(t, client, wTest4)
defer runCleanup4()

c, err := client.Organizations.Capacity(ctx, orgTest.Name)
c, err := client.Organizations.ReadCapacity(ctx, orgTest.Name)
require.NoError(t, err)
assert.Equal(t, 2, c.Pending)
assert.Equal(t, 2, c.Running)
Expand All @@ -289,7 +289,7 @@ func TestOrganizationsCapacity(t *testing.T) {
})
}

func TestOrganizationsEntitlements(t *testing.T) {
func TestOrganizationsReadEntitlements(t *testing.T) {
skipIfEnterprise(t)
skipIfFreeOnly(t)

Expand All @@ -300,7 +300,7 @@ func TestOrganizationsEntitlements(t *testing.T) {
defer orgTestCleanup()

t.Run("when the org exists", func(t *testing.T) {
entitlements, err := client.Organizations.Entitlements(ctx, orgTest.Name)
entitlements, err := client.Organizations.ReadEntitlements(ctx, orgTest.Name)
require.NoError(t, err)

assert.NotEmpty(t, entitlements.ID)
Expand All @@ -317,18 +317,18 @@ func TestOrganizationsEntitlements(t *testing.T) {
})

t.Run("with invalid name", func(t *testing.T) {
entitlements, err := client.Organizations.Entitlements(ctx, badIdentifier)
entitlements, err := client.Organizations.ReadEntitlements(ctx, badIdentifier)
assert.Nil(t, entitlements)
assert.EqualError(t, err, ErrInvalidOrg.Error())
})

t.Run("when the org does not exist", func(t *testing.T) {
_, err := client.Organizations.Entitlements(ctx, randomString(t))
_, err := client.Organizations.ReadEntitlements(ctx, randomString(t))
assert.Equal(t, ErrResourceNotFound, err)
})
}

func TestOrganizationsRunQueue(t *testing.T) {
func TestOrganizationsReadRunQueue(t *testing.T) {
t.Skip("Capacity queues are not available in the API")
client := testClient(t)
ctx := context.Background()
Expand All @@ -346,7 +346,7 @@ func TestOrganizationsRunQueue(t *testing.T) {
defer wTestCleanup4()

t.Run("without queued runs", func(t *testing.T) {
rq, err := client.Organizations.RunQueue(ctx, orgTest.Name, RunQueueOptions{})
rq, err := client.Organizations.ReadRunQueue(ctx, orgTest.Name, ReadRunQueueOptions{})
require.NoError(t, err)
assert.Equal(t, 0, len(rq.Items))
})
Expand All @@ -364,7 +364,7 @@ func TestOrganizationsRunQueue(t *testing.T) {
// For this test FRQ should be enabled and have a
// limit of 2 concurrent runs per organization.
t.Run("with queued runs", func(t *testing.T) {
rq, err := client.Organizations.RunQueue(ctx, orgTest.Name, RunQueueOptions{})
rq, err := client.Organizations.ReadRunQueue(ctx, orgTest.Name, ReadRunQueueOptions{})
require.NoError(t, err)

found := []string{}
Expand All @@ -379,7 +379,7 @@ func TestOrganizationsRunQueue(t *testing.T) {
})

t.Run("without queue options", func(t *testing.T) {
rq, err := client.Organizations.RunQueue(ctx, orgTest.Name, RunQueueOptions{})
rq, err := client.Organizations.ReadRunQueue(ctx, orgTest.Name, ReadRunQueueOptions{})
require.NoError(t, err)

found := []string{}
Expand All @@ -399,7 +399,7 @@ func TestOrganizationsRunQueue(t *testing.T) {
// Request a page number which is out of range. The result should
// be successful, but return no results if the paging options are
// properly passed along.
rq, err := client.Organizations.RunQueue(ctx, orgTest.Name, RunQueueOptions{
rq, err := client.Organizations.ReadRunQueue(ctx, orgTest.Name, ReadRunQueueOptions{
ListOptions: ListOptions{
PageNumber: 999,
PageSize: 100,
Expand Down
4 changes: 2 additions & 2 deletions organization_tags_integration_test.go
Expand Up @@ -176,11 +176,11 @@ func TestOrganizationTagsAddWorkspace(t *testing.T) {
require.NoError(t, err)

//Ensure the tag was properly associated with the workspaces
fetched, err := client.Workspaces.Tags(ctx, workspaceToAdd1.ID, nil)
fetched, err := client.Workspaces.ListTags(ctx, workspaceToAdd1.ID, nil)
require.NoError(t, err)
assert.Equal(t, fetched.Items[0].ID, tagID)

fetched, err = client.Workspaces.Tags(ctx, workspaceToAdd2.ID, nil)
fetched, err = client.Workspaces.ListTags(ctx, workspaceToAdd2.ID, nil)
require.NoError(t, err)
assert.Equal(t, fetched.Items[0].ID, tagID)
})
Expand Down
8 changes: 4 additions & 4 deletions organization_token.go
Expand Up @@ -16,8 +16,8 @@ var _ OrganizationTokens = (*organizationTokens)(nil)
// TFE API docs:
// https://www.terraform.io/docs/cloud/api/organization-tokens.html
type OrganizationTokens interface {
// Generate a new organization token, replacing any existing token.
Generate(ctx context.Context, organization string) (*OrganizationToken, error)
// Create a new organization token, replacing any existing token.
Create(ctx context.Context, organization string) (*OrganizationToken, error)

// Read an organization token.
Read(ctx context.Context, organization string) (*OrganizationToken, error)
Expand All @@ -40,8 +40,8 @@ type OrganizationToken struct {
Token string `jsonapi:"attr,token"`
}

// Generate a new organization token, replacing any existing token.
func (s *organizationTokens) Generate(ctx context.Context, organization string) (*OrganizationToken, error) {
// Create a new organization token, replacing any existing token.
func (s *organizationTokens) Create(ctx context.Context, organization string) (*OrganizationToken, error) {
if !validStringID(&organization) {
return nil, ErrInvalidOrg
}
Expand Down
8 changes: 4 additions & 4 deletions organization_token_integration_test.go
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/stretchr/testify/require"
)

func TestOrganizationTokensGenerate(t *testing.T) {
func TestOrganizationTokensCreate(t *testing.T) {
client := testClient(t)
ctx := context.Background()

Expand All @@ -20,21 +20,21 @@ func TestOrganizationTokensGenerate(t *testing.T) {

var tkToken string
t.Run("with valid options", func(t *testing.T) {
ot, err := client.OrganizationTokens.Generate(ctx, orgTest.Name)
ot, err := client.OrganizationTokens.Create(ctx, orgTest.Name)
require.NoError(t, err)
require.NotEmpty(t, ot.Token)
tkToken = ot.Token
})

t.Run("when a token already exists", func(t *testing.T) {
ot, err := client.OrganizationTokens.Generate(ctx, orgTest.Name)
ot, err := client.OrganizationTokens.Create(ctx, orgTest.Name)
require.NoError(t, err)
require.NotEmpty(t, ot.Token)
assert.NotEqual(t, tkToken, ot.Token)
})

t.Run("without valid organization", func(t *testing.T) {
ot, err := client.OrganizationTokens.Generate(ctx, badIdentifier)
ot, err := client.OrganizationTokens.Create(ctx, badIdentifier)
assert.Nil(t, ot)
assert.EqualError(t, err, ErrInvalidOrg.Error())
})
Expand Down
4 changes: 2 additions & 2 deletions plan.go
Expand Up @@ -24,7 +24,7 @@ type Plans interface {
Logs(ctx context.Context, planID string) (io.Reader, error)

// Retrieve the JSON execution plan
JSONOutput(ctx context.Context, planID string) ([]byte, error)
ReadJSONOutput(ctx context.Context, planID string) ([]byte, error)
}

// plans implements Plans.
Expand Down Expand Up @@ -139,7 +139,7 @@ func (s *plans) Logs(ctx context.Context, planID string) (io.Reader, error) {
}

// Retrieve the JSON execution plan
func (s *plans) JSONOutput(ctx context.Context, planID string) ([]byte, error) {
func (s *plans) ReadJSONOutput(ctx context.Context, planID string) ([]byte, error) {
if !validStringID(&planID) {
return nil, ErrInvalidPlanID
}
Expand Down
4 changes: 2 additions & 2 deletions plan_integration_test.go
Expand Up @@ -123,7 +123,7 @@ func TestPlansJSONOutput(t *testing.T) {
defer rTestCleanup()

t.Run("when the JSON output exists", func(t *testing.T) {
d, err := client.Plans.JSONOutput(ctx, rTest.Plan.ID)
d, err := client.Plans.ReadJSONOutput(ctx, rTest.Plan.ID)
require.NoError(t, err)
var m map[string]interface{}
err = json.Unmarshal(d, &m)
Expand All @@ -133,7 +133,7 @@ func TestPlansJSONOutput(t *testing.T) {
})

t.Run("when the JSON output does not exist", func(t *testing.T) {
d, err := client.Plans.JSONOutput(ctx, "nonexisting")
d, err := client.Plans.ReadJSONOutput(ctx, "nonexisting")
assert.Nil(t, d)
assert.Error(t, err)
})
Expand Down

0 comments on commit 015b037

Please sign in to comment.