Skip to content

Commit

Permalink
feat: initial commit (#1)
Browse files Browse the repository at this point in the history
* chore: initial commit

* chore: check in generated methods

* update

* chore: push runner sdk
  • Loading branch information
jonmorehouse authored Sep 4, 2024
1 parent 110cdee commit 21a56fd
Show file tree
Hide file tree
Showing 403 changed files with 98,759 additions and 0 deletions.
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2024 Nuon Inc

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
69 changes: 69 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,71 @@
# nuon-runner-go

The SDK that powers the Nuon runner.

## Overview

The Nuon runner is deployed in each install, and powers everything from updates, releases and rollouts to day 2
monitoring.

Full documentation is available at https://runner.nuon.co/docs/index.html.

All endpoints in the API follow REST conventions and standard HTTP methods. You can find the OpenAPI Spec
[here](https://runner.nuon.co/oapi/v3)

## Installation

In your project, you can install the package directly using `go get`:

```bash
go get github.com/nuonco/nuon-runner-go
```

In your code, add the following import:

```
import nuonrunner "github.com/nuonco/nuon-runner-go"
```

## Create a client

Create a new api client, using an API key set in the environment.

```go
apiURL := "https://runner.nuon.co"
apiToken := os.Getenv("NUON_API_TOKEN")
runnerID := os.Getenv("NUON_RUNNER_ID")

apiClient, err := client.New(s.v,
client.WithAuthToken(apiToken),
client.WithURL(apiURL),
client.WithRunnerID(orgID),
)
if err != nil {
return fmt.Errorf("unable to get api client: %w", err)
}
```

## Example usage

### List current available jobs

```go
jobs, err := apiClient.AvailableJobs(ctx)
```

## Contributing

Please submit a PR, and if you would like help, contact us on our [community
slack](https://join.slack.com/t/nuoncommunity/shared_invite/zt-1q323vw9z-C8ztRP~HfWjZx6AXi50VRA).

You can generate mock code using:

```bash
$ go generate ./...
```

You can also change the open api spec to generate against, by setting the `API_URL` field to a different value:

```bash
$ NUON_API_URL=http://localhost:8081 go generate ./...
```
26 changes: 26 additions & 0 deletions app_transport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package nuonrunner

import (
"net/http"
)

// appTransport is a transport that injects our authentication token and org id into the api request
type appTransport struct {
authToken string
orgID string

transport http.RoundTripper
}

func (t *appTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Set("Authorization", "Bearer "+t.authToken)
if t.orgID != "" {
req.Header.Set("X-Nuon-Org-ID", t.orgID)
}

return t.transport.RoundTrip(req)
}

func (c *client) SetOrgID(orgID string) {
c.appTransport.orgID = orgID
}
16 changes: 16 additions & 0 deletions auth_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package nuonrunner

import (
"github.com/go-openapi/runtime"
runtimeclient "github.com/go-openapi/runtime/client"
)

func (c *client) getAuthInfo() runtime.ClientAuthInfoWriter {
return runtimeclient.Compose(
c.getApiKeyAuthInfo(),
)
}

func (c *client) getApiKeyAuthInfo() runtime.ClientAuthInfoWriter {
return runtimeclient.BearerToken(c.APIToken)
}
123 changes: 123 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package nuonrunner

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

httptransport "github.com/go-openapi/runtime/client"
"github.com/go-playground/validator/v10"

genclient "github.com/nuonco/nuon-runner-go/client"

"github.com/nuonco/nuon-runner-go/models"
)

//go:generate ./generate.sh
type Client interface {
SetRunnerID(runnerID string)

GetSettings(ctx context.Context) (*models.AppRunnerGroupSettings, error)

// heartbeat and health checks
CreateHeartBeat(ctx context.Context, req *models.ServiceCreateRunnerHeartBeatRequest) (*models.AppRunnerHeartBeat, error)
CreateHealthCheck(ctx context.Context, req *models.ServiceCreateRunnerHealthCheckRequest) (*models.AppRunnerHealthCheck, error)

// jobs
GetJobs(ctx context.Context, grp models.AppRunnerJobGroup, status models.AppRunnerJobStatus, limit *int64) ([]*models.AppRunnerJob, error)
GetJob(ctx context.Context, jobID string) (*models.AppRunnerJob, error)
GetJobPlan(ctx context.Context, jobID string) (*models.Planv1Plan, error)

// job executions
GetJobExecutions(ctx context.Context, jobID string) ([]*models.AppRunnerJobExecution, error)
CreateJobExecution(ctx context.Context, jobID string, req *models.ServiceCreateRunnerJobExecutionRequest) (*models.AppRunnerJobExecution, error)
UpdateJobExecution(ctx context.Context, jobID, jobExecutionID string, req *models.ServiceUpdateRunnerJobExecutionRequest) (*models.AppRunnerJobExecution, error)
CreateJobExecutionResult(ctx context.Context, jobID, jobExecutionID string, req *models.ServiceCreateRunnerJobExecutionResultRequest) (*models.AppRunnerJobExecutionResult, error)

// otel operations
WriteOTELLogs(ctx context.Context, req interface{}) error
WriteOTELTraces(ctx context.Context, req interface{}) error
WriteOTELMetrics(ctx context.Context, req interface{}) error
}

var _ Client = (*client)(nil)

type client struct {
v *validator.Validate

APIURL string `validate:"required"`
APIToken string
RunnerID string

genClient *genclient.NuonRunnerAPI
appTransport *appTransport
}

type clientOption func(*client) error

func New(opts ...clientOption) (*client, error) {
c := &client{}
for _, opt := range opts {
if err := opt(c); err != nil {
return nil, err
}
}

if c.v == nil {
c.v = validator.New()
}

if err := c.v.Struct(c); err != nil {
return nil, err
}

apiURL, err := url.Parse(c.APIURL)
if err != nil {
return nil, fmt.Errorf("unable to parse api url: %w", err)
}

transport := httptransport.New(apiURL.Host, "", []string{apiURL.Scheme})
appTransport := &appTransport{
authToken: c.APIToken,
transport: http.DefaultTransport,
}
c.appTransport = appTransport
transport.Transport = appTransport
genClient := genclient.New(transport, nil)
c.genClient = genClient

return c, nil
}

// WithAuthToken specifies the auth token to use
func WithAuthToken(token string) clientOption {
return func(c *client) error {
c.APIToken = token
return nil
}
}

// WithURL specifies the url to use
func WithURL(url string) clientOption {
return func(c *client) error {
c.APIURL = url
return nil
}
}

// WithRunnerID specifies the runner id to use
func WithRunnerID(runnerID string) clientOption {
return func(c *client) error {
c.RunnerID = runnerID
return nil
}
}

// WithValidator specifies a validator to use
func WithValidator(v *validator.Validate) clientOption {
return func(c *client) error {
c.v = v
return nil
}
}
1 change: 1 addition & 0 deletions client/deps.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package client
111 changes: 111 additions & 0 deletions client/nuon_runner_api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 21a56fd

Please sign in to comment.