This repository has been archived by the owner on Jul 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
78 lines (61 loc) · 1.54 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"context"
"fmt"
"github.com/hashicorp/go-tfe"
)
func fetchAllOrgs(ctx context.Context, client *tfe.Client) ([]string, error) {
var orgs []string
currentPage := 1
for {
options := &tfe.OrganizationListOptions{
ListOptions: tfe.ListOptions{
PageSize: 25,
PageNumber: currentPage,
},
}
result, err := client.Organizations.List(ctx, options)
if err != nil {
return nil, fmt.Errorf("could not fetch organizations: %w", err)
}
for _, org := range result.Items {
orgs = append(orgs, org.Name)
}
if result.NextPage == 0 {
break
}
currentPage++
}
return orgs, nil
}
func fetchAllWorkspaces(ctx context.Context, client *tfe.Client, org string) ([]*tfe.Workspace, error) {
var workspaces []*tfe.Workspace
currentPage := 1
for {
options := &tfe.WorkspaceListOptions{
ListOptions: tfe.ListOptions{
PageSize: 25,
PageNumber: currentPage,
},
}
result, err := client.Workspaces.List(ctx, org, options)
if err != nil {
return nil, fmt.Errorf("could not fetch workspaces: %w", err)
}
for _, workspace := range result.Items {
workspaces = append(workspaces, workspace)
}
if result.NextPage == 0 {
break
}
currentPage++
}
return workspaces, nil
}
func fetchAllResources(ctx context.Context, client *tfe.Client, workspaceId string) ([]*tfe.StateVersionResources, error) {
state, err := client.StateVersions.ReadCurrent(ctx, workspaceId)
if err != nil {
return nil, fmt.Errorf("could not fetch current state: %w", err)
}
return state.Resources, nil
}