Skip to content

Commit

Permalink
added fixes and readme
Browse files Browse the repository at this point in the history
  • Loading branch information
emocharnik committed May 31, 2023
1 parent a12d55b commit 92138a2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
25 changes: 25 additions & 0 deletions tfc-runs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# TFC Runs
Get the total number of runs executed in TFC

## Requirements:
* `python3` and `pip3`
* `pip3 install click requests --quiet`
* `terraform login` has been performed from the the device this is being exeuted on (script uses the well known credentials file)

## Usage

`python3 runs.py list-all`

### Expected Result

```
python3 runs.py list-all
Workspace demo1 has had 2 runs.
Workspace VPC has had 5 runs.
Workspaces3 has had 0 runs.
Workspace MigrationDemo has had 2 runs.
Workspace Scalr has had 1 runs.
---------------------------
The total runs count across all organizations: 10
```
33 changes: 23 additions & 10 deletions debuger/runs.py → tfc-runs/runs.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,30 @@ def fetch_tfc(route, filters=None):
sys.exit(1)
return response.json()

def fetch_organizations(page_number=1):
return fetch_tfc("organizations", [('page[size]', '100'), ('page[number]', page_number)])

def fetch_workspaces(org_name, page_number=1):
return fetch_tfc(f"organizations/{org_name}/workspaces", [('page[size]', '100'), ('page[number]', page_number)])

total_runs = 0
organizations = fetch_tfc("organizations")
for organization in organizations["data"]:
name = organization["attributes"]["name"]
workspaces = fetch_tfc(f"organizations/{name}/workspaces")
for workspace in workspaces["data"]:
ws_name = workspace["attributes"]["name"]
runs = fetch_tfc(f"workspaces/{workspace['id']}/runs")
ws_total = runs["meta"]["status-counts"]["total"]
print(f"Workspace {ws_name} has had {ws_total} runs.")
total_runs += ws_total
organizations_token = 1

while organizations_token:
organizations = fetch_organizations(organizations_token)
organizations_token = organizations["meta"]["pagination"]["next-page"]

for organization in organizations["data"]:
name = organization["attributes"]["name"]
workspaces_token = 1
while workspaces_token:
workspaces = fetch_workspaces(name, workspaces_token)
workspaces_token = workspaces["meta"]["pagination"]["next-page"]
for workspace in workspaces["data"]:
ws_name = workspace["attributes"]["name"]
ws_total = fetch_tfc(f"workspaces/{workspace['id']}/runs")["meta"]["status-counts"]["total"]
print(f"Workspace {name}/{ws_name} has had {ws_total} runs.")
total_runs += ws_total
print("---------------------------")
print(f"The total runs count across all organizations: {total_runs}")
sys.exit(0)
Expand Down

0 comments on commit 92138a2

Please sign in to comment.