Skip to content

Commit

Permalink
azurerm_dev_test_virtual_network: add support for `shared_public_ip…
Browse files Browse the repository at this point in the history
…_address_configuration` (#26299)

* azurerm_dev_test_virtual_network: add support for shared_public_ip_address_configuration

* Rename attribute, update test

* Address comments, remove redundant lines
  • Loading branch information
jiaweitao001 authored Jul 10, 2024
1 parent b0f8938 commit d10895e
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 4 deletions.
101 changes: 97 additions & 4 deletions internal/services/devtestlabs/dev_test_virtual_network_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,37 @@ func resourceArmDevTestVirtualNetwork() *pluginsdk.Resource {
Default: string(virtualnetworks.UsagePermissionTypeAllow),
ValidateFunc: validate.DevTestVirtualNetworkUsagePermissionType(),
},

"shared_public_ip_address": {
Type: pluginsdk.TypeList,
Optional: true,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"allowed_ports": {
Type: pluginsdk.TypeList,
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"backend_port": {
Type: pluginsdk.TypeInt,
Optional: true,
},

"transport_protocol": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
string(virtualnetworks.TransportProtocolTcp),
string(virtualnetworks.TransportProtocolUdp),
}, false),
},
},
},
},
},
},
},
},
},
},
Expand Down Expand Up @@ -305,18 +336,48 @@ func expandDevTestVirtualNetworkSubnets(input []interface{}, subscriptionId, res

for _, val := range input {
v := val.(map[string]interface{})

subnet := virtualnetworks.SubnetOverride{
ResourceId: pointer.To(subnetId.ID()),
LabSubnetName: pointer.To(name),
UsePublicIPAddressPermission: pointer.To(virtualnetworks.UsagePermissionType(v["use_public_ip_address"].(string))),
UseInVMCreationPermission: pointer.To(virtualnetworks.UsagePermissionType(v["use_in_virtual_machine_creation"].(string))),
ResourceId: pointer.To(subnetId.ID()),
LabSubnetName: pointer.To(name),
UsePublicIPAddressPermission: pointer.To(virtualnetworks.UsagePermissionType(v["use_public_ip_address"].(string))),
UseInVMCreationPermission: pointer.To(virtualnetworks.UsagePermissionType(v["use_in_virtual_machine_creation"].(string))),
SharedPublicIPAddressConfiguration: expandDevTestVirtualNetworkSubnetIpAddressConfiguration(v["shared_public_ip_address"].([]interface{})),
}
results = append(results, subnet)
}

return &results
}

func expandDevTestVirtualNetworkSubnetIpAddressConfiguration(input []interface{}) *virtualnetworks.SubnetSharedPublicIPAddressConfiguration {
if len(input) == 0 {
return nil
}

v := input[0].(map[string]interface{})

return &virtualnetworks.SubnetSharedPublicIPAddressConfiguration{
AllowedPorts: expandDevTestVirtualNetworkSubnetAllowedPorts(v["allowed_ports"].([]interface{})),
}
}

func expandDevTestVirtualNetworkSubnetAllowedPorts(input []interface{}) *[]virtualnetworks.Port {
results := make([]virtualnetworks.Port, 0)

for _, val := range input {
v := val.(map[string]interface{})

allowedPort := virtualnetworks.Port{
BackendPort: pointer.To(int64(v["backend_port"].(int))),
TransportProtocol: pointer.To(virtualnetworks.TransportProtocol(v["transport_protocol"].(string))),
}
results = append(results, allowedPort)
}

return &results
}

func flattenDevTestVirtualNetworkSubnets(input *[]virtualnetworks.SubnetOverride) []interface{} {
outputs := make([]interface{}, 0)
if input == nil {
Expand All @@ -330,9 +391,41 @@ func flattenDevTestVirtualNetworkSubnets(input *[]virtualnetworks.SubnetOverride
}
output["use_public_ip_address"] = v.UsePublicIPAddressPermission
output["use_in_virtual_machine_creation"] = v.UseInVMCreationPermission
output["shared_public_ip_address"] = flattenDevTestVirtualNetworkSubnetIpAddressConfiguration(v.SharedPublicIPAddressConfiguration)

outputs = append(outputs, output)
}

return outputs
}

func flattenDevTestVirtualNetworkSubnetIpAddressConfiguration(input *virtualnetworks.SubnetSharedPublicIPAddressConfiguration) []interface{} {
outputs := make([]interface{}, 0)

if input == nil {
return outputs
}

output := make(map[string]interface{})
if input.AllowedPorts != nil {
output["allowed_ports"] = flattenDevTestVirtualNetworkSubnetAllowedPorts(input.AllowedPorts)
}
outputs = append(outputs, output)
return outputs
}

func flattenDevTestVirtualNetworkSubnetAllowedPorts(input *[]virtualnetworks.Port) []interface{} {
outputs := make([]interface{}, 0)
if input == nil {
return outputs
}

for _, v := range *input {
output := make(map[string]interface{})
output["backend_port"] = pointer.From(v.BackendPort)
output["transport_protocol"] = pointer.From(v.TransportProtocol)
outputs = append(outputs, output)
}

return outputs
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,16 @@ resource "azurerm_dev_test_virtual_network" "test" {
subnet {
use_public_ip_address = "Deny"
use_in_virtual_machine_creation = "Allow"
shared_public_ip_address {
allowed_ports {
backend_port = 22
transport_protocol = "Tcp"
}
allowed_ports {
backend_port = 80
transport_protocol = "Tcp"
}
}
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger)
Expand Down
16 changes: 16 additions & 0 deletions website/docs/r/dev_test_virtual_network.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ A `subnet` block supports the following:

* `use_in_virtual_machine_creation` - (Optional) Can this subnet be used for creating Virtual Machines? Possible values are `Allow`, `Default` and `Deny`. Defaults to `Allow`.

* `shared_public_ip_address` - (Optional) A `shared_public_ip_address` block as defined below.

---

A `shared_public_ip_address` block supports the following:

* `allowed_ports` - (Optional) A list of `allowed_ports` blocks as defined below.

---

An `allowed_ports` block supports the following:

* `backend_port` - (Optional) The port on the Virtual Machine that the traffic will be sent to.

* `transport_protocol` - (Optional) The transport protocol that the traffic will use. Possible values are `TCP` and `UDP`.

## Attributes Reference

In addition to the Arguments listed above - the following Attributes are exported:
Expand Down

0 comments on commit d10895e

Please sign in to comment.