@@ -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