Skip to content

Commit

Permalink
Fix some issues with NAT
Browse files Browse the repository at this point in the history
(1) When creating REFLEXIVE Nat rules, the
destination_network should be set to nil instead
of empty string (issue #758)

(2) Allow translated_network field to be optional
when for no_snat/no_dnat (issue #753)
  • Loading branch information
2ez4szliu committed Jun 2, 2022
1 parent b4a8fcd commit 8e8711a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
7 changes: 4 additions & 3 deletions nsxt/policy_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,17 +200,18 @@ func collectSeparatedStringListToMap(stringList []string, separator string) map[
return strMap
}

func stringListToCommaSeparatedString(stringList []string) string {
var str string
func stringListToCommaSeparatedString(stringList []string) *string {
if len(stringList) > 0 {
var str string
for i, seg := range stringList {
str += seg
if i < len(stringList)-1 {
str += ","
}
}
return &str
}
return str
return nil
}

func commaSeparatedStringToStringList(commaString string) []string {
Expand Down
35 changes: 26 additions & 9 deletions nsxt/resource_nsxt_policy_nat_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func resourceNsxtPolicyNATRule() *schema.Resource {
"translated_networks": {
Type: schema.TypeList,
Description: "The translated network(s) for the NAT Rule",
Required: true,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validateCidrOrIPOrRange(),
Expand Down Expand Up @@ -206,6 +206,10 @@ func getNsxtPolicyNATRuleByID(connector *client.RestConnector, gwID string, isT0

func patchNsxtPolicyNATRule(connector *client.RestConnector, gwID string, rule model.PolicyNatRule, isT0 bool, isGlobalManager bool) error {
natType := getNatTypeByAction(*rule.Action)
_, err := getTranslatedNetworks(rule)
if err != nil {
return err
}
if isGlobalManager {
rawObj, err := convertModelBindingType(rule, model.PolicyNatRuleBindingType(), gm_model.PolicyNatRuleBindingType())
if err != nil {
Expand Down Expand Up @@ -234,6 +238,19 @@ func getNatTypeByAction(action string) string {
return model.PolicyNat_NAT_TYPE_USER
}

func translatedNetworksNeeded(action string) bool {
return action != model.PolicyNatRule_ACTION_NO_SNAT && action != model.PolicyNatRule_ACTION_NO_DNAT
}

func getTranslatedNetworks(rule model.PolicyNatRule) (*string, error) {
tNets := rule.TranslatedNetwork
action := rule.Action
if tNets == nil && translatedNetworksNeeded(*action) {
return tNets, fmt.Errorf("Translated Network must be specified for action type: %s", *action)
}
return tNets, nil
}

func resourceNsxtPolicyNATRuleRead(d *schema.ResourceData, m interface{}) error {
connector := getPolicyConnector(m)

Expand Down Expand Up @@ -328,12 +345,12 @@ func resourceNsxtPolicyNATRuleCreate(d *schema.ResourceData, m interface{}) erro
Description: &description,
Tags: tags,
Action: &action,
DestinationNetwork: &dNets,
DestinationNetwork: dNets,
Enabled: &enabled,
Logging: &logging,
SequenceNumber: &priority,
Service: &service,
TranslatedNetwork: &tNets,
TranslatedNetwork: tNets,
Scope: scope,
}

Expand All @@ -345,8 +362,8 @@ func resourceNsxtPolicyNATRuleCreate(d *schema.ResourceData, m interface{}) erro
ruleStruct.TranslatedPorts = &ports
}

if len(sNets) > 0 {
ruleStruct.SourceNetwork = &sNets
if sNets != nil && len(*sNets) > 0 {
ruleStruct.SourceNetwork = sNets
}

log.Printf("[INFO] Creating NAT Rule with ID %s", id)
Expand Down Expand Up @@ -395,12 +412,12 @@ func resourceNsxtPolicyNATRuleUpdate(d *schema.ResourceData, m interface{}) erro
Description: &description,
Tags: tags,
Action: &action,
DestinationNetwork: &dNets,
DestinationNetwork: dNets,
Enabled: &enabled,
Logging: &logging,
SequenceNumber: &priority,
Service: &service,
TranslatedNetwork: &tNets,
TranslatedNetwork: tNets,
Scope: scope,
}

Expand All @@ -413,8 +430,8 @@ func resourceNsxtPolicyNATRuleUpdate(d *schema.ResourceData, m interface{}) erro
if tPorts != "" {
ruleStruct.TranslatedPorts = &tPorts
}
if len(sNets) > 0 {
ruleStruct.SourceNetwork = &sNets
if sNets != nil && len(*sNets) > 0 {
ruleStruct.SourceNetwork = sNets
}

log.Printf("[INFO] Updating NAT Rule with ID %s", id)
Expand Down

0 comments on commit 8e8711a

Please sign in to comment.