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