Skip to content

Commit

Permalink
chore: delete unused code
Browse files Browse the repository at this point in the history
Signed-off-by: nscuro <nscuro@protonmail.com>
  • Loading branch information
nscuro committed Jun 4, 2023
1 parent e61588d commit 4133aa1
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 65 deletions.
52 changes: 0 additions & 52 deletions internal/opa/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,62 +92,10 @@ func (c Client) Decision(ctx context.Context, decisionPath string, input any, re
return
}

// Health checks whether OPA is operational.
// See: https://www.openpolicyagent.org/docs/latest/rest-api/#health-api
func (c Client) Health(ctx context.Context) (err error) {
healthURL, err := c.baseURL.Parse(path.Join("/health"))
if err != nil {
err = fmt.Errorf("failed to parse request url: %w", err)
return
}

req, err := http.NewRequestWithContext(ctx, http.MethodGet, healthURL.String(), nil)
if err != nil {
err = fmt.Errorf("failed to create request: %w", err)
return
}

req.Header.Set("Accept", "application/json")
req.Header.Set("User-Agent", "dtapac")

res, err := c.httpClient.Do(req)
if err != nil {
err = fmt.Errorf("failed to send request: %w", err)
return
}
defer res.Body.Close()

if res.StatusCode == http.StatusOK {
return
} else if res.StatusCode == http.StatusInternalServerError {
var healthRes healthResponse
err = json.NewDecoder(res.Body).Decode(&healthRes)
if err != nil {
err = fmt.Errorf("failed to decode response: %w", err)
return
}

if healthRes.Error == "" {
err = fmt.Errorf("response status is %d, but no error field was returned", http.StatusInternalServerError)
return
}

err = errors.New(healthRes.Error)
return
}

err = fmt.Errorf("unexpected response code: %d", res.StatusCode)
return
}

type decisionRequest struct {
Input any `json:"input"`
}

type decisionResponse struct {
Result *json.RawMessage `json:"result"`
}

type healthResponse struct {
Error string `json:"error"`
}
17 changes: 4 additions & 13 deletions internal/opa/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"bytes"
"context"
"fmt"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
"net/http"
"os"
"testing"

"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)

func TestClient_Decision(t *testing.T) {
Expand Down Expand Up @@ -46,16 +47,6 @@ func TestClient_Decision(t *testing.T) {
})
}

func TestClient_Health(t *testing.T) {
opaURL := setupOPA(t)

client, err := NewClient(opaURL)
require.NoError(t, err)

err = client.Health(context.TODO())
require.NoError(t, err)
}

func setupOPA(t *testing.T) string {
req := testcontainers.ContainerRequest{
Image: "openpolicyagent/opa:0.40.0-rootless",
Expand Down

0 comments on commit 4133aa1

Please sign in to comment.