Skip to content

Commit

Permalink
Merge pull request #585 from F5Networks/vs_vlan_disabled
Browse files Browse the repository at this point in the history
fixed ltm_snat and ltm vitual vlan_disabled issues
  • Loading branch information
RavinderReddyF5 authored Jan 11, 2022
2 parents 2d73dab + 27db25b commit f2671cb
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 78 deletions.
96 changes: 47 additions & 49 deletions bigip/resource_bigip_ltm_snat.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,67 +23,71 @@ func resourceBigipLtmSnat() *schema.Resource {
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{

"name": {
Type: schema.TypeString,
Required: true,
Description: "Snat list Name",
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateF5Name,
Description: "Name of the SNAT",
},
"partition": {
Type: schema.TypeString,
Optional: true,
Description: "Which partition on BIG-IP",
Description: "Partition or path to which the SNAT belongs",
},

"full_path": {
Type: schema.TypeString,
Optional: true,
Description: "Fullpath ",
},

"autolasthop": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Specifies whether to automatically map last hop for pools or not. The default is to use next level's defaul",
},
"mirror": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Enables or disables mirroring of SNAT connections.",
},
"sourceport": {
Type: schema.TypeString,
Optional: true,
Description: "Specifies whether the system preserves the source port of the connection. ",
Default: "preserve",
Description: "Specifies how the SNAT object handles the client's source port. The default is Preserve.",
},
"translation": {
Type: schema.TypeString,
Optional: true,
Description: "Specifies the name of a translated IP address.",
Type: schema.TypeString,
Optional: true,
Description: "Specifies a particular IP address that you want the SNAT to use as a translation address. When you select IP Address, you also type the IP address in the associated text box",
ConflictsWith: []string{"snatpool"},
},
"snatpool": {
Type: schema.TypeString,
Optional: true,
Description: "Specifies the name of a SNAT pool. You can only use this option when automap and translation are not used",
Type: schema.TypeString,
Optional: true,
Description: "Specifies an existing SNAT pool to which you want to map the client IP address. When you select SNAT Pool, you also select an existing SNAT pool from the associated list.",
ConflictsWith: []string{"translation"},
},
"vlansdisabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "Disables the SNAT on all VLANs.",
},
"vlans": {
Type: schema.TypeSet,
Set: schema.HashString,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Description: "Vlans or Vlan list",
Description: "Specifies the VLANs or tunnels for which the SNAT is enabled or disabled. The default is All",
},

"origins": {
Type: schema.TypeList,
Required: true,
Type: schema.TypeList,
Required: true,
Description: "Specifies, for each SNAT that you create, the origin addresses that are to be members of that SNAT. Specify origin addresses by their IP addresses and service ports",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Expand All @@ -106,23 +110,23 @@ func resourceBigipLtmSnat() *schema.Resource {
func resourceBigipLtmSnatCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*bigip.BigIP)
name := d.Get("name").(string)
log.Println("[INFO] Creating Snat" + name)
log.Println("[INFO] Creating Snat: " + name)

p := dataToSnat(name, d)
d.SetId(name)
err := client.CreateSnat(&p)
if err != nil {
log.Printf("[ERROR] Unable to Create Snat (%s) (%v) ", name, err)
return err
}
d.SetId(name)
return resourceBigipLtmSnatRead(d, meta)
}

func resourceBigipLtmSnatRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*bigip.BigIP)
name := d.Id()

log.Println("[INFO] Fetching Ltm Snat " + name)
log.Printf("[INFO] Fetching Ltm Snat:%+v", name)
p, err := client.GetSnat(name)
if err != nil {
log.Printf("[ERROR] Unable to Retrieve Snat (%s) (%v) ", name, err)
Expand All @@ -133,37 +137,27 @@ func resourceBigipLtmSnatRead(d *schema.ResourceData, meta interface{}) error {
d.SetId("")
return nil
}
d.Set("partition", p.Partition)
if err := d.Set("full_path", p.FullPath); err != nil {
return fmt.Errorf("[DEBUG] Error saving FullPath to state for Snat (%s): %s", d.Id(), err)
}
_ = d.Set("name", p.FullPath)
if err := d.Set("autolasthop", p.AutoLasthop); err != nil {
return fmt.Errorf("[DEBUG] Error saving AutoLasthop to state for Snat (%s): %s", d.Id(), err)
}
d.Set("mirror", p.Mirror)
_ = d.Set("mirror", p.Mirror)
if err := d.Set("sourceport", p.SourcePort); err != nil {
return fmt.Errorf("[DEBUG] Error saving SourcePort to state for Snat (%s): %s", d.Id(), err)
}
if err := d.Set("translation", p.Translation); err != nil {
return fmt.Errorf("[DEBUG] Error saving Translation to state for Snat (%s): %s", d.Id(), err)
}

if err := d.Set("snatpool", p.Snatpool); err != nil {
return fmt.Errorf("[DEBUG] Error saving Snatpool to state for Snat (%s): %s", d.Id(), err)
}
d.Set("vlansdisabled", p.VlansDisabled)

if err != nil {
return err
}

return SnatToData(p, d)
}

func resourceBigipLtmSnatUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*bigip.BigIP)
name := d.Id()
log.Println("[INFO] Updating LtmSnat " + name)
log.Printf("[INFO] Updating Ltm Snat:%+v", name)
p := dataToSnat(name, d)
err := client.UpdateSnat(name, &p)
if err != nil {
Expand All @@ -176,6 +170,7 @@ func resourceBigipLtmSnatUpdate(d *schema.ResourceData, meta interface{}) error
func resourceBigipLtmSnatDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*bigip.BigIP)
name := d.Id()
log.Printf("[INFO] Deleting Ltm Snat:%+v", name)
err := client.DeleteSnat(name)
if err != nil {
log.Printf("[ERROR] Unable to Delete Snat (%s) (%v) ", name, err)
Expand All @@ -187,7 +182,6 @@ func resourceBigipLtmSnatDelete(d *schema.ResourceData, meta interface{}) error

func dataToSnat(name string, d *schema.ResourceData) bigip.Snat {
var p bigip.Snat

p.Name = name
p.Partition = d.Get("partition").(string)
p.FullPath = d.Get("full_path").(string)
Expand All @@ -196,7 +190,11 @@ func dataToSnat(name string, d *schema.ResourceData) bigip.Snat {
p.SourcePort = d.Get("sourceport").(string)
p.Translation = d.Get("translation").(string)
p.Snatpool = d.Get("snatpool").(string)
p.VlansDisabled = d.Get("vlansdisabled").(bool)
if d.Get("vlansdisabled").(bool) {
p.VlansDisabled = d.Get("vlansdisabled").(bool)
} else {
p.VlansEnabled = true
}
p.Vlans = setToStringSlice(d.Get("vlans").(*schema.Set))
originsCount := d.Get("origins.#").(int)
p.Origins = make([]bigip.Originsrecord, 0, originsCount)
Expand All @@ -206,27 +204,27 @@ func dataToSnat(name string, d *schema.ResourceData) bigip.Snat {
r.Name = d.Get(prefix + ".name").(string)
p.Origins = append(p.Origins, r)
}

log.Println("I am in DatatoSnat value of p ", p)

return p
}

func SnatToData(p *bigip.Snat, d *schema.ResourceData) error {
d.Set("partition", p.Partition)
d.Set("full_path", p.FullPath)
d.Set("autolasthop", p.AutoLasthop)
d.Set("mirror", p.Mirror)
d.Set("sourceport", p.SourcePort)
d.Set("translation", p.Translation)
d.Set("snatpool", p.Snatpool)
d.Set("vlansdisabled", p.VlansDisabled)
_ = d.Set("autolasthop", p.AutoLasthop)
_ = d.Set("mirror", p.Mirror)
_ = d.Set("sourceport", p.SourcePort)
_ = d.Set("translation", p.Translation)
_ = d.Set("snatpool", p.Snatpool)
if p.VlansDisabled {
_ = d.Set("vlansdisabled", p.VlansDisabled)
}
if p.VlansEnabled {
_ = d.Set("vlansdisabled", false)
}
if err := d.Set("vlans", p.Vlans); err != nil {
return fmt.Errorf("error setting Vlans for resource %s: %s", d.Id(), err)
}
for i, r := range p.Origins {
origins := fmt.Sprintf("origins.%d", i)
d.Set(fmt.Sprintf("%s.name", origins), r.Name)
_ = d.Set(fmt.Sprintf("%s.name", origins), r.Name)
}
return nil
}
42 changes: 17 additions & 25 deletions docs/resources/bigip_ltm_snat.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,43 @@ description: |-

# bigip\_ltm\_snat

`bigip_ltm_snat` Manages a snat configuration

For resources should be named with their "full path". The full path is the combination of the partition + name of the resource. For example /Common/my-pool.
`bigip_ltm_snat` Manages a SNAT configuration

For resources should be named with their `full path`. The full path is the combination of the `partition + name` of the resource.For example `/Common/test-snat`.

## Example Usage


```hcl
resource "bigip_ltm_snat" "test-snat" {
name = "TEST_SNAT_NAME"
translation = "/Common/136.1.1.1"
origins {
name = "2.2.2.2"
}
name = "/Common/test-snat"
translation = "/Common/136.1.1.2"
sourceport = "preserve"
origins {
name = "3.3.3.3"
name = "0.0.0.0/0"
}
vlansdisabled = true
autolasthop = "default"
mirror = "disabled"
partition = "Common"
full_path = "/Common/test-snat"
vlans = [
"/Common/internal",
]
vlansdisabled = false
}
```

## Argument Reference

* `name` - (Required) Name of the snat
* `name` - (Required) Name of the SNAT, name of SNAT should be full path. Full path is the combination of the `partition + SNAT name`,For example `/Common/test-snat`.

* `partition` - (Optional) Displays the administrative partition within which this profile resides
* `origins` - (Required) Specifies, for each SNAT that you create, the origin addresses that are to be members of that SNAT. Specify origin addresses by their IP addresses and service ports

* `origins` - (Optional) IP or hostname of the snat
* `translation` - (Optional) Specifies the IP address configured for translation. Note that translated addresses are outside the traffic management system. You can only use this option when `automap` and `snatpool` are not used.

* `snatpool` - (Optional) Specifies the name of a SNAT pool. You can only use this option when automap and translation are not used.
* `snatpool` - (Optional) Specifies the name of a SNAT pool. You can only use this option when `automap` and `translation` are not used.

* `mirror` - (Optional) Enables or disables mirroring of SNAT connections.

* `autolasthop` -(Optional) Specifies whether to automatically map last hop for pools or not. The default is to use next level's default.

* `sourceport` - (Optional) Specifies whether the system preserves the source port of the connection. The default is preserve. Use of the preserve-strict setting should be restricted to UDP only under very special circumstances such as nPath or transparent (that is, no translation of any other L3/L4 field), where there is a 1:1 relationship between virtual IP addresses and node addresses, or when clustered multi-processing (CMP) is disabled. The change setting is useful for obfuscating internal network addresses.

* `translation` - (Optional) Specifies the name of a translated IP address. Note that translated addresses are outside the traffic management system. You can only use this option when automap and snatpool are not used.
* `sourceport` - (Optional) Specifies how the SNAT object handles the client's source port. The default is `preserve`.

* `vlansdisabled` - (Optional) Disables the SNAT on all VLANs.
* `vlansdisabled` - (Optional,bool) Specifies the VLANs or tunnels for which the SNAT is enabled or disabled. The default is `true`, vlandisabled on VLANS specified by `vlans`,if set to `false` vlanEnabled set on VLANS specified by `vlans` .

* `vlans` - (Optional) Specifies the name of the VLAN to which you want to assign the SNAT. The default is vlans-enabled.
* `vlans` - (Optional) Specifies the available VLANs or tunnels and those for which the SNAT is enabled or disabled.
11 changes: 7 additions & 4 deletions vendor/github.com/f5devcentral/go-bigip/ltm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f2671cb

Please sign in to comment.