client

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 4, 2022 License: Apache-2.0 Imports: 21 Imported by: 1

README

Community Go Client for STACKIT

Go Report Card Unit Tests Coverage Status GitHub go.mod Go version of a Go module GoDoc reference License


This repo's goal is to create a go-based http client for consuming STACKIT APIs

The client is community-supported and not an official STACKIT release, it is maintained by internal Schwarz IT teams integrating with STACKIT

Usage example

To get started, a Service Account[^1] and a Customer Account[^2] must be in place

If you're not sure how to get this information, please contact STACKIT support

package main

import (
	"context"
	"fmt"
	"os"

	client "github.com/SchwarzIT/community-stackit-go-client"
)

func main() {
	c, err := client.New(context.Background(), &client.Config{
		ServiceAccountID: os.Getenv("STACKIT_SERVICE_ACCOUNT_ID"), 
		Token:            os.Getenv("STACKIT_SERVICE_ACCOUNT_TOKEN"),
		OrganizationID:   os.Getenv("STACKIT_CUSTOMER_ACCOUNT_ID"), 
	})
	if err != nil {
		panic(err)
	}

	projectID := "1234-56789-101112"
	bucketName := "example"

	err = c.ObjectStorage.Buckets.Create(context.TODO(), projectID, bucketName)
	if err != nil {
		panic(err)
	}

	fmt.Printf("bucket '%s' created successfully", bucketName)
}

Another usage example can be found in terraform-provider-stackit which is built using the community client

[^1]: In order to use the client, a Service Account and Token must be created using Service Account API
After creation, assign roles to the Service Account using Membership API
If your Service Account needs to operate outside the scope of your project, you may need to contact STACKIT to assign further permissions


[^2]: The Customer Account ID is also referred to as Organization ID

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AuthAPIClientCredentialFlowRes

type AuthAPIClientCredentialFlowRes struct {
	AccessToken string `json:"access_token"`
	TokenType   string `json:"token_type"`
	ExpiresIn   int    `json:"expires_in"`
	Scope       string `json:"scope"`
	JTI         string `json:"jti"`
}

AuthAPIClientCredentialFlowRes response structure for client credentials flow

type AuthClient

type AuthClient struct {
	Client *http.Client
	Config *AuthConfig
}

AuthClient manages communication with Auth API

func MockAuthServer

func MockAuthServer() (c *AuthClient, mux *http.ServeMux, teardown func(), err error)

MockAuthServer mocks an authentication server and returns an auth client pointing to it, mux, teardown function and an error indicator

func NewAuth

func NewAuth(ctx context.Context, cfg *AuthConfig) (*AuthClient, error)

NewAuth returns a new Auth API client

func (*AuthClient) Do

func (c *AuthClient) Do(req *http.Request, v interface{}, errorHandlers ...func(*http.Response) error) (*http.Response, error)

Do performs the request and decodes the response if given interface != nil

func (*AuthClient) GetToken

func (c *AuthClient) GetToken(ctx context.Context) (res AuthAPIClientCredentialFlowRes, err error)

GetToken returns a token from Auth API

func (*AuthClient) Request

func (c *AuthClient) Request(ctx context.Context, method, path string, body string) (*http.Request, error)

Request creates an API request

type AuthConfig

type AuthConfig struct {
	BaseUrl      *url.URL
	ClientID     string
	ClientSecret string
}

AuthConfig holds information for using auth API

func (*AuthConfig) SetURL

func (c *AuthConfig) SetURL(value string) error

SetURL sets a given url string as the base url in the config if the given value is empty, the default auth base URL will be used

func (*AuthConfig) Validate

func (c *AuthConfig) Validate() error

Validate verifies that the given config is valid

type Client

type Client struct {

	// Productive services - services that are ready to be used in production
	ProductiveServices

	// Incubator - services under development or currently being tested
	// not ready for production usage
	Incubator IncubatorServices
	// contains filtered or unexported fields
}

Client service for managing interactions with STACKIT API

func MockServer

func MockServer() (c *Client, mux *http.ServeMux, teardown func(), err error)

MockServer mocks STACKIT api server and returns a client pointing to it, mux, teardown function and an error indicator

func New

func New(ctx context.Context, cfg *Config) (*Client, error)

New returns a new client

func (*Client) Do

func (c *Client) Do(req *http.Request, v interface{}, errorHandlers ...func(*http.Response) error) (*http.Response, error)

Do performs the request, including retry if set To set retry, use WithRetry() which returns a shalow copy of the client

func (*Client) GetConfig

func (c *Client) GetConfig() *Config

func (*Client) GetHTTPClient

func (c *Client) GetHTTPClient() *http.Client

func (*Client) OrganizationID

func (c *Client) OrganizationID() string

OrganizationID returns the organization ID defined in the configuration

func (*Client) Request

func (c *Client) Request(ctx context.Context, method, path string, body []byte) (*http.Request, error)

Request creates a new API request

func (*Client) SetToken

func (c *Client) SetToken(token string)

func (*Client) WithRetry added in v0.2.0

func (c *Client) WithRetry(r *retry.Retry) *Client

WithRetry sets retry.Retry in a shallow copy of the given client and returns the new copy

type Config

type Config struct {
	BaseUrl          *url.URL
	Token            string
	ServiceAccountID string
	OrganizationID   string
}

Config is the STACKIT client configuration

func (*Config) SetURL

func (c *Config) SetURL(value string) error

SetURL sets a given url string as the base url in the config if the given value is empty, the default base URL will be used

func (*Config) Validate

func (c *Config) Validate() error

Validate verifies that the given config is valid

type IncubatorServices

type IncubatorServices struct {
	Membership      *membership.MembershipService
	ResourceManager *resourceManager.ResourceManagerService
}

IncubatorServices is the struct representing all services that are under development

type ProductiveServices

type ProductiveServices struct {
	Argus         *argus.ArgusService
	Costs         *costs.CostsService
	Kubernetes    *kubernetes.KubernetesService
	ObjectStorage *objectstorage.ObjectStorageService
	Projects      *projects.ProjectService
	Roles         *roles.RolesService
	Users         *users.UsersService
}

ProductiveServices is the struct representing all productive services

Directories

Path Synopsis
internal
common
client file in package common holds the client interface and service struct used by each service that the client is connecting with services using the Service struct are located under pkg/api
client file in package common holds the client interface and service struct used by each service that the client is connecting with services using the Service struct are located under pkg/api
pkg
api/v2/membership/roles
package roles is used for creating and managing custom roles (and permissions assigned to them)
package roles is used for creating and managing custom roles (and permissions assigned to them)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL