phy

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2023 License: Apache-2.0 Imports: 7 Imported by: 7

README

phy-api-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-api-goは現在開発中です。v1に達するまでは後方互換性のない変更が行われ得ることに注意してください。

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

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

	"github.com/sacloud/phy-api-go"
	v1 "github.com/sacloud/phy-api-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-api-go

Then import the phy package into your own code.

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

Fakeサーバ(スタンドアロン)

FakeサーバはGoのコード以外からでも利用できるようにスタンドアロン版を提供しています。

Fakeサーバのインストール

リリースページからダウンロード or go installしてください。

リリースページ: https://github.com/sacloud/phy-api-go/releases/latest

go install github.com/sacloud/phy-api-go/cmd/phy-api-go-fake-server
Fakeサーバの利用方法
$ phy-api-go-fake-server --help
Start the web server

Usage:
  phy-api-go-fake-server [flags]

Flags:
      --addr string      the address for the server to listen on (default ":8080")
      --data string      the file path to the fake data JSON file
  -h, --help             help for phy-api-go-fake-server
      --output-example   the flag to output a fake data JSON example
  -v, --version          version for phy-api-go-fake-server
  • --addr: Fakeサーバがリッスンするアドレス
  • --data: FakeデータのJSONファイルへのパス、省略した場合はデフォルトのダミーデータが利用される
  • --output-example: FakeデータのJSONファイルの例を出力

起動したら次のようにリクエストを行えます。

# localhost:8080で起動した場合の例
$ curl http://localhost:8080/services/
Fakeデータのカスタマイズ

--output-exampleでJSONファイルの雛形を出力し、編集、その後--dataでファイルパスを指定します。

# 雛形を出力
$ phy-api-go-fake-server --output-example > fake.json
# 編集
$ vi fake.json
# データファイルのパスを指定して起動
$ phy-api-go-fake-server --data=fake.json

License

phy-api-go Copyright 2021-2023 The phy-api-go authors.

This project is published under Apache 2.0 License.

Documentation

Overview

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

Example
client := &phy.Client{
	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 UserAgent = fmt.Sprintf(
	"phy-api-go/v%s (%s/%s; +https://github.com/sacloud/phy-api-go) %s",
	Version,
	runtime.GOOS,
	runtime.GOARCH,
	client.DefaultUserAgent,
)

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

View Source
var (
	// Version app version
	Version = "0.1.3"
)

Functions

This section is empty.

Types

type Client

type Client struct {
	// Profile usacloud互換プロファイル名
	Profile string

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

	// AccessToken APIキー:トークン
	// Optionsでの指定より優先される
	AccessToken string
	// AccessTokenSecret APIキー:シークレット
	// Optionsでの指定より優先される
	AccessTokenSecret string

	// Options HTTPクライアント関連オプション
	Options *client.Options

	// DisableProfile usacloud互換プロファイルからの設定読み取りを無効化
	DisableProfile bool

	// DisableEnv 環境変数からの設定読み取りを無効化
	DisableEnv bool
	// 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
client := &phy.Client{
	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, 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
client := &phy.Client{
	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