Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add effective tag binding include relation to projects and workspaces #1043

Merged
merged 2 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Unreleased

## Enhancements

* Adds `EffectiveTagBindings` relation to projects and workspaces, allowing the relation to be included when listing projects or workspaces by @sebasslash [#1043](https://github.com/hashicorp/go-tfe/pull/1043)

# v1.74.1

## Enhancements
Expand Down
12 changes: 11 additions & 1 deletion project.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,16 @@ type Project struct {
AutoDestroyActivityDuration jsonapi.NullableAttr[string] `jsonapi:"attr,auto-destroy-activity-duration,omitempty"`

// Relations
Organization *Organization `jsonapi:"relation,organization"`
Organization *Organization `jsonapi:"relation,organization"`
EffectiveTagBindings []*EffectiveTagBinding `jsonapi:"relation,effective-tag-bindings"`
}

type ProjectIncludeOpt string

const (
ProjectEffectiveTagBindings ProjectIncludeOpt = "effective_tag_bindings"
)

// ProjectListOptions represents the options for listing projects
type ProjectListOptions struct {
ListOptions
Expand All @@ -89,6 +96,9 @@ type ProjectListOptions struct {
// Optional: A filter string to list projects filtered by key/value tags.
// These are not annotated and therefore not encoded by go-querystring
TagBindings []*TagBinding

// Optional: A list of relations to include
Include []ProjectIncludeOpt `url:"include,omitempty"`
}

// ProjectCreateOptions represents the options for creating a project
Expand Down
27 changes: 27 additions & 0 deletions projects_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,33 @@ func TestProjectsList(t *testing.T) {
assert.Len(t, pl3.Items, 1)
assert.Contains(t, pl3.Items, p2)
})

t.Run("when including effective tags relationship", func(t *testing.T) {
skipUnlessBeta(t)

orgTest2, orgTest2Cleanup := createOrganization(t, client)
t.Cleanup(orgTest2Cleanup)

_, pTestCleanup1 := createProjectWithOptions(t, client, orgTest2, ProjectCreateOptions{
Name: randomStringWithoutSpecialChar(t),
TagBindings: []*TagBinding{
{Key: "key1", Value: "value1"},
{Key: "key2", Value: "value2a"},
},
})
t.Cleanup(pTestCleanup1)

pl, err := client.Projects.List(ctx, orgTest2.Name, &ProjectListOptions{
Include: []ProjectIncludeOpt{ProjectEffectiveTagBindings},
})
require.NoError(t, err)
require.Len(t, pl.Items, 2)
require.Len(t, pl.Items[0].EffectiveTagBindings, 2)
assert.NotEmpty(t, pl.Items[0].EffectiveTagBindings[0].Key)
assert.NotEmpty(t, pl.Items[0].EffectiveTagBindings[0].Value)
assert.NotEmpty(t, pl.Items[0].EffectiveTagBindings[1].Key)
assert.NotEmpty(t, pl.Items[0].EffectiveTagBindings[1].Value)
})
}

func TestProjectsRead(t *testing.T) {
Expand Down
26 changes: 14 additions & 12 deletions workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,18 +217,19 @@ type Workspace struct {
SettingOverwrites *WorkspaceSettingOverwrites `jsonapi:"attr,setting-overwrites"`

// Relations
AgentPool *AgentPool `jsonapi:"relation,agent-pool"`
CurrentRun *Run `jsonapi:"relation,current-run"`
CurrentStateVersion *StateVersion `jsonapi:"relation,current-state-version"`
Organization *Organization `jsonapi:"relation,organization"`
SSHKey *SSHKey `jsonapi:"relation,ssh-key"`
Outputs []*WorkspaceOutputs `jsonapi:"relation,outputs"`
Project *Project `jsonapi:"relation,project"`
Tags []*Tag `jsonapi:"relation,tags"`
CurrentConfigurationVersion *ConfigurationVersion `jsonapi:"relation,current-configuration-version,omitempty"`
LockedBy *LockedByChoice `jsonapi:"polyrelation,locked-by"`
Variables []*Variable `jsonapi:"relation,vars"`
TagBindings []*TagBinding `jsonapi:"relation,tag-bindings"`
AgentPool *AgentPool `jsonapi:"relation,agent-pool"`
CurrentRun *Run `jsonapi:"relation,current-run"`
CurrentStateVersion *StateVersion `jsonapi:"relation,current-state-version"`
Organization *Organization `jsonapi:"relation,organization"`
SSHKey *SSHKey `jsonapi:"relation,ssh-key"`
Outputs []*WorkspaceOutputs `jsonapi:"relation,outputs"`
Project *Project `jsonapi:"relation,project"`
Tags []*Tag `jsonapi:"relation,tags"`
CurrentConfigurationVersion *ConfigurationVersion `jsonapi:"relation,current-configuration-version,omitempty"`
LockedBy *LockedByChoice `jsonapi:"polyrelation,locked-by"`
Variables []*Variable `jsonapi:"relation,vars"`
TagBindings []*TagBinding `jsonapi:"relation,tag-bindings"`
EffectiveTagBindings []*EffectiveTagBinding `jsonapi:"relation,effective-tag-bindings"`

// Deprecated: Use DataRetentionPolicyChoice instead.
DataRetentionPolicy *DataRetentionPolicy
Expand Down Expand Up @@ -314,6 +315,7 @@ const (
WSCurrentRunPlan WSIncludeOpt = "current_run.plan"
WSCurrentRunConfigVer WSIncludeOpt = "current_run.configuration_version"
WSCurrentrunConfigVerIngress WSIncludeOpt = "current_run.configuration_version.ingress_attributes"
WSEffectiveTagBindings WSIncludeOpt = "effective_tag_bindings"
WSLockedBy WSIncludeOpt = "locked_by"
WSReadme WSIncludeOpt = "readme"
WSOutputs WSIncludeOpt = "outputs"
Expand Down
27 changes: 27 additions & 0 deletions workspace_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,33 @@ func TestWorkspacesList(t *testing.T) {
assert.Contains(t, wl3.Items, w2)
})

t.Run("when including effective tag bindings", func(t *testing.T) {
skipUnlessBeta(t)

orgTest2, orgTest2Cleanup := createOrganization(t, client)
t.Cleanup(orgTest2Cleanup)

_, wTestCleanup1 := createWorkspaceWithOptions(t, client, orgTest2, WorkspaceCreateOptions{
Name: String(randomString(t)),
TagBindings: []*TagBinding{
{Key: "key1", Value: "value1"},
{Key: "key2", Value: "value2a"},
},
})
t.Cleanup(wTestCleanup1)

wl, err := client.Workspaces.List(ctx, orgTest2.Name, &WorkspaceListOptions{
Include: []WSIncludeOpt{WSEffectiveTagBindings},
})
require.NoError(t, err)
require.Len(t, wl.Items, 1)
require.Len(t, wl.Items[0].EffectiveTagBindings, 2)
assert.NotEmpty(t, wl.Items[0].EffectiveTagBindings[0].Key)
assert.NotEmpty(t, wl.Items[0].EffectiveTagBindings[0].Value)
assert.NotEmpty(t, wl.Items[0].EffectiveTagBindings[1].Key)
assert.NotEmpty(t, wl.Items[0].EffectiveTagBindings[1].Value)
})

t.Run("when using project id filter and project contains workspaces", func(t *testing.T) {
// create a project in the orgTest
p, pTestCleanup := createProject(t, client, orgTest)
Expand Down
Loading