apprun

package module
v0.8.2 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2026 License: Apache-2.0 Imports: 8 Imported by: 1

README

sacloud/apprun-api-go

Go Reference Tests Go Report Card

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

AppRun共用型ドキュメント: https://manual.sakura.ad.jp/cloud/manual-sakura-apprun.html AppRun共用型 APIドキュメント: https://manual.sakura.ad.jp/api/cloud/portal/?api=apprun-shared-api

概要

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"
	"github.com/sacloud/saclient-go"
)

func main() {
	// デフォルトでusacloud互換プロファイル or 環境変数(SAKURA_ACCESS_TOKEN{_SECRET})が利用される
	var theClient saclient.Client
	client, err := apprun.NewClient(&theClient)
	if err != nil {
		panic(err)
	}

	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.PostApplicationBodyComponentsItem{
			{
				Name:      "component1",
				MaxCPU:    v1.PostApplicationBodyComponentsItemMaxCPU05,
				MaxMemory: v1.PostApplicationBodyComponentsItemMaxMemory1Gi,
				DeploySource: v1.PostApplicationBodyComponentsItemDeploySource{
					ContainerRegistry: v1.NewOptPostApplicationBodyComponentsItemDeploySourceContainerRegistry(
						v1.PostApplicationBodyComponentsItemDeploySourceContainerRegistry{
							Image: "sakura-oss-dev.sakuracr.jp/test:latest",
						},
					),
				},
				Probe: v1.NewOptNilPostApplicationBodyComponentsItemProbe(
					v1.PostApplicationBodyComponentsItemProbe{
						HTTPGet: v1.NewOptNilPostApplicationBodyComponentsItemProbeHTTPGet(
							v1.PostApplicationBodyComponentsItemProbeHTTPGet{
								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に達するまでは互換性のない形で変更される可能性がありますのでご注意ください。

apprun-api-goはv0.8からogenベースの実装となっています。oapi-codegenベースの実装を使いたい場合にはv0.7系を使ってください。ただし新機能は追加されないため、新規APIを利用したい場合には移行が必要となります。

License

apprun-api-go Copyright (C) 2021-2026 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"
	"github.com/sacloud/saclient-go"
)

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

var serverURL = defaultServerURL

func main() {
	var theClient saclient.Client
	client, err := apprun.NewClientWithAPIRootURL(&theClient, serverURL)
	if err != nil {
		panic(err)
	}

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

	created, err := appOp.Create(ctx, &v1.PostApplicationBody{
		Name:           "example-app",
		TimeoutSeconds: 100,
		Port:           80,
		MinScale:       0,
		MaxScale:       1,
		Components: []v1.PostApplicationBodyComponentsItem{
			{
				Name:      "component1",
				MaxCPU:    v1.PostApplicationBodyComponentsItemMaxCPU05,
				MaxMemory: v1.PostApplicationBodyComponentsItemMaxMemory1Gi,
				DeploySource: v1.PostApplicationBodyComponentsItemDeploySource{
					ContainerRegistry: v1.NewOptPostApplicationBodyComponentsItemDeploySourceContainerRegistry(
						v1.PostApplicationBodyComponentsItemDeploySourceContainerRegistry{
							Image:    "apprun-test.sakuracr.jp/apprun/test1:latest",
							Server:   v1.NewOptNilString("apprun-test.sakuracr.jp"),
							Username: v1.NewOptNilString("test-user"),
							Password: v1.NewOptNilString("test-password"),
						},
					),
				},
				Probe: v1.NewOptNilPostApplicationBodyComponentsItemProbe(
					v1.PostApplicationBodyComponentsItemProbe{
						HTTPGet: v1.NewOptNilPostApplicationBodyComponentsItemProbeHTTPGet(
							v1.PostApplicationBodyComponentsItemProbeHTTPGet{
								Path: "/",
								Port: 80,
							},
						),
					},
				),
			},
		},
	})
	if err != nil {
		panic(err)
	}

	// アプリケーションの参照
	application, err := appOp.Read(ctx, created.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"
	"github.com/sacloud/saclient-go"
)

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

var serverURL = defaultServerURL

func main() {
	var theClient saclient.Client
	client, err := apprun.NewClientWithAPIRootURL(&theClient, serverURL)
	if err != nil {
		panic(err)
	}

	// アプリケーションの作成
	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.PostApplicationBodyComponentsItem{
			{
				Name:      "component1",
				MaxCPU:    v1.PostApplicationBodyComponentsItemMaxCPU05,
				MaxMemory: v1.PostApplicationBodyComponentsItemMaxMemory1Gi,
				DeploySource: v1.PostApplicationBodyComponentsItemDeploySource{
					ContainerRegistry: v1.NewOptPostApplicationBodyComponentsItemDeploySourceContainerRegistry(
						v1.PostApplicationBodyComponentsItemDeploySourceContainerRegistry{
							Image:    "apprun-test.sakuracr.jp/apprun/test1:latest",
							Server:   v1.NewOptNilString("apprun-test.sakuracr.jp"),
							Username: v1.NewOptNilString("test-user"),
							Password: v1.NewOptNilString("test-password"),
						},
					),
				},
				Probe: v1.NewOptNilPostApplicationBodyComponentsItemProbe(
					v1.PostApplicationBodyComponentsItemProbe{
						HTTPGet: v1.NewOptNilPostApplicationBodyComponentsItemProbeHTTPGet(
							v1.PostApplicationBodyComponentsItemProbeHTTPGet{
								Path: "/",
								Port: 80,
							},
						),
					},
				),
			},
		},
	})
	if err != nil {
		panic(err)
	}

	// アプリケーションの更新
	timeoutSeconds := 10
	_, err = appOp.Update(ctx, application.ID, &v1.PatchApplicationBody{
		TimeoutSeconds: v1.NewOptInt(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

	trafficBody := v1.PutTrafficsBody{
		v1.NewPutTrafficsBodyItem0PutTrafficsBodyItem(v1.PutTrafficsBodyItem0{
			IsLatestVersion: v0IsLatestVersion,
			Percent:         v0Percent,
		}),
		v1.NewPutTrafficsBodyItem1PutTrafficsBodyItem(v1.PutTrafficsBodyItem1{
			VersionName: v1Name,
			Percent:     v1Percent,
		}),
	}

	_, err = trafficOp.Update(ctx, application.ID, &trafficBody)
	if err != nil {
		panic(err)
	}

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

	for _, data := range traffics.Data {
		if data.IsLatestVersion {
			fmt.Printf("is_latest_version: %t, percent: %d", data.IsLatestVersion, data.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"
	"github.com/sacloud/saclient-go"
)

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

var serverURL = defaultServerURL

func main() {
	var theClient saclient.Client
	client, err := apprun.NewClientWithAPIRootURL(&theClient, serverURL)
	if err != nil {
		panic(err)
	}

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

	fmt.Println(res.Limit.ApplicationCount >= 0)
}
Output:
true
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"
	"github.com/sacloud/saclient-go"
)

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

var serverURL = defaultServerURL

func main() {
	var theClient saclient.Client
	client, err := apprun.NewClientWithAPIRootURL(&theClient, serverURL)
	if err != nil {
		panic(err)
	}

	// アプリケーションの作成
	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.PostApplicationBodyComponentsItem{
			{
				Name:      "component1",
				MaxCPU:    v1.PostApplicationBodyComponentsItemMaxCPU05,
				MaxMemory: v1.PostApplicationBodyComponentsItemMaxMemory1Gi,
				DeploySource: v1.PostApplicationBodyComponentsItemDeploySource{
					ContainerRegistry: v1.NewOptPostApplicationBodyComponentsItemDeploySourceContainerRegistry(
						v1.PostApplicationBodyComponentsItemDeploySourceContainerRegistry{
							Image:    "apprun-test.sakuracr.jp/apprun/test1:latest",
							Server:   v1.NewOptNilString("apprun-test.sakuracr.jp"),
							Username: v1.NewOptNilString("test-user"),
							Password: v1.NewOptNilString("test-password"),
						},
					),
				},
				Probe: v1.NewOptNilPostApplicationBodyComponentsItemProbe(
					v1.PostApplicationBodyComponentsItemProbe{
						HTTPGet: v1.NewOptNilPostApplicationBodyComponentsItemProbeHTTPGet(
							v1.PostApplicationBodyComponentsItemProbeHTTPGet{
								Path: "/",
								Port: 80,
							},
						),
					},
				),
			},
		},
	})
	if err != nil {
		panic(err)
	}

	// アプリケーションの更新
	timeoutSeconds := 10
	_, err = appOp.Update(ctx, application.ID, &v1.PatchApplicationBody{
		TimeoutSeconds: v1.NewOptInt(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 デフォルトのAPIルートURL
	DefaultAPIRootURL = "https://secure.sakura.ad.jp/cloud/api/apprun/1.0/apprun/api"
	ServiceKey        = "apprun_shared"
	OldServiceKey     = "apprun" // 互換性のためにapprunもチェックする
)

Variables

View Source
var (
	// Version app version
	Version = "v0.8.2"
	// 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)",
	Version,
	runtime.GOOS,
	runtime.GOARCH,
)

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

ソート順

バージョンステータス

Functions

func NewClient added in v0.8.0

func NewClient(client saclient.ClientAPI) (*v1.Client, error)

func NewClientWithAPIRootURL added in v0.8.0

func NewClientWithAPIRootURL(client saclient.ClientAPI, apiRootURL string) (*v1.Client, error)

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.HandlerPostApplication, error)
	// Read アプリケーション詳細を取得
	Read(ctx context.Context, id string) (*v1.HandlerGetApplication, 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.HandlerGetApplicationOnlyStatus, error)
}

func NewApplicationOp

func NewApplicationOp(client *v1.Client) ApplicationAPI

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

type Error added in v0.8.0

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

func NewAPIError added in v0.8.0

func NewAPIError(method string, code int, err error) *Error

func NewError added in v0.8.0

func NewError(msg string, err error) *Error

func (*Error) Error added in v0.8.0

func (e *Error) Error() string

func (*Error) Unwrap added in v0.8.0

func (e *Error) Unwrap() error

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 *v1.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.PutTrafficsBody) (*v1.HandlerPutTraffics, error)
}

func NewTrafficOp

func NewTrafficOp(client *v1.Client) TrafficAPI

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

type UserAPI

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

func NewUserOp

func NewUserOp(client *v1.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

	ReadStatus(ctx context.Context, appId, versionId string) (*v1.HandlerGetApplicationVersionOnlyStatus, error)
}

func NewVersionOp

func NewVersionOp(client *v1.Client) VersionAPI

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

Directories

Path Synopsis
apis
v1
Code generated by ogen, DO NOT EDIT.
Code generated by ogen, DO NOT EDIT.
cmd

Jump to

Keyboard shortcuts

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