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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable workspace and organization Health Assessment (drift detection) setting management #462

Merged
merged 11 commits into from Sep 14, 2022
2 changes: 2 additions & 0 deletions CHANGELOG.md
@@ -1,6 +1,8 @@
# Unreleased
* Add `Query` param field to `OrganizationListOptions` to allow searching based on name or email by @laurenolivia [#529](https://github.com/hashicorp/go-tfe/pull/529)

* Add support for managing workspace and organization health assessment (drift detection) setting [#462](https://github.com/hashicorp/go-tfe/pull/462)

## Bug Fixes
* Fixes null value returned in variable set relationship in `VariableSetVariable` by @sebasslash [#521](https://github.com/hashicorp/go-tfe/pull/521)

Expand Down
7 changes: 7 additions & 0 deletions organization.go
Expand Up @@ -64,6 +64,7 @@ type OrganizationList struct {
// Organization represents a Terraform Enterprise organization.
type Organization struct {
Name string `jsonapi:"primary,organizations"`
AssessmentsEnforced bool `jsonapi:"attr,assessments-enforced"`
CollaboratorAuthPolicy AuthPolicyType `jsonapi:"attr,collaborator-auth-policy"`
CostEstimationEnabled bool `jsonapi:"attr,cost-estimation-enabled"`
CreatedAt time.Time `jsonapi:"attr,created-at,iso8601"`
Expand Down Expand Up @@ -142,6 +143,9 @@ type OrganizationCreateOptions struct {
// Required: Name of the organization.
Name *string `jsonapi:"attr,name"`

// Optional: AssessmentsEnforced toggles whether health assessment enablement is enforced across all assessable workspaces (those with a minimum terraform versio of 0.15.4 and not running in local execution mode) or if the decision to enabled health assessments is delegated to the workspace setting AssessmentsEnabled.
AssessmentsEnforced *bool `jsonapi:"attr,assessments-enforced,omitempty"`

// Required: Admin email address.
Email *string `jsonapi:"attr,email"`

Expand Down Expand Up @@ -175,6 +179,9 @@ type OrganizationUpdateOptions struct {
// New name for the organization.
Name *string `jsonapi:"attr,name,omitempty"`

// Optional: AssessmentsEnforced toggles whether health assessment enablement is enforced across all assessable workspaces (those with a minimum terraform versio of 0.15.4 and not running in local execution mode) or if the decision to enabled health assessments is delegated to the workspace setting AssessmentsEnabled.
AssessmentsEnforced *bool `jsonapi:"attr,assessments-enforced,omitempty"`

// New admin email address.
Email *string `jsonapi:"attr,email,omitempty"`

Expand Down
2 changes: 2 additions & 0 deletions organization_integration_test.go
Expand Up @@ -478,6 +478,7 @@ func TestOrganization_Unmarshal(t *testing.T) {
"type": "organizations",
"id": "org-name",
"attributes": map[string]interface{}{
"assessments-enforced": true,
"collaborator-auth-policy": AuthPolicyPassword,
"cost-estimation-enabled": true,
"created-at": "2018-03-02T23:42:06.651Z",
Expand All @@ -500,6 +501,7 @@ func TestOrganization_Unmarshal(t *testing.T) {
parsedTime, err := time.Parse(iso8601TimeFormat, "2018-03-02T23:42:06.651Z")
require.NoError(t, err)
assert.Equal(t, org.Name, "org-name")
assert.Equal(t, org.AssessmentsEnforced, true)
assert.Equal(t, org.CreatedAt, parsedTime)
assert.Equal(t, org.CollaboratorAuthPolicy, AuthPolicyPassword)
assert.Equal(t, org.CostEstimationEnabled, true)
Expand Down
11 changes: 11 additions & 0 deletions workspace.go
Expand Up @@ -111,6 +111,7 @@ type Workspace struct {
Actions *WorkspaceActions `jsonapi:"attr,actions"`
AgentPoolID string `jsonapi:"attr,agent-pool-id"`
AllowDestroyPlan bool `jsonapi:"attr,allow-destroy-plan"`
AssessmentsEnabled bool `jsonapi:"attr,assessments-enabled"`
AutoApply bool `jsonapi:"attr,auto-apply"`
CanQueueDestroyPlan bool `jsonapi:"attr,can-queue-destroy-plan"`
CreatedAt time.Time `jsonapi:"attr,created-at,iso8601"`
Expand Down Expand Up @@ -264,6 +265,11 @@ type WorkspaceCreateOptions struct {
// Optional: Whether destroy plans can be queued on the workspace.
AllowDestroyPlan *bool `jsonapi:"attr,allow-destroy-plan,omitempty"`

// Optional: Whether to enable health assessments (drift detection etc.) for the workspace.
// Reference: https://www.terraform.io/cloud-docs/api-docs/workspaces#create-a-workspace
// Requires remote execution mode, Terraform Cloud Business entitlement, and a valid agent pool to work
AssessmentsEnabled *bool `jsonapi:"attr,assessments-enabled,omitempty"`

// Optional: Whether to automatically apply changes when a Terraform plan is successful.
AutoApply *bool `jsonapi:"attr,auto-apply,omitempty"`

Expand Down Expand Up @@ -377,6 +383,11 @@ type WorkspaceUpdateOptions struct {
// Optional: Whether destroy plans can be queued on the workspace.
AllowDestroyPlan *bool `jsonapi:"attr,allow-destroy-plan,omitempty"`

// Optional: Whether to enable health assessments (drift detection etc.) for the workspace.
// Reference: https://www.terraform.io/cloud-docs/api-docs/workspaces#update-a-workspace
// Requires remote execution mode, Terraform Cloud Business entitlement, and a valid agent pool to work
AssessmentsEnabled *bool `jsonapi:"attr,assessments-enabled,omitempty"`

// Optional: Whether to automatically apply changes when a Terraform plan is successful.
AutoApply *bool `jsonapi:"attr,auto-apply,omitempty"`

Expand Down
18 changes: 12 additions & 6 deletions workspace_integration_test.go
Expand Up @@ -327,6 +327,7 @@ func TestWorkspacesCreate(t *testing.T) {
AllowDestroyPlan: Bool(false),
AutoApply: Bool(true),
Description: String("qux"),
AssessmentsEnabled: Bool(false),
FileTriggersEnabled: Bool(true),
Operations: Bool(true),
QueueAllRuns: Bool(true),
Expand Down Expand Up @@ -363,6 +364,7 @@ func TestWorkspacesCreate(t *testing.T) {
assert.Equal(t, *options.Description, item.Description)
assert.Equal(t, *options.AllowDestroyPlan, item.AllowDestroyPlan)
assert.Equal(t, *options.AutoApply, item.AutoApply)
assert.Equal(t, *options.AssessmentsEnabled, item.AssessmentsEnabled)
assert.Equal(t, *options.FileTriggersEnabled, item.FileTriggersEnabled)
assert.Equal(t, *options.Operations, item.Operations)
assert.Equal(t, *options.QueueAllRuns, item.QueueAllRuns)
Expand Down Expand Up @@ -715,16 +717,19 @@ func TestWorkspacesUpdate(t *testing.T) {
orgTest, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()

upgradeOrganizationSubscription(t, client, orgTest)

wTest, _ := createWorkspace(t, client, orgTest)

t.Run("when updating a subset of values", func(t *testing.T) {
rexredinger marked this conversation as resolved.
Show resolved Hide resolved
options := WorkspaceUpdateOptions{
Name: String(wTest.Name),
AllowDestroyPlan: Bool(false),
AutoApply: Bool(true),
Operations: Bool(true),
QueueAllRuns: Bool(true),
TerraformVersion: String("0.10.0"),
Name: String(wTest.Name),
AllowDestroyPlan: Bool(false),
AutoApply: Bool(true),
Operations: Bool(true),
QueueAllRuns: Bool(true),
AssessmentsEnabled: Bool(true),
TerraformVersion: String("0.15.4"),
}

wAfter, err := client.Workspaces.Update(ctx, orgTest.Name, wTest.Name, options)
Expand All @@ -734,6 +739,7 @@ func TestWorkspacesUpdate(t *testing.T) {
assert.NotEqual(t, wTest.AllowDestroyPlan, wAfter.AllowDestroyPlan)
assert.NotEqual(t, wTest.AutoApply, wAfter.AutoApply)
assert.NotEqual(t, wTest.QueueAllRuns, wAfter.QueueAllRuns)
assert.NotEqual(t, wTest.AssessmentsEnabled, wAfter.AssessmentsEnabled)
assert.NotEqual(t, wTest.TerraformVersion, wAfter.TerraformVersion)
assert.Equal(t, wTest.WorkingDirectory, wAfter.WorkingDirectory)
})
Expand Down