|
| 1 | +package terraform |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "net/url" |
| 8 | + "time" |
| 9 | + |
| 10 | + "strconv" |
| 11 | + |
| 12 | + "github.com/avast/retry-go" |
| 13 | + "github.com/charmbracelet/log" |
| 14 | + "github.com/hashicorp/go-tfe" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + Kind = "Workspace" |
| 19 | + Version = "terraform/v1" |
| 20 | +) |
| 21 | + |
| 22 | +type WorkspaceResource struct { |
| 23 | + Config map[string]interface{} |
| 24 | + Identifier string |
| 25 | + Kind string |
| 26 | + Metadata map[string]string |
| 27 | + Name string |
| 28 | + Version string |
| 29 | +} |
| 30 | + |
| 31 | +func getLinksMetadata(workspace *tfe.Workspace, baseURL url.URL) *string { |
| 32 | + if workspace.Organization == nil { |
| 33 | + return nil |
| 34 | + } |
| 35 | + links := map[string]string{ |
| 36 | + "Terraform Workspace": fmt.Sprintf("%s/app/%s/workspaces/%s", baseURL.String(), workspace.Organization.Name, workspace.Name), |
| 37 | + } |
| 38 | + linksJSON, err := json.Marshal(links) |
| 39 | + if err != nil { |
| 40 | + log.Error("Failed to marshal links", "error", err) |
| 41 | + return nil |
| 42 | + } |
| 43 | + linksString := string(linksJSON) |
| 44 | + return &linksString |
| 45 | +} |
| 46 | + |
| 47 | +func getWorkspaceVariables(workspace *tfe.Workspace) map[string]string { |
| 48 | + variables := make(map[string]string) |
| 49 | + for _, variable := range workspace.Variables { |
| 50 | + if variable != nil && variable.Category == tfe.CategoryTerraform && !variable.Sensitive { |
| 51 | + key := fmt.Sprintf("terraform-cloud/variables/%s", variable.Key) |
| 52 | + variables[key] = variable.Value |
| 53 | + } |
| 54 | + } |
| 55 | + return variables |
| 56 | +} |
| 57 | + |
| 58 | +func getWorkspaceVcsRepo(workspace *tfe.Workspace) map[string]string { |
| 59 | + vcsRepo := make(map[string]string) |
| 60 | + if workspace.VCSRepo != nil { |
| 61 | + vcsRepo["terraform-cloud/vcs-repo/identifier"] = workspace.VCSRepo.Identifier |
| 62 | + vcsRepo["terraform-cloud/vcs-repo/branch"] = workspace.VCSRepo.Branch |
| 63 | + vcsRepo["terraform-cloud/vcs-repo/repository-http-url"] = workspace.VCSRepo.RepositoryHTTPURL |
| 64 | + } |
| 65 | + return vcsRepo |
| 66 | +} |
| 67 | + |
| 68 | +func getWorkspaceTags(workspace *tfe.Workspace) map[string]string { |
| 69 | + tags := make(map[string]string) |
| 70 | + for _, tag := range workspace.Tags { |
| 71 | + if tag != nil { |
| 72 | + key := fmt.Sprintf("terraform-cloud/tag/%s", tag.Name) |
| 73 | + tags[key] = "true" |
| 74 | + } |
| 75 | + } |
| 76 | + return tags |
| 77 | +} |
| 78 | + |
| 79 | +func convertWorkspaceToResource(workspace *tfe.Workspace, baseURL url.URL) (WorkspaceResource, error) { |
| 80 | + if workspace == nil { |
| 81 | + return WorkspaceResource{}, fmt.Errorf("workspace is nil") |
| 82 | + } |
| 83 | + version := Version |
| 84 | + kind := Kind |
| 85 | + name := workspace.Name |
| 86 | + identifier := workspace.ID |
| 87 | + config := map[string]interface{}{ |
| 88 | + "workspaceId": workspace.ID, |
| 89 | + } |
| 90 | + metadata := map[string]string{ |
| 91 | + "ctrlplane/external-id": workspace.ID, |
| 92 | + "terraform-cloud/workspace-name": workspace.Name, |
| 93 | + "terraform-cloud/workspace-auto-apply": strconv.FormatBool(workspace.AutoApply), |
| 94 | + "terraform/version": workspace.TerraformVersion, |
| 95 | + } |
| 96 | + |
| 97 | + if workspace.Organization != nil { |
| 98 | + metadata["terraform-cloud/organization"] = workspace.Organization.Name |
| 99 | + } |
| 100 | + |
| 101 | + linksMetadata := getLinksMetadata(workspace, baseURL) |
| 102 | + if linksMetadata != nil { |
| 103 | + metadata["ctrlplane/links"] = *linksMetadata |
| 104 | + } |
| 105 | + |
| 106 | + moreValues := []map[string]string{ |
| 107 | + getWorkspaceVariables(workspace), |
| 108 | + getWorkspaceTags(workspace), |
| 109 | + getWorkspaceVcsRepo(workspace), |
| 110 | + } |
| 111 | + |
| 112 | + for _, moreValue := range moreValues { |
| 113 | + for key, value := range moreValue { |
| 114 | + metadata[key] = value |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return WorkspaceResource{ |
| 119 | + Version: version, |
| 120 | + Kind: kind, |
| 121 | + Name: name, |
| 122 | + Identifier: identifier, |
| 123 | + Config: config, |
| 124 | + Metadata: metadata, |
| 125 | + }, nil |
| 126 | +} |
| 127 | + |
| 128 | +func listWorkspacesWithRetry(ctx context.Context, client *tfe.Client, organization string, pageNum, pageSize int) (*tfe.WorkspaceList, error) { |
| 129 | + var workspaces *tfe.WorkspaceList |
| 130 | + err := retry.Do( |
| 131 | + func() error { |
| 132 | + var err error |
| 133 | + workspaces, err = client.Workspaces.List(ctx, organization, &tfe.WorkspaceListOptions{ |
| 134 | + ListOptions: tfe.ListOptions{ |
| 135 | + PageNumber: pageNum, |
| 136 | + PageSize: pageSize, |
| 137 | + }, |
| 138 | + }) |
| 139 | + return err |
| 140 | + }, |
| 141 | + retry.Attempts(5), |
| 142 | + retry.Delay(time.Second), |
| 143 | + retry.MaxDelay(5*time.Second), |
| 144 | + ) |
| 145 | + return workspaces, err |
| 146 | +} |
| 147 | + |
| 148 | +func listAllWorkspaces(ctx context.Context, client *tfe.Client, organization string) ([]*tfe.Workspace, error) { |
| 149 | + var allWorkspaces []*tfe.Workspace |
| 150 | + pageNum := 1 |
| 151 | + pageSize := 100 |
| 152 | + |
| 153 | + for { |
| 154 | + workspaces, err := listWorkspacesWithRetry(ctx, client, organization, pageNum, pageSize) |
| 155 | + if err != nil { |
| 156 | + return nil, fmt.Errorf("failed to list workspaces: %w", err) |
| 157 | + } |
| 158 | + |
| 159 | + allWorkspaces = append(allWorkspaces, workspaces.Items...) |
| 160 | + if len(workspaces.Items) < pageSize { |
| 161 | + break |
| 162 | + } |
| 163 | + pageNum++ |
| 164 | + } |
| 165 | + |
| 166 | + return allWorkspaces, nil |
| 167 | +} |
| 168 | + |
| 169 | +func getWorkspacesInOrg(ctx context.Context, client *tfe.Client, organization string) ([]WorkspaceResource, error) { |
| 170 | + workspaces, err := listAllWorkspaces(ctx, client, organization) |
| 171 | + if err != nil { |
| 172 | + return nil, err |
| 173 | + } |
| 174 | + |
| 175 | + workspaceResources := []WorkspaceResource{} |
| 176 | + for _, workspace := range workspaces { |
| 177 | + workspaceResource, err := convertWorkspaceToResource(workspace, client.BaseURL()) |
| 178 | + if err != nil { |
| 179 | + log.Error("Failed to convert workspace to resource", "error", err, "workspace", workspace.Name) |
| 180 | + continue |
| 181 | + } |
| 182 | + workspaceResources = append(workspaceResources, workspaceResource) |
| 183 | + } |
| 184 | + return workspaceResources, nil |
| 185 | +} |
0 commit comments