yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aliyun/shell/natgateway.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/aliyun"
    20  	"yunion.io/x/onecloud/pkg/util/shellutils"
    21  )
    22  
    23  func init() {
    24  	type NatGatewayListOptions struct {
    25  		VpcId  string `help:"Vpc Id"`
    26  		NatId  string `help:"NatGateway Id"`
    27  		Limit  int    `help:"page size"`
    28  		Offset int    `help:"page offset"`
    29  	}
    30  	shellutils.R(&NatGatewayListOptions{}, "nat-list", "List NAT gateways", func(cli *aliyun.SRegion, args *NatGatewayListOptions) error {
    31  		gws, total, e := cli.GetNatGateways(args.VpcId, args.NatId, args.Offset, args.Limit)
    32  		if e != nil {
    33  			return e
    34  		}
    35  		printList(gws, total, args.Offset, args.Limit, []string{})
    36  		return nil
    37  	})
    38  
    39  	type NatGatewayDeleteOptions struct {
    40  		ID    string `help:"Nat Id"`
    41  		Force bool   `help:"Force Delete Nat"`
    42  	}
    43  
    44  	shellutils.R(&NatGatewayDeleteOptions{}, "nat-delete", "Delete nat gateways", func(cli *aliyun.SRegion, args *NatGatewayDeleteOptions) error {
    45  		return cli.DeleteNatGateway(args.ID, args.Force)
    46  	})
    47  
    48  	type NatSEntryListOptions struct {
    49  		ID     string `help:"SNat Table ID"`
    50  		Limit  int    `help:"page size"`
    51  		Offset int    `help:"page offset"`
    52  	}
    53  	shellutils.R(&NatSEntryListOptions{}, "snat-entry-list", "List SNAT entries", func(cli *aliyun.SRegion, args *NatSEntryListOptions) error {
    54  		entries, total, e := cli.GetSNATEntries(args.ID, args.Offset, args.Limit)
    55  		if e != nil {
    56  			return e
    57  		}
    58  		printList(entries, total, args.Offset, args.Limit, []string{})
    59  		return nil
    60  	})
    61  
    62  	type NatDEntryListOptions struct {
    63  		ID     string `help:"DNat Table ID"`
    64  		Limit  int    `help:"page size"`
    65  		Offset int    `help:"page offset"`
    66  	}
    67  	shellutils.R(&NatDEntryListOptions{}, "dnat-entry-list", "List DNAT entries", func(cli *aliyun.SRegion, args *NatDEntryListOptions) error {
    68  		entries, total, e := cli.GetForwardTableEntries(args.ID, args.Offset, args.Limit)
    69  		if e != nil {
    70  			return e
    71  		}
    72  		printList(entries, total, args.Offset, args.Limit, []string{})
    73  		return nil
    74  	})
    75  
    76  	type SCreateDNatOptions struct {
    77  		GatewayID    string `help:"Nat Gateway ID" positional:"true"`
    78  		Protocol     string `help:"Protocol(tcp/udp)" positional:"true"`
    79  		ExternalIP   string `help:"External IP" positional:"true"`
    80  		ExternalPort int    `help:"External Port" positional:"true"`
    81  		InternalIP   string `help:"Internal IP" positional:"true"`
    82  		InternalPort int    `help:"Nat Gateway ID" positional:"true"`
    83  	}
    84  	shellutils.R(&SCreateDNatOptions{}, "dnat-entry-create", "Create DNAT entry", func(region *aliyun.SRegion, args *SCreateDNatOptions) error {
    85  		rule := cloudprovider.SNatDRule{
    86  			Protocol:     args.Protocol,
    87  			ExternalIP:   args.ExternalIP,
    88  			ExternalPort: args.ExternalPort,
    89  			InternalIP:   args.InternalIP,
    90  			InternalPort: args.InternalPort,
    91  		}
    92  		dnat, err := region.CreateForwardTableEntry(rule, args.GatewayID)
    93  		if err != nil {
    94  			return err
    95  		}
    96  		printObject(dnat)
    97  		return nil
    98  	})
    99  
   100  	type SCreateSNatOptions struct {
   101  		GatewayID  string `help:"Nat Gateway ID" positional:"true"`
   102  		SourceCIDR string `help:"Source cidr" positional:"true"`
   103  		ExternalIP string `help:"External IP" positional:"true"`
   104  	}
   105  	shellutils.R(&SCreateSNatOptions{}, "snat-entry-create", "Create SNAT entry", func(region *aliyun.SRegion, args *SCreateSNatOptions) error {
   106  		rule := cloudprovider.SNatSRule{
   107  			SourceCIDR: args.SourceCIDR,
   108  			ExternalIP: args.ExternalIP,
   109  		}
   110  		snat, err := region.CreateSNATTableEntry(rule, args.GatewayID)
   111  		if err != nil {
   112  			return err
   113  		}
   114  		printObject(snat)
   115  		return nil
   116  	})
   117  
   118  	type SShowSNatOptions struct {
   119  		TableID string `help:"SNat Table ID" positional:"true"`
   120  		NatID   string `help:"SNat Entry ID" positional:"true"`
   121  	}
   122  	shellutils.R(&SShowSNatOptions{}, "snat-entry-show", "show SNAT entry", func(region *aliyun.SRegion, args *SShowSNatOptions) error {
   123  		snat, err := region.GetSNATEntry(args.TableID, args.NatID)
   124  		if err != nil {
   125  			return err
   126  		}
   127  		printObject(snat)
   128  		return nil
   129  	})
   130  
   131  	type SShowDNatOptions struct {
   132  		TableID string `help:"DNat Table ID" positional:"true"`
   133  		NatID   string `help:"DNat Entry ID" positional:"true"`
   134  	}
   135  	shellutils.R(&SShowDNatOptions{}, "dnat-entry-show", "show SNAT entry", func(region *aliyun.SRegion, args *SShowDNatOptions) error {
   136  		dnat, err := region.GetForwardTableEntry(args.TableID, args.NatID)
   137  		if err != nil {
   138  			return err
   139  		}
   140  		printObject(dnat)
   141  		return nil
   142  	})
   143  
   144  	type SDeleteSNatOptions struct {
   145  		TableID string `help:"DNat Table ID" positional:"true"`
   146  		NatID   string `help:"SNat Entry ID" positional:"true"`
   147  	}
   148  	shellutils.R(&SDeleteSNatOptions{}, "snat-entry-delete", "Delete SNAT entry", func(region *aliyun.SRegion, args *SDeleteSNatOptions) error {
   149  		err := region.DeleteSnatEntry(args.TableID, args.NatID)
   150  		if err != nil {
   151  			return err
   152  		}
   153  		return nil
   154  	})
   155  
   156  	type SDeleteDNatOptions struct {
   157  		TableID string `help:"DNat Table ID" positional:"true"`
   158  		NatID   string `help:"DNat Entry ID" positional:"true"`
   159  	}
   160  	shellutils.R(&SDeleteDNatOptions{}, "dnat-entry-delete", "Delete DNAT entry", func(region *aliyun.SRegion, args *SDeleteDNatOptions) error {
   161  		err := region.DeleteForwardTableEntry(args.TableID, args.NatID)
   162  		if err != nil {
   163  			return err
   164  		}
   165  		return nil
   166  	})
   167  }