-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnat.go
57 lines (53 loc) · 1.52 KB
/
nat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main
import (
"fmt"
"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/nat/v2/model"
)
func CreateNat(client Client, config *Config, vpcId, networkId string) error {
request := &model.CreateNatGatewayRequest{}
request.Body = &model.CreateNatGatewayRequestBody{
NatGateway: &model.CreateNatGatewayOption{
Name: fmt.Sprintf("nat-%s", GenerateRandomString(4)),
RouterId: vpcId,
InternalNetworkId: networkId,
Spec: getNatSpec(config.Nat.Spec),
},
}
response, err := client.CreateNatGateway(request)
if err != nil {
return err
}
publicIpId, err := CreatePublicIp(client, config)
if err != nil {
return err
}
snatRequest := &model.CreateNatGatewaySnatRuleRequest{}
snatRequest.Body = &model.CreateNatGatewaySnatRuleRequestOption{
SnatRule: &model.CreateNatGatewaySnatRuleOption{
NatGatewayId: response.NatGateway.Id,
FloatingIpId: publicIpId,
NetworkId: &networkId,
},
}
_, err = client.CreateNatGatewaySnatRule(snatRequest)
if err != nil {
return err
}
return nil
}
func getNatSpec(spec int32) model.CreateNatGatewayOptionSpec {
var specEnum model.CreateNatGatewayOptionSpec
switch spec {
case 1:
specEnum = model.GetCreateNatGatewayOptionSpecEnum().E_1
case 2:
specEnum = model.GetCreateNatGatewayOptionSpecEnum().E_2
case 3:
specEnum = model.GetCreateNatGatewayOptionSpecEnum().E_3
case 4:
specEnum = model.GetCreateNatGatewayOptionSpecEnum().E_4
default:
specEnum = model.GetCreateNatGatewayOptionSpecEnum().E_1
}
return specEnum
}