Skip to content

Commit 7b16e86

Browse files
Feat/rum (#153)
Adds resource count to workspace command output.
1 parent 6af6659 commit 7b16e86

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

cmd/admin_metrics.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func getAllMetrics() (*MetricsAll, error) {
182182

183183
func getAllOrganizationWorkspaces(ctx context.Context, client *tfe.Client, orgName string) (*MetricsWorkspaces, error) {
184184
result := &MetricsWorkspaces{}
185-
workspaces, err := workspaceListAllForOrganization(getTfxClientContext(), orgName, "")
185+
workspaces, err := workspaceListAllForOrganization(getTfxClientContext(), orgName, "", "")
186186
if err != nil {
187187
return nil, err
188188
}
@@ -247,7 +247,7 @@ func getAllMetricsWorkspace(orgName string, since time.Time) (*MetricsWorkspaceR
247247

248248
func getOrganizationMetricWorkspaces(ctx context.Context, client *tfe.Client, orgName string, runSinceTime time.Time) (*[]MetricsWorkspace, error) {
249249
result := []MetricsWorkspace{}
250-
workspaces, err := workspaceListAllForOrganization(getTfxClientContext(), orgName, "")
250+
workspaces, err := workspaceListAllForOrganization(getTfxClientContext(), orgName, "", "")
251251
if err != nil {
252252
return nil, err
253253
}

cmd/workspace.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,17 @@ var (
5454
getTfxClientContext(),
5555
*viperString("search"),
5656
*viperString("repository"),
57-
*viperString("run-status"))
57+
*viperString("run-status"),
58+
*viperString("project-id"),
59+
)
5860
} else {
5961
return workspaceList(
6062
getTfxClientContext(),
6163
*viperString("search"),
6264
*viperString("repository"),
63-
*viperString("run-status"))
65+
*viperString("run-status"),
66+
*viperString("project-id"),
67+
)
6468
}
6569
},
6670
}
@@ -83,6 +87,8 @@ func init() {
8387
workspaceListCmd.Flags().StringP("search", "s", "", "Search string for Workspace Name (optional).")
8488
workspaceListCmd.Flags().StringP("repository", "r", "", "Filter on Repository Identifier (i.e. username/repo_name) (optional).")
8589
workspaceListCmd.Flags().String("run-status", "", "Filter on current run status (optional).")
90+
workspaceListCmd.Flags().String("project-id", "", "Filter on project ID (optional).")
91+
8692
workspaceListCmd.Flags().BoolP("all", "a", false, "List All Organizations Workspaces (optional).")
8793

8894
// `tfx workspace show`
@@ -94,14 +100,15 @@ func init() {
94100
workspaceCmd.AddCommand(workspaceShowCmd)
95101
}
96102

97-
func workspaceListAllForOrganization(c TfxClientContext, orgName string, searchString string) ([]*tfe.Workspace, error) {
103+
func workspaceListAllForOrganization(c TfxClientContext, orgName string, searchString string, projectID string) ([]*tfe.Workspace, error) {
98104
allItems := []*tfe.Workspace{}
99105
opts := tfe.WorkspaceListOptions{
100106
ListOptions: tfe.ListOptions{PageNumber: 1, PageSize: 100},
101107
Search: searchString,
102108
// Tags: "",
103109
// ExcludeTags: "",
104-
Include: []tfe.WSIncludeOpt{"organization", "current_run"},
110+
ProjectID: projectID,
111+
Include: []tfe.WSIncludeOpt{"organization", "current_run"},
105112
}
106113
for {
107114
items, err := c.Client.Workspaces.List(c.Context, orgName, &opts)
@@ -163,9 +170,9 @@ func workspaceListAllRemoteStateConsumers(c TfxClientContext, workspaceId string
163170
return allItems, nil
164171
}
165172

166-
func workspaceList(c TfxClientContext, searchString string, repoIdentifier string, runStatus string) error {
173+
func workspaceList(c TfxClientContext, searchString string, repoIdentifier string, runStatus string, projectID string) error {
167174
o.AddMessageUserProvided("List Workspaces for Organization:", c.OrganizationName)
168-
items, err := workspaceListAllForOrganization(c, c.OrganizationName, searchString)
175+
items, err := workspaceListAllForOrganization(c, c.OrganizationName, searchString, projectID)
169176
if err != nil {
170177
return errors.Wrap(err, "failed to list workspaces")
171178
}
@@ -180,7 +187,7 @@ func workspaceList(c TfxClientContext, searchString string, repoIdentifier strin
180187
o.AddFormattedMessageCalculated("Found %d Filtered Workspaces", len(items))
181188
}
182189

183-
o.AddTableHeader("Name", "Id", "Current Run Created", "Status", "Repository", "Locked")
190+
o.AddTableHeader("Name", "Id", "Resource Count", "Current Run Created", "Status", "Repository", "Locked")
184191
for _, i := range items {
185192
cr_created_at := ""
186193
cr_status := ""
@@ -193,13 +200,13 @@ func workspaceList(c TfxClientContext, searchString string, repoIdentifier strin
193200
ws_repo = i.VCSRepo.DisplayIdentifier
194201
}
195202

196-
o.AddTableRows(i.Name, i.ID, cr_created_at, cr_status, ws_repo, i.Locked)
203+
o.AddTableRows(i.Name, i.ID, i.ResourceCount, cr_created_at, cr_status, ws_repo, i.Locked)
197204
}
198205

199206
return nil
200207
}
201208

202-
func workspaceListAll(c TfxClientContext, searchString string, repoIdentifier string, runStatus string) error {
209+
func workspaceListAll(c TfxClientContext, searchString string, repoIdentifier string, runStatus string, projectID string) error {
203210
o.AddMessageUserProvided("List Workspaces for all available Organizations", "")
204211
orgs, err := organizationListAll(c)
205212
if err != nil {
@@ -208,7 +215,7 @@ func workspaceListAll(c TfxClientContext, searchString string, repoIdentifier st
208215

209216
var allWorkspaceList []*tfe.Workspace
210217
for _, v := range orgs {
211-
workspaceList, err := workspaceListAllForOrganization(c, v.Name, searchString)
218+
workspaceList, err := workspaceListAllForOrganization(c, v.Name, searchString, projectID)
212219
if err != nil {
213220
logError(err, "failed to list workspaces for organization")
214221
}
@@ -229,7 +236,7 @@ func workspaceListAll(c TfxClientContext, searchString string, repoIdentifier st
229236
o.AddFormattedMessageCalculated("Found %d Filtered Workspaces", len(allWorkspaceList))
230237
}
231238

232-
o.AddTableHeader("Organization", "Name", "Id", "Current Run Created", "Status", "Repository", "Locked")
239+
o.AddTableHeader("Organization", "Name", "Id", "Resource Count", "Current Run Created", "Status", "Repository", "Locked")
233240
for _, i := range allWorkspaceList {
234241
cr_created_at := ""
235242
cr_status := ""
@@ -242,7 +249,7 @@ func workspaceListAll(c TfxClientContext, searchString string, repoIdentifier st
242249
ws_repo = i.VCSRepo.DisplayIdentifier
243250
}
244251

245-
o.AddTableRows(i.Organization.Name, i.Name, i.ID, cr_created_at, cr_status, ws_repo, i.Locked)
252+
o.AddTableRows(i.Organization.Name, i.Name, i.ID, i.ResourceCount, cr_created_at, cr_status, ws_repo, i.Locked)
246253
}
247254

248255
return nil
@@ -310,6 +317,7 @@ func workspaceShow(c TfxClientContext, workspaceName string) error {
310317
}
311318

312319
o.AddDeferredMessageRead("ID", w.ID)
320+
o.AddDeferredMessageRead("Resource Count", w.ResourceCount)
313321
o.AddDeferredMessageRead("Terraform Version", w.TerraformVersion)
314322
o.AddDeferredMessageRead("Execution Mode", w.ExecutionMode)
315323
o.AddDeferredMessageRead("Auto Apply", w.AutoApply)

cmd/workspace_lock.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ var (
4747
RunE: func(cmd *cobra.Command, args []string) error {
4848
return workspaceLockAll(
4949
getTfxClientContext(),
50-
*viperString("search"))
50+
*viperString("search"),
51+
)
5152
},
5253
}
5354

@@ -71,7 +72,8 @@ var (
7172
RunE: func(cmd *cobra.Command, args []string) error {
7273
return workspaceUnlockAll(
7374
getTfxClientContext(),
74-
*viperString("search"))
75+
*viperString("search"),
76+
)
7577
},
7678
}
7779
)
@@ -111,7 +113,7 @@ func workspaceLock(c TfxClientContext, workspaceName string) error {
111113

112114
func workspaceLockAll(c TfxClientContext, searchString string) error {
113115
o.AddMessageUserProvided("Lock All Workspace in Organization:", c.OrganizationName)
114-
workspaceList, err := workspaceListAllForOrganization(c, c.OrganizationName, searchString)
116+
workspaceList, err := workspaceListAllForOrganization(c, c.OrganizationName, searchString, "")
115117
if err != nil {
116118
return errors.Wrap(err, "failed to list workspaces")
117119
}
@@ -145,7 +147,7 @@ func workspaceUnlock(c TfxClientContext, workspaceName string) error {
145147

146148
func workspaceUnlockAll(c TfxClientContext, searchString string) error {
147149
o.AddMessageUserProvided("Unlock All Workspace in Organization:", c.OrganizationName)
148-
workspaceList, err := workspaceListAllForOrganization(c, c.OrganizationName, searchString)
150+
workspaceList, err := workspaceListAllForOrganization(c, c.OrganizationName, searchString, "")
149151
if err != nil {
150152
return errors.Wrap(err, "failed to list workspaces")
151153
}

0 commit comments

Comments
 (0)