Skip to content

Commit

Permalink
Refactor: moving inbound_nat_rule out to it's own object
Browse files Browse the repository at this point in the history
  • Loading branch information
tombuildsstuff committed Oct 11, 2018
1 parent b0821a4 commit 1d17f17
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 53 deletions.
64 changes: 64 additions & 0 deletions azurerm/helpers/azure/devtest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package azure

import (
"github.com/Azure/azure-sdk-for-go/services/devtestlabs/mgmt/2016-05-15/dtl"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func SchemaDevTestVirtualMachineInboundNatRule() *schema.Schema {
return &schema.Schema{
Type: schema.TypeSet,
Optional: true,
// since these aren't returned from the API
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"protocol": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
string(dtl.TCP),
string(dtl.UDP),
}, false),
},

"backend_port": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
ValidateFunc: validate.PortNumber,
},

"frontend_port": {
Type: schema.TypeInt,
Computed: true,
},
},
},
}
}

func ExpandDevTestLabVirtualMachineNatRules(input *schema.Set) []dtl.InboundNatRule {
rules := make([]dtl.InboundNatRule, 0)
if input == nil {
return rules
}

for _, val := range input.List() {
v := val.(map[string]interface{})
backendPort := v["backend_port"].(int)
protocol := v["protocol"].(string)

rule := dtl.InboundNatRule{
TransportProtocol: dtl.TransportProtocol(protocol),
BackendPort: utils.Int32(int32(backendPort)),
}

rules = append(rules, rule)
}

return rules
}
56 changes: 3 additions & 53 deletions azurerm/resource_arm_dev_test_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/Azure/azure-sdk-for-go/services/devtestlabs/mgmt/2016-05-15/dtl"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand Down Expand Up @@ -146,36 +147,7 @@ func resourceArmDevTestVirtualMachine() *schema.Resource {
},
},

"inbound_nat_rule": {
Type: schema.TypeSet,
Optional: true,
// since these aren't returned from the API
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"protocol": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
string(dtl.TCP),
string(dtl.UDP),
}, false),
},

"backend_port": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
ValidateFunc: validate.PortNumber,
},

"frontend_port": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},
"inbound_nat_rule": azure.SchemaDevTestVirtualMachineInboundNatRule(),

"notes": {
Type: schema.TypeString,
Expand Down Expand Up @@ -225,7 +197,7 @@ func resourceArmDevTestVirtualMachineCreateUpdate(d *schema.ResourceData, meta i
galleryImageReference := expandDevTestLabVirtualMachineGalleryImageReference(galleryImageReferenceRaw, osType)

natRulesRaw := d.Get("inbound_nat_rule").(*schema.Set)
natRules := expandDevTestLabVirtualMachineNatRules(natRulesRaw)
natRules := azure.ExpandDevTestLabVirtualMachineNatRules(natRulesRaw)

if len(natRules) > 0 && disallowPublicIPAddress {
return fmt.Errorf("If `inbound_nat_rule` is specified then `disallow_public_ip_address` must be set to true.")
Expand Down Expand Up @@ -421,25 +393,3 @@ func flattenDevTestVirtualMachineGalleryImage(input *dtl.GalleryImageReference)

return results
}

func expandDevTestLabVirtualMachineNatRules(input *schema.Set) []dtl.InboundNatRule {
rules := make([]dtl.InboundNatRule, 0)
if input == nil {
return rules
}

for _, val := range input.List() {
v := val.(map[string]interface{})
backendPort := v["backend_port"].(int)
protocol := v["protocol"].(string)

rule := dtl.InboundNatRule{
TransportProtocol: dtl.TransportProtocol(protocol),
BackendPort: utils.Int32(int32(backendPort)),
}

rules = append(rules, rule)
}

return rules
}

0 comments on commit 1d17f17

Please sign in to comment.