Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new workspace fields #192

Merged
merged 5 commits into from
Mar 23, 2021
Merged

Add new workspace fields #192

merged 5 commits into from
Mar 23, 2021

Conversation

mwhooker
Copy link
Contributor

@mwhooker mwhooker commented Mar 11, 2021

Description

Adds support for new fields in the workspace object

Example Output

(*tfe.Workspace)(0xc00016a000)({
...,
 UpdatedAt: (time.Time) 2020-12-16 18:05:40.569 +0000 UTC,
 ResourceCount: (int) 2,
 ApplyDurationAverage: (time.Duration) 41s,
 PlanDurationAverage: (time.Duration) 25s,
 PolicyCheckFailures: (int) 0,
 RunFailures: (int) 1,
 RunsCount: (int) 6,
})

// spew.Dump of workspaces.Readme:

([]uint8) (len=11 cap=1536) {
 00000000  23 20 74 66 74 65 73 74  0a 0a 31                 |# tftest..1|
}

Testing plan

Added a unit test, but it won't work because this feature is not generally available yet. It should pass once it is.

The fields have been tested manually against an existing workspace with real data.

@mwhooker mwhooker added the enhancement New feature or request label Mar 11, 2021
@mwhooker mwhooker force-pushed the workspace-additions branch from b96b547 to eaf1a09 Compare March 11, 2021 00:09
@mwhooker mwhooker requested a review from omarismail March 11, 2021 00:10
Copy link

@anamahalo anamahalo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pre-emptively ✅ this for GA.

Copy link
Member

@chrisarcand chrisarcand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heyo! I know this isn't quite ready for review yet, but thought I'd drop some notes because I've done a bit more thinking on this given the current API regarding the README and what I think the interface should be here in the best interests of the client.

I believe the Readme() function (which should be in the Workspaces interface, by the way) should make a separate call to fetch the README, and should return an io.Reader. This is consistent with the API elsewhere, like fetching plan/apply logs or a run's JSON formatted execution plan.

Note that the current API being non-streamable isn't the point here - it's about the fact that this interface could be streamed with a separate endpoint in the future and no backwards incompatible change in type here in the client. In other words, although not useful at this immediate moment, returning an io.Reader here doesn't hurt anything and just future proofs it for an API more appropriate for the client. Today, the API call to fulfill this interface is indeed just a nearly-duplicated /workspaces/:id?include=readme call, but it's only out of current circumstance 👍🏻

@mwhooker mwhooker force-pushed the workspace-additions branch 2 times, most recently from 0fbe7bc to 0b8d2e8 Compare March 18, 2021 23:43
@mwhooker
Copy link
Contributor Author

I think I got to your feedback @chrisarcand . LMK

@mwhooker mwhooker requested a review from chrisarcand March 19, 2021 01:18
@mwhooker
Copy link
Contributor Author

mwhooker commented Mar 19, 2021

which should be in the Workspaces interface, by the way

I missed this the first time. Can you elaborate on this? I must have misunderstood you when we spoke about this. The readme doesn't really seem to belong to workspaces, it seems like it belongs to a workspace. In the case where we move the readme out of the workspace type and into say .../workspaces/.../readme, wouldn't it be a link off of workspace?

@chrisarcand
Copy link
Member

Sure thing:

This client is organized as a set of interfaces tied to API calls. The plural name Workspaces doesn't literally refer to just collection calls (/workspaces); it's just the name of the interface in which all Workspace API endpoints are organized. No methods are defined on the struct representing the resource itself, but on the interface to the specific API.

As a simpler example to grok than workspaces, check out plan.go:

go-tfe/plan.go

Lines 19 to 25 in 1de9e44

type Plans interface {
// Read a plan by its ID.
Read(ctx context.Context, planID string) (*Plan, error)
// Logs retrieves the logs of a plan.
Logs(ctx context.Context, planID string) (io.Reader, error)
}

go-tfe/plan.go

Lines 48 to 61 in 1de9e44

// Plan represents a Terraform Enterprise plan.
type Plan struct {
ID string `jsonapi:"primary,plans"`
HasChanges bool `jsonapi:"attr,has-changes"`
LogReadURL string `jsonapi:"attr,log-read-url"`
ResourceAdditions int `jsonapi:"attr,resource-additions"`
ResourceChanges int `jsonapi:"attr,resource-changes"`
ResourceDestructions int `jsonapi:"attr,resource-destructions"`
Status PlanStatus `jsonapi:"attr,status"`
StatusTimestamps *PlanStatusTimestamps `jsonapi:"attr,status-timestamps"`
// Relations
Exports []*PlanExport `jsonapi:"relation,exports"`
}

Note that to retrieve the logs for a plan, is not a method on Plan but a function in the Plans interface.

In your case, this means you should:

  • Take all of the logic of making a call for the readme, unmarshalling it, and returning it in a function Readme() included in the Workspaces interface and method on workspaces.
  • The readme field in Workspace should be removed, along with the readmeReader type (you don't really need it)
  • The logic for immediately fetching the readme in Read() and ReadByID() should be removed.

@mwhooker mwhooker force-pushed the workspace-additions branch 2 times, most recently from f70a92f to 5e86452 Compare March 19, 2021 19:49
@mwhooker mwhooker marked this pull request as ready for review March 19, 2021 20:01
@mwhooker mwhooker requested a review from a team March 19, 2021 20:01
@mwhooker mwhooker force-pushed the workspace-additions branch from a6c39e7 to b4c11cd Compare March 19, 2021 20:02
@mwhooker mwhooker force-pushed the workspace-additions branch from b4c11cd to b0a5cbd Compare March 19, 2021 20:02
Copy link
Contributor

@omarismail omarismail left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@chrisarcand
Copy link
Member

This requires a bit of fiddling with our CI to be green, and I'll get to that today 👍🏻

@chrisarcand chrisarcand merged commit 074a0e4 into master Mar 23, 2021
@chrisarcand chrisarcand deleted the workspace-additions branch March 23, 2021 21:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants