yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcs/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/hcs" 20 "yunion.io/x/onecloud/pkg/util/shellutils" 21 ) 22 23 func init() { 24 type NatGatewayOptions struct { 25 NatGatewayId string `help:"Nat Gateway Id"` 26 VpcId string `help:"Vpc Id"` 27 } 28 shellutils.R(&NatGatewayOptions{}, "nat-list", "List nat gateway", func(region *hcs.SRegion, args *NatGatewayOptions) error { 29 natGateways, err := region.GetNatGateways(args.VpcId) 30 if err != nil { 31 return err 32 } 33 printList(natGateways, 0, 0, 0, nil) 34 return nil 35 }) 36 37 type NatGatewayIdOptions struct { 38 Id string `help:"Nat Id"` 39 } 40 41 shellutils.R(&NatGatewayIdOptions{}, "nat-delete", "Delete nat gateways", func(cli *hcs.SRegion, args *NatGatewayIdOptions) error { 42 return cli.DeleteNatGateway(args.Id) 43 }) 44 45 shellutils.R(&NatGatewayIdOptions{}, "nat-show", "Show nat gateways", func(cli *hcs.SRegion, args *NatGatewayIdOptions) error { 46 nat, err := cli.GetNatGateway(args.Id) 47 if err != nil { 48 return err 49 } 50 printObject(nat) 51 return nil 52 }) 53 54 type SCreateDNatOptions struct { 55 GatewayId string `help:"Nat Gateway Id" positional:"true"` 56 Protocol string `help:"Protocol(tcp/udp)" positional:"true"` 57 ExternalIPId string `help:"External IP Id" positional:"true"` 58 ExternalPort int `help:"External Port" positional:"true"` 59 InternalIP string `help:"Internal IP" positional:"true"` 60 InternalPort int `help:"Nat Gateway Id" positional:"true"` 61 } 62 shellutils.R(&SCreateDNatOptions{}, "dnat-create", "Create dnat", func(region *hcs.SRegion, args *SCreateDNatOptions) error { 63 rule := cloudprovider.SNatDRule{ 64 Protocol: args.Protocol, 65 ExternalIPID: args.ExternalIPId, 66 ExternalPort: args.ExternalPort, 67 InternalIP: args.InternalIP, 68 InternalPort: args.InternalPort, 69 } 70 dnat, err := region.CreateNatDEntry(rule, args.GatewayId) 71 if err != nil { 72 return err 73 } 74 printObject(dnat) 75 return nil 76 }) 77 78 type SCreateSNatOptions struct { 79 GatewayId string `help:"Nat Gateway Id" positional:"true"` 80 SourceCIDR string `help:"Source cidr" positional:"true"` 81 ExternalIPId string `help:"External IP Id" positional:"true"` 82 } 83 shellutils.R(&SCreateSNatOptions{}, "snat-create", "Create snat", func(region *hcs.SRegion, args *SCreateSNatOptions) error { 84 rule := cloudprovider.SNatSRule{ 85 SourceCIDR: args.SourceCIDR, 86 ExternalIPID: args.ExternalIPId, 87 } 88 snat, err := region.CreateNatSEntry(rule, args.GatewayId) 89 if err != nil { 90 return err 91 } 92 printObject(snat) 93 return nil 94 }) 95 96 type SShowSNatOptions struct { 97 NatId string `help:"SNat Id" positional:"true"` 98 } 99 shellutils.R(&SShowSNatOptions{}, "snat-show", "Show snat", func(region *hcs.SRegion, args *SShowSNatOptions) error { 100 snat, err := region.GetNatSEntry(args.NatId) 101 if err != nil { 102 return err 103 } 104 printObject(snat) 105 return nil 106 }) 107 108 type SShowDNatOptions struct { 109 NatId string `help:"DNat Id" positional:"true"` 110 } 111 shellutils.R(&SShowDNatOptions{}, "dnat-show", "Show dnat", func(region *hcs.SRegion, args *SShowDNatOptions) error { 112 dnat, err := region.GetNatDEntry(args.NatId) 113 if err != nil { 114 return err 115 } 116 printObject(dnat) 117 return nil 118 }) 119 120 type SDeleteSNatOptions struct { 121 NatId string `help:"SNat Id" positional:"true"` 122 } 123 shellutils.R(&SDeleteSNatOptions{}, "snat-delete", "Delete snat", func(region *hcs.SRegion, args *SDeleteSNatOptions) error { 124 err := region.DeleteNatSEntry(args.NatId) 125 if err != nil { 126 return err 127 } 128 return nil 129 }) 130 131 type SDeleteDNatOptions struct { 132 NatId string `help:"DNat Id" positional:"true"` 133 } 134 shellutils.R(&SDeleteDNatOptions{}, "dnat-delete", "Delete dnat", func(region *hcs.SRegion, args *SDeleteDNatOptions) error { 135 err := region.DeleteNatDEntry(args.NatId) 136 if err != nil { 137 return err 138 } 139 return nil 140 }) 141 }