Skip to content

Commit

Permalink
add new agent_pool_allowed_workspaces resource
Browse files Browse the repository at this point in the history
  • Loading branch information
hs26gill committed May 9, 2023
1 parent 40b8e30 commit 5f1f22c
Show file tree
Hide file tree
Showing 5 changed files with 398 additions and 83 deletions.
71 changes: 36 additions & 35 deletions tfe/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,41 +131,42 @@ func Provider() *schema.Provider {
},

ResourcesMap: map[string]*schema.Resource{
"tfe_admin_organization_settings": resourceTFEAdminOrganizationSettings(),
"tfe_agent_pool": resourceTFEAgentPool(),
"tfe_agent_token": resourceTFEAgentToken(),
"tfe_notification_configuration": resourceTFENotificationConfiguration(),
"tfe_oauth_client": resourceTFEOAuthClient(),
"tfe_organization": resourceTFEOrganization(),
"tfe_organization_membership": resourceTFEOrganizationMembership(),
"tfe_organization_module_sharing": resourceTFEOrganizationModuleSharing(),
"tfe_organization_run_task": resourceTFEOrganizationRunTask(),
"tfe_organization_token": resourceTFEOrganizationToken(),
"tfe_policy": resourceTFEPolicy(),
"tfe_policy_set": resourceTFEPolicySet(),
"tfe_policy_set_parameter": resourceTFEPolicySetParameter(),
"tfe_project": resourceTFEProject(),
"tfe_project_variable_set": resourceTFEProjectVariableSet(),
"tfe_registry_module": resourceTFERegistryModule(),
"tfe_no_code_module": resourceTFENoCodeModule(),
"tfe_run_trigger": resourceTFERunTrigger(),
"tfe_sentinel_policy": resourceTFESentinelPolicy(),
"tfe_ssh_key": resourceTFESSHKey(),
"tfe_team": resourceTFETeam(),
"tfe_team_access": resourceTFETeamAccess(),
"tfe_team_organization_member": resourceTFETeamOrganizationMember(),
"tfe_team_organization_members": resourceTFETeamOrganizationMembers(),
"tfe_team_project_access": resourceTFETeamProjectAccess(),
"tfe_team_member": resourceTFETeamMember(),
"tfe_team_members": resourceTFETeamMembers(),
"tfe_team_token": resourceTFETeamToken(),
"tfe_terraform_version": resourceTFETerraformVersion(),
"tfe_workspace": resourceTFEWorkspace(),
"tfe_workspace_run_task": resourceTFEWorkspaceRunTask(),
"tfe_variable": resourceTFEVariable(),
"tfe_variable_set": resourceTFEVariableSet(),
"tfe_workspace_variable_set": resourceTFEWorkspaceVariableSet(),
"tfe_workspace_policy_set": resourceTFEWorkspacePolicySet(),
"tfe_admin_organization_settings": resourceTFEAdminOrganizationSettings(),
"tfe_agent_pool": resourceTFEAgentPool(),
"tfe_agent_pool_allowed_workspaces": resourceTFEAgentPoolAllowedWorkspaces(),
"tfe_agent_token": resourceTFEAgentToken(),
"tfe_notification_configuration": resourceTFENotificationConfiguration(),
"tfe_oauth_client": resourceTFEOAuthClient(),
"tfe_organization": resourceTFEOrganization(),
"tfe_organization_membership": resourceTFEOrganizationMembership(),
"tfe_organization_module_sharing": resourceTFEOrganizationModuleSharing(),
"tfe_organization_run_task": resourceTFEOrganizationRunTask(),
"tfe_organization_token": resourceTFEOrganizationToken(),
"tfe_policy": resourceTFEPolicy(),
"tfe_policy_set": resourceTFEPolicySet(),
"tfe_policy_set_parameter": resourceTFEPolicySetParameter(),
"tfe_project": resourceTFEProject(),
"tfe_project_variable_set": resourceTFEProjectVariableSet(),
"tfe_registry_module": resourceTFERegistryModule(),
"tfe_no_code_module": resourceTFENoCodeModule(),
"tfe_run_trigger": resourceTFERunTrigger(),
"tfe_sentinel_policy": resourceTFESentinelPolicy(),
"tfe_ssh_key": resourceTFESSHKey(),
"tfe_team": resourceTFETeam(),
"tfe_team_access": resourceTFETeamAccess(),
"tfe_team_organization_member": resourceTFETeamOrganizationMember(),
"tfe_team_organization_members": resourceTFETeamOrganizationMembers(),
"tfe_team_project_access": resourceTFETeamProjectAccess(),
"tfe_team_member": resourceTFETeamMember(),
"tfe_team_members": resourceTFETeamMembers(),
"tfe_team_token": resourceTFETeamToken(),
"tfe_terraform_version": resourceTFETerraformVersion(),
"tfe_workspace": resourceTFEWorkspace(),
"tfe_workspace_run_task": resourceTFEWorkspaceRunTask(),
"tfe_variable": resourceTFEVariable(),
"tfe_variable_set": resourceTFEVariableSet(),
"tfe_workspace_variable_set": resourceTFEWorkspaceVariableSet(),
"tfe_workspace_policy_set": resourceTFEWorkspacePolicySet(),
},
ConfigureContextFunc: configure(),
}
Expand Down
34 changes: 0 additions & 34 deletions tfe/resource_tfe_agent_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,6 @@ func resourceTFEAgentPool() *schema.Resource {
Optional: true,
Default: true,
},

"allowed_workspace_ids": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
Expand All @@ -68,14 +61,6 @@ func resourceTFEAgentPoolCreate(d *schema.ResourceData, meta interface{}) error
OrganizationScoped: tfe.Bool(d.Get("organization_scoped").(bool)),
}

if allowedWorkspaceIDs, allowedWorkspaceSet := d.GetOk("allowed_workspace_ids"); !*options.OrganizationScoped && allowedWorkspaceSet {
for _, workspaceID := range allowedWorkspaceIDs.(*schema.Set).List() {
if val, ok := workspaceID.(string); ok {
options.AllowedWorkspaces = append(options.AllowedWorkspaces, &tfe.Workspace{ID: val})
}
}
}

log.Printf("[DEBUG] Create new agent pool for organization: %s", organization)
agentPool, err := config.Client.AgentPools.Create(ctx, organization, options)
if err != nil {
Expand Down Expand Up @@ -107,12 +92,6 @@ func resourceTFEAgentPoolRead(d *schema.ResourceData, meta interface{}) error {
d.Set("organization", agentPool.Organization.Name)
d.Set("organization_scoped", agentPool.OrganizationScoped)

var wids []interface{}
for _, workspace := range agentPool.AllowedWorkspaces {
wids = append(wids, workspace.ID)
}
d.Set("allowed_workspace_ids", wids)

return nil
}

Expand All @@ -125,19 +104,6 @@ func resourceTFEAgentPoolUpdate(d *schema.ResourceData, meta interface{}) error
OrganizationScoped: tfe.Bool(d.Get("organization_scoped").(bool)),
}

//if d.HasChange("organization_scoped") {
// options.OrganizationScoped = tfe.Bool(d.Get("organization_scoped").(bool))
//}

if allowedWorkspaceIDs, allowedWorkspaceSet := d.GetOk("allowed_workspace_ids"); !*options.OrganizationScoped && allowedWorkspaceSet {
options.AllowedWorkspaces = []*tfe.Workspace{}
for _, workspaceID := range allowedWorkspaceIDs.(*schema.Set).List() {
if val, ok := workspaceID.(string); ok {
options.AllowedWorkspaces = append(options.AllowedWorkspaces, &tfe.Workspace{ID: val})
}
}
}

log.Printf("[DEBUG] Update agent pool: %s", d.Id())
_, err := config.Client.AgentPools.Update(ctx, d.Id(), options)
if err != nil {
Expand Down
169 changes: 169 additions & 0 deletions tfe/resource_tfe_agent_pool_allowed_workspaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package tfe

import (
"fmt"
"github.com/hashicorp/go-tfe"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"log"
)

func resourceTFEAgentPoolAllowedWorkspaces() *schema.Resource {
return &schema.Resource{
Create: resourceTFEAgentPoolAllowedWorkspacesCreate,
Read: resourceTFEAgentPoolAllowedWorkspacesRead,
Update: resourceTFEAgentPoolAllowedWorkspacesUpdate,
Delete: resourceTFEAgentPoolAllowedWorkspacesDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},

Schema: map[string]*schema.Schema{
"agent_pool_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"allowed_workspace_ids": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}

func resourceTFEAgentPoolAllowedWorkspacesCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(ConfiguredClient)

apID := d.Get("agent_pool_id").(string)
agentPool, err := config.Client.AgentPools.Read(ctx, apID)
if err != nil {
if err == tfe.ErrResourceNotFound {
log.Printf("[DEBUG] agent pool %s no longer exists", apID)
d.SetId("")
return nil
}
return fmt.Errorf("Error reading configuration of agent pool %s: %w", apID, err)
}

// Create a new options struct.
options := tfe.AgentPoolUpdateOptions{
Name: tfe.String(agentPool.Name),
}

if allowedWorkspaceIDs, allowedWorkspaceSet := d.GetOk("allowed_workspace_ids"); allowedWorkspaceSet {
options.AllowedWorkspaces = []*tfe.Workspace{}
for _, workspaceID := range allowedWorkspaceIDs.(*schema.Set).List() {
if val, ok := workspaceID.(string); ok {
options.AllowedWorkspaces = append(options.AllowedWorkspaces, &tfe.Workspace{ID: val})
}
}
}

log.Printf("[DEBUG] Update agent pool: %s", apID)
_, err = config.Client.AgentPools.Update(ctx, apID, options)
if err != nil {
return fmt.Errorf("Error updating agent pool %s: %w", apID, err)
}

d.SetId(apID)

return nil
}

func resourceTFEAgentPoolAllowedWorkspacesRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(ConfiguredClient)

agentPool, err := config.Client.AgentPools.Read(ctx, d.Id())
if err != nil {
if err == tfe.ErrResourceNotFound {
log.Printf("[DEBUG] agent pool %s no longer exists", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("Error reading configuration of agent pool %s: %w", d.Id(), err)
}

var allowedWorkspaceIDs []string
for _, workspace := range agentPool.AllowedWorkspaces {
allowedWorkspaceIDs = append(allowedWorkspaceIDs, workspace.ID)
}
d.Set("allowed_workspace_ids", allowedWorkspaceIDs)
d.Set("agent_pool_id", agentPool.ID)

return nil
}

func resourceTFEAgentPoolAllowedWorkspacesUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(ConfiguredClient)

apID := d.Get("agent_pool_id").(string)
agentPool, err := config.Client.AgentPools.Read(ctx, apID)
if err != nil {
if err == tfe.ErrResourceNotFound {
log.Printf("[DEBUG] agent pool %s no longer exists", apID)
d.SetId("")
return nil
}
return fmt.Errorf("Error reading configuration of agent pool %s: %w", apID, err)
}

// Create a new options struct.
options := tfe.AgentPoolUpdateOptions{
Name: tfe.String(agentPool.Name),
}

if allowedWorkspaceIDs, allowedWorkspaceSet := d.GetOk("allowed_workspace_ids"); allowedWorkspaceSet {
options.AllowedWorkspaces = []*tfe.Workspace{}
for _, workspaceID := range allowedWorkspaceIDs.(*schema.Set).List() {
if val, ok := workspaceID.(string); ok {
options.AllowedWorkspaces = append(options.AllowedWorkspaces, &tfe.Workspace{ID: val})
}
}
}

log.Printf("[DEBUG] Update agent pool: %s", apID)
_, err = config.Client.AgentPools.Update(ctx, apID, options)
if err != nil {
return fmt.Errorf("Error updating agent pool %s: %w", apID, err)
}

d.SetId(apID)

return nil
}

func resourceTFEAgentPoolAllowedWorkspacesDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(ConfiguredClient)

apID := d.Get("agent_pool_id").(string)
agentPool, err := config.Client.AgentPools.Read(ctx, apID)
if err != nil {
if err == tfe.ErrResourceNotFound {
log.Printf("[DEBUG] agent pool %s no longer exists", apID)
d.SetId("")
return nil
}
return fmt.Errorf("Error reading configuration of agent pool %s: %w", apID, err)
}

// Create a new options struct.
options := tfe.AgentPoolUpdateOptions{
Name: tfe.String(agentPool.Name),
AllowedWorkspaces: nil,
}

log.Printf("[DEBUG] Update agent pool: %s", apID)
_, err = config.Client.AgentPools.Update(ctx, apID, options)
if err != nil {
return fmt.Errorf("Error updating agent pool %s: %w", apID, err)
}

d.SetId(apID)

return nil
}
Loading

0 comments on commit 5f1f22c

Please sign in to comment.