Skip to content

Commit

Permalink
add missing include field for listOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
uturunku1 committed Mar 1, 2022
1 parent 2948e27 commit 0832278
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 12 deletions.
7 changes: 7 additions & 0 deletions agent_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,16 @@ type AgentPool struct {
Organization *Organization `jsonapi:"relation,organization"`
}

// A list of relations to include
type AgentPoolIncludeOps string

const AgentPoolWorkspaces AgentPoolIncludeOps = "workspaces"

// AgentPoolListOptions represents the options for listing agent pools.
type AgentPoolListOptions struct {
ListOptions

Include []AgentPoolIncludeOps `url:"include,omitempty"`
}

// AgentPoolCreateOptions represents the options for creating an agent pool.
Expand Down
9 changes: 9 additions & 0 deletions agent_pool_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ func TestAgentPoolsList(t *testing.T) {
assert.Equal(t, 1, pools.TotalCount)
})

t.Run("with Include list options", func(t *testing.T) {
pools, err := client.AgentPools.List(ctx, orgTest.Name, &AgentPoolListOptions{
Include: []AgentPoolIncludeOps{AgentPoolWorkspaces},
})
require.NoError(t, err)
assert.NotEmpty(t, pools.Items)
assert.NotEmpty(t, pools.Items[0].Organization.Name)
})

t.Run("without a valid organization", func(t *testing.T) {
pools, err := client.AgentPools.List(ctx, badIdentifier, nil)
assert.Nil(t, pools)
Expand Down
6 changes: 5 additions & 1 deletion errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ var (
ErrUnsupportedOperations = errors.New("operations is deprecated and cannot be specified when execution mode is used")

ErrUnsupportedPrivateKey = errors.New("private Key can only be present with Azure DevOps Server service provider")

ErrUnsupportedRunTriggerType = errors.New(`"RunTriggerType" must be "inbound" when requesting "include" query params`)
)

// Library errors that usually indicate a bug in the implementation of go-tfe
Expand Down Expand Up @@ -116,7 +118,9 @@ var (

ErrInvalidRunTriggerID = errors.New("invalid value for run trigger ID")

ErrInvalidRunTriggerType = errors.New(`invalid value for RunTriggerType. It must be either "inbound" or "outbound"`)
ErrInvalidRunTriggerType = errors.New(`invalid value or no value for RunTriggerType. It must be either "inbound" or "outbound"`)

ErrInvalidRunTriggerInclude = errors.New(`invalid value for "include" field`)

ErrInvalidSHHKeyID = errors.New("invalid value for SSH key ID")

Expand Down
6 changes: 6 additions & 0 deletions oauth_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,16 @@ type OAuthClient struct {
OAuthTokens []*OAuthToken `jsonapi:"relation,oauth-tokens"`
}

type OAuthClientIncludeOps string

const OauthClientOauthTokens OAuthClientIncludeOps = "oauth_tokens"

// OAuthClientListOptions represents the options for listing
// OAuth clients.
type OAuthClientListOptions struct {
ListOptions

Include []OAuthClientIncludeOps `url:"include,omitempty"`
}

// OAuthClientCreateOptions represents the options for creating an OAuth client.
Expand Down
6 changes: 6 additions & 0 deletions organization_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,17 @@ type OrganizationTag struct {
Organization *Organization `jsonapi:"relation,organization"`
}

type OrgTagsIncludeOps string

const OrgTagsEntitlementSet OrgTagsIncludeOps = "entitlement_set"

// OrganizationTagsListOptions represents the options for listing organization tags
type OrganizationTagsListOptions struct {
ListOptions
// Optional:
Filter string `url:"filter[exclude][taggable][id],omitempty"`

Include []OrgTagsIncludeOps `url:"include,omitempty"`
}

// OrganizationTagsDeleteOptions represents the request body for deleting a tag in an organization
Expand Down
6 changes: 6 additions & 0 deletions policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,18 @@ type EnforcementOptions struct {
Mode *EnforcementLevel `json:"mode"`
}

type PolicyIncludeOps string

const PolicyPolicySets PolicyIncludeOps = "policy_sets"

// PolicyListOptions represents the options for listing policies.
type PolicyListOptions struct {
ListOptions

// Optional: A search string (partial policy name) used to filter the results.
Search string `url:"search[name],omitempty"`

Include []PolicyIncludeOps `url:"include,omitempty"`
}

// PolicyCreateOptions represents the options for creating a new policy.
Expand Down
9 changes: 9 additions & 0 deletions policy_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,18 @@ type PolicyStatusTimestamps struct {
SoftFailedAt time.Time `jsonapi:"attr,soft-failed-at,rfc3339"`
}

type PolicyCheckIncludeOps string

const (
PolicyCheckRunWorkspace PolicyCheckIncludeOps = "run.workspace"
PolicyCheckRun PolicyCheckIncludeOps = "run"
)

// PolicyCheckListOptions represents the options for listing policy checks.
type PolicyCheckListOptions struct {
ListOptions

Include []PolicyCheckIncludeOps `url:"include,omitempty"`
}

// List all policy checks of the given run.
Expand Down
40 changes: 30 additions & 10 deletions run_trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,25 @@ type RunTrigger struct {
type RunTriggerFilterOps string

const (
RunTriggerOutbound RunTriggerFilterOps = "outbound"
RunTriggerInbound RunTriggerFilterOps = "inbound"
RunTriggerOutbound RunTriggerFilterOps = "outbound" // create runs in other workspaces.
RunTriggerInbound RunTriggerFilterOps = "inbound" // create runs in the specified workspace
)

// List of available filter options for RunTriggerType
var validRunTriggerType = map[RunTriggerFilterOps]struct{}{
RunTriggerOutbound: {},
RunTriggerInbound: {}, // we set it to empty struct because it has the smallest memory footprint and we only need keys, not values
}
// A list of relations to include
// https://www.terraform.io/cloud-docs/api-docs/run-triggers#available-related-resources
type RunTriggerIncludeOps string

const (
RunTriggerWorkspace RunTriggerIncludeOps = "workspace"
RunTriggerSourceable RunTriggerIncludeOps = "sourceable"
)

// RunTriggerListOptions represents the options for listing
// run triggers.
type RunTriggerListOptions struct {
ListOptions
RunTriggerType RunTriggerFilterOps `url:"filter[run-trigger][type]"`
RunTriggerType RunTriggerFilterOps `url:"filter[run-trigger][type]"` //Required
Include []RunTriggerIncludeOps `url:"include,omitempty"` // optional
}

// RunTriggerCreateOptions represents the options for
Expand Down Expand Up @@ -175,10 +179,26 @@ func (o *RunTriggerListOptions) valid() error {
if o == nil {
return ErrRequiredRunTriggerListOps
}
_, isValidRunTriggerType := validRunTriggerType[o.RunTriggerType]
if !isValidRunTriggerType {

switch o.RunTriggerType {
case RunTriggerOutbound, RunTriggerInbound:
// Do nothing
default:
return ErrInvalidRunTriggerType
}

for _, i := range o.Include {
switch i {
case RunTriggerWorkspace, RunTriggerSourceable:
if o.RunTriggerType != RunTriggerInbound {
return ErrUnsupportedRunTriggerType
}
// Do nothing
default:
return ErrUnsupportedOperations
}
}

return nil
}

Expand Down
43 changes: 42 additions & 1 deletion run_trigger_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestRunTriggerList(t *testing.T) {
assert.EqualError(t, err, ErrInvalidWorkspaceID.Error())
})

t.Run("without defining RunTriggerFilterOps as a filter param", func(t *testing.T) {
t.Run("without defining RunTriggerListOptions", func(t *testing.T) {
rtl, err := client.RunTriggers.List(
ctx,
wTest.ID,
Expand All @@ -90,6 +90,21 @@ func TestRunTriggerList(t *testing.T) {
assert.Equal(t, err, ErrRequiredRunTriggerListOps)
})

t.Run("without defining RunTriggerFilterOps as a filter param", func(t *testing.T) {
rtl, err := client.RunTriggers.List(
ctx,
wTest.ID,
&RunTriggerListOptions{
ListOptions: ListOptions{
PageNumber: 999,
PageSize: 100,
},
},
)
assert.Nil(t, rtl)
assert.Equal(t, err, ErrInvalidRunTriggerType)
})

t.Run("with invalid option for runTriggerType", func(t *testing.T) {
rtl, err := client.RunTriggers.List(
ctx,
Expand All @@ -101,6 +116,32 @@ func TestRunTriggerList(t *testing.T) {
assert.Nil(t, rtl)
assert.Equal(t, err, ErrInvalidRunTriggerType)
})

t.Run("with sourceable include option", func(t *testing.T) {
rtl, err := client.RunTriggers.List(
ctx,
wTest.ID,
&RunTriggerListOptions{
RunTriggerType: RunTriggerInbound,
Include: []RunTriggerIncludeOps{RunTriggerSourceable},
},
)
require.NoError(t, err)
assert.NotEmpty(t, rtl.Items)
assert.NotEmpty(t, rtl.Items[0].Sourceable.Name)
})

t.Run("with a RunTriggerType that does not return included data", func(t *testing.T) {
_, err := client.RunTriggers.List(
ctx,
wTest.ID,
&RunTriggerListOptions{
RunTriggerType: RunTriggerOutbound,
Include: []RunTriggerIncludeOps{RunTriggerSourceable},
},
)
assert.Equal(t, err, ErrUnsupportedRunTriggerType)
})
}

func TestRunTriggerCreate(t *testing.T) {
Expand Down

0 comments on commit 0832278

Please sign in to comment.