From 1d17f1753a50c293d09862cdacc4289d7c8fee3d Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Thu, 11 Oct 2018 13:27:39 -1000 Subject: [PATCH] Refactor: moving `inbound_nat_rule` out to it's own object --- azurerm/helpers/azure/devtest.go | 64 +++++++++++++++++++ .../resource_arm_dev_test_virtual_machine.go | 56 +--------------- 2 files changed, 67 insertions(+), 53 deletions(-) create mode 100644 azurerm/helpers/azure/devtest.go diff --git a/azurerm/helpers/azure/devtest.go b/azurerm/helpers/azure/devtest.go new file mode 100644 index 000000000000..c0daac24adc7 --- /dev/null +++ b/azurerm/helpers/azure/devtest.go @@ -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 +} diff --git a/azurerm/resource_arm_dev_test_virtual_machine.go b/azurerm/resource_arm_dev_test_virtual_machine.go index 868474ecdaa5..bd03b9ae7891 100644 --- a/azurerm/resource_arm_dev_test_virtual_machine.go +++ b/azurerm/resource_arm_dev_test_virtual_machine.go @@ -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" ) @@ -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, @@ -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.") @@ -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 -}