forked from yunionio/cloudmux
/
loadbalancer_acl.go
114 lines (91 loc) · 2.83 KB
/
loadbalancer_acl.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
// Copyright 2019 Yunion
//
// 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 hcso
import (
"strings"
"github.com/nyl1001/pkg/jsonutils"
api "github.com/nyl1001/cloudmux/pkg/apis/compute"
"github.com/nyl1001/cloudmux/pkg/cloudprovider"
"github.com/nyl1001/cloudmux/pkg/multicloud"
"github.com/nyl1001/cloudmux/pkg/multicloud/huawei"
)
type SElbACL struct {
multicloud.SResourceBase
huawei.HuaweiTags
region *SRegion
ID string `json:"id"`
ListenerID string `json:"listener_id"`
TenantID string `json:"tenant_id"`
EnableWhitelist bool `json:"enable_whitelist"`
Whitelist string `json:"whitelist"`
}
func (self *SElbACL) GetAclListenerID() string {
return self.ListenerID
}
func (self *SElbACL) GetId() string {
return self.ID
}
func (self *SElbACL) GetName() string {
return self.ID
}
func (self *SElbACL) GetGlobalId() string {
return self.GetId()
}
func (self *SElbACL) GetStatus() string {
if self.EnableWhitelist {
return api.LB_BOOL_ON
}
return api.LB_BOOL_OFF
}
func (self *SElbACL) Refresh() error {
acl, err := self.region.GetLoadBalancerAclById(self.GetId())
if err != nil {
return err
}
err = jsonutils.Update(self, acl)
if err != nil {
return err
}
return nil
}
func (self *SElbACL) IsEmulated() bool {
return false
}
func (self *SElbACL) GetProjectId() string {
return ""
}
func (self *SElbACL) GetAclEntries() []cloudprovider.SLoadbalancerAccessControlListEntry {
ret := []cloudprovider.SLoadbalancerAccessControlListEntry{}
for _, cidr := range strings.Split(self.Whitelist, ",") {
ret = append(ret, cloudprovider.SLoadbalancerAccessControlListEntry{CIDR: cidr})
}
return ret
}
func (self *SElbACL) Sync(acl *cloudprovider.SLoadbalancerAccessControlList) error {
whiteList := ""
cidrs := []string{}
for _, entry := range acl.Entrys {
cidrs = append(cidrs, entry.CIDR)
}
whiteList = strings.Join(cidrs, ",")
params := jsonutils.NewDict()
whiteListObj := jsonutils.NewDict()
whiteListObj.Set("whitelist", jsonutils.NewString(whiteList))
whiteListObj.Set("enable_whitelist", jsonutils.NewBool(acl.AccessControlEnable))
params.Set("whitelist", whiteListObj)
return DoUpdate(self.region.ecsClient.ElbWhitelist.Update, self.GetId(), params, nil)
}
func (self *SElbACL) Delete() error {
return DoDelete(self.region.ecsClient.ElbWhitelist.Delete, self.GetId(), nil, nil)
}