apprun

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2025 License: Apache-2.0 Imports: 9 Imported by: 1

README

sacloud/apprun-api-go

Go Reference Tests Go Report Card

Go言語向けのさくらのクラウド AppRun APIライブラリ

AppRun APIドキュメント: https://manual.sakura.ad.jp/sakura-apprun-api/spec.html

概要

sacloud/apprun-api-goはさくらのクラウド AppRun APIをGo言語から利用するためのAPIライブラリです。

利用イメージ:

package main

import (
	"context"
	"fmt"

	"github.com/sacloud/apprun-api-go"
	v1 "github.com/sacloud/apprun-api-go/apis/v1"
)

func main() {
	// デフォルトでusacloud互換プロファイル or 環境変数(SAKURACLOUD_ACCESS_TOKEN{_SECRET})が利用される
	client := &apprun.Client{}

	ctx := context.Background()

	// アプリケーションを作成
	appOp := apprun.NewApplicationOp(client)
	application, err := appOp.Create(ctx, &v1.PostApplicationBody{
		Name:           "example-app1",
		TimeoutSeconds: 100,
		Port:           80,
		MinScale:       0,
		MaxScale:       1,
		Components: []v1.PostApplicationBodyComponent{
			{
				Name:      "component1",
				MaxCpu:    "0.1",
				MaxMemory: "256Mi",
				DeploySource: v1.PostApplicationBodyComponentDeploySource{
					ContainerRegistry: &v1.PostApplicationBodyComponentDeploySourceContainerRegistry{
						Image: "apprun-test.sakuracr.jp/apprun/test1:latest",
					},
				},
				Probe: &v1.PostApplicationBodyComponentProbe{
					HttpGet: &v1.PostApplicationBodyComponentProbeHttpGet{
						Path: "/",
						Port: 80,
					},
				},
			},
		},
	})
	if err != nil {
		panic(err)
	}

	// アプリケーションバージョンを取得
	versionOp := apprun.NewVersionOp(client)
	versions, err := versionOp.List(ctx, *application.Id, &v1.ListApplicationVersionsParams{})
	if err != nil {
		panic(err)
	}

	// アプリケーションの削除
	defer func() {
		if err := appOp.Delete(ctx, *application.Id); err != nil {
			panic(err)
		}
	}()

	v := (*versions.Data)[0]
	fmt.Println(*v.Name)
}

⚠ v1.0に達するまでは互換性のない形で変更される可能性がありますのでご注意ください。

License

apprun-api-go Copyright (C) 2022-2023 The sacloud/apprun-api-go authors. This project is published under Apache 2.0 License.

Documentation

Overview

Example (ApplicationAPI)

Example_applicationAPI アプリケーションAPIの利用例

package main

import (
	"context"
	"fmt"

	apprun "github.com/sacloud/apprun-api-go"
	v1 "github.com/sacloud/apprun-api-go/apis/v1"
)

const defaultServerURL = "https://secure.sakura.ad.jp/cloud/api/apprun/1.0/apprun/api"

var serverURL = defaultServerURL

func main() {
	client := &apprun.Client{
		APIRootURL: serverURL, // 省略可能
	}

	// アプリケーションの作成
	ctx := context.Background()
	appOp := apprun.NewApplicationOp(client)

	application, err := appOp.Create(ctx, &v1.PostApplicationBody{
		Name:           "example-app",
		TimeoutSeconds: 100,
		Port:           80,
		MinScale:       0,
		MaxScale:       1,
		Components: []v1.PostApplicationBodyComponent{
			{
				Name:      "component1",
				MaxCpu:    "0.1",
				MaxMemory: "256Mi",
				DeploySource: v1.PostApplicationBodyComponentDeploySource{
					ContainerRegistry: &v1.PostApplicationBodyComponentDeploySourceContainerRegistry{
						Image: "apprun-test.sakuracr.jp/apprun/test1:latest",
					},
				},
				Probe: &v1.PostApplicationBodyComponentProbe{
					HttpGet: &v1.PostApplicationBodyComponentProbeHttpGet{
						Path: "/",
						Port: 80,
					},
				},
			},
		},
	})
	if err != nil {
		panic(err)
	}

	// アプリケーションの参照
	application, err = appOp.Read(ctx, application.Id)
	if err != nil {
		panic(err)
	}

	// アプリケーションの削除
	err = appOp.Delete(ctx, application.Id)
	if err != nil {
		panic(err)
	}

	fmt.Println(application.Name)
}
Output:

example-app
Example (TrafficAPI)

Example_trafficAPI アプリケーショントラフィックAPIの利用例

package main

import (
	"context"
	"fmt"

	apprun "github.com/sacloud/apprun-api-go"
	v1 "github.com/sacloud/apprun-api-go/apis/v1"
)

const defaultServerURL = "https://secure.sakura.ad.jp/cloud/api/apprun/1.0/apprun/api"

var serverURL = defaultServerURL

func main() {
	client := &apprun.Client{
		APIRootURL: serverURL, // 省略可能
	}

	// アプリケーションの作成
	ctx := context.Background()
	appOp := apprun.NewApplicationOp(client)
	versionOp := apprun.NewVersionOp(client)
	trafficOp := apprun.NewTrafficOp(client)

	application, err := appOp.Create(ctx, &v1.PostApplicationBody{
		Name:           "example-app",
		TimeoutSeconds: 100,
		Port:           80,
		MinScale:       0,
		MaxScale:       1,
		Components: []v1.PostApplicationBodyComponent{
			{
				Name:      "component1",
				MaxCpu:    "0.1",
				MaxMemory: "256Mi",
				DeploySource: v1.PostApplicationBodyComponentDeploySource{
					ContainerRegistry: &v1.PostApplicationBodyComponentDeploySourceContainerRegistry{
						Image: "apprun-test.sakuracr.jp/apprun/test1:latest",
					},
				},
				Probe: &v1.PostApplicationBodyComponentProbe{
					HttpGet: &v1.PostApplicationBodyComponentProbeHttpGet{
						Path: "/",
						Port: 80,
					},
				},
			},
		},
	})
	if err != nil {
		panic(err)
	}

	// アプリケーションの更新
	timeoutSeconds := 10
	_, err = appOp.Update(ctx, application.Id, &v1.PatchApplicationBody{
		TimeoutSeconds: &timeoutSeconds,
	})
	if err != nil {
		panic(err)
	}

	// バージョン一覧の取得
	versions, err := versionOp.List(ctx, application.Id, &v1.ListApplicationVersionsParams{})
	if err != nil {
		panic(err)
	}

	// トラフィック分散を更新
	v0IsLatestVersion := true
	v0Percent := 90

	v1Name := versions.Data[1].Name
	v1Percent := 10

	v0tr := &v1.Traffic{}
	if err := v0tr.FromTrafficWithLatestVersion(v1.TrafficWithLatestVersion{
		IsLatestVersion: v0IsLatestVersion,
		Percent:         v0Percent,
	}); err != nil {
		panic(err)
	}

	v1tr := &v1.Traffic{}
	if err := v1tr.FromTrafficWithVersionName(v1.TrafficWithVersionName{
		VersionName: v1Name,
		Percent:     v1Percent,
	}); err != nil {
		panic(err)
	}

	_, err = trafficOp.Update(ctx, application.Id, &[]v1.Traffic{*v0tr, *v1tr})
	if err != nil {
		panic(err)
	}

	// トラフィック分散を取得
	traffics, err := trafficOp.List(ctx, application.Id)
	if err != nil {
		panic(err)
	}

	for _, data := range traffics.Data {
		withLatest, _ := data.AsTrafficWithLatestVersion()

		if withLatest.IsLatestVersion == true {
			fmt.Printf("is_latest_version: %t, percent: %d", withLatest.IsLatestVersion, withLatest.Percent)
		}
	}
}
Output:

is_latest_version: true, percent: 90
Example (UserAPI)

Example_userAPI ユーザーAPIの利用例

package main

import (
	"context"
	"fmt"

	apprun "github.com/sacloud/apprun-api-go"
)

const defaultServerURL = "https://secure.sakura.ad.jp/cloud/api/apprun/1.0/apprun/api"

var serverURL = defaultServerURL

func main() {
	client := &apprun.Client{
		APIRootURL: serverURL, // 省略可能
	}

	// ユーザー情報の取得
	userOp := apprun.NewUserOp(client)
	res, err := userOp.Read(context.Background())
	if err != nil {
		panic(err)
	}

	fmt.Println(res.StatusCode)
}
Output:

200
Example (VersionAPI)

Example_versionAPI アプリケーションバージョンAPIの利用例

package main

import (
	"context"
	"fmt"

	apprun "github.com/sacloud/apprun-api-go"
	v1 "github.com/sacloud/apprun-api-go/apis/v1"
)

const defaultServerURL = "https://secure.sakura.ad.jp/cloud/api/apprun/1.0/apprun/api"

var serverURL = defaultServerURL

func main() {
	client := &apprun.Client{
		APIRootURL: serverURL, // 省略可能
	}

	// アプリケーションの作成
	ctx := context.Background()
	appOp := apprun.NewApplicationOp(client)
	versionOp := apprun.NewVersionOp(client)

	application, err := appOp.Create(ctx, &v1.PostApplicationBody{
		Name:           "example-app",
		TimeoutSeconds: 100,
		Port:           80,
		MinScale:       0,
		MaxScale:       1,
		Components: []v1.PostApplicationBodyComponent{
			{
				Name:      "component1",
				MaxCpu:    "0.1",
				MaxMemory: "256Mi",
				DeploySource: v1.PostApplicationBodyComponentDeploySource{
					ContainerRegistry: &v1.PostApplicationBodyComponentDeploySourceContainerRegistry{
						Image: "apprun-test.sakuracr.jp/apprun/test1:latest",
					},
				},
				Probe: &v1.PostApplicationBodyComponentProbe{
					HttpGet: &v1.PostApplicationBodyComponentProbeHttpGet{
						Path: "/",
						Port: 80,
					},
				},
			},
		},
	})
	if err != nil {
		panic(err)
	}

	// アプリケーションの更新
	timeoutSeconds := 10
	_, err = appOp.Update(ctx, application.Id, &v1.PatchApplicationBody{
		TimeoutSeconds: &timeoutSeconds,
	})
	if err != nil {
		panic(err)
	}

	// バージョン一覧の取得
	versions, err := versionOp.List(ctx, application.Id, &v1.ListApplicationVersionsParams{})
	if err != nil {
		panic(err)
	}
	if len(versions.Data) != 2 {
		fmt.Println(len(versions.Data))
		panic("ListVersions failed")
	}

	d0 := versions.Data[0]
	d1 := versions.Data[1]

	// バージョンの削除
	err = versionOp.Delete(ctx, application.Id, d0.Id)
	if err != nil {
		panic(err)
	}

	// バージョンの参照
	version, err := versionOp.Read(ctx, application.Id, d1.Id)
	if err != nil {
		panic(err)
	}

	fmt.Printf("version status: %s", version.Status)
}
Output:

version status: Healthy

Index

Examples

Constants

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

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

Variables

View Source
var (
	// Version app version
	Version = "v0.4.0"
	// Revision git commit short commit hash
	Revision = "xxxxxx" // set on build time
)

コンポーネントの最大CPU数

コンポーネントの最大メモリ

ソート順

アプリケーションステータス

View Source
var UserAgent = fmt.Sprintf(
	"apprun-api-go/%s (%s/%s; +https://github.com/sacloud/apprun-api-go) %s",
	Version,
	runtime.GOOS,
	runtime.GOARCH,
	client.DefaultUserAgent,
)

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

ソート順

バージョンステータス

Functions

This section is empty.

Types

type ApplicationAPI

type ApplicationAPI interface {
	// List アプリケーション一覧を取得
	List(ctx context.Context, params *v1.ListApplicationsParams) (*v1.HandlerListApplications, error)
	// Create アプリケーションを作成
	Create(ctx context.Context, params *v1.PostApplicationBody) (*v1.Application, error)
	// Read アプリケーション詳細を取得
	Read(ctx context.Context, id string) (*v1.Application, error)
	// Update アプリケーションを部分的に変更
	Update(ctx context.Context, id string, params *v1.PatchApplicationBody) (*v1.HandlerPatchApplication, error)
	// Delete アプリケーションを削除
	Delete(ctx context.Context, id string) error
	// ReadStatus アプリケーションステータスを取得
	ReadStatus(ctx context.Context, id string) (*v1.HandlerGetApplicationStatusResponse, error)
}

func NewApplicationOp

func NewApplicationOp(client *Client) ApplicationAPI

NewApplicationOp アプリケーション操作関連API

type Client

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

	// Token APIキー: トークン
	Token string
	// Token APIキー: シークレット
	Secret string

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

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

	// DisableProfile usacloud互換プロファイルからの設定読み取りを無効化
	DisableProfile bool
	// DisableEnv 環境変数からの設定読み取りを無効化
	DisableEnv bool
	// contains filtered or unexported fields
}

Client APIクライアント

type PacketFilterAPI added in v0.4.0

type PacketFilterAPI interface {
	// Read パケットフィルタ詳細を取得
	Read(ctx context.Context, appId string) (*v1.HandlerGetPacketFilter, error)
	// Update パケットフィルタを部分的に変更
	Update(ctx context.Context, appId string, params *v1.PatchPacketFilter) (*v1.HandlerPatchPacketFilter, error)
}

func NewPacketFilterOp added in v0.4.0

func NewPacketFilterOp(client *Client) PacketFilterAPI

NewPacketFilterOp アプリケーショントラフィック分散関連API

type TrafficAPI

type TrafficAPI interface {
	// List アプリケーショントラフィック分散を取得
	List(ctx context.Context, appId string) (*v1.HandlerListTraffics, error)
	// Update アプリケーショントラフィック分散を変更
	Update(ctx context.Context, appId string, params *v1.PutApplicationTrafficJSONRequestBody) (*v1.HandlerPutTraffics, error)
}

func NewTrafficOp

func NewTrafficOp(client *Client) TrafficAPI

NewTrafficOp アプリケーショントラフィック分散関連API

type UserAPI

type UserAPI interface {
	// Read ログイン中のユーザー情報を取得
	Read(ctx context.Context) (*http.Response, error)
	// Create さくらのAppRunにサインアップ
	Create(ctx context.Context) (*http.Response, error)
}

func NewUserOp

func NewUserOp(client *Client) UserAPI

NewUserOp ユーザー操作関連API

type VersionAPI

type VersionAPI interface {
	// List アプリケーションバージョン一覧を取得
	List(ctx context.Context, appId string, params *v1.ListApplicationVersionsParams) (*v1.HandlerListVersions, error)
	// Read アプリケーションバージョン詳細を取得
	Read(ctx context.Context, appId, versionId string) (*v1.HandlerGetVersion, error)
	// Delete アプリケーションバージョンを削除
	Delete(ctx context.Context, appId, versionId string) error
}

func NewVersionOp

func NewVersionOp(client *Client) VersionAPI

NewVersionOp アプリケーションバージョン操作関連API

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