1// Copyright 2016-2020 The Libsacloud Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package sacloud
16
17import (
18	"time"
19)
20
21// Resource IDを持つ、さくらのクラウド上のリソース
22type Resource struct {
23	ID ID // ID
24}
25
26// ResourceIDHolder ID保持インターフェース
27type ResourceIDHolder interface {
28	SetID(id ID)
29	GetID() ID
30}
31
32// EmptyID 空ID
33const EmptyID = ID(0)
34
35// NewResource 新規リソース作成
36func NewResource(id ID) *Resource {
37	return &Resource{ID: id}
38}
39
40// NewResourceByStringID ID文字列からリソース作成
41func NewResourceByStringID(id string) *Resource {
42	return &Resource{ID: StringID(id)}
43}
44
45// SetID ID 設定
46func (n *Resource) SetID(id ID) {
47	n.ID = id
48}
49
50// GetID ID 取得
51func (n *Resource) GetID() ID {
52	if n == nil {
53		return EmptyID
54	}
55	return n.ID
56}
57
58// GetStrID 文字列でID取得
59func (n *Resource) GetStrID() string {
60	return n.ID.String()
61}
62
63// EAvailability 有効状態
64type EAvailability string
65
66var (
67	// EAAvailable 有効
68	EAAvailable = EAvailability("available")
69	// EAUploading アップロード中
70	EAUploading = EAvailability("uploading")
71	// EAFailed 失敗
72	EAFailed = EAvailability("failed")
73	// EAMigrating マイグレーション中
74	EAMigrating = EAvailability("migrating")
75)
76
77// IsAvailable 有効状態が"有効"か判定
78func (e EAvailability) IsAvailable() bool {
79	return e == EAAvailable
80}
81
82// IsUploading 有効状態が"アップロード中"か判定
83func (e EAvailability) IsUploading() bool {
84	return e == EAUploading
85}
86
87// IsFailed 有効状態が"失敗"か判定
88func (e EAvailability) IsFailed() bool {
89	return e == EAFailed
90}
91
92// IsMigrating 有効状態が"マイグレーション中"か判定
93func (e EAvailability) IsMigrating() bool {
94	return e == EAMigrating
95}
96
97// EInterfaceDriver インターフェースドライバ
98type EInterfaceDriver string
99
100var (
101	// InterfaceDriverVirtIO virtio
102	InterfaceDriverVirtIO = EInterfaceDriver("virtio")
103	// InterfaceDriverE1000 e1000
104	InterfaceDriverE1000 = EInterfaceDriver("e1000")
105)
106
107// EServerInstanceStatus サーバーインスタンスステータス
108type EServerInstanceStatus struct {
109	Status       string `json:",omitempty"` // 現在のステータス
110	BeforeStatus string `json:",omitempty"` // 前のステータス
111}
112
113// IsUp インスタンスが起動しているか判定
114func (e *EServerInstanceStatus) IsUp() bool {
115	return e.Status == "up"
116}
117
118// IsDown インスタンスがダウンしているか確認
119func (e *EServerInstanceStatus) IsDown() bool {
120	return e.Status == "down"
121}
122
123// GetStatus ステータス 取得
124func (e *EServerInstanceStatus) GetStatus() string {
125	return e.Status
126}
127
128// GetBeforeStatus 以前のステータス 取得
129func (e *EServerInstanceStatus) GetBeforeStatus() string {
130	return e.BeforeStatus
131}
132
133// EScope スコープ
134type EScope string
135
136var (
137	// ESCopeShared sharedスコープ
138	ESCopeShared = EScope("shared")
139	// ESCopeUser userスコープ
140	ESCopeUser = EScope("user")
141)
142
143// EDiskConnection ディスク接続方法
144type EDiskConnection string
145
146// EUpstreamNetworkType 上流ネットワーク種別
147type EUpstreamNetworkType string
148
149// String EUpstreamNetworkTypeの文字列表現
150func (t EUpstreamNetworkType) String() string {
151	return string(t)
152}
153
154var (
155	// EUpstreamNetworkUnknown 不明
156	EUpstreamNetworkUnknown = EUpstreamNetworkType("unknown")
157	// EUpstreamNetworkShared 共有セグメント
158	EUpstreamNetworkShared = EUpstreamNetworkType("shared")
159	// EUpstreamNetworkSwitch スイッチ(非スイッチ+ルータ)
160	EUpstreamNetworkSwitch = EUpstreamNetworkType("switch")
161	// EUpstreamNetworkRouter ルータ(スイッチ+ルータのスイッチ)
162	EUpstreamNetworkRouter = EUpstreamNetworkType("router")
163	// EUpstreamNetworkNone 接続なし
164	EUpstreamNetworkNone = EUpstreamNetworkType("none")
165
166	// UpstreamNetworks 文字列とEUpstreamNetworkTypeのマッピング
167	UpstreamNetworks = map[string]EUpstreamNetworkType{
168		"unknown": EUpstreamNetworkUnknown,
169		"shared":  EUpstreamNetworkShared,
170		"switch":  EUpstreamNetworkSwitch,
171		"router":  EUpstreamNetworkRouter,
172		"none":    EUpstreamNetworkNone,
173	}
174)
175
176// ECommitment サーバプランCPUコミットメント
177type ECommitment string
178
179var (
180	// ECommitmentStandard 通常
181	ECommitmentStandard = ECommitment("standard")
182	// ECommitmentDedicatedCPU コア専有
183	ECommitmentDedicatedCPU = ECommitment("dedicatedcpu")
184)
185
186// SakuraCloudResources さくらのクラウド上のリソース種別一覧
187type SakuraCloudResources struct {
188	Server          *Server             `json:",omitempty"`     // サーバー
189	Disk            *Disk               `json:",omitempty"`     // ディスク
190	Note            *Note               `json:",omitempty"`     // スタートアップスクリプト
191	Archive         *Archive            `json:",omitempty"`     // アーカイブ
192	PacketFilter    *PacketFilter       `json:",omitempty"`     // パケットフィルタ
193	PrivateHost     *PrivateHost        `json:",omitempty"`     // 専有ホスト
194	Bridge          *Bridge             `json:",omitempty"`     // ブリッジ
195	Icon            *Icon               `json:",omitempty"`     // アイコン
196	Image           *Image              `json:",omitempty"`     // 画像
197	Interface       *Interface          `json:",omitempty"`     // インターフェース
198	Internet        *Internet           `json:",omitempty"`     // ルーター
199	IPAddress       *IPAddress          `json:",omitempty"`     // IPv4アドレス
200	IPv6Addr        *IPv6Addr           `json:",omitempty"`     // IPv6アドレス
201	IPv6Net         *IPv6Net            `json:",omitempty"`     // IPv6ネットワーク
202	License         *License            `json:",omitempty"`     // ライセンス
203	Switch          *Switch             `json:",omitempty"`     // スイッチ
204	CDROM           *CDROM              `json:",omitempty"`     // ISOイメージ
205	SSHKey          *SSHKey             `json:",omitempty"`     // 公開鍵
206	Subnet          *Subnet             `json:",omitempty"`     // IPv4ネットワーク
207	DiskPlan        *ProductDisk        `json:",omitempty"`     // ディスクプラン
208	InternetPlan    *ProductInternet    `json:",omitempty"`     // ルータープラン
209	LicenseInfo     *ProductLicense     `json:",omitempty"`     // ライセンス情報
210	ServerPlan      *ProductServer      `json:",omitempty"`     // サーバープラン
211	PrivateHostPlan *ProductPrivateHost `json:",omitempty"`     // 専有ホストプラン
212	Region          *Region             `json:",omitempty"`     // リージョン
213	Zone            *Zone               `json:",omitempty"`     // ゾーン
214	FTPServer       *FTPServer          `json:",omitempty"`     // FTPサーバー情報
215	WebAccelSite    *WebAccelSite       `json:"Site,omitempty"` // ウェブアクセラレータ サイト
216	//REMARK: CommonServiceItemとApplianceはapiパッケージにて別途定義
217}
218
219// SakuraCloudResourceList さくらのクラウド上のリソース種別一覧(複数形)
220type SakuraCloudResourceList struct {
221	Servers          []Server             `json:",omitempty"`      // サーバー
222	Disks            []Disk               `json:",omitempty"`      // ディスク
223	Notes            []Note               `json:",omitempty"`      // スタートアップスクリプト
224	Archives         []Archive            `json:",omitempty"`      // アーカイブ
225	PacketFilters    []PacketFilter       `json:",omitempty"`      // パケットフィルタ
226	PrivateHosts     []PrivateHost        `json:",omitempty"`      // 専有ホスト
227	Bridges          []Bridge             `json:",omitempty"`      // ブリッジ
228	Icons            []Icon               `json:",omitempty"`      // アイコン
229	Interfaces       []Interface          `json:",omitempty"`      // インターフェース
230	Internet         []Internet           `json:",omitempty"`      // ルーター
231	IPAddress        []IPAddress          `json:",omitempty"`      // IPv4アドレス
232	IPv6Addrs        []IPv6Addr           `json:",omitempty"`      // IPv6アドレス
233	IPv6Nets         []IPv6Net            `json:",omitempty"`      // IPv6ネットワーク
234	Licenses         []License            `json:",omitempty"`      // ライセンス
235	Switches         []Switch             `json:",omitempty"`      // スイッチ
236	CDROMs           []CDROM              `json:",omitempty"`      // ISOイメージ
237	SSHKeys          []SSHKey             `json:",omitempty"`      // 公開鍵
238	Subnets          []Subnet             `json:",omitempty"`      // IPv4ネットワーク
239	DiskPlans        []ProductDisk        `json:",omitempty"`      // ディスクプラン
240	InternetPlans    []ProductInternet    `json:",omitempty"`      // ルータープラン
241	LicenseInfo      []ProductLicense     `json:",omitempty"`      // ライセンス情報
242	ServerPlans      []ProductServer      `json:",omitempty"`      // サーバープラン
243	PrivateHostPlans []ProductPrivateHost `json:",omitempty"`      // 専有ホストプラン
244	Regions          []Region             `json:",omitempty"`      // リージョン
245	Zones            []Zone               `json:",omitempty"`      // ゾーン
246	ServiceClasses   []PublicPrice        `json:",omitempty"`      // サービスクラス(価格情報)
247	WebAccelSites    []WebAccelSite       `json:"Sites,omitempty"` // ウェブアクセラレータ サイト
248
249	//REMARK:CommonServiceItemとApplianceはapiパッケージにて別途定義
250}
251
252// Request APIリクエスト型
253//
254// FromとCountに0を指定するとページングが無効となる
255type Request struct {
256	SakuraCloudResources                        // さくらのクラウドリソース
257	From                 int                    // ページング FROM
258	Count                int                    // 取得件数
259	Sort                 []string               `json:",omitempty"` // ソート
260	Filter               map[string]interface{} `json:",omitempty"` // フィルタ
261	Exclude              []string               `json:",omitempty"` // 除外する項目
262	Include              []string               `json:",omitempty"` // 取得する項目
263	DistantFrom          []ID                   `json:",omitempty"` // ストレージ隔離対象ディスク
264}
265
266// AddFilter フィルタの追加
267func (r *Request) AddFilter(key string, value interface{}) *Request {
268	if r.Filter == nil {
269		r.Filter = map[string]interface{}{}
270	}
271	r.Filter[key] = value
272	return r
273}
274
275// AddSort ソートの追加
276func (r *Request) AddSort(keyName string) *Request {
277	if r.Sort == nil {
278		r.Sort = []string{}
279	}
280	r.Sort = append(r.Sort, keyName)
281	return r
282}
283
284// AddExclude 除外対象の追加
285func (r *Request) AddExclude(keyName string) *Request {
286	if r.Exclude == nil {
287		r.Exclude = []string{}
288	}
289	r.Exclude = append(r.Exclude, keyName)
290	return r
291}
292
293// AddInclude 選択対象の追加
294func (r *Request) AddInclude(keyName string) *Request {
295	if r.Include == nil {
296		r.Include = []string{}
297	}
298	r.Include = append(r.Include, keyName)
299	return r
300}
301
302// ResultFlagValue レスポンス値でのフラグ項目
303type ResultFlagValue struct {
304	IsOk    bool `json:"is_ok,omitempty"` // is_ok項目
305	Success bool `json:",omitempty"`      // success項目
306}
307
308// SearchResponse 検索レスポンス
309type SearchResponse struct {
310	Total                    int        `json:",omitempty"` // トータル件数
311	From                     int        `json:",omitempty"` // ページング開始ページ
312	Count                    int        `json:",omitempty"` // 件数
313	ResponsedAt              *time.Time `json:",omitempty"` // 応答日時
314	*SakuraCloudResourceList            // さくらのクラウドリソース(複数形)
315}
316
317// Response レスポンス型
318type Response struct {
319	*ResultFlagValue      // フラグ値
320	*SakuraCloudResources // さくらのクラウドリソース(単数形)
321}
322
323// ResultErrorValue レスポンスエラー型
324type ResultErrorValue struct {
325	IsFatal      bool   `json:"is_fatal,omitempty"`   // IsFatal
326	Serial       string `json:"serial,omitempty"`     // Serial
327	Status       string `json:"status,omitempty"`     // Status
328	ErrorCode    string `json:"error_code,omitempty"` // ErrorCode
329	ErrorMessage string `json:"error_msg,omitempty"`  // ErrorMessage
330
331}
332
333// MigrationJobStatus マイグレーションジョブステータス
334type MigrationJobStatus struct {
335	Status      string          `json:",omitempty"` // ステータス
336	ConfigError *JobConfigError `json:",omitempty"`
337	Delays      *struct {       // Delays
338		Start *struct { // 開始
339			Max int `json:",omitempty"` // 最大
340			Min int `json:",omitempty"` // 最小
341		} `json:",omitempty"`
342
343		Finish *struct { // 終了
344			Max int `json:",omitempty"` // 最大
345			Min int `json:",omitempty"` // 最小
346		} `json:",omitempty"`
347	}
348}
349
350// JobConfigError マイグレーションジョブのエラー
351type JobConfigError struct {
352	ErrorCode string `json:",omitempty"`
353	ErrorMsg  string `json:",omitempty"`
354	Status    string `json:",omitempty"`
355}
356
357var (
358	// TagGroupA サーバをグループ化し起動ホストを分離します(グループA)
359	TagGroupA = "@group=a"
360	// TagGroupB サーバをグループ化し起動ホストを分離します(グループB)
361	TagGroupB = "@group=b"
362	// TagGroupC サーバをグループ化し起動ホストを分離します(グループC)
363	TagGroupC = "@group=b"
364	// TagGroupD サーバをグループ化し起動ホストを分離します(グループD)
365	TagGroupD = "@group=b"
366
367	// TagAutoReboot サーバ停止時に自動起動します
368	TagAutoReboot = "@auto-reboot"
369
370	// TagKeyboardUS リモートスクリーン画面でUSキーボード入力します
371	TagKeyboardUS = "@keyboard-us"
372
373	// TagBootCDROM 優先ブートデバイスをCD-ROMに設定します
374	TagBootCDROM = "@boot-cdrom"
375	// TagBootNetwork 優先ブートデバイスをPXE bootに設定します
376	TagBootNetwork = "@boot-network"
377
378	// TagCPUTopology CPUソケット数を1と認識させる
379	TagCPUTopology = "@cpu-topology"
380)
381
382// DatetimeLayout さくらのクラウドAPIで利用される日付型のレイアウト(RFC3339)
383var DatetimeLayout = "2006-01-02T15:04:05-07:00"
384
385// PlanGenerations サーバプラン世代
386type PlanGenerations int
387
388var (
389	// PlanDefault デフォルト
390	PlanDefault = PlanGenerations(0)
391	// PlanG1 第1世代(Generation:100)
392	PlanG1 = PlanGenerations(100)
393	// PlanG2 第2世代(Generation:200)
394	PlanG2 = PlanGenerations(200)
395)
396