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

tools/data-api-sdk: scaffolding out the Data API SDK #3761

Merged
merged 1 commit into from
Feb 8, 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
1 change: 1 addition & 0 deletions tools/data-api-sdk/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor/
27 changes: 27 additions & 0 deletions tools/data-api-sdk/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
## `./tools/data-api-sdk`

This package provides a Go SDK (and thus common models) which allows querying the Data API.

### Example Usage

```go
package main

import (
"context"
"log"

"github.com/hashicorp/pandora/tools/data-api-sdk/v1"
"github.com/hashicorp/pandora/tools/data-api-sdk/v1/models"
)

func main() {
ctx := context.TODO()
client := v1.NewClient("http://localhost:8888", models.ResourceManagerSourceDataType)
resp, err := client.Health(ctx)
if err != nil {
log.Fatalf("%+v", err)
}
log.Printf("Data API is available: %t", resp.Available)
}
```
7 changes: 7 additions & 0 deletions tools/data-api-sdk/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module github.com/hashicorp/pandora/tools/data-api-sdk

go 1.21

require github.com/hashicorp/go-retryablehttp v0.7.5

require github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
9 changes: 9 additions & 0 deletions tools/data-api-sdk/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-hclog v0.9.2 h1:CG6TE5H9/JXsFWJCfoIVpKFIkFe6ysEuHirp4DxCsHI=
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
github.com/hashicorp/go-retryablehttp v0.7.5 h1:bJj+Pj19UZMIweq/iie+1u5YCdGrnxCT9yvm0e+Nd5M=
github.com/hashicorp/go-retryablehttp v0.7.5/go.mod h1:Jy/gPYAdjqffZ/yFGCFV2doI5wjtH1ewM9u8iYVjtX8=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
13 changes: 13 additions & 0 deletions tools/data-api-sdk/v1/available_source_data_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package v1

import "github.com/hashicorp/pandora/tools/data-api-sdk/v1/models"

// AvailableSourceDataTypes returns a list of the supported Source Data Types.
// This can be used to allow tooling to automatically support new Source Data
// Types as these are defined here, for example by using this information in CLIs.
func AvailableSourceDataTypes() []models.SourceDataType {
return []models.SourceDataType{
models.MicrosoftGraphSourceDataType,
models.ResourceManagerSourceDataType,
}
}
30 changes: 30 additions & 0 deletions tools/data-api-sdk/v1/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package v1

import (
"net/http"

"github.com/hashicorp/pandora/tools/data-api-sdk/v1/models"
)

type Client struct {
// client is the HTTP Client used for HTTP requests
client *http.Client

// endpoint specifies the base endpoint for the Data API.
// Typically this will be `http://localhost:8080` but can vary.
endpoint string

// sourceDataType specifies the Data Source Type being queried.
sourceDataType models.SourceDataType
}

// NewClient returns an instance of Client configured for the current endpoint
// and sourceDataType combination - used to retrieve information from the Data API.
func NewClient(endpoint string, sourceDataType models.SourceDataType) *Client {
return &Client{
// NOTE: this is retryable to account for tooling interfering with connections
client: retryablehttp.NewClient().StandardClient(),
endpoint: endpoint,
sourceDataType: sourceDataType,
}
}
35 changes: 35 additions & 0 deletions tools/data-api-sdk/v1/health.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package v1

import (
"context"
"fmt"
"net/http"
)

type HealthResponse struct {
// Available specifies whether the Data API is available or not.
Available bool

// HttpResponse is the raw HTTP Response
HttpResponse *http.Response
}

// Health checks the current status of the Data API, returning whether it's ready
// to accept requests or not.
func (c *Client) Health(ctx context.Context) (*HealthResponse, error) {
uri := fmt.Sprintf("%s/v1/health", c.endpoint)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, fmt.Errorf("checking the health endpoint: %+v", err)
}

resp, err := c.client.Do(req)
if err != nil {
return nil, fmt.Errorf("performing request: %+v", err)
}

return &HealthResponse{
Available: resp.StatusCode == http.StatusOK,
HttpResponse: resp,
}, nil
}
12 changes: 12 additions & 0 deletions tools/data-api-sdk/v1/models/source_data_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package models

// SourceDataType defines a type of Source Data.
type SourceDataType string

const (
// MicrosoftGraphSourceDataType defines that this Data is related to Microsoft Graph.
MicrosoftGraphSourceDataType SourceDataType = "microsoft-graph"

// ResourceManagerSourceDataType defines that this Data is related to Azure Resource Manager.
ResourceManagerSourceDataType SourceDataType = "resource-manager"
)