arukas

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2019 License: Apache-2.0 Imports: 17 Imported by: 3

README

go-arukas

Go Report Card Build Status

This project provides various Go packages to perform operations on Arukas.

Install

go get github.com/yamamoto-febc/go-arukas

Documentation

GoDoc

License

go-arukas Copyright (C) 2018-2019 Kazumichi Yamamoto.

This project is published under Apache 2.0 License.

Author

Documentation

Overview

Package arukas is library for Arukas API clients

ref: https://arukas.io/en/documents-en/arukas-api-reference-en/

Example
package main

import (
	"context"
	"time"

	"github.com/yamamoto-febc/go-arukas"
)

func main() {

	// Initialize API Client
	token := "<your-api-token>"
	secret := "<your-api-secret>"

	client, err := arukas.NewClient(&arukas.ClientParam{
		Token:  token,
		Secret: secret,
	})
	if err != nil {
		panic(err)
	}

	// Create container(app and service)
	app, err := client.CreateApp(&arukas.RequestParam{
		Name:      "<your-app-name>",
		Image:     "nginx:latest",  // supports only DockerHub public repositories
		Plan:      arukas.PlanFree, // allows [PlanFree / PlanHobby / PlanStandard1 / PlanStandard2]
		Instances: 1,               // number of instance count
		SubDomain: "",              // subdomain(EndPoint will be <subdomain>.arukascloud.io). if empty, using random domain.
		Ports: []*arukas.Port{
			{
				Protocol: "tcp",
				Number:   80,
			},
		},
		Environment: []*arukas.Env{
			{
				Key:   "FOO",
				Value: "BAR",
			},
		},
		Command: "nginx -g daemon off;",
	})

	if err != nil {
		panic(err)
	}

	// Get IDs
	appID := app.AppID()
	serviceID := app.ServiceID()

	// Power on
	err = client.PowerOn(serviceID)
	if err != nil {
		panic(err)
	}

	// Wait until container is running...
	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
	defer cancel()

	errChan := make(chan error)
	go func() {
		errChan <- client.WaitForState(ctx, serviceID, arukas.StatusRunning)
	}()

	select {
	case e := <-errChan:
		if e != nil {
			panic(e)
		}
	case <-ctx.Done():
		panic(ctx.Err())
	}

	// Update container(service)
	_, err = client.UpdateService(serviceID, &arukas.RequestParam{
		Image: "httpd:latest",
	})
	if err != nil {
		panic(err)
	}

	// Delete container(app)
	err = client.DeleteApp(appID)
	if err != nil {
		panic(err)
	}

}
Example (CreateContainer)
package main

import (
	"fmt"

	"github.com/yamamoto-febc/go-arukas"
)

func main() {
	// Initialize API Client
	token := "<your-api-token>"
	secret := "<your-api-secret>"

	client, err := arukas.NewClient(&arukas.ClientParam{
		Token:  token,
		Secret: secret,
	})
	if err != nil {
		panic(err)
	}

	// Create container(app and service)
	app, err := client.CreateApp(&arukas.RequestParam{
		Name:      "<your-app-name>",
		Image:     "nginx:latest",  // supports only DockerHub public repositories
		Plan:      arukas.PlanFree, // allows [PlanFree / PlanHobby / PlanStandard1 / PlanStandard2]
		Instances: 1,               // number of instance count
		SubDomain: "",              // subdomain(EndPoint will be <subdomain>.arukascloud.io). if empty, using random domain.
		Ports: []*arukas.Port{
			{
				Protocol: "tcp",
				Number:   80,
			},
		},
		Environment: []*arukas.Env{
			{
				Key:   "FOO",
				Value: "BAR",
			},
		},
		Command: "nginx -g daemon off;",
	})

	if err != nil {
		panic(err)
	}

	fmt.Printf("AppName: %s", app.Name())
}
Example (DeleteApp)
package main

import (
	"github.com/yamamoto-febc/go-arukas"
)

func main() {
	// Initialize API Client
	token := "<your-api-token>"
	secret := "<your-api-secret>"
	appID := "<your-app-id>" // UUID format is required for appID

	client, err := arukas.NewClient(&arukas.ClientParam{
		Token:  token,
		Secret: secret,
	})
	if err != nil {
		panic(err)
	}

	// Delete app
	err = client.DeleteApp(appID)
	if err != nil {
		panic(err)
	}
}
Example (ListApps)
package main

import (
	"fmt"

	"github.com/yamamoto-febc/go-arukas"
)

func main() {

	// Initialize API Client
	token := "<your-api-token>"
	secret := "<your-api-secret>"

	client, err := arukas.NewClient(&arukas.ClientParam{
		Token:  token,
		Secret: secret,
	})
	if err != nil {
		panic(err)
	}

	// List all apps
	apps, err := client.ListApps()
	if err != nil {
		panic(err)
	}
	for _, app := range apps.Data {
		fmt.Printf("AppName: %s", app.Name())
	}

}
Example (ListServices)
package main

import (
	"fmt"

	"github.com/yamamoto-febc/go-arukas"
)

func main() {
	// Initialize API Client
	token := "<your-api-token>"
	secret := "<your-api-secret>"

	client, err := arukas.NewClient(&arukas.ClientParam{
		Token:  token,
		Secret: secret,
	})
	if err != nil {
		panic(err)
	}

	// List all services
	services, err := client.ListServices()
	if err != nil {
		panic(err)
	}

	for _, service := range services.Data {
		fmt.Printf("ServiceID: %s\nEndPoint URL: %s", service.ID, service.EndPoint())
	}
}
Example (PowerOff)
package main

import (
	"context"
	"time"

	"github.com/yamamoto-febc/go-arukas"
)

func main() {
	// Initialize API Client
	token := "<your-api-token>"
	secret := "<your-api-secret>"
	serviceID := "<your-service-id>" // UUID format is required for serviceID

	client, err := arukas.NewClient(&arukas.ClientParam{
		Token:  token,
		Secret: secret,
	})
	if err != nil {
		panic(err)
	}

	// PowerOff
	if err := client.PowerOff(serviceID); err != nil {
		panic(err)
	}

	// Wait until container is stopped...
	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
	defer cancel()

	errChan := make(chan error)
	go func() {
		errChan <- client.WaitForState(ctx, serviceID, arukas.StatusStopped)
	}()

	select {
	case e := <-errChan:
		if e != nil {
			panic(e)
		}
	case <-ctx.Done():
		panic(ctx.Err())
	}
}
Example (PowerOn)
package main

import (
	"context"
	"time"

	"github.com/yamamoto-febc/go-arukas"
)

func main() {
	// Initialize API Client
	token := "<your-api-token>"
	secret := "<your-api-secret>"
	serviceID := "<your-service-id>" // UUID format is required for serviceID

	client, err := arukas.NewClient(&arukas.ClientParam{
		Token:  token,
		Secret: secret,
	})
	if err != nil {
		panic(err)
	}

	// PowerOn
	if err := client.PowerOn(serviceID); err != nil {
		panic(err)
	}

	// Wait until container is running...
	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
	defer cancel()

	errChan := make(chan error)
	go func() {
		errChan <- client.WaitForState(ctx, serviceID, arukas.StatusRunning)
	}()

	select {
	case e := <-errChan:
		if e != nil {
			panic(e)
		}
	case <-ctx.Done():
		panic(ctx.Err())
	}
}
Example (ReadApp)
package main

import (
	"fmt"

	"github.com/yamamoto-febc/go-arukas"
)

func main() {
	// Initialize API Client
	token := "<your-api-token>"
	secret := "<your-api-secret>"
	appID := "<your-app-id>" // UUID format is required for appID

	client, err := arukas.NewClient(&arukas.ClientParam{
		Token:  token,
		Secret: secret,
	})
	if err != nil {
		panic(err)
	}

	// Read app
	app, err := client.ReadApp(appID)
	if err != nil {
		panic(err)
	}
	fmt.Printf("AppName: %s", app.Name())
}
Example (UpdateService)
package main

import (
	"fmt"

	"github.com/yamamoto-febc/go-arukas"
)

func main() {
	// Initialize API Client
	token := "<your-api-token>"
	secret := "<your-api-secret>"
	serviceID := "<your-service-id>" // UUID format is required for serviceID

	client, err := arukas.NewClient(&arukas.ClientParam{
		Token:  token,
		Secret: secret,
	})
	if err != nil {
		panic(err)
	}

	// Update service
	updatedService, err := client.UpdateService(serviceID, &arukas.RequestParam{
		Instances: 2,
		Image:     "nginx:latst",
		Ports: []*arukas.Port{
			{
				Protocol: "tcp",
				Number:   80,
			},
			{
				Protocol: "tcp",
				Number:   443,
			},
		},
		Environment: []*arukas.Env{
			{
				Key:   "MY_ENV",
				Value: "my_value",
			},
		},
	})

	if err != nil {
		panic(err)
	}

	fmt.Printf("Services: %#v", updatedService)
}

Index

Examples

Constants

View Source
const (
	// PlanFree represents the "free" plan
	PlanFree = "free"
	// PlanHobby represents the "hobby" plan
	PlanHobby = "hobby"
	// PlanStandard1 represents the "standard-1" plan
	PlanStandard1 = "standard-1"
	// PlanStandard2 represents the "standard-2" plan
	PlanStandard2 = "standard-2"
)
View Source
const (
	// TypeApps represents the "apps" type
	TypeApps = "apps"
	// TypeServices represents the "services" type
	TypeServices = "services"
	// TypeServicePlans represents the "service_plans" type
	TypeServicePlans = "service-plans"
	// TypeUsers represents the "users" type
	TypeUsers = "users"
	// TypeRegions represents the "regions" type
	TypeRegions = "regions"
)
View Source
const (
	// StatusBooting represents the "booting" status
	StatusBooting = "booting"
	// StatusTerminated represents the "terminated" status
	StatusTerminated = "terminated"
	// StatusRunning represents the "running" status
	StatusRunning = "running"
	// StatusStopping represents the "stopping" status
	StatusStopping = "stopping"
	// StatusStopped represents the "stopped" status
	StatusStopped = "stopped"
	// StatusRebooting represents the "rebooting" status
	StatusRebooting = "rebooting"
)
View Source
const LinkID = 1

LinkID used when creating new app/service

View Source
const (
	// RegionJPTokyo represents the "jp-tokyo" region
	RegionJPTokyo = "jp-tokyo"
)

Variables

View Source
var (
	// ValidRegions is a list of valid regions
	ValidRegions = []string{RegionJPTokyo}
	// ValidPlans is a list of valid
	ValidPlans = []string{PlanFree, PlanHobby, PlanStandard1, PlanStandard2}
)
View Source
var ValidProtocols = []string{"tcp", "udp"}

ValidProtocols is a list of valid protocol

View Source
var Version = "0.1.0"

Version returns current version

Functions

func PlanID

func PlanID(region, plan string) string

PlanID generates Plan ID from region name and plan name

Types

type App

type App struct {
	ID            string           `json:"id"`
	Type          string           `json:"type,omitempty"`
	Attributes    *AppAttr         `json:"attributes,omitempty"`
	Relationships *AppRelationship `json:"relationships,omitempty"`
}

App represents a app object

func (*App) AppID

func (a *App) AppID() string

AppID returns data.id

func (*App) CreatedAt

func (a *App) CreatedAt() *time.Time

CreatedAt returns data.attributes.created_at

func (*App) Name

func (a *App) Name() string

Name returns data.attributes.Name

func (*App) ServiceID

func (a *App) ServiceID() string

ServiceID returns service id

func (*App) UpdatedAt

func (a *App) UpdatedAt() *time.Time

UpdatedAt returns data.attributes.updated_at

type AppAttr

type AppAttr struct {
	Name      string     `json:"name,omitempty"`
	CreatedAt *time.Time `json:"created-at,omitempty"`
	UpdatedAt *time.Time `json:"updated-at,omitempty"`
}

AppAttr represents app.attributes object

type AppData

type AppData struct {
	Data     *App          `json:"data"`
	Included []interface{} `json:"included,omitempty"`
}

AppData represents data object(included app and child service)

func (*AppData) AppID

func (a *AppData) AppID() string

AppID returns data.id

func (*AppData) CreatedAt

func (a *AppData) CreatedAt() *time.Time

CreatedAt returns data.attributes.created_at

func (*AppData) Name

func (a *AppData) Name() string

Name returns data.attributes.Name

func (*AppData) Service

func (a *AppData) Service() *Service

Service returns service(from included)

func (*AppData) ServiceID

func (a *AppData) ServiceID() string

ServiceID returns service id

func (*AppData) Type

func (a *AppData) Type() string

Type returns data.type

func (*AppData) UpdatedAt

func (a *AppData) UpdatedAt() *time.Time

UpdatedAt returns data.attributes.updated_at

type AppListData

type AppListData struct {
	Data     []*App        `json:"data"`
	Included []interface{} `json:"included,omitempty"`
}

AppListData represents data object(included []app and child services)

type AppRelationship

type AppRelationship struct {
	User     *RelationshipData     `json:"user,omitempty"`
	Services *RelationshipDataList `json:"services,omitempty"`
}

AppRelationship represents App relationship data

func NewAppRelationship

func NewAppRelationship() *AppRelationship

NewAppRelationship creates new AppRelationship with default values

type Client

type Client interface {
	ListApps() (*AppListData, error)
	ReadApp(id string) (*AppData, error)
	CreateApp(param *RequestParam) (*AppData, error)
	DeleteApp(id string) error

	ListServices() (*ServiceListData, error)
	ReadService(id string) (*ServiceData, error)
	UpdateService(id string, param *RequestParam) (*ServiceData, error)
	PowerOn(id string) error
	PowerOff(id string) error

	WaitForState(ctx context.Context, serviceID string, status string) error

	Version() string
}

Client is Arukas API Client interface

func NewClient

func NewClient(p *ClientParam) (Client, error)

NewClient returns a new arukas API Client, requires an authorization key. You can generate a API key by visiting the Keys section of the Arukas control panel for your account.

type ClientParam

type ClientParam struct {
	APIBaseURL string
	Token      string
	Secret     string
	UserAgent  string
	Trace      bool
	TraceOut   io.Writer
	Timeout    time.Duration
}

ClientParam represents parameters to use arukas.API

type CustomDomain

type CustomDomain struct {
	Name string `json:"name"`
}

CustomDomain represents custom_domain object

func CustomDomains

func CustomDomains(domains ...string) []*CustomDomain

CustomDomains returns array of *CustomDomain created from domain name list

type Env

type Env struct {
	Key   string `json:"key"`
	Value string `json:"value"`
}

Env represents environment object

type ErrorNotFound added in v0.0.2

type ErrorNotFound error

ErrorNotFound represents 404 not found error

type Port

type Port struct {
	// Protocol is either tcp/udp
	Protocol string `json:"protocol"`
	// Number is port number that container exposes.
	// Valid value range is [1 - 65535]
	Number int32 `json:"number"`
}

Port represents the port information you want expose in container

func (*Port) MarshalJSON added in v0.1.0

func (p *Port) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler

type PortMapping

type PortMapping struct {
	Host          string `json:"host"`
	Protocol      string `json:"protocol"`
	ContainerPort int32  `json:"container-port"`
	ServicePort   int32  `json:"service-port"`
}

PortMapping represents actual port mapping information on running container

type Ports added in v0.0.8

type Ports []*Port

Ports is a slice of Ports. A service can have multiple ports.

func (*Ports) UnmarshalJSON added in v0.0.8

func (ports *Ports) UnmarshalJSON(data []byte) error

UnmarshalJSON parses ports in both old and new port format and convert them to Port.

type Relationship

type Relationship struct {
	ID     string `json:"id,omitempty"`
	LinkID int32  `json:"lid,omitempty"`
	Type   string `json:"type,omitempty"`
}

Relationship represents relationship body

type RelationshipData

type RelationshipData struct {
	Data *Relationship `json:"data,omitempty"`
}

RelationshipData represents relationship data

type RelationshipDataList added in v0.0.7

type RelationshipDataList struct {
	Data []*Relationship `json:"data,omitempty"`
}

RelationshipDataList represents relationship data(list)

type RequestParam

type RequestParam struct {
	// Name needs only when create app
	Name          string
	Command       string
	CustomDomains []string
	Image         string
	Instances     int32
	Ports         Ports
	Environment   []*Env
	SubDomain     string
	Region        string
	Plan          string
}

RequestParam represents request parameter of Arukas.API

func (*RequestParam) ToAppData

func (p *RequestParam) ToAppData() *AppData

ToAppData returns *AppData built from RequestParam

func (*RequestParam) ToServiceData

func (p *RequestParam) ToServiceData() *ServiceData

ToServiceData returns *ServiceData built from RequestParam

func (*RequestParam) ValidateForCreate

func (p *RequestParam) ValidateForCreate() error

ValidateForCreate returns error if parameter is invalid

func (*RequestParam) ValidateForUpdate

func (p *RequestParam) ValidateForUpdate() error

ValidateForUpdate returns error if parameter is invalid

type Service

type Service struct {
	ID            string               `json:"id,omitempty"`
	LinkID        int32                `json:"lid,omitempty"`
	Type          string               `json:"type,omitempty"`
	Attributes    *ServiceAttr         `json:"attributes,omitempty"`
	Relationships *ServiceRelationship `json:"relationships,omitempty"`
}

Service represents service object

func (*Service) AppID

func (s *Service) AppID() string

AppID returns data.attributes.app_id

func (*Service) CPUs

func (s *Service) CPUs() float32

CPUs returns data.attributes.cups

func (*Service) Command

func (s *Service) Command() string

Command returns data.attributes.command

func (*Service) CreatedAt

func (s *Service) CreatedAt() *time.Time

CreatedAt returns data.attributes.created_at

func (*Service) EndPoint

func (s *Service) EndPoint() string

EndPoint returns data.attributes.endpoint

func (*Service) Environment

func (s *Service) Environment() []*Env

Environment returns data.attributes.environment

func (*Service) Image

func (s *Service) Image() string

Image returns data.attributes.image

func (*Service) Instances

func (s *Service) Instances() int32

Instances returns data.attributes.instances

func (*Service) Memory

func (s *Service) Memory() int32

Memory returns data.attributes.memory

func (*Service) PlanID

func (s *Service) PlanID() string

PlanID returns data.relationship.service_plan.data.id

func (*Service) PortMapping

func (s *Service) PortMapping() []*PortMapping

PortMapping returns data.attributes.port_mappings[0]

func (*Service) PortMappings

func (s *Service) PortMappings() [][]*PortMapping

PortMappings returns data.attributes.port_mappings

func (*Service) Ports

func (s *Service) Ports() Ports

Ports returns data.attributes.ports

func (*Service) Status

func (s *Service) Status() string

Status returns data.attributes.status

func (*Service) SubDomain

func (s *Service) SubDomain() string

SubDomain returns data.attributes.subdomain

func (*Service) UpdatedAt

func (s *Service) UpdatedAt() *time.Time

UpdatedAt returns data.attributes.updated_at

type ServiceAttr

type ServiceAttr struct {
	AppID                    string           `json:"app-id,omitempty"`
	Image                    string           `json:"image"`
	Command                  string           `json:"command"`
	Instances                int32            `json:"instances"`
	CPUs                     float32          `json:"cups,omitempty"`
	Memory                   int32            `json:"memory,omitempty"`
	Environment              []*Env           `json:"environment"`
	Ports                    Ports            `json:"ports,omitempty"`
	PortMappings             [][]*PortMapping `json:"port-mappings,omitempty"`
	CreatedAt                *time.Time       `json:"created-at,omitempty"`
	UpdatedAt                *time.Time       `json:"updated-at,omitempty"`
	Status                   string           `json:"status,omitempty"`
	SubDomain                string           `json:"subdomain,omitempty"`
	EndPoint                 string           `json:"endpoint,omitempty"`
	CustomDomains            []*CustomDomain  `json:"custom-domains,omitempty"`
	LastInstanceFailedAt     *time.Time       `json:"last-instance-failed-at,omitempty"`
	LastInstanceFailedStatus interface{}      `json:"last-instance-failed-status,omitempty"`
}

ServiceAttr represents service.attributes object

type ServiceData

type ServiceData struct {
	Data *Service `json:"data"`
}

ServiceData represents service data

func (*ServiceData) AppID added in v0.0.2

func (s *ServiceData) AppID() string

AppID returns data.attributes.app_id

func (*ServiceData) CPUs

func (s *ServiceData) CPUs() float32

CPUs returns data.attributes.cups

func (*ServiceData) Command

func (s *ServiceData) Command() string

Command returns data.attributes.command

func (*ServiceData) CreatedAt

func (s *ServiceData) CreatedAt() *time.Time

CreatedAt returns data.attributes.created_at

func (*ServiceData) EndPoint

func (s *ServiceData) EndPoint() string

EndPoint returns data.attributes.endpoint

func (*ServiceData) Environment

func (s *ServiceData) Environment() []*Env

Environment returns data.attributes.environment

func (*ServiceData) Image

func (s *ServiceData) Image() string

Image returns data.attributes.image

func (*ServiceData) Instances

func (s *ServiceData) Instances() int32

Instances returns data.attributes.instances

func (*ServiceData) Memory

func (s *ServiceData) Memory() int32

Memory returns data.attributes.memory

func (*ServiceData) PlanID

func (s *ServiceData) PlanID() string

PlanID returns data.relationship.service_plan.data.id

func (*ServiceData) PortMapping

func (s *ServiceData) PortMapping() []*PortMapping

PortMapping returns data.attributes.port_mappings[0]

func (*ServiceData) PortMappings

func (s *ServiceData) PortMappings() [][]*PortMapping

PortMappings returns data.attributes.port_mappings

func (*ServiceData) Ports

func (s *ServiceData) Ports() Ports

Ports returns data.attributes.ports

func (*ServiceData) ServiceID

func (s *ServiceData) ServiceID() string

ServiceID returns data.id

func (*ServiceData) Status

func (s *ServiceData) Status() string

Status returns data.attributes.status

func (*ServiceData) SubDomain

func (s *ServiceData) SubDomain() string

SubDomain returns data.attributes.subdomain

func (*ServiceData) Type

func (s *ServiceData) Type() string

Type returns data.type

func (*ServiceData) UpdatedAt

func (s *ServiceData) UpdatedAt() *time.Time

UpdatedAt returns data.attributes.updated_at

type ServiceListData

type ServiceListData struct {
	Data []*Service `json:"data"`
}

ServiceListData represents services data

type ServiceRelationship

type ServiceRelationship struct {
	App         *RelationshipData `json:"app,omitempty"`
	ServicePlan *RelationshipData `json:"service-plan,omitempty"`
}

ServiceRelationship represents Services relationship data

func NewServiceRelationship

func NewServiceRelationship(region, plan string) *ServiceRelationship

NewServiceRelationship creates new ServiceRelationship with PlanID

Jump to

Keyboard shortcuts

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