Created
January 6, 2021 15:51
-
-
Save SkYNewZ/38ea3bbebcdaca62f3bbaeee49ce156b to your computer and use it in GitHub Desktop.
List Terraform Cloud workspaces and related runs matching date
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"sync" | |
"time" | |
log "github.com/sirupsen/logrus" | |
"github.com/hashicorp/go-tfe" | |
"github.com/XXX/google-groups-terraform-sync/terraform" | |
) | |
const ( | |
org string = "XXX" | |
token string = "REDACTED" | |
v1VCS = "XXX/iwc-gcp-landingzone" | |
) | |
var ( | |
wantedCreatedAtRun = time.Date(2021, time.January, 1, 0, 0, 0, 0, time.UTC) | |
) | |
func main() { | |
tfc, err := terraform.New(context.Background(), &terraform.Config{ | |
Organization: org, | |
AccessToken: token, | |
HTTPClient: nil, | |
}) | |
if err != nil { | |
log.Fatalln(err) | |
} | |
// List runs for this workspace | |
t, err := tfe.NewClient(&tfe.Config{ | |
Token: token, | |
}) | |
if err != nil { | |
log.Fatalln(err) | |
} | |
// List workspaces | |
workspaces, err := tfc.ListWorkspaces() | |
if err != nil { | |
log.Fatalln(err) | |
} | |
var v1workspaces = make([]*terraform.Workspace, 0) | |
var v1workspacesRuns = make([][]*tfe.Run, 0) | |
var lock = &sync.Mutex{} | |
var wg = &sync.WaitGroup{} | |
wg.Add(len(workspaces.Items)) | |
for _, workspace := range workspaces.Items { | |
go func(ws *terraform.Workspace) { | |
defer wg.Done() | |
// Filter then by lzv1 | |
if ws.VCSRepo != nil && ws.VCSRepo.Identifier != v1VCS { | |
return | |
} | |
// List runs | |
runs, err := listAllRuns(ws.ID, t) | |
if err != nil { | |
log.Errorln(err) | |
return | |
} | |
// To use runs | |
var r = make([]*tfe.Run, 0) | |
for _, run := range runs { | |
// If run created BEFORE what we want, ignore it | |
if run.CreatedAt.Before(wantedCreatedAtRun) { | |
continue | |
} | |
// Ignore destroy | |
if run.IsDestroy { | |
continue | |
} | |
r = append(r, run) | |
} | |
if len(r) > 0 { | |
// Append them into lists | |
lock.Lock() | |
defer lock.Unlock() | |
v1workspaces = append(v1workspaces, ws) | |
v1workspacesRuns = append(v1workspacesRuns, r) | |
} | |
}(workspace) | |
} | |
wg.Wait() | |
// Print result | |
for i, ws := range v1workspaces { | |
fmt.Printf("%q -> %d runs\n", ws.Name, len(v1workspacesRuns[i])) | |
} | |
} | |
func listAllRuns(workspaceID string, tfc *tfe.Client) ([]*tfe.Run, error) { | |
if workspaceID == "" { | |
return nil, fmt.Errorf("missing workspaceID") | |
} | |
var results = make([]*tfe.Run, 0) | |
var opts = tfe.ListOptions{ | |
PageNumber: 1, | |
PageSize: 100, | |
} | |
// Initial list | |
runs, err := tfc.Runs.List(context.Background(), workspaceID, tfe.RunListOptions{ListOptions: opts}) | |
if err != nil { | |
return nil, err | |
} | |
// If we reach the end | |
if runs.NextPage == 0 { | |
return runs.Items, nil | |
} | |
// Save | |
results = runs.Items | |
// Paginate | |
for i := 2; i <= runs.TotalPages; i++ { | |
opts.PageNumber = i | |
runs, err := tfc.Runs.List(context.Background(), workspaceID, tfe.RunListOptions{ListOptions: opts}) | |
if err != nil { | |
return nil, err | |
} | |
results = append(results, runs.Items...) | |
} | |
return results, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment