-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 75897ba
Showing
6 changed files
with
764 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Some bits about go. | ||
GOPATH := $(shell go env GOPATH) | ||
GOBIN := $(if $(shell go env GOBIN),$(shell go env GOBIN),$(GOPATH)/bin) | ||
|
||
# Defines the linter version. | ||
LINT_VERSION=v1.57.1 | ||
|
||
# Perform linting. | ||
# This must pass or you will be denied by CI. | ||
.PHOMY: lint | ||
lint: $(GENDIR) | ||
@go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(LINT_VERSION) | ||
$(GOBIN)/golangci-lint run ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Unikorn Go Client | ||
|
||
![Unikorn Logo](https://raw.githubusercontent.com/unikorn-cloud/assets/main/images/logos/light-on-dark/logo.svg#gh-dark-mode-only) | ||
![Unikorn Logo](https://raw.githubusercontent.com/unikorn-cloud/assets/main/images/logos/dark-on-light/logo.svg#gh-light-mode-only) | ||
|
||
Provides API level access for CLI tooling. | ||
|
||
## Getting Started | ||
|
||
There is an [example](examples/client.go) of how to use this in tree. | ||
|
||
In order to use this example you must first download a personal access token from the unikorn UI (under the `Identity > Tokens` section), and save it in `~/.config/unikorn/token`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
Copyright 2024 the Unikorn Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/spf13/pflag" | ||
|
||
"github.com/unikorn-cloud/client-go/pkg/client" | ||
) | ||
|
||
func main() { | ||
ctx := context.Background() | ||
|
||
// Initialize the client with some default values. | ||
// This allows you to tailor to your specific environment and | ||
// brand if you aren't a fan of unicorns. Shame on you. | ||
client := client.New() | ||
client.IdentityEndpoint = "https://identity.spjmurray.co.uk" | ||
client.KubernetesEndpoint = "https://api.spjmurray.co.uk" | ||
client.TokenFile = filepath.Join(os.Getenv("HOME"), ".config/unikorn/token") | ||
|
||
// Allow flags to override these values. | ||
client.AddFlags(pflag.CommandLine) | ||
pflag.Parse() | ||
|
||
// Shutdown should be called at least once to ensure tokens are | ||
// flushed to disk. If you are running this in a CI/CD environment | ||
// then you need to ensure that file is stashed somewhere persistent | ||
// e.g. blob storage. | ||
defer func() { | ||
if err := client.Shutdown(); err != nil { | ||
fmt.Println("WARN: unable to shutdown client:", err) | ||
} | ||
}() | ||
|
||
// Do some work. Be careful with API calls, they aren't reentrant by default | ||
// so you may risk races with concurrent token refresh unless the APIs are | ||
// decorated with middleware of some variety. | ||
// TODO: that ^^. | ||
identity, err := client.Identity(ctx) | ||
if err != nil { | ||
fmt.Println("FATAL: unable to initialize client:", err) | ||
os.Exit(1) | ||
} | ||
|
||
response, err := identity.GetApiV1OrganizationsWithResponse(ctx) | ||
if err != nil { | ||
fmt.Println("FATAL: unable to read organizations:", err) | ||
os.Exit(1) | ||
} | ||
|
||
if response.HTTPResponse.StatusCode != http.StatusOK { | ||
fmt.Println("FATAL: unexpected status code:", response.HTTPResponse.StatusCode) | ||
os.Exit(1) | ||
} | ||
|
||
for _, organization := range *response.JSON200 { | ||
fmt.Println(organization.Name) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
module github.com/unikorn-cloud/client-go | ||
|
||
go 1.22.1 | ||
|
||
require ( | ||
github.com/coreos/go-oidc/v3 v3.10.0 | ||
github.com/spf13/pflag v1.0.5 | ||
github.com/unikorn-cloud/identity v0.2.3 | ||
github.com/unikorn-cloud/unikorn v0.2.8 | ||
golang.org/x/oauth2 v0.20.0 | ||
) | ||
|
||
require ( | ||
github.com/BurntSushi/toml v1.3.2 // indirect | ||
github.com/CloudyKit/fastprinter v0.0.0-20200109182630-33d98a066a53 // indirect | ||
github.com/CloudyKit/jet/v6 v6.2.0 // indirect | ||
github.com/Joker/jade v1.1.3 // indirect | ||
github.com/Shopify/goreferrer v0.0.0-20220729165902-8cddb4f5de06 // indirect | ||
github.com/andybalholm/brotli v1.1.0 // indirect | ||
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect | ||
github.com/aymerick/douceur v0.2.0 // indirect | ||
github.com/bytedance/sonic v1.11.3 // indirect | ||
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect | ||
github.com/chenzhuoyu/iasm v0.9.1 // indirect | ||
github.com/deepmap/oapi-codegen v1.16.2 // indirect | ||
github.com/fatih/structs v1.1.0 // indirect | ||
github.com/flosch/pongo2/v4 v4.0.2 // indirect | ||
github.com/gabriel-vasile/mimetype v1.4.3 // indirect | ||
github.com/getkin/kin-openapi v0.123.0 // indirect | ||
github.com/gin-contrib/sse v0.1.0 // indirect | ||
github.com/gin-gonic/gin v1.9.1 // indirect | ||
github.com/go-chi/chi/v5 v5.0.12 // indirect | ||
github.com/go-jose/go-jose/v4 v4.0.1 // indirect | ||
github.com/go-openapi/jsonpointer v0.21.0 // indirect | ||
github.com/go-openapi/swag v0.23.0 // indirect | ||
github.com/go-playground/locales v0.14.1 // indirect | ||
github.com/go-playground/universal-translator v0.18.1 // indirect | ||
github.com/go-playground/validator/v10 v10.19.0 // indirect | ||
github.com/goccy/go-json v0.10.2 // indirect | ||
github.com/golang/snappy v0.0.4 // indirect | ||
github.com/gomarkdown/markdown v0.0.0-20231222211730-1d6d20845b47 // indirect | ||
github.com/google/uuid v1.6.0 // indirect | ||
github.com/gorilla/css v1.0.1 // indirect | ||
github.com/invopop/yaml v0.2.0 // indirect | ||
github.com/iris-contrib/schema v0.0.6 // indirect | ||
github.com/josharian/intern v1.0.0 // indirect | ||
github.com/json-iterator/go v1.1.12 // indirect | ||
github.com/kataras/blocks v0.0.8 // indirect | ||
github.com/kataras/golog v0.1.11 // indirect | ||
github.com/kataras/iris/v12 v12.2.10 // indirect | ||
github.com/kataras/pio v0.0.13 // indirect | ||
github.com/kataras/sitemap v0.0.6 // indirect | ||
github.com/kataras/tunnel v0.0.4 // indirect | ||
github.com/klauspost/compress v1.17.7 // indirect | ||
github.com/klauspost/cpuid/v2 v2.2.7 // indirect | ||
github.com/labstack/echo/v4 v4.11.4 // indirect | ||
github.com/labstack/gommon v0.4.2 // indirect | ||
github.com/leodido/go-urn v1.4.0 // indirect | ||
github.com/mailgun/raymond/v2 v2.0.48 // indirect | ||
github.com/mailru/easyjson v0.7.7 // indirect | ||
github.com/mattn/go-colorable v0.1.13 // indirect | ||
github.com/mattn/go-isatty v0.0.20 // indirect | ||
github.com/microcosm-cc/bluemonday v1.0.26 // indirect | ||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect | ||
github.com/modern-go/reflect2 v1.0.2 // indirect | ||
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect | ||
github.com/pelletier/go-toml/v2 v2.2.0 // indirect | ||
github.com/perimeterx/marshmallow v1.1.5 // indirect | ||
github.com/russross/blackfriday/v2 v2.1.0 // indirect | ||
github.com/schollz/closestmatch v2.1.0+incompatible // indirect | ||
github.com/sirupsen/logrus v1.9.3 // indirect | ||
github.com/tdewolff/minify/v2 v2.20.19 // indirect | ||
github.com/tdewolff/parse/v2 v2.7.12 // indirect | ||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect | ||
github.com/ugorji/go/codec v1.2.12 // indirect | ||
github.com/valyala/bytebufferpool v1.0.0 // indirect | ||
github.com/valyala/fasttemplate v1.2.2 // indirect | ||
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect | ||
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect | ||
github.com/yosssi/ace v0.0.5 // indirect | ||
golang.org/x/arch v0.7.0 // indirect | ||
golang.org/x/crypto v0.21.0 // indirect | ||
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 // indirect | ||
golang.org/x/net v0.22.0 // indirect | ||
golang.org/x/sys v0.18.0 // indirect | ||
golang.org/x/text v0.14.0 // indirect | ||
golang.org/x/time v0.5.0 // indirect | ||
google.golang.org/protobuf v1.33.0 // indirect | ||
gopkg.in/ini.v1 v1.67.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
Oops, something went wrong.