Skip to content

Commit

Permalink
Make all *bool literals and assignments easier to read
Browse files Browse the repository at this point in the history
Added a small boolPtr helper function for this.

As a TODO, I'd like to streamline helpers possibly into their own
package, and add one for int32 which seems to be the standard int type
in govmomi.
  • Loading branch information
vancluever committed Aug 31, 2017
1 parent edb0836 commit b061d07
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
22 changes: 11 additions & 11 deletions vsphere/host_network_policy_structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,16 @@ func expandHostNicFailureCriteria(d *schema.ResourceData) *types.HostNicFailureC
obj := &types.HostNicFailureCriteria{}

if v, ok := d.GetOkExists("check_beacon"); ok {
obj.CheckBeacon = &([]bool{v.(bool)}[0])
obj.CheckBeacon = boolPtr(v.(bool))
}

// These fields are deprecated and are set only to make things work. They are
// not exposed to Terraform.
obj.CheckSpeed = "minimum"
obj.Speed = 10
obj.CheckDuplex = &([]bool{false}[0])
obj.FullDuplex = &([]bool{false}[0])
obj.CheckErrorPercent = &([]bool{false}[0])
obj.CheckDuplex = boolPtr(false)
obj.FullDuplex = boolPtr(false)
obj.CheckErrorPercent = boolPtr(false)
obj.Percentage = 0

return obj
Expand Down Expand Up @@ -168,17 +168,17 @@ func expandHostNicTeamingPolicy(d *schema.ResourceData) *types.HostNicTeamingPol
Policy: d.Get("teaming_policy").(string),
}
if v, ok := d.GetOkExists("failback"); ok {
obj.RollingOrder = &([]bool{!v.(bool)}[0])
obj.RollingOrder = boolPtr(!v.(bool))
}
if v, ok := d.GetOkExists("notify_switches"); ok {
obj.NotifySwitches = &([]bool{v.(bool)}[0])
obj.NotifySwitches = boolPtr(v.(bool))
}
obj.FailureCriteria = expandHostNicFailureCriteria(d)
obj.NicOrder = expandHostNicOrderPolicy(d)

// These fields are deprecated and are set only to make things work. They are
// not exposed to Terraform.
obj.ReversePolicy = &([]bool{true}[0])
obj.ReversePolicy = boolPtr(true)

return obj
}
Expand Down Expand Up @@ -207,13 +207,13 @@ func flattenHostNicTeamingPolicy(d *schema.ResourceData, obj *types.HostNicTeami
func expandHostNetworkSecurityPolicy(d *schema.ResourceData) *types.HostNetworkSecurityPolicy {
obj := &types.HostNetworkSecurityPolicy{}
if v, ok := d.GetOkExists("allow_promiscuous"); ok {
obj.AllowPromiscuous = &([]bool{v.(bool)}[0])
obj.AllowPromiscuous = boolPtr(v.(bool))
}
if v, ok := d.GetOkExists("allow_forged_transmits"); ok {
obj.ForgedTransmits = &([]bool{v.(bool)}[0])
obj.ForgedTransmits = boolPtr(v.(bool))
}
if v, ok := d.GetOkExists("allow_mac_changes"); ok {
obj.MacChanges = &([]bool{v.(bool)}[0])
obj.MacChanges = boolPtr(v.(bool))
}
return obj
}
Expand Down Expand Up @@ -242,7 +242,7 @@ func expandHostNetworkTrafficShapingPolicy(d *schema.ResourceData) *types.HostNe
PeakBandwidth: int64(d.Get("shaping_peak_bandwidth").(int)),
}
if v, ok := d.GetOkExists("shaping_enabled"); ok {
obj.Enabled = &([]bool{v.(bool)}[0])
obj.Enabled = boolPtr(v.(bool))
}
return obj
}
Expand Down
8 changes: 8 additions & 0 deletions vsphere/structure_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,11 @@ func mergeSchema(dst, src map[string]*schema.Schema) {
dst[k] = v
}
}

// boolPtr makes a *bool out of the value passed in through v.
//
// vSphere uses nil values in bools to omit values in the SOAP XML request, and
// helps denote inheritance in certain cases.
func boolPtr(v bool) *bool {
return &v
}

0 comments on commit b061d07

Please sign in to comment.