phy

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2021 License: Apache-2.0 Imports: 9 Imported by: 0

README

phy-go

Go Reference Tests Go Report Card

Library for PHY API / さくらの専用サーバPHYAPI をGo言語から扱うためのライブラリ

PHY API: https://manual.sakura.ad.jp/ds/phy/api/api-spec.html

Overview

oapi-codegen によって生成されたGoのコードに加え、 Fake/Stubサーバの実装やより簡易に使えるようにラップしたクライアントコードを提供します。

⚠ phy-goは現在開発中です。v1に達するまでは後方互換性のない変更が行われる可能性があります。

phy-goを利用したクライアントコードの例
package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/sacloud/phy-go"
	v1 "github.com/sacloud/phy-go/apis/v1"
)

func main() {
	// APIキー
	token := os.Getenv("SAKURACLOUD_ACCESS_TOKEN")
	secret := os.Getenv("SAKURACLOUD_ACCESS_TOKEN_SECRET")

	// httpクライアント
	client := &phy.Client{
		Token:  token,
		Secret: secret,
	}

	// サーバ一覧
	serverOp := phy.NewServerOp(client)
	found, err := serverOp.List(context.Background(), &v1.ListServersParams{})
	if err != nil {
		log.Fatal(err)
	}
	for _, server := range found.Servers {
		fmt.Println(server.Service.Nickname)
	}
}

Installation

Use go get.

go get github.com/sacloud/phy-go

Then import the phy package into your own code.

import "github.com/sacloud/phy-go"

License

phy-go Copyright 2021 The phy-go authors.

This project is published under Apache 2.0 License.

Documentation

Overview

Package phy さくらの専用サーバPHY API ライブラリ

Example
token := os.Getenv("SAKURACLOUD_ACCESS_TOKEN")
secret := os.Getenv("SAKURACLOUD_ACCESS_TOKEN_SECRET")

client := &phy.Client{
	Token:      token,
	Secret:     secret,
	APIRootURL: serverURL, // 省略可
}

// サーバ操作
serverOp := phy.NewServerOp(client)
found, err := serverOp.List(context.Background(), &v1.ListServersParams{})
if err != nil {
	panic(err)
}
for _, sv := range found.Servers {
	fmt.Println(sv.Service.Nickname)
}
Output:

server01

Index

Examples

Constants

View Source
const DefaultAPIRootURL = "https://secure.sakura.ad.jp/cloud/api/dedicated-phy/1.0/"

DefaultAPIRootURL デフォルトのAPIルートURL

Variables

View Source
var (
	// Version app version
	Version = "v0.0.2"
	// Revision git commit short commithash
	Revision = "xxxxxx" // set on build time
)
View Source
var UserAgent = fmt.Sprintf(
	"phy-go/%s (%s/%s; +https://github.com/sacloud/phy-go) %s",
	Version,
	runtime.GOOS,
	runtime.GOARCH,
	sacloudhttp.DefaultUserAgent,
)

UserAgent APIリクエスト時のユーザーエージェント

Functions

This section is empty.

Types

type Client

type Client struct {
	// Token APIキー: トークン
	Token string
	// Token APIキー: シークレット
	Secret string

	// AcceptLanguage APIリクエスト時のAccept-Languageヘッダーの値
	AcceptLanguage string

	// Gzip APIリクエストでgzipを有効にするかのフラグ
	Gzip bool

	// APIRootURL APIのリクエスト先URLプレフィックス、省略可能
	APIRootURL string

	// Trace トレースログ出力フラグ
	Trace bool

	// HTTPClient APIリクエストで使用されるHTTPクライアント
	//
	// 省略した場合はhttp.DefaultClientが使用される
	HTTPClient *http.Client
	// contains filtered or unexported fields
}

Client APIクライアント

type DedicatedSubnetAPI

type DedicatedSubnetAPI interface {
	List(ctx context.Context, params *v1.ListDedicatedSubnetsParams) (*v1.DedicatedSubnets, error)
	Read(ctx context.Context, dedicatedSubnetId v1.DedicatedSubnetId, refresh bool) (*v1.DedicatedSubnet, error)
}

DedicatedSubnetAPI 専用グローバルネットワーク関連API

func NewDedicatedSubnetOp

func NewDedicatedSubnetOp(client *Client) DedicatedSubnetAPI

type DedicatedSubnetOp

type DedicatedSubnetOp struct {
	// contains filtered or unexported fields
}

func (*DedicatedSubnetOp) List

func (*DedicatedSubnetOp) Read

func (op *DedicatedSubnetOp) Read(ctx context.Context, dedicatedSubnetId v1.DedicatedSubnetId, refresh bool) (*v1.DedicatedSubnet, error)

type PrivateNetworkAPI

type PrivateNetworkAPI interface {
	List(ctx context.Context, params *v1.ListPrivateNetworksParams) (*v1.PrivateNetworks, error)
	Read(ctx context.Context, privateNetworkId v1.PrivateNetworkId) (*v1.PrivateNetwork, error)
}

PrivateNetworkAPI ローカルネットワーク関連API

func NewPrivateNetworkOp

func NewPrivateNetworkOp(client *Client) PrivateNetworkAPI

type PrivateNetworkOp

type PrivateNetworkOp struct {
	// contains filtered or unexported fields
}

func (*PrivateNetworkOp) List

func (*PrivateNetworkOp) Read

func (op *PrivateNetworkOp) Read(ctx context.Context, privateNetworkId v1.PrivateNetworkId) (*v1.PrivateNetwork, error)

type ServerAPI

type ServerAPI interface {
	List(ctx context.Context, params *v1.ListServersParams) (*v1.Servers, error)
	Read(ctx context.Context, serverId v1.ServerId) (*v1.Server, error)
	ListOSImages(ctx context.Context, serverId v1.ServerId) ([]*v1.OsImage, error)
	OSInstall(ctx context.Context, serverId v1.ServerId, params v1.OsInstallParameter) error
	ReadPortChannel(ctx context.Context, serverId v1.ServerId, portChannelId v1.PortChannelId) (*v1.PortChannel, error)
	ConfigureBonding(ctx context.Context, serverId v1.ServerId, portChannelId v1.PortChannelId, params v1.ConfigureBondingParameter) (*v1.PortChannel, error)
	ReadPort(ctx context.Context, serverId v1.ServerId, portId v1.PortId) (*v1.InterfacePort, error)
	UpdatePort(ctx context.Context, serverId v1.ServerId, portId v1.PortId, params v1.UpdateServerPortParameter) (*v1.InterfacePort, error)
	EnablePort(ctx context.Context, serverId v1.ServerId, portId v1.PortId, enable bool) (*v1.InterfacePort, error)
	AssignNetwork(ctx context.Context, serverId v1.ServerId, portId v1.PortId, params v1.AssignNetworkParameter) (*v1.InterfacePort, error)
	ReadTrafficByPort(ctx context.Context, serverId v1.ServerId, portId v1.PortId, params v1.ReadServerTrafficByPortParams) (*v1.TrafficGraph, error)
	PowerControl(ctx context.Context, serverId v1.ServerId, operation v1.ServerPowerOperations) error
	ReadPowerStatus(ctx context.Context, serverId v1.ServerId) (*v1.ServerPowerStatus, error)
	ReadRAIDStatus(ctx context.Context, serverId v1.ServerId, refresh bool) (*v1.RaidStatus, error)
}
Example
token := os.Getenv("SAKURACLOUD_ACCESS_TOKEN")
secret := os.Getenv("SAKURACLOUD_ACCESS_TOKEN_SECRET")

client := &phy.Client{
	Token:      token,
	Secret:     secret,
	APIRootURL: serverURL, // 省略可
}

// サーバ操作
ctx := context.Background()
serverOp := phy.NewServerOp(client)
found, err := serverOp.List(ctx, &v1.ListServersParams{})
if err != nil {
	panic(err)
}

// 電源がONのサーバをシャットダウン
for _, sv := range found.Servers {
	if sv.CachedPowerStatus.Status == v1.CachedPowerStatusStatusOn {
		// v1.ServerPowerOperationsSoft == ACPIシャットダウン
		if err := serverOp.PowerControl(ctx, v1.ServerId(sv.ServerId), v1.ServerPowerOperationsSoft); err != nil {
			panic(err)
		}
		fmt.Printf("shutting down: %s\n", sv.Service.Nickname)
	}
}
Output:

shutting down: server01

func NewServerOp

func NewServerOp(client *Client) ServerAPI

type ServerOp

type ServerOp struct {
	// contains filtered or unexported fields
}

func (*ServerOp) AssignNetwork

func (op *ServerOp) AssignNetwork(ctx context.Context, serverId v1.ServerId, portId v1.PortId, params v1.AssignNetworkParameter) (*v1.InterfacePort, error)

func (*ServerOp) ConfigureBonding

func (op *ServerOp) ConfigureBonding(ctx context.Context, serverId v1.ServerId, portChannelId v1.PortChannelId, params v1.ConfigureBondingParameter) (*v1.PortChannel, error)

func (*ServerOp) EnablePort

func (op *ServerOp) EnablePort(ctx context.Context, serverId v1.ServerId, portId v1.PortId, enable bool) (*v1.InterfacePort, error)

func (*ServerOp) List

func (op *ServerOp) List(ctx context.Context, params *v1.ListServersParams) (*v1.Servers, error)

func (*ServerOp) ListOSImages

func (op *ServerOp) ListOSImages(ctx context.Context, serverId v1.ServerId) ([]*v1.OsImage, error)

func (*ServerOp) OSInstall

func (op *ServerOp) OSInstall(ctx context.Context, serverId v1.ServerId, params v1.OsInstallParameter) error

func (*ServerOp) PowerControl

func (op *ServerOp) PowerControl(ctx context.Context, serverId v1.ServerId, operation v1.ServerPowerOperations) error

func (*ServerOp) Read

func (op *ServerOp) Read(ctx context.Context, serverId v1.ServerId) (*v1.Server, error)

func (*ServerOp) ReadPort

func (op *ServerOp) ReadPort(ctx context.Context, serverId v1.ServerId, portId v1.PortId) (*v1.InterfacePort, error)

func (*ServerOp) ReadPortChannel

func (op *ServerOp) ReadPortChannel(ctx context.Context, serverId v1.ServerId, portChannelId v1.PortChannelId) (*v1.PortChannel, error)

func (*ServerOp) ReadPowerStatus

func (op *ServerOp) ReadPowerStatus(ctx context.Context, serverId v1.ServerId) (*v1.ServerPowerStatus, error)

func (*ServerOp) ReadRAIDStatus

func (op *ServerOp) ReadRAIDStatus(ctx context.Context, serverId v1.ServerId, refresh bool) (*v1.RaidStatus, error)

func (*ServerOp) ReadTrafficByPort

func (op *ServerOp) ReadTrafficByPort(ctx context.Context, serverId v1.ServerId, portId v1.PortId, params v1.ReadServerTrafficByPortParams) (*v1.TrafficGraph, error)

func (*ServerOp) UpdatePort

func (op *ServerOp) UpdatePort(ctx context.Context, serverId v1.ServerId, portId v1.PortId, params v1.UpdateServerPortParameter) (*v1.InterfacePort, error)

type ServiceAPI

type ServiceAPI interface {
	List(ctx context.Context, params *v1.ListServicesParams) (*v1.Services, error)
	Read(ctx context.Context, serviceId v1.ServiceId) (*v1.Service, error)
	Update(ctx context.Context, serviceId v1.ServiceId, params v1.UpdateServiceParameter) (*v1.Service, error)
}

ServiceAPI サービス関連API

Example
token := os.Getenv("SAKURACLOUD_ACCESS_TOKEN")
secret := os.Getenv("SAKURACLOUD_ACCESS_TOKEN_SECRET")

client := &phy.Client{
	Token:      token,
	Secret:     secret,
	APIRootURL: serverURL, // 省略可
}

// サービス一覧取得
serviceOp := phy.NewServiceOp(client)
found, err := serviceOp.List(context.Background(), &v1.ListServicesParams{})
if err != nil {
	panic(err)
}
for _, svc := range found.Services {
	fmt.Println(svc.Nickname)
}
Output:

server01

func NewServiceOp

func NewServiceOp(client *Client) ServiceAPI

type ServiceOp

type ServiceOp struct {
	// contains filtered or unexported fields
}

ServiceOp サービス関連の操作

func (*ServiceOp) List

func (op *ServiceOp) List(ctx context.Context, params *v1.ListServicesParams) (*v1.Services, error)

func (*ServiceOp) Read

func (op *ServiceOp) Read(ctx context.Context, serviceId v1.ServiceId) (*v1.Service, error)

func (*ServiceOp) Update

func (op *ServiceOp) Update(ctx context.Context, serviceId v1.ServiceId, params v1.UpdateServiceParameter) (*v1.Service, error)

Directories

Path Synopsis
apis
v1
Package v1 provides primitives to interact with the openapi HTTP API.
Package v1 provides primitives to interact with the openapi HTTP API.
cmd

Jump to

Keyboard shortcuts

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