Skip to content

Commit

Permalink
chore: use SDK to manage workspace API keys (#366)
Browse files Browse the repository at this point in the history
  • Loading branch information
zepatrik authored Jul 16, 2024
1 parent 6ba63d2 commit ed90e25
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 58 deletions.
67 changes: 12 additions & 55 deletions cmd/cloudx/client/api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@ package client

import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"

cloud "github.com/ory/client-go"
"github.com/ory/x/cmdx"
Expand All @@ -24,8 +20,7 @@ func (h *CommandHelper) CreateProjectAPIKey(ctx context.Context, projectID, name

token, res, err := c.ProjectAPI.CreateProjectApiKey(ctx, projectID).CreateProjectApiKeyRequest(cloud.CreateProjectApiKeyRequest{Name: name}).Execute()
if err != nil {
fmt.Printf("res: %+v\nreq: %+v\n", res, res.Request)
return nil, err
return nil, handleError("unable to create project API key", res, err)
}

return token, nil
Expand All @@ -37,72 +32,34 @@ func (h *CommandHelper) DeleteProjectAPIKey(ctx context.Context, projectID, keyI
return err
}

if _, err := c.ProjectAPI.DeleteProjectApiKey(ctx, projectID, keyID).Execute(); err != nil {
return err
if res, err := c.ProjectAPI.DeleteProjectApiKey(ctx, projectID, keyID).Execute(); err != nil {
return handleError("unable to delete project API key", res, err)
}

return nil
}

func (h *CommandHelper) CreateWorkspaceAPIKey(ctx context.Context, workspaceID, name string) (*cloud.WorkspaceApiKey, error) {
// TODO replace with SDK method
baseURL := CloudConsoleURL("api")
c, err := h.newConsoleHTTPClient(ctx)
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(
ctx,
http.MethodPost,
fmt.Sprintf("%s/workspaces/%s/tokens", baseURL.String(), workspaceID),
strings.NewReader(fmt.Sprintf(`{"name":"%s"}`, name)),
)
c, err := h.newConsoleAPIClient(ctx)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
res, err := c.Do(req)

key, res, err := c.WorkspaceAPI.CreateWorkspaceApiKey(ctx, workspaceID).CreateWorkspaceApiKeyBody(cloud.CreateWorkspaceApiKeyBody{Name: name}).Execute()
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusCreated {
resBody, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
return nil, fmt.Errorf("expected status code %d but got %d\n%s", http.StatusCreated, res.StatusCode, resBody)
return nil, handleError("unable to create workspace API key", res, err)
}
key := cloud.WorkspaceApiKey{}
if err := json.NewDecoder(res.Body).Decode(&key); err != nil {
return nil, err
}
return &key, nil
return key, nil
}

func (h *CommandHelper) DeleteWorkspaceAPIKey(ctx context.Context, workspaceID, keyID string) error {
// TODO replace with SDK method
baseURL := CloudConsoleURL("api")
c, err := h.newConsoleHTTPClient(ctx)
if err != nil {
return err
}
req, err := http.NewRequestWithContext(
ctx,
http.MethodDelete,
fmt.Sprintf("%s/workspaces/%s/tokens/%s", baseURL.String(), workspaceID, keyID),
nil,
)
if err != nil {
return err
}
res, err := c.Do(req)
c, err := h.newConsoleAPIClient(ctx)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != http.StatusNoContent {
return fmt.Errorf("expected status code %d but got %d", http.StatusNoContent, res.StatusCode)

if res, err := c.WorkspaceAPI.DeleteWorkspaceApiKey(ctx, workspaceID, keyID).Execute(); err != nil {
return handleError("unable to delete workspace API key", res, err)
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ require (
github.com/hashicorp/go-retryablehttp v0.7.7
github.com/imdario/mergo v0.3.16
github.com/jackc/pgx/v4 v4.18.2
github.com/ory/client-go v1.13.10
github.com/ory/client-go v1.14.0
github.com/ory/gochimp3 v0.0.0-20200417124117-ccd242db3655
github.com/ory/graceful v0.1.4-0.20230301144740-e222150c51d0
github.com/ory/herodot v0.10.3-0.20230807143059-27cd6936499b
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -891,8 +891,8 @@ github.com/openzipkin/zipkin-go v0.4.2 h1:zjqfqHjUpPmB3c1GlCvvgsM1G4LkvqQbBDueDO
github.com/openzipkin/zipkin-go v0.4.2/go.mod h1:ZeVkFjuuBiSy13y8vpSDCjMi9GoI3hPpCJSBx/EYFhY=
github.com/ory/analytics-go/v5 v5.0.1 h1:LX8T5B9FN8KZXOtxgN+R3I4THRRVB6+28IKgKBpXmAM=
github.com/ory/analytics-go/v5 v5.0.1/go.mod h1:lWCiCjAaJkKfgR/BN5DCLMol8BjKS1x+4jxBxff/FF0=
github.com/ory/client-go v1.13.10 h1:ap3RlCjxWPjnclkBJ3htTmtgCEU9Ax3Fm8L6Bdbaozw=
github.com/ory/client-go v1.13.10/go.mod h1:GSRIUSsTcnMnRnj2Dd0fLc1XQ4juvkrA3aQW0gWlqtk=
github.com/ory/client-go v1.14.0 h1:ZDM6MexwlL1X4UH3kOTuFBqmeOJAoQv7s6+sF6fcwwE=
github.com/ory/client-go v1.14.0/go.mod h1:GSRIUSsTcnMnRnj2Dd0fLc1XQ4juvkrA3aQW0gWlqtk=
github.com/ory/dockertest/v3 v3.10.1-0.20240704115616-d229e74b748d h1:By96ZSVuH5LyjXLVVMfvJoLVGHaT96LdOnwgFSLVf0E=
github.com/ory/dockertest/v3 v3.10.1-0.20240704115616-d229e74b748d/go.mod h1:F2FIjwwAk6CsNAs//B8+aPFQF0t84pbM8oliyNXwQrk=
github.com/ory/fosite v0.44.1-0.20231218095112-ac9ae4bd99d7 h1:EZEUk9sdC9cIKSqXipBz4eO84byOLLeVUnptgX7QFvM=
Expand Down

0 comments on commit ed90e25

Please sign in to comment.