customize_server.go raw
1 // Copyright 2022-2025 The sacloud/iaas-api-go 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
15 package iaas
16
17 import "github.com/sacloud/iaas-api-go/types"
18
19 // BandWidthAt 指定インデックスのNICの帯域幅を算出
20 //
21 // 不明な場合は-1を、制限なしの場合は0を、以外の場合はMbps単位で返す
22 func (o *Server) BandWidthAt(index int) int {
23 if len(o.Interfaces) <= index {
24 return -1
25 }
26
27 nic := o.Interfaces[index]
28
29 switch nic.UpstreamType {
30 case types.UpstreamNetworkTypes.None:
31 return -1
32 case types.UpstreamNetworkTypes.Shared:
33 return 100
34 case types.UpstreamNetworkTypes.Switch, types.UpstreamNetworkTypes.Router:
35 //
36 // 上流ネットワークがスイッチだった場合の帯域制限
37 // https://manual.sakura.ad.jp/cloud/support/technical/network.html#support-network-03
38 //
39
40 // 専有ホストの場合は制限なし
41 if !o.PrivateHostID.IsEmpty() {
42 return 0
43 }
44
45 // メモリに応じた制限
46 memory := o.GetMemoryGB()
47 switch {
48 case memory < 32:
49 return 1000
50 case 32 <= memory && memory < 128:
51 return 2000
52 case 128 <= memory && memory < 224:
53 return 5000
54 case 224 <= memory:
55 return 10000
56 default:
57 return -1
58 }
59 default:
60 return -1
61 }
62 }
63
64 // GetInstanceStatus データベース(サービス)ステータスを返すためのアダプター実装
65 // PostgreSQLまたはMariaDBのステータス(詳細は以下)をInstanceStatusにラップして返す
66 //
67 // ステータス: GET /appliance/:id/status -> Appliance.ResponseStatus.DBConf.{MariaDB | postgres}.status
68 //
69 // 主にStateWaiterで利用する。
70 func (o *DatabaseStatus) GetInstanceStatus() types.EServerInstanceStatus {
71 if o.MariaDBStatus == "running" || o.PostgresStatus == "running" {
72 return types.ServerInstanceStatuses.Up
73 }
74 return types.ServerInstanceStatuses.Unknown
75 }
76
77 // SetInstanceStatus データベース(サービス)ステータスを返すためのアダプター実装
78 // accessor.InstanceStatusを満たすためのスタブ実装
79 func (o *DatabaseStatus) SetInstanceStatus(types.EServerInstanceStatus) {
80 // noop
81 }
82