Skip to content

Commit 64cc9eb

Browse files
chore: correctly fetch terraform variables
1 parent f82c3cb commit 64cc9eb

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

cmd/ctrlc/root/sync/terraform/terraform.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import (
66
"github.com/MakeNowJust/heredoc/v2"
77
"github.com/charmbracelet/log"
88
"github.com/ctrlplanedev/cli/internal/api"
9-
"github.com/ctrlplanedev/cli/internal/cliutil"
9+
10+
// "github.com/ctrlplanedev/cli/internal/cliutil"
1011
"github.com/google/uuid"
1112
"github.com/hashicorp/go-tfe"
1213
"github.com/spf13/cobra"
@@ -86,14 +87,15 @@ func NewSyncTerraformCmd() *cobra.Command {
8687
resources = append(resources, resource)
8788
}
8889

89-
upsertResp, err := ctrlplaneClient.SetResourceProvidersResources(ctx, providerId, api.SetResourceProvidersResourcesJSONRequestBody{
90+
_, err = ctrlplaneClient.SetResourceProvidersResources(ctx, providerId, api.SetResourceProvidersResourcesJSONRequestBody{
9091
Resources: resources,
9192
})
9293
if err != nil {
9394
return fmt.Errorf("failed to upsert resources: %w", err)
9495
}
9596

96-
return cliutil.HandleResponseOutput(cmd, upsertResp)
97+
return nil
98+
// return cliutil.HandleResponseOutput(cmd, upsertResp)
9799
},
98100
}
99101

cmd/ctrlc/root/sync/terraform/terraform_workspaces.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,25 @@ func getLinksMetadata(workspace *tfe.Workspace, baseURL url.URL) *string {
4444
return &linksString
4545
}
4646

47-
func getWorkspaceVariables(workspace *tfe.Workspace) map[string]string {
47+
func getWorkspaceVariables(ctx context.Context, workspace *tfe.Workspace, client *tfe.Client) map[string]string {
4848
variables := make(map[string]string)
4949
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
50+
if variable == nil || variable.Sensitive {
51+
continue
52+
}
53+
54+
fetchedVariable, err := client.Variables.Read(ctx, workspace.ID, variable.ID)
55+
if err != nil {
56+
log.Error("Failed to read variable", "error", err, "variable", variable.Key)
57+
continue
5358
}
59+
60+
if fetchedVariable.Category != tfe.CategoryTerraform || fetchedVariable.Sensitive {
61+
continue
62+
}
63+
64+
variables[fetchedVariable.Key] = fetchedVariable.Value
65+
time.Sleep(200 * time.Millisecond)
5466
}
5567
return variables
5668
}
@@ -76,7 +88,7 @@ func getWorkspaceTags(workspace *tfe.Workspace) map[string]string {
7688
return tags
7789
}
7890

79-
func convertWorkspaceToResource(workspace *tfe.Workspace, baseURL url.URL) (WorkspaceResource, error) {
91+
func convertWorkspaceToResource(ctx context.Context, workspace *tfe.Workspace, client *tfe.Client) (WorkspaceResource, error) {
8092
if workspace == nil {
8193
return WorkspaceResource{}, fmt.Errorf("workspace is nil")
8294
}
@@ -98,13 +110,13 @@ func convertWorkspaceToResource(workspace *tfe.Workspace, baseURL url.URL) (Work
98110
metadata["terraform-cloud/organization"] = workspace.Organization.Name
99111
}
100112

101-
linksMetadata := getLinksMetadata(workspace, baseURL)
113+
linksMetadata := getLinksMetadata(workspace, client.BaseURL())
102114
if linksMetadata != nil {
103115
metadata["ctrlplane/links"] = *linksMetadata
104116
}
105117

106118
moreValues := []map[string]string{
107-
getWorkspaceVariables(workspace),
119+
getWorkspaceVariables(ctx, workspace, client),
108120
getWorkspaceTags(workspace),
109121
getWorkspaceVcsRepo(workspace),
110122
}
@@ -174,12 +186,13 @@ func getWorkspacesInOrg(ctx context.Context, client *tfe.Client, organization st
174186

175187
workspaceResources := []WorkspaceResource{}
176188
for _, workspace := range workspaces {
177-
workspaceResource, err := convertWorkspaceToResource(workspace, client.BaseURL())
189+
workspaceResource, err := convertWorkspaceToResource(ctx, workspace, client)
178190
if err != nil {
179191
log.Error("Failed to convert workspace to resource", "error", err, "workspace", workspace.Name)
180192
continue
181193
}
182194
workspaceResources = append(workspaceResources, workspaceResource)
195+
time.Sleep(1 * time.Second)
183196
}
184197
return workspaceResources, nil
185198
}

0 commit comments

Comments
 (0)