yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcs/shell/lb.go (about) 1 // Copyright 2019 Yunion 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 shell 16 17 import ( 18 "yunion.io/x/cloudmux/pkg/cloudprovider" 19 "yunion.io/x/cloudmux/pkg/multicloud/hcs" 20 "yunion.io/x/onecloud/pkg/util/shellutils" 21 ) 22 23 func init() { 24 type ElbListOptions struct { 25 } 26 shellutils.R(&ElbListOptions{}, "elb-list", "List loadbalancers", func(cli *hcs.SRegion, args *ElbListOptions) error { 27 elbs, err := cli.GetILoadBalancers() 28 if err != nil { 29 return err 30 } 31 printList(elbs, len(elbs), 0, 0, []string{}) 32 return nil 33 }) 34 35 type ElbCreateOptions struct { 36 Name string `help:"loadblancer name"` 37 SUBNET string `help:"subnet id"` 38 PrivateIP string `help:"loadbalancer private ip address"` 39 EipID string `help:"loadbalancer public ip id"` 40 } 41 shellutils.R(&ElbCreateOptions{}, "elb-create", "create loadbalancer", func(cli *hcs.SRegion, args *ElbCreateOptions) error { 42 loadbalancer := &cloudprovider.SLoadbalancer{ 43 Name: args.Name, 44 NetworkIDs: []string{args.SUBNET}, 45 EipID: args.EipID, 46 Address: args.PrivateIP, 47 } 48 49 elb, err := cli.CreateLoadBalancer(loadbalancer) 50 if err != nil { 51 return err 52 } 53 54 printObject(elb) 55 return nil 56 }) 57 58 type ElbIdOptions struct { 59 ID string `help:"loadblancer id"` 60 } 61 shellutils.R(&ElbIdOptions{}, "elb-delete", "delete loadbalancer", func(cli *hcs.SRegion, args *ElbIdOptions) error { 62 err := cli.DeleteLoadBalancer(args.ID) 63 if err != nil { 64 return err 65 } 66 67 return nil 68 }) 69 70 shellutils.R(&ElbIdOptions{}, "elb-show", "show loadbalancer", func(cli *hcs.SRegion, args *ElbIdOptions) error { 71 lb, err := cli.GetLoadbalancer(args.ID) 72 if err != nil { 73 return err 74 } 75 printObject(lb) 76 return nil 77 }) 78 79 type ElbListenerListOptions struct { 80 ElbID string `help:"loadblancer id"` 81 } 82 shellutils.R(&ElbListenerListOptions{}, "elb-listener-list", "list loadbalancer listener", func(cli *hcs.SRegion, args *ElbListenerListOptions) error { 83 listeners, err := cli.GetLoadBalancerListeners(args.ElbID) 84 if err != nil { 85 return err 86 } 87 88 printList(listeners, len(listeners), 0, 0, []string{}) 89 return nil 90 }) 91 92 type ElbListenerCreateOptions struct { 93 Name string `help:"listener name"` 94 Desc string `help:"listener Description"` 95 Http2 bool `help:"http2 enable status"` 96 PoolID string `help:"default backend group id"` 97 CertId string `help:"default certification id"` 98 XForwardedFor bool `help:"XForwardedFor enable status"` 99 LISTENER_TYPE string `help:"listener type" choices:"tcp|udp|http|https"` 100 LISTENER_PORT int `help:"listener port"` 101 ELB_ID string `help:"loadbalancer id"` 102 } 103 shellutils.R(&ElbListenerCreateOptions{}, "elb-listener-create", "create loadbalancer listener", func(cli *hcs.SRegion, args *ElbListenerCreateOptions) error { 104 input := &cloudprovider.SLoadbalancerListener{ 105 Name: args.Name, 106 LoadbalancerID: args.ELB_ID, 107 ListenerType: args.LISTENER_TYPE, 108 ListenerPort: args.LISTENER_PORT, 109 BackendGroupID: args.PoolID, 110 EnableHTTP2: args.Http2, 111 CertificateID: args.CertId, 112 Description: args.Desc, 113 XForwardedFor: args.XForwardedFor, 114 } 115 listener, err := cli.CreateLoadBalancerListener(input) 116 if err != nil { 117 return err 118 } 119 120 printObject(listener) 121 return nil 122 }) 123 124 type ElbListenerUpdateOptions struct { 125 Name string `help:"listener name"` 126 Desc string `help:"listener Description"` 127 Http2 bool `help:"http2 enable status"` 128 PoolID string `help:"default backend group id"` 129 CertId string `help:"default certification id"` 130 XForwardedFor bool `help:"XForwardedFor enable status"` 131 LISTENER_ID string `help:"listener id"` 132 } 133 shellutils.R(&ElbListenerUpdateOptions{}, "elb-listener-update", "update loadbalancer listener", func(cli *hcs.SRegion, args *ElbListenerUpdateOptions) error { 134 input := &cloudprovider.SLoadbalancerListener{ 135 Name: args.Name, 136 BackendGroupID: args.PoolID, 137 EnableHTTP2: args.Http2, 138 CertificateID: args.CertId, 139 Description: args.Desc, 140 XForwardedFor: args.XForwardedFor, 141 } 142 143 err := cli.UpdateLoadBalancerListener(args.LISTENER_ID, input) 144 if err != nil { 145 return err 146 } 147 148 return nil 149 }) 150 151 type ElbBackendGroupListOptions struct { 152 ElbID string `help:"loadbalancer id"` 153 } 154 shellutils.R(&ElbBackendGroupListOptions{}, "elb-backend-group-list", "List backend groups", func(cli *hcs.SRegion, args *ElbBackendGroupListOptions) error { 155 elbbg, err := cli.GetLoadBalancerBackendGroups(args.ElbID) 156 if err != nil { 157 return err 158 } 159 160 printList(elbbg, len(elbbg), 0, 0, []string{}) 161 return nil 162 }) 163 164 type ElbBackendGroupCreateOptions struct { 165 Name string `help:"backend group name"` 166 Desc string `help:"backend group description"` 167 PROTOCOL string `help:"backend group protocol" choices:"tcp|udp|http"` 168 ALGORITHM string `help:"backend group algorithm" choices:"rr|wlc|sch"` 169 ListenerID string `help:"listener id to binding"` 170 ElbID string `help:"loadbalancer id belong to"` 171 StickySessionType string `help:"sticky session type" choices:"insert|server"` 172 StickySessionCookieName string `help:"sticky session cookie name"` 173 StickySessionTimeout int `help:"sticky session timeout. udp/tcp 1~60. http 1~1440"` 174 HealthCheck bool `help:"enable health check"` 175 HealthCheckType string `help:"health check type protocol" choices:"tcp|udp|http"` 176 HealthCheckTimeout int `help:"health check timeout"` 177 HealthCheckDomain string `help:"health check domain"` 178 HealthCheckURI string `help:"health check uri path"` 179 HealthCheckInterval int `help:"health check interval"` 180 HealthCheckRise int `help:"health check max retries"` 181 } 182 shellutils.R(&ElbBackendGroupCreateOptions{}, "elb-backend-group-create", "Create backend groups", func(cli *hcs.SRegion, args *ElbBackendGroupCreateOptions) error { 183 var health *cloudprovider.SLoadbalancerHealthCheck 184 if args.HealthCheck { 185 health = &cloudprovider.SLoadbalancerHealthCheck{ 186 HealthCheckType: args.HealthCheckType, 187 HealthCheckTimeout: args.HealthCheckTimeout, 188 HealthCheckDomain: args.HealthCheckDomain, 189 HealthCheckURI: args.HealthCheckURI, 190 HealthCheckInterval: args.HealthCheckInterval, 191 HealthCheckRise: args.HealthCheckRise, 192 } 193 } 194 195 var sticky *cloudprovider.SLoadbalancerStickySession 196 if len(args.StickySessionType) > 0 { 197 sticky = &cloudprovider.SLoadbalancerStickySession{ 198 StickySessionCookie: args.StickySessionCookieName, 199 StickySessionType: args.StickySessionType, 200 StickySessionCookieTimeout: args.StickySessionTimeout, 201 } 202 } 203 204 group := &cloudprovider.SLoadbalancerBackendGroup{ 205 Name: args.Name, 206 LoadbalancerID: args.ElbID, 207 ListenerID: args.ListenerID, 208 ListenType: args.PROTOCOL, 209 Scheduler: args.ALGORITHM, 210 StickySession: sticky, 211 HealthCheck: health, 212 } 213 214 elbbg, err := cli.CreateLoadBalancerBackendGroup(group) 215 if err != nil { 216 return err 217 } 218 219 printObject(elbbg) 220 return nil 221 }) 222 223 type ElbBackendGroupUpdateOptions struct { 224 POOL_ID string `help:"backend group id"` 225 Name string `help:"backend group name"` 226 } 227 shellutils.R(&ElbBackendGroupUpdateOptions{}, "elb-backend-group-update", "Update backend groups", func(cli *hcs.SRegion, args *ElbBackendGroupUpdateOptions) error { 228 group := &cloudprovider.SLoadbalancerBackendGroup{ 229 Name: args.Name, 230 } 231 232 elbbg, err := cli.UpdateLoadBalancerBackendGroup(args.POOL_ID, group) 233 if err != nil { 234 return err 235 } 236 237 printObject(elbbg) 238 return nil 239 }) 240 241 type ElbBackendGroupDeleteOptions struct { 242 POOL_ID string `help:"backend group id"` 243 } 244 shellutils.R(&ElbBackendGroupDeleteOptions{}, "elb-backend-group-delete", "Delete backend group", func(cli *hcs.SRegion, args *ElbBackendGroupDeleteOptions) error { 245 err := cli.DeleteLoadBalancerBackendGroup(args.POOL_ID) 246 if err != nil { 247 return err 248 } 249 250 return nil 251 }) 252 253 type ElbBackendAddOptions struct { 254 Name string `help:"backend name"` 255 POOL_ID string `help:"backend group id"` 256 SUBNET_ID string `help:"instance subnet id"` 257 ADDRESS string `help:"instance ip address"` 258 PORT int `help:"backend protocol port [1,65535]"` 259 Weight int `help:"backend weight [0,100]" default:"1"` 260 } 261 shellutils.R(&ElbBackendAddOptions{}, "elb-backend-add", "Add backend to backendgroup", func(cli *hcs.SRegion, args *ElbBackendAddOptions) error { 262 elbb, err := cli.AddLoadBalancerBackend(args.POOL_ID, args.SUBNET_ID, args.ADDRESS, args.PORT, args.Weight) 263 if err != nil { 264 return err 265 } 266 267 printObject(elbb) 268 return nil 269 }) 270 271 type ElbBackendListOptions struct { 272 POOL_ID string `help:"backend group id"` 273 } 274 shellutils.R(&ElbBackendListOptions{}, "elb-backend-list", "list backend", func(cli *hcs.SRegion, args *ElbBackendListOptions) error { 275 elbb, err := cli.GetLoadBalancerBackends(args.POOL_ID) 276 if err != nil { 277 return err 278 } 279 280 printList(elbb, len(elbb), 0, 0, []string{}) 281 return nil 282 }) 283 284 type ElbListenerPolicyListOptions struct { 285 ListenerID string `help:"listener id"` 286 } 287 shellutils.R(&ElbListenerPolicyListOptions{}, "elb-listener-policy-list", "List listener policies", func(cli *hcs.SRegion, args *ElbListenerPolicyListOptions) error { 288 elblp, err := cli.GetLoadBalancerPolicies(args.ListenerID) 289 if err != nil { 290 return err 291 } 292 293 printList(elblp, len(elblp), 0, 0, []string{}) 294 return nil 295 }) 296 297 type ElbListenerPolicyCreateOptions struct { 298 LISTENER_ID string `help:"listener id"` 299 Name string `help:"policy name"` 300 Domain string `help:"policy domain"` 301 Path string `help:"policy path"` 302 PoolID string `help:"backend group name"` 303 } 304 shellutils.R(&ElbListenerPolicyCreateOptions{}, "elb-listener-policy-create", "Create listener policy", func(cli *hcs.SRegion, args *ElbListenerPolicyCreateOptions) error { 305 rule := &cloudprovider.SLoadbalancerListenerRule{ 306 Name: args.Name, 307 Domain: args.Domain, 308 Path: args.Path, 309 BackendGroupID: args.PoolID, 310 } 311 312 elblp, err := cli.CreateLoadBalancerPolicy(args.LISTENER_ID, rule) 313 if err != nil { 314 return err 315 } 316 317 printObject(elblp) 318 return nil 319 }) 320 321 type ElbListenerPolicyDeleteOptions struct { 322 POLICY_ID string `help:"policy id"` 323 } 324 shellutils.R(&ElbListenerPolicyDeleteOptions{}, "elb-listener-policy-delete", "Delete listener policy", func(cli *hcs.SRegion, args *ElbListenerPolicyDeleteOptions) error { 325 err := cli.DeleteLoadBalancerPolicy(args.POLICY_ID) 326 if err != nil { 327 return err 328 } 329 330 return nil 331 }) 332 333 type ElbListenerPolicyRuleListOptions struct { 334 POLICY_ID string `help:"policy id"` 335 } 336 shellutils.R(&ElbListenerPolicyRuleListOptions{}, "elb-listener-policyrule-list", "List listener policy rules", func(cli *hcs.SRegion, args *ElbListenerPolicyRuleListOptions) error { 337 elblpr, err := cli.GetLoadBalancerPolicyRules(args.POLICY_ID) 338 if err != nil { 339 return err 340 } 341 342 printList(elblpr, len(elblpr), 0, 0, []string{}) 343 return nil 344 }) 345 }