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

v2: added support for DevicesResource #90

Merged
merged 1 commit into from
Aug 2, 2024
Merged

Conversation

oxtoacart
Copy link
Contributor

@oxtoacart oxtoacart commented Aug 1, 2024

Updates tailscale/corp#21867

Depends on #89

v2/devices.go Show resolved Hide resolved
v2/client.go Show resolved Hide resolved
for _, pathElement := range pathElements {
elem = append(elem, fmt.Sprint(pathElement))
}
return c.BaseURL.JoinPath(elem...)
Copy link
Member

Choose a reason for hiding this comment

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

Does JoinPath handle URL encoding / escaping the strings under the hood? e.g, if we end up with an /tailnet/mario@example.com/ URL does the @ get properly escaped?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that was my reason for using it.

v2/devices.go Outdated

// List lists the devices in a tailnet.
func (dr *DevicesResource) List(ctx context.Context) ([]Device, error) {
req, err := dr.buildRequest(ctx, http.MethodGet, dr.buildURL("api", "v2", "tailnet", dr.Tailnet, "devices"))
Copy link
Member

Choose a reason for hiding this comment

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

Nit: could maybe have a buildV2URL helper or something similar that prefixes with /api/v2?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh yeah, I can just make that part of buildURL, as I suspect all of our URLs will be api/v2

v2/devices.go Outdated
return nil, err
}

return &device, nil
Copy link
Member

Choose a reason for hiding this comment

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

Have also seen us do something more similar to the following format:

var device Device
return &device, dr.performRequest(req, &device)

Not sure if we have a preference between the two.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, we can do even better, hang on ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How do you like it now?

assert.Equal(t, server.ResponseBody, routes)
}

func TestClient_AuthorizeDevice(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

Think we can likely drop this test since it looks like we've removed AuthorizeDevice?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Base automatically changed from percy/add_v2_skeleton to main August 2, 2024 20:27
@oxtoacart oxtoacart force-pushed the percy/v2_devices branch 5 times, most recently from 554f9f4 to cd64779 Compare August 2, 2024 21:12
v2/client.go Outdated
@@ -224,12 +236,13 @@ func (c *Client) performRequest(req *http.Request, out interface{}) error {
}
}

return json.Unmarshal(body, out)
err = json.Unmarshal(body, out)
return err
Copy link
Member

Choose a reason for hiding this comment

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

Nit:

Suggested change
return err
return json.Unmarshal(body, out)

}

if res.StatusCode != http.StatusOK && res.StatusCode != http.StatusCreated {
var apiErr APIError
if err = json.Unmarshal(body, &apiErr); err != nil {
if err := json.Unmarshal(body, &apiErr); err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

Nit x 2 if the above is accepted to bring it back to how it was:

Suggested change
if err := json.Unmarshal(body, &apiErr); err != nil {
if err = json.Unmarshal(body, &apiErr); err != nil {

Updates tailscale/corp#21867

Signed-off-by: Percy Wegmann <percy@tailscale.com>
@oxtoacart oxtoacart merged commit 7518e66 into main Aug 2, 2024
2 checks passed
@oxtoacart oxtoacart deleted the percy/v2_devices branch August 2, 2024 21:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants