|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package provider |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "errors" |
| 9 | + "fmt" |
| 10 | + "log" |
| 11 | + "strings" |
| 12 | + |
| 13 | + tfe "github.com/hashicorp/go-tfe" |
| 14 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 15 | +) |
| 16 | + |
| 17 | +func resourceTFEWorkspacePolicySetExclusion() *schema.Resource { |
| 18 | + return &schema.Resource{ |
| 19 | + Create: resourceTFEWorkspacePolicySetExclusionCreate, |
| 20 | + Read: resourceTFEWorkspacePolicySetExclusionRead, |
| 21 | + Delete: resourceTFEWorkspacePolicySetExclusionDelete, |
| 22 | + Importer: &schema.ResourceImporter{ |
| 23 | + StateContext: resourceTFEWorkspacePolicySetExclusionImporter, |
| 24 | + }, |
| 25 | + |
| 26 | + Schema: map[string]*schema.Schema{ |
| 27 | + "policy_set_id": { |
| 28 | + Type: schema.TypeString, |
| 29 | + Required: true, |
| 30 | + ForceNew: true, |
| 31 | + }, |
| 32 | + |
| 33 | + "workspace_id": { |
| 34 | + Type: schema.TypeString, |
| 35 | + Required: true, |
| 36 | + ForceNew: true, |
| 37 | + }, |
| 38 | + }, |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func resourceTFEWorkspacePolicySetExclusionCreate(d *schema.ResourceData, meta interface{}) error { |
| 43 | + config := meta.(ConfiguredClient) |
| 44 | + |
| 45 | + policySetID := d.Get("policy_set_id").(string) |
| 46 | + workspaceExclusionID := d.Get("workspace_id").(string) |
| 47 | + |
| 48 | + policySetAddWorkspaceExclusionsOptions := tfe.PolicySetAddWorkspaceExclusionsOptions{} |
| 49 | + policySetAddWorkspaceExclusionsOptions.WorkspaceExclusions = append(policySetAddWorkspaceExclusionsOptions.WorkspaceExclusions, &tfe.Workspace{ID: workspaceExclusionID}) |
| 50 | + |
| 51 | + err := config.Client.PolicySets.AddWorkspaceExclusions(ctx, policySetID, policySetAddWorkspaceExclusionsOptions) |
| 52 | + if err != nil { |
| 53 | + return fmt.Errorf( |
| 54 | + "error adding workspace exclusion %s to policy set id %s: %w", workspaceExclusionID, policySetID, err) |
| 55 | + } |
| 56 | + |
| 57 | + d.SetId(fmt.Sprintf("%s_%s", workspaceExclusionID, policySetID)) |
| 58 | + |
| 59 | + return resourceTFEWorkspacePolicySetExclusionRead(d, meta) |
| 60 | +} |
| 61 | + |
| 62 | +func resourceTFEWorkspacePolicySetExclusionRead(d *schema.ResourceData, meta interface{}) error { |
| 63 | + config := meta.(ConfiguredClient) |
| 64 | + |
| 65 | + policySetID := d.Get("policy_set_id").(string) |
| 66 | + workspaceExclusionsID := d.Get("workspace_id").(string) |
| 67 | + |
| 68 | + log.Printf("[DEBUG] Read configuration of excluded workspace policy set: %s", policySetID) |
| 69 | + policySet, err := config.Client.PolicySets.ReadWithOptions(ctx, policySetID, &tfe.PolicySetReadOptions{ |
| 70 | + Include: []tfe.PolicySetIncludeOpt{tfe.PolicySetWorkspaceExclusions}, |
| 71 | + }) |
| 72 | + if err != nil { |
| 73 | + if errors.Is(err, tfe.ErrResourceNotFound) { |
| 74 | + log.Printf("[DEBUG] Policy set %s no longer exists", policySetID) |
| 75 | + d.SetId("") |
| 76 | + return nil |
| 77 | + } |
| 78 | + return fmt.Errorf("error reading configuration of policy set %s: %w", policySetID, err) |
| 79 | + } |
| 80 | + |
| 81 | + isWorkspaceExclusionsAttached := false |
| 82 | + for _, excludedWorkspace := range policySet.WorkspaceExclusions { |
| 83 | + if excludedWorkspace.ID == workspaceExclusionsID { |
| 84 | + isWorkspaceExclusionsAttached = true |
| 85 | + d.Set("workspace_id", workspaceExclusionsID) |
| 86 | + break |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + if !isWorkspaceExclusionsAttached { |
| 91 | + log.Printf("[DEBUG] Excluded workspace %s not attached to policy set %s. Removing from state.", workspaceExclusionsID, policySetID) |
| 92 | + d.SetId("") |
| 93 | + return nil |
| 94 | + } |
| 95 | + |
| 96 | + d.Set("policy_set_id", policySetID) |
| 97 | + return nil |
| 98 | +} |
| 99 | + |
| 100 | +func resourceTFEWorkspacePolicySetExclusionDelete(d *schema.ResourceData, meta interface{}) error { |
| 101 | + config := meta.(ConfiguredClient) |
| 102 | + |
| 103 | + policySetID := d.Get("policy_set_id").(string) |
| 104 | + workspaceExclusionsID := d.Get("workspace_id").(string) |
| 105 | + |
| 106 | + log.Printf("[DEBUG] Removing excluded workspace (%s) from policy set (%s)", workspaceExclusionsID, policySetID) |
| 107 | + policySetRemoveWorkspaceExclusionsOptions := tfe.PolicySetRemoveWorkspaceExclusionsOptions{} |
| 108 | + policySetRemoveWorkspaceExclusionsOptions.WorkspaceExclusions = append(policySetRemoveWorkspaceExclusionsOptions.WorkspaceExclusions, &tfe.Workspace{ID: workspaceExclusionsID}) |
| 109 | + |
| 110 | + err := config.Client.PolicySets.RemoveWorkspaceExclusions(ctx, policySetID, policySetRemoveWorkspaceExclusionsOptions) |
| 111 | + if err != nil { |
| 112 | + return fmt.Errorf( |
| 113 | + "error removing excluded workspace %s from policy set %s: %w", workspaceExclusionsID, policySetID, err) |
| 114 | + } |
| 115 | + |
| 116 | + return nil |
| 117 | +} |
| 118 | + |
| 119 | +func resourceTFEWorkspacePolicySetExclusionImporter(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { |
| 120 | + // The format of the import ID is <ORGANIZATION/WORKSPACE NAME/POLICYSET NAME> |
| 121 | + splitID := strings.SplitN(d.Id(), "/", 3) |
| 122 | + if len(splitID) != 3 { |
| 123 | + return nil, fmt.Errorf( |
| 124 | + "invalid excluded workspace policy set input format: %s (expected <ORGANIZATION>/<WORKSPACE NAME>/<POLICYSET NAME>)", |
| 125 | + splitID, |
| 126 | + ) |
| 127 | + } |
| 128 | + |
| 129 | + organization, wsName, pSName := splitID[0], splitID[1], splitID[2] |
| 130 | + |
| 131 | + config := meta.(ConfiguredClient) |
| 132 | + |
| 133 | + // Ensure the named workspace exists before fetching all the policy sets in the org |
| 134 | + _, err := config.Client.Workspaces.Read(ctx, organization, wsName) |
| 135 | + if err != nil { |
| 136 | + return nil, fmt.Errorf("error reading configuration of the workspace to exclude %s in organization %s: %w", wsName, organization, err) |
| 137 | + } |
| 138 | + |
| 139 | + options := &tfe.PolicySetListOptions{Include: []tfe.PolicySetIncludeOpt{tfe.PolicySetWorkspaceExclusions}} |
| 140 | + for { |
| 141 | + list, err := config.Client.PolicySets.List(ctx, organization, options) |
| 142 | + if err != nil { |
| 143 | + return nil, fmt.Errorf("error retrieving policy sets: %w", err) |
| 144 | + } |
| 145 | + for _, policySet := range list.Items { |
| 146 | + if policySet.Name != pSName { |
| 147 | + continue |
| 148 | + } |
| 149 | + |
| 150 | + for _, ws := range policySet.WorkspaceExclusions { |
| 151 | + if ws.Name != wsName { |
| 152 | + continue |
| 153 | + } |
| 154 | + |
| 155 | + d.Set("workspace_id", ws.ID) |
| 156 | + d.Set("policy_set_id", policySet.ID) |
| 157 | + d.SetId(fmt.Sprintf("%s_%s", ws.ID, policySet.ID)) |
| 158 | + |
| 159 | + return []*schema.ResourceData{d}, nil |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + // Exit the loop when we've seen all pages. |
| 164 | + if list.CurrentPage >= list.TotalPages { |
| 165 | + break |
| 166 | + } |
| 167 | + |
| 168 | + // Update the page number to get the next page. |
| 169 | + options.PageNumber = list.NextPage |
| 170 | + } |
| 171 | + |
| 172 | + return nil, fmt.Errorf("excluded workspace %s has not been added to policy set %s", wsName, pSName) |
| 173 | +} |
0 commit comments