-
Notifications
You must be signed in to change notification settings - Fork 13
/
database.go
342 lines (294 loc) · 13.9 KB
/
database.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Copyright 2016-2022 The Libsacloud Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package naked
import (
"encoding/json"
"strconv"
"strings"
"time"
"github.com/sacloud/libsacloud/v2/sacloud/types"
)
// Database データベース
type Database struct {
ID types.ID `json:",omitempty" yaml:"id,omitempty" structs:",omitempty"`
Name string `json:",omitempty" yaml:"name,omitempty" structs:",omitempty"`
Description string `yaml:"description"`
Tags types.Tags `yaml:"tags"`
Icon *Icon `json:",omitempty" yaml:"icon,omitempty" structs:",omitempty"`
CreatedAt *time.Time `json:",omitempty" yaml:"created_at,omitempty" structs:",omitempty"`
ModifiedAt *time.Time `json:",omitempty" yaml:"modified_at,omitempty" structs:",omitempty"`
Availability types.EAvailability `json:",omitempty" yaml:"availability,omitempty" structs:",omitempty"`
Class string `json:",omitempty" yaml:"class,omitempty" structs:",omitempty"`
ServiceClass string `json:",omitempty" yaml:"service_class,omitempty" structs:",omitempty"`
Plan *AppliancePlan `json:",omitempty" yaml:"plan,omitempty" structs:",omitempty"`
Instance *Instance `json:",omitempty" yaml:"instance,omitempty" structs:",omitempty"`
Interfaces []*Interface `json:",omitempty" yaml:"interfaces,omitempty" structs:",omitempty"`
Switch *Switch `json:",omitempty" yaml:"switch,omitempty" structs:",omitempty"`
Settings *DatabaseSettings `json:",omitempty" yaml:"settings,omitempty" structs:",omitempty"`
SettingsHash string `json:",omitempty" yaml:"settings_hash,omitempty" structs:",omitempty"`
Remark *ApplianceRemark `json:",omitempty" yaml:"remark,omitempty" structs:",omitempty"`
Generation interface{}
}
// DatabaseSettingsUpdate データベース
type DatabaseSettingsUpdate struct {
Settings *DatabaseSettings `json:",omitempty" yaml:"settings,omitempty" structs:",omitempty"`
SettingsHash string `json:",omitempty" yaml:"settings_hash,omitempty" structs:",omitempty"`
}
// DatabaseSettings データベース設定
type DatabaseSettings struct {
DBConf *DatabaseSetting `json:",omitempty" yaml:"db_conf,omitempty" structs:",omitempty"`
}
// DatabaseSetting データベース設定
type DatabaseSetting struct {
Common *DatabaseSettingCommon `json:",omitempty" yaml:"common,omitempty" structs:",omitempty"`
Backup *DatabaseSettingBackup `json:",omitempty" yaml:"backup,omitempty" structs:",omitempty"`
Replication *DatabaseSettingReplication `json:",omitempty" yaml:"replication,omitempty" structs:",omitempty"`
}
// DatabaseSettingCommon データベース設定 汎用項目設定
type DatabaseSettingCommon struct {
// WebUI WebUIの有効/無効、またはアクセスするためのアドレス
//
// [HACK] Create時はbool型、Read/Update時は文字列(FQDN or IP)となる。
// また、無効にするにはJSONで要素自体を指定しないことで行う。
WebUI interface{} `yaml:"web_ui"`
ServicePort int `json:",omitempty" yaml:"service_port,omitempty" structs:",omitempty"`
SourceNetwork DatabaseSettingSourceNetworks `yaml:"source_network"`
DefaultUser string `json:",omitempty" yaml:"default_user,omitempty" structs:",omitempty"`
UserPassword string `json:",omitempty" yaml:"user_password,omitempty" structs:",omitempty"`
ReplicaUser string `json:",omitempty" yaml:"replica_user,omitempty" structs:",omitempty"`
ReplicaPassword string `json:",omitempty" yaml:"replica_password,omitempty" structs:",omitempty"`
}
// DatabaseSettingSourceNetworks データベースへのアクセスを許可するCIDRリスト
//
// Note: すべての接続先を許可する場合は"0.0.0.0/0"を指定する。
// この処理はMarshalJSON時にDatabaseSettingSourceNetwork側で行われるため、
// APIクライアント側は許可したいCIDRブロックのリストを指定する。
// libsacloudではすべての接続を拒否する設定はサポートしない。
type DatabaseSettingSourceNetworks []string
// MarshalJSON すべての接続先を許可する場合は"0.0.0.0/0"を指定するための対応
func (d DatabaseSettingSourceNetworks) MarshalJSON() ([]byte, error) {
type alias DatabaseSettingSourceNetworks
dest := alias(d)
if dest == nil || len(dest) == 0 {
dest = append(dest, "0.0.0.0/0")
}
return json.Marshal(dest)
}
func (d *DatabaseSettingSourceNetworks) UnmarshalJSON(b []byte) error {
if string(b) == `""` || string(b) == "" {
return nil
}
type alias DatabaseSettingSourceNetworks
var a alias
if err := json.Unmarshal(b, &a); err != nil {
return err
}
if len(a) == 1 && a[0] == "0.0.0.0/0" {
return nil
}
*d = DatabaseSettingSourceNetworks(a)
return nil
}
// DatabaseSettingBackup データベース設定 バックアップ設定
type DatabaseSettingBackup struct {
Rotate int `json:",omitempty" yaml:"rotate,omitempty" structs:",omitempty"`
Time string `json:",omitempty" yaml:"time,omitempty" structs:",omitempty"`
DayOfWeek []types.EBackupSpanWeekday `json:",omitempty" yaml:"day_of_week,omitempty" structs:",omitempty"`
}
// UnmarshalJSON 配列/オブジェクトが混在することへの対応
func (d *DatabaseSettingBackup) UnmarshalJSON(b []byte) error {
if string(b) == "[]" {
return nil
}
type alias DatabaseSettingBackup
var a alias
if err := json.Unmarshal(b, &a); err != nil {
return err
}
*d = DatabaseSettingBackup(a)
return nil
}
// DatabaseSettingReplication レプリケーション設定
type DatabaseSettingReplication struct {
Model types.EDatabaseReplicationModel `json:",omitempty" yaml:"model,omitempty" structs:",omitempty"`
Appliance *struct {
ID types.ID
} `json:",omitempty" yaml:"appliance,omitempty" structs:",omitempty"`
IPAddress string `json:",omitempty" yaml:"ip_address,omitempty" structs:",omitempty"`
Port int `json:",omitempty" yaml:"port,omitempty" structs:",omitempty"`
User string `json:",omitempty" yaml:"user,omitempty" structs:",omitempty"`
Password string `json:",omitempty" yaml:"password,omitempty" structs:",omitempty"`
}
// DatabaseStatusResponse Status APIの戻り値
type DatabaseStatusResponse struct {
SettingsResponse *DatabaseStatus `json:",omitempty" yaml:"settings_response,omitempty" structs:",omitempty"`
}
// DatabaseStatus データベースのステータス
type DatabaseStatus struct {
Status types.EServerInstanceStatus `json:",omitempty" yaml:"status,omitempty" structs:",omitempty"`
IsFatal bool `json:"is_fatal"`
DBConf *DatabaseStatusDBConf `json:",omitempty" yaml:"db_conf,omitempty" structs:",omitempty"`
}
// DatabaseStatusDBConf データベース設定
type DatabaseStatusDBConf struct {
Version *DatabaseStatusVersion `json:"version,omitempty" yaml:"version,omitempty" structs:",omitempty"`
Log []*DatabaseLog `json:"log,omitempty" yaml:"log,omitempty" structs:",omitempty"`
Backup *DatabaseBackupInfo `json:"backup,omitempty" yaml:"backup,omitempty" structs:",omitempty"`
MariaDB *DatabaseStatusMariaDB `json:",omitempty" yaml:"maria_db,omitempty" structs:",omitempty"`
Postgres *DatabaseStatusPostgreSQL `json:"postgres,omitempty" yaml:"postgres,omitempty" structs:",omitempty"`
// 以下フィールドはサポートしない
// Replication
}
type DatabaseStatusMariaDB struct {
Status string `json:"status,omitempty"`
}
type DatabaseStatusPostgreSQL struct {
Status string `json:"status,omitempty"`
}
// DatabaseStatusVersion データベース設定バージョン情報
type DatabaseStatusVersion struct {
LastModified string `json:"lastmodified,omitempty" yaml:"last_modified,omitempty" structs:",omitempty"`
CommitHash string `json:"commithash,omitempty" yaml:"commit_hash,omitempty" structs:",omitempty"`
Status string `json:"status,omitempty" yaml:"status,omitempty" structs:",omitempty"`
Tag string `json:"tag,omitempty" yaml:"tag,omitempty" structs:",omitempty"`
Expire string `json:"expire,omitempty" yaml:"expire,omitempty" structs:",omitempty"`
}
// DatabaseLog データベースログ
type DatabaseLog struct {
Name string `json:"name,omitempty" yaml:"name,omitempty" structs:",omitempty"`
Data string `json:"data,omitempty" yaml:"data,omitempty" structs:",omitempty"`
Size types.StringNumber `json:"size,omitempty" yaml:"size,omitempty" structs:",omitempty"`
}
// IsSystemdLog systemcltのログか判定
func (l *DatabaseLog) IsSystemdLog() bool {
return l.Name == "systemctl"
}
// Logs ログボディ取得
func (l *DatabaseLog) Logs() []string {
return strings.Split(l.Data, "\n")
}
// ID ログのID取得
func (l *DatabaseLog) ID() string {
return l.Name
}
// DatabaseBackupInfo データベースバックアップ情報
type DatabaseBackupInfo struct {
History []*DatabaseBackupHistory `json:"history,omitempty" yaml:"history,omitempty" structs:",omitempty"`
}
// DatabaseBackupHistory データベースバックアップ履歴情報
type DatabaseBackupHistory struct {
CreatedAt time.Time `json:"createdat,omitempty" yaml:"created_at,omitempty" structs:",omitempty"`
Availability string `json:"availability,omitempty" yaml:"availability,omitempty" structs:",omitempty"`
RecoveredAt *time.Time `json:"recoveredat,omitempty" yaml:"recovered_at,omitempty" structs:",omitempty"`
Size int64 `json:"size,omitempty" yaml:"size,omitempty" structs:",omitempty"`
}
// ID バックアップ履歴のID取得
func (h *DatabaseBackupHistory) ID() string {
return h.CreatedAt.Format(time.RFC3339)
}
// FormatCreatedAt 指定のレイアウトで作成日時を文字列化
func (h *DatabaseBackupHistory) FormatCreatedAt(layout string) string {
return h.CreatedAt.Format(layout)
}
// FormatRecoveredAt 指定のレイアウトで復元日時を文字列化
//
// 復元日時がnilの場合は空の文字列を返す
func (h *DatabaseBackupHistory) FormatRecoveredAt(layout string) string {
if h.RecoveredAt == nil {
return ""
}
return h.RecoveredAt.Format(layout)
}
// UnmarshalJSON JSON復号処理
func (h *DatabaseBackupHistory) UnmarshalJSON(data []byte) error {
var tmpMap = map[string]interface{}{}
if err := json.Unmarshal(data, &tmpMap); err != nil {
return err
}
if recoveredAt, ok := tmpMap["recoveredat"]; ok {
if strRecoveredAt, ok := recoveredAt.(string); ok {
if _, err := time.Parse(time.RFC3339, strRecoveredAt); err != nil {
tmpMap["recoveredat"] = nil
}
}
}
data, err := json.Marshal(tmpMap)
if err != nil {
return err
}
tmp := &struct {
CreatedAt time.Time `json:"createdat,omitempty"`
Availability string `json:"availability,omitempty"`
RecoveredAt *time.Time `json:"recoveredat,omitempty"`
Size string `json:"size,omitempty"`
}{}
if err := json.Unmarshal(data, &tmp); err != nil {
return err
}
h.CreatedAt = tmp.CreatedAt
h.Availability = tmp.Availability
h.RecoveredAt = tmp.RecoveredAt
s, err := strconv.ParseInt(tmp.Size, 10, 64)
if err == nil {
h.Size = s
} else {
return err
}
return nil
}
// DatabaseParameter RDBMSごとに固有のパラメータ設定
type DatabaseParameter struct {
Parameter *DatabaseParameterSetting `json:",omitempty" yaml:",omitempty" structs:",omitempty"`
Remark *DatabaseParameterRemark `json:",omitempty" yaml:",omitempty" structs:",omitempty"`
}
type DatabaseParameterSetting struct {
NoteID types.ID `json:",omitempty" yaml:"note_id,omitempty" structs:",omitempty"`
Attr DatabaseParameterSettingAttr `json:",omitempty" yaml:"attr,omitempty" structs:",omitempty"`
}
type DatabaseParameterSettingAttr map[string]interface{}
// UnmarshalJSON 配列/オブジェクトが混在することへの対応
func (d *DatabaseParameterSettingAttr) UnmarshalJSON(b []byte) error {
if string(b) == "[]" {
return nil
}
type alias map[string]interface{}
var a alias
if err := json.Unmarshal(b, &a); err != nil {
return err
}
*d = DatabaseParameterSettingAttr(a)
return nil
}
type DatabaseParameterRemark struct {
Settings []interface{} // どのような値が入るのか不明
Form []*DatabaseParameterFormMeta `json:",omitempty" yaml:"form,omitempty" structs:",omitempty"`
}
type DatabaseParameterFormMeta struct {
Type string `json:"type" yaml:"yaml"`
Name string `json:"name" yaml:"name"`
Label string `json:"label" yaml:"label"`
Options *DatabaseParameterFormMetaOptions `json:"options" yaml:"options"`
Items [][]interface{} `json:"items,omitempty" yaml:"items,omitempty" structs:",omitempty"` // 例: [["value1", "text1"],[ "value2", "text2"]] ※ valueは数値となる可能性がある
}
type DatabaseParameterFormMetaOptions struct {
Validator string `json:"validator" yaml:"validator"`
Example string `json:"ex" yaml:"ex"`
Min float64 `json:"min" yaml:"min"`
Max float64 `json:"max" yaml:"max"`
MaxLen int `json:"maxlen" yaml:"maxlen"`
Text string `json:"text" yaml:"text"`
Reboot string `json:"reboot" yaml:"reboot"`
Type string `json:"type" yaml:"type"`
Integer bool `json:"integer" yaml:"integer"` // postgres用のパラメータにだけ存在する模様
}