Skip to content
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Unreleased

## Enhancements
* Adds `UserTokensEnabled` field to `Organization` to support enabling/disabling user tokens for an organization by @JarrettSpiker [#1225](https://github.com/hashicorp/go-tfe/pull/1225)

# v1.97.0

## Enhancements
Expand Down
7 changes: 7 additions & 0 deletions organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ type Organization struct {
RemainingTestableCount int `jsonapi:"attr,remaining-testable-count"`
SpeculativePlanManagementEnabled bool `jsonapi:"attr,speculative-plan-management-enabled"`
EnforceHYOK bool `jsonapi:"attr,enforce-hyok"`
UserTokensEnabled *bool `jsonapi:"attr,user-tokens-enabled"`
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we use a bool pointer type? false and nil are both logically equivalent, correct?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question: it is because this setting defaults to true. Making this a bool pointer allows us to distinguish between "this is unspecified on the organization" (ie, because it is an older version of TFE) and "this is explicitly false" (ie, because this is an up-to-date TFE and the user has disabled the setting)

That is important because in the provider when we read an organization from an old TFE version, we dont want the the value to appear as false. That could lead to the provider always showing drift for the org and/or be misleading since the behaviour of the old org is that user tokens are enabled.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A similar example would be like OrganizationScoped for agent pools

// Optional: If enabled, SendPassingStatusesForUntriggeredSpeculativePlans needs to be false.
AggregatedCommitStatusEnabled bool `jsonapi:"attr,aggregated-commit-status-enabled,omitempty"`
// Note: This will be false for TFE versions older than v202211, where the setting was introduced.
Expand Down Expand Up @@ -271,6 +272,9 @@ type OrganizationCreateOptions struct {

// Optional: RegistryMonorepoSupportEnabled toggles whether monorepo support is enabled for the organization
RegistryMonorepoSupportEnabled *bool `jsonapi:"attr,registry-monorepo-support-enabled,omitempty"`

// Optional: UserTokensEnabled toggles whether user tokens may be used to access resources in this organization.
UserTokensEnabled *bool `jsonapi:"attr,user-tokens-enabled,omitempty"`
}

// OrganizationUpdateOptions represents the options for updating an organization.
Expand Down Expand Up @@ -332,6 +336,9 @@ type OrganizationUpdateOptions struct {

// Optional: RegistryMonorepoSupportEnabled toggles whether monorepo support is enabled for the organization
RegistryMonorepoSupportEnabled *bool `jsonapi:"attr,registry-monorepo-support-enabled,omitempty"`

// Optional: UserTokensEnabled toggles whether user tokens may be used to access resources in this organization.
UserTokensEnabled *bool `jsonapi:"attr,user-tokens-enabled,omitempty"`
}

// ReadRunQueueOptions represents the options for showing the queue.
Expand Down
58 changes: 58 additions & 0 deletions organization_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,64 @@ func TestOrganizationsUpdate(t *testing.T) {
}
})

t.Run("with new UserTokensEnabled option", func(t *testing.T) {
orgTest, orgTestCleanup := createOrganization(t, client)
t.Cleanup(orgTestCleanup)

assert.True(t, *orgTest.UserTokensEnabled, "user tokens enabled by default")

// we need to switch to an owner's team token, otherwise the client (which auths with a user token)
// wont be able to delete the org after we disable user tokens
teamList, err := client.Teams.List(ctx, orgTest.Name, &TeamListOptions{
Names: []string{"owners"},
})
require.NoError(t, err)

// it should be the only team, we just created the org...
require.Len(t, teamList.Items, 1)
ownersTeam := teamList.Items[0]

ownerToken, ownerTokenCleanup := createTeamToken(t, client, ownersTeam)
t.Cleanup(ownerTokenCleanup)

ownerClient := testClient(t)
ownerClient.token = ownerToken.Token

// disable user tokens for the organization
options := OrganizationUpdateOptions{
UserTokensEnabled: Bool(false),
}

org, err := ownerClient.Organizations.Update(ctx, orgTest.Name, options)
require.NoError(t, err)
assert.False(t, *org.UserTokensEnabled, "user tokens disabled")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider including assertions that validate the change had the intended effect, i.e. try and look up some resources and expect an error here. (& the inverse when the setting is off)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the suggestion, I have added some more assertions!

// try reading something with the user token client and verify that it fails, where the team token client
// succeeds
_, err = client.Organizations.Read(ctx, orgTest.Name)
assert.Error(t, err)
assert.ErrorContains(t, err, "unauthorized")

org, err = ownerClient.Organizations.Read(ctx, orgTest.Name)
assert.NoError(t, err)
assert.Equal(t, orgTest.Name, org.Name)
assert.False(t, *org.UserTokensEnabled, "user tokens disabled")

// re-enable user tokens
options = OrganizationUpdateOptions{
UserTokensEnabled: Bool(true),
}
org, err = ownerClient.Organizations.Update(ctx, orgTest.Name, options)
require.NoError(t, err)
assert.True(t, *org.UserTokensEnabled, "user tokens re-enabled")

// try reading with the user token again and verify that it works
org, err = client.Organizations.Read(ctx, orgTest.Name)
assert.NoError(t, err)
assert.Equal(t, orgTest.Name, org.Name)
assert.True(t, *org.UserTokensEnabled, "user tokens re-enabled")
})

t.Run("with valid options", func(t *testing.T) {
orgTest, orgTestCleanup := createOrganization(t, client)

Expand Down
Loading