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