customize_gslb.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 type GSLBServers []*GSLBServer
18
19 // NewGSLBServer GSLB実サーバの作成
20 func NewGSLBServer(ip string) *GSLBServer {
21 return &GSLBServer{
22 IPAddress: ip,
23 Enabled: true,
24 Weight: 1,
25 }
26 }
27
28 // AddGSLBServer サーバの追加
29 func (o *GSLBServers) Add(server *GSLBServer) {
30 if o.Exist(server) {
31 return // noop if already exists
32 }
33 *o = append(*o, server)
34 }
35
36 // Exist サーバの存在確認
37 func (o *GSLBServers) Exist(server *GSLBServer) bool {
38 for _, s := range *o {
39 if s.IPAddress == server.IPAddress {
40 return true
41 }
42 }
43 return false
44 }
45
46 // ExistAt サーバの存在確認
47 func (o *GSLBServers) ExistAt(ip string) bool {
48 return o.Exist(NewGSLBServer(ip))
49 }
50
51 // Find サーバの検索
52 func (o *GSLBServers) Find(server *GSLBServer) *GSLBServer {
53 for _, s := range *o {
54 if s.IPAddress == server.IPAddress {
55 return s
56 }
57 }
58 return nil
59 }
60
61 // FindAt サーバの検索
62 func (o *GSLBServers) FindAt(ip string) *GSLBServer {
63 return o.Find(NewGSLBServer(ip))
64 }
65
66 // Update サーバの更新
67 func (o *GSLBServers) Update(old *GSLBServer, new *GSLBServer) {
68 for _, s := range *o {
69 if s.IPAddress == old.IPAddress {
70 *s = *new
71 return
72 }
73 }
74 }
75
76 // UpdateAt サーバの更新
77 func (o *GSLBServers) UpdateAt(ip string, new *GSLBServer) {
78 o.Update(NewGSLBServer(ip), new)
79 }
80
81 // Delete サーバの削除
82 func (o *GSLBServers) Delete(server *GSLBServer) {
83 var res []*GSLBServer
84 for _, s := range *o {
85 if s.IPAddress != server.IPAddress {
86 res = append(res, s)
87 }
88 }
89 *o = res
90 }
91
92 // DeleteAt サーバの削除
93 func (o *GSLBServers) DeleteAt(ip string) {
94 o.Delete(NewGSLBServer(ip))
95 }
96