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

Upgrade all the things #109

Merged
merged 3 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.18
go-version: 1.23
-
name: Run checks
run: make ci
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.18
go-version: 1.23
- name: Import GPG key
id: import_gpg
uses: crazy-max/ghaction-import-gpg@v5.0.0
Expand Down
14 changes: 14 additions & 0 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,24 @@ build: ## Builds the app
generate: ## Generates the docs
go generate

.PHONY: fmt
fmt: ## Formats the codebase. If this doesn't work, run `tools` first
#!/usr/bin/env bash
goimports -w .
gofumpt -w .
golines --base-formatter '' -w .

.PHONY: tools
tools: ## Formats the codebase. If this doesn't work, run `tools` first
go install golang.org/x/tools/cmd/goimports@v0.1.11
go install github.com/segmentio/golines@v0.12.2
go install mvdan.cc/gofumpt@v0.6.0

.PHONY: ci
ci: ## Performs the same checks as ci
go build
go generate
go fmt
git diff --exit-code || (echo 'missing commits - were generated docs checked in?' && exit 1)

.PHONY: install
Expand Down
4 changes: 1 addition & 3 deletions client/access_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@ func (a accessToken) IsValid() (jwt.Token, bool) {

token, err := jwt.ParseString(string(a),
jwt.WithVerify(jwa.RS256, rsaPublicKey))

// Invalid signature
if err != nil {
return token, false
}

err = jwt.Verify(token, jwt.WithAcceptableSkew(30*time.Second))

err = jwt.Validate(token, jwt.WithAcceptableSkew(30*time.Second))
// Token has expired
if err != nil {
return token, false
Expand Down
7 changes: 4 additions & 3 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package client
import (
"errors"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"net/http"
"net/url"
"os"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"

"github.com/hashicorp/go-cleanhttp"
)

Expand All @@ -27,7 +28,7 @@ func (config *Config) validate() error {

if _, err := os.Stat(config.TokenStore); err != nil {
if os.IsNotExist(err) {
err := os.MkdirAll(config.TokenStore, 0700)
err := os.MkdirAll(config.TokenStore, 0o700)
if err != nil {
return fmt.Errorf("cannot create path %q: %w", config.TokenStore, err)
}
Expand Down Expand Up @@ -95,7 +96,7 @@ func New(opts *Config) (*Client, error) {
func (c *Client) addAuthorizationHeader(req *http.Request) diag.Diagnostics {
token, err := c.accessToken(false)
if err != nil {
return diag.Errorf("error obtaining access token: %w", err)
return diag.FromErr(fmt.Errorf("error obtaining access token: %w", err))
}

req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token.AccessToken))
Expand Down
31 changes: 24 additions & 7 deletions client/cluster_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"path"

Expand Down Expand Up @@ -31,18 +32,34 @@ type CreateManagedClusterResponse struct {
ClusterID string `json:"id"`
}

func (c *Client) ManagedClusterCreate(ctx context.Context, req *CreateManagedClusterRequest) (*CreateManagedClusterResponse, diag.Diagnostics) {
func (c *Client) ManagedClusterCreate(
ctx context.Context,
req *CreateManagedClusterRequest,
) (*CreateManagedClusterResponse, diag.Diagnostics) {
requestBody, err := json.Marshal(req)
if err != nil {
return nil, diag.Errorf("error marshalling request: %w", err)
return nil, diag.FromErr(fmt.Errorf("error marshalling request: %w", err))
}

requestURL := *c.apiURL
requestURL.Path = path.Join("mesdb", "v1", "organizations", req.OrganizationID, "projects", req.ProjectID, "clusters")
requestURL.Path = path.Join(
"mesdb",
"v1",
"organizations",
req.OrganizationID,
"projects",
req.ProjectID,
"clusters",
)

request, err := http.NewRequestWithContext(ctx, http.MethodPost, requestURL.String(), bytes.NewReader(requestBody))
request, err := http.NewRequestWithContext(
ctx,
http.MethodPost,
requestURL.String(),
bytes.NewReader(requestBody),
)
if err != nil {
return nil, diag.Errorf("error constructing request: %w", err)
return nil, diag.FromErr(fmt.Errorf("error constructing request: %w", err))
}
request.Header.Add("Content-Type", "application/json")
if err := c.addAuthorizationHeader(request); err != nil {
Expand All @@ -51,7 +68,7 @@ func (c *Client) ManagedClusterCreate(ctx context.Context, req *CreateManagedClu

resp, err := c.httpClient.Do(request)
if err != nil {
return nil, diag.Errorf("error sending request: %w", err)
return nil, diag.FromErr(fmt.Errorf("error sending request: %w", err))
}
defer closeIgnoreError(resp.Body)

Expand All @@ -62,7 +79,7 @@ func (c *Client) ManagedClusterCreate(ctx context.Context, req *CreateManagedClu
decoder := json.NewDecoder(resp.Body)
result := CreateManagedClusterResponse{}
if err := decoder.Decode(&result); err != nil {
return nil, diag.Errorf("error parsing response: %w", err)
return nil, diag.FromErr(fmt.Errorf("error parsing response: %w", err))
}

return &result, nil
Expand Down
24 changes: 19 additions & 5 deletions client/cluster_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package client

import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"fmt"
"net/http"
"path"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
)

type DeleteManagedClusterRequest struct {
Expand All @@ -13,21 +15,33 @@ type DeleteManagedClusterRequest struct {
ClusterID string
}

func (c *Client) ManagedClusterDelete(ctx context.Context, req *DeleteManagedClusterRequest) diag.Diagnostics {
func (c *Client) ManagedClusterDelete(
ctx context.Context,
req *DeleteManagedClusterRequest,
) diag.Diagnostics {
requestURL := *c.apiURL
requestURL.Path = path.Join("mesdb", "v1", "organizations", req.OrganizationID, "projects", req.ProjectID, "clusters", req.ClusterID)
requestURL.Path = path.Join(
"mesdb",
"v1",
"organizations",
req.OrganizationID,
"projects",
req.ProjectID,
"clusters",
req.ClusterID,
)

request, err := http.NewRequestWithContext(ctx, http.MethodDelete, requestURL.String(), nil)
if err != nil {
return diag.Errorf("error constructing request: %w", err)
return diag.FromErr(fmt.Errorf("error constructing request: %w", err))
}
if err := c.addAuthorizationHeader(request); err != nil {
return err
}

resp, err := c.httpClient.Do(request)
if err != nil {
return diag.Errorf("error sending request: %w", err)
return diag.FromErr(fmt.Errorf("error sending request: %w", err))
}
defer closeIgnoreError(resp.Body)

Expand Down
23 changes: 18 additions & 5 deletions client/cluster_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package client
import (
"context"
"encoding/json"
"fmt"
"net/http"
"path"

Expand Down Expand Up @@ -41,21 +42,33 @@ type GetManagedClusterResponse struct {
ManagedCluster ManagedCluster `json:"cluster"`
}

func (c *Client) ManagedClusterGet(ctx context.Context, req *GetManagedClusterRequest) (*GetManagedClusterResponse, diag.Diagnostics) {
func (c *Client) ManagedClusterGet(
ctx context.Context,
req *GetManagedClusterRequest,
) (*GetManagedClusterResponse, diag.Diagnostics) {
requestURL := *c.apiURL
requestURL.Path = path.Join("mesdb", "v1", "organizations", req.OrganizationID, "projects", req.ProjectID, "clusters", req.ClusterID)
requestURL.Path = path.Join(
"mesdb",
"v1",
"organizations",
req.OrganizationID,
"projects",
req.ProjectID,
"clusters",
req.ClusterID,
)

request, err := http.NewRequestWithContext(ctx, http.MethodGet, requestURL.String(), nil)
if err != nil {
return nil, diag.Errorf("error constructing request: %w", err)
return nil, diag.FromErr(fmt.Errorf("error constructing request: %w", err))
}
if err := c.addAuthorizationHeader(request); err != nil {
return nil, err
}

resp, err := c.httpClient.Do(request)
if err != nil {
return nil, diag.Errorf("error sending request: %w", err)
return nil, diag.FromErr(fmt.Errorf("error sending request: %w", err))
}
defer closeIgnoreError(resp.Body)

Expand All @@ -66,7 +79,7 @@ func (c *Client) ManagedClusterGet(ctx context.Context, req *GetManagedClusterRe
decoder := json.NewDecoder(resp.Body)
result := GetManagedClusterResponse{}
if err := decoder.Decode(&result); err != nil {
return nil, diag.Errorf("error parsing response: %w", err)
return nil, diag.FromErr(fmt.Errorf("error parsing response: %w", err))
}

return &result, nil
Expand Down
Loading
Loading