yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/azure/shell/loadbalancer.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  	"strings"
    19  
    20  	"yunion.io/x/cloudmux/pkg/multicloud/azure"
    21  	"yunion.io/x/onecloud/pkg/util/shellutils"
    22  )
    23  
    24  func init() {
    25  	type LoadbalancerListOptions struct {
    26  	}
    27  	shellutils.R(&LoadbalancerListOptions{}, "lb-list", "List loadbalancers", func(cli *azure.SRegion, args *LoadbalancerListOptions) error {
    28  		lbs, err := cli.GetILoadBalancers()
    29  		if err != nil {
    30  			return err
    31  		}
    32  		printList(lbs, len(lbs), 0, 0, []string{})
    33  		return nil
    34  	})
    35  
    36  	type LoadbalancerOptions struct {
    37  		ID string `help:"ID of loadbalancer"`
    38  	}
    39  	shellutils.R(&LoadbalancerOptions{}, "lb-show", "Show loadbalancer", func(cli *azure.SRegion, args *LoadbalancerOptions) error {
    40  		lb, err := cli.GetILoadBalancerById(args.ID)
    41  		if err != nil {
    42  			return err
    43  		}
    44  		printObject(lb)
    45  		return nil
    46  	})
    47  
    48  	shellutils.R(&LoadbalancerListOptions{}, "lbcert-list", "List loadbalancers certs", func(cli *azure.SRegion, args *LoadbalancerListOptions) error {
    49  		certs, err := cli.GetILoadBalancerCertificates()
    50  		if err != nil {
    51  			return err
    52  		}
    53  		printList(certs, len(certs), 0, 0, []string{})
    54  		return nil
    55  	})
    56  
    57  	type LoadbalancerCertOptions struct {
    58  		ID string `help:"ID of loadbalancer cert"`
    59  	}
    60  
    61  	shellutils.R(&LoadbalancerCertOptions{}, "lbcert-show", "Show loadbalancer cert", func(cli *azure.SRegion, args *LoadbalancerCertOptions) error {
    62  		cert, err := cli.GetILoadBalancerCertificateById(args.ID)
    63  		if err != nil {
    64  			return err
    65  		}
    66  		printObject(cert)
    67  		return nil
    68  	})
    69  
    70  	shellutils.R(&LoadbalancerOptions{}, "lbl-list", "List loadbalancer listeners", func(cli *azure.SRegion, args *LoadbalancerOptions) error {
    71  		lb, err := cli.GetILoadBalancerById(args.ID)
    72  		if err != nil {
    73  			return err
    74  		}
    75  		lbl, err := lb.GetILoadBalancerListeners()
    76  		if err != nil {
    77  			return err
    78  		}
    79  		printList(lbl, len(lbl), 0, 0, []string{})
    80  		return nil
    81  	})
    82  
    83  	shellutils.R(&LoadbalancerOptions{}, "lbbg-list", "List loadbalancer listeners", func(cli *azure.SRegion, args *LoadbalancerOptions) error {
    84  		lb, err := cli.GetILoadBalancerById(args.ID)
    85  		if err != nil {
    86  			return err
    87  		}
    88  		lbbgs, err := lb.GetILoadBalancerBackendGroups()
    89  		if err != nil {
    90  			return err
    91  		}
    92  		printList(lbbgs, len(lbbgs), 0, 0, []string{})
    93  		return nil
    94  	})
    95  
    96  	type LoadbalancerBackendOptions struct {
    97  		BGID string `help:"ID of loadbalancer backendgroup"`
    98  	}
    99  	shellutils.R(&LoadbalancerBackendOptions{}, "lbb-list", "List loadbalancer listeners", func(cli *azure.SRegion, args *LoadbalancerBackendOptions) error {
   100  		lb, err := cli.GetILoadBalancerById(strings.Split(args.BGID, "/backendAddressPools")[0])
   101  		if err != nil {
   102  			return err
   103  		}
   104  		lbbg, err := lb.GetILoadBalancerBackendGroupById(args.BGID)
   105  		if err != nil {
   106  			return err
   107  		}
   108  
   109  		lbbs, err := lbbg.GetILoadbalancerBackends()
   110  		if err != nil {
   111  			return err
   112  		}
   113  
   114  		printList(lbbs, len(lbbs), 0, 0, []string{})
   115  		return nil
   116  	})
   117  
   118  	type LoadbalancerRuleOptions struct {
   119  		LISTENID string `help:"ID of loadbalancer listener"`
   120  	}
   121  	shellutils.R(&LoadbalancerRuleOptions{}, "lbr-list", "List loadbalancer listener rules", func(cli *azure.SRegion, args *LoadbalancerRuleOptions) error {
   122  		lbId := ""
   123  		if strings.Index(args.LISTENID, "/requestRoutingRules") > 0 {
   124  			lbId = strings.Split(args.LISTENID, "/requestRoutingRules")[0]
   125  		} else {
   126  			return nil
   127  		}
   128  		lb, err := cli.GetILoadBalancerById(lbId)
   129  		if err != nil {
   130  			return err
   131  		}
   132  		lbl, err := lb.GetILoadBalancerListenerById(args.LISTENID)
   133  		if err != nil {
   134  			return err
   135  		}
   136  
   137  		lbrs, err := lbl.GetILoadbalancerListenerRules()
   138  		if err != nil {
   139  			return err
   140  		}
   141  
   142  		printList(lbrs, len(lbrs), 0, 0, []string{})
   143  		return nil
   144  	})
   145  }