Skip to content

Commit

Permalink
Merge pull request #8 from suborbital/jagger/exec-token
Browse files Browse the repository at this point in the history
Add execution token support
  • Loading branch information
Jagger De Leo authored Nov 19, 2021
2 parents 2e4c044 + baf2cc3 commit ada789d
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 30 deletions.
12 changes: 7 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,27 @@ import (

// Client is used for interacting with the Suborbital Compute API
type Client struct {
config *Config
config *Config
envToken string
}

// NewClient creates a Client with a Config
func NewClient(config *Config) (*Client, error) {
func NewClient(config *Config, envToken string) (*Client, error) {
if config == nil {
return nil, errors.New("failed to NewClient: config cannot be nil")
}

client := &Client{
config: config,
config: config,
envToken: envToken,
}

return client, nil
}

// NewLocalClient quickly sets up a Client with a LocalConfig. Useful for testing.
func NewLocalClient() (*Client, error) {
return NewClient(LocalConfig())
func NewLocalClient(envToken string) (*Client, error) {
return NewClient(LocalConfig(), envToken)
}

func (c *Client) adminRequestBuilder(method string, endpoint string, body io.Reader) (*http.Request, error) {
Expand Down
18 changes: 13 additions & 5 deletions compute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package compute_test

import (
"encoding/base64"
"log"
"os"
"testing"

Expand All @@ -10,8 +11,15 @@ import (
)

var userID = ""
var envToken = ""

func TestMain(m *testing.M) {
tok, exists := os.LookupEnv("SCC_ENV_TOKEN")
if !exists {
log.Fatal("SCC_ENV_TOKEN must be set to run tests!")
}
envToken = tok

// creates a new user for every test run
uuid := uuid.New()
raw, _ := uuid.MarshalBinary()
Expand All @@ -30,7 +38,7 @@ func TestUserID(t *testing.T) {

// TestBuilder must run before tests that depend on Runnables existing in SCN
func TestBuilder(t *testing.T) {
client, err := compute.NewLocalClient()
client, err := compute.NewLocalClient(envToken)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -84,7 +92,7 @@ func TestBuilder(t *testing.T) {
func TestUserFunctions(t *testing.T) {
t.Parallel()

client, err := compute.NewLocalClient()
client, err := compute.NewLocalClient(envToken)
if err != nil {
t.Fatal(err)
}
Expand All @@ -102,7 +110,7 @@ func TestUserFunctions(t *testing.T) {
func TestGetAndExec(t *testing.T) {
t.Parallel()

client, err := compute.NewLocalClient()
client, err := compute.NewLocalClient(envToken)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -155,7 +163,7 @@ func TestGetAndExec(t *testing.T) {
func TestBuilderHealth(t *testing.T) {
t.Parallel()

client, err := compute.NewLocalClient()
client, err := compute.NewLocalClient(envToken)
if err != nil {
t.Fatal(err)
}
Expand All @@ -173,7 +181,7 @@ func TestBuilderHealth(t *testing.T) {
func TestBuilderFeatures(t *testing.T) {
t.Parallel()

client, err := compute.NewLocalClient()
client, err := compute.NewLocalClient(envToken)
if err != nil {
t.Fatal(err)
}
Expand Down
50 changes: 32 additions & 18 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,51 @@ type Config struct {
builderURL *url.URL
}

// DefaultConfig takes the given host and creates a Compute config with the default ports.
// Everything except the scheme and hostname are considered.
func DefaultConfig(host string) (*Config, error) {
u, err := url.Parse(host)
// DefaultConfig takes the given host and creates a Compute config with the K8S default ports.
// Everything except the scheme and hostname are considered. You need to provide your builder
// host domain.
func DefaultConfig(builderHost string) (*Config, error) {
execUrl, err := url.Parse("http://scc-atmo-service.suborbital.svc.cluster.local:80")
if err != nil {
return nil, err
}

hostname := u.Hostname()
scheme := u.Scheme
adminUrl, err := url.Parse("http://scc-controlplane-service.suborbital.svc.cluster.local:8081")
if err != nil {
return nil, err
}

builderUrl, err := url.Parse(builderHost)
if err != nil {
return nil, err
}
builderUrl.Scheme = "https"

conf := &Config{
executionURL: execUrl,
adminURL: adminUrl,
builderURL: builderUrl,
}

return conf, nil
}

// LocalConfig generates a Configuration for Compute running in docker-compose
func LocalConfig() *Config {
conf := &Config{
executionURL: &url.URL{
Scheme: scheme,
Host: hostname + ":8080",
Scheme: "http",
Host: "local.suborbital.network:8080",
},
adminURL: &url.URL{
Scheme: scheme,
Host: hostname + ":8081",
Scheme: "http",
Host: "local.suborbital.network:8081",
},
builderURL: &url.URL{
Scheme: scheme,
Host: hostname + ":8082",
Scheme: "http",
Host: "local.suborbital.network:8082",
},
}

return conf, nil
}

// LocalConfig generates a DefaultConfig() for localhost
func LocalConfig() *Config {
conf, _ := DefaultConfig("http://local.suborbital.network")
return conf
}
5 changes: 4 additions & 1 deletion examples/basic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ package main

import (
"log"
"os"

"github.com/suborbital/compute-go"
)

// a basic example without much error handling
func main() {
token, _ := os.LookupEnv("SCC_ENV_TOKEN")

conf, _ := compute.DefaultConfig("http://localhost") // use your own base URL here
client, _ := compute.NewClient(conf)
client, _ := compute.NewClient(conf, token)

// create a runnable that can be passed into compute.Client
helloRunnable := compute.NewRunnable("com.suborbital", "acmeco", "default", "hello-world", "assemblyscript")
Expand Down
8 changes: 7 additions & 1 deletion examples/getrunnables/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ package main

import (
"log"
"os"

"github.com/suborbital/compute-go"
)

func main() {
client, err := compute.NewLocalClient()
token, exists := os.LookupEnv("SCC_ENV_TOKEN")
if !exists {
log.Fatal("SCC_ENV_TOKEN environment variable not set")
}

client, err := compute.NewLocalClient(token)
if err != nil {
log.Fatal(err)
}
Expand Down
1 change: 1 addition & 0 deletions execution_requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func (c *Client) Exec(runnable *directive.Runnable, body io.Reader) ([]byte, err
}

req, err := c.execRequestBuilder(http.MethodPost, runnable.FQFNURI, body)
req.Header.Set("Authorization", "Bearer "+c.envToken)

if err != nil {
return nil, err
Expand Down

0 comments on commit ada789d

Please sign in to comment.