Skip to content

Commit

Permalink
azurerm_dev_test_virtual_network: add support for shared_public_ip_ad…
Browse files Browse the repository at this point in the history
…dress_configuration
  • Loading branch information
jiaweitao001 committed Jun 25, 2024
1 parent 8f6ec5c commit 2d79a25
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 4 deletions.
110 changes: 106 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_configuration": {
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,53 @@ func expandDevTestVirtualNetworkSubnets(input []interface{}, subscriptionId, res

for _, val := range input {
v := val.(map[string]interface{})
sharedPublicIpAddressConfiguration := expandDevTestVirtualNetworkSubnetIpAddressConfiguration(v["shared_public_ip_address_configuration"].([]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: sharedPublicIpAddressConfiguration,
}
results = append(results, subnet)
}

return &results
}

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

v := input[0].(map[string]interface{})
allowedPortsRaw := v["allowed_ports"].([]interface{})
allowedPorts := expandDevTestVirtualNetworkSubnetAllowedPorts(allowedPortsRaw)

return &virtualnetworks.SubnetSharedPublicIPAddressConfiguration{
AllowedPorts: allowedPorts,
}
}

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

for _, val := range input {
v := val.(map[string]interface{})
backendPort := int64(v["backend_port"].(int))
transportProtocol := virtualnetworks.TransportProtocol(v["transport_protocol"].(string))

allowedPort := virtualnetworks.Port{
BackendPort: pointer.To(backendPort),
TransportProtocol: pointer.To(transportProtocol),
}
results = append(results, allowedPort)
}

return &results
}

func flattenDevTestVirtualNetworkSubnets(input *[]virtualnetworks.SubnetOverride) []interface{} {
outputs := make([]interface{}, 0)
if input == nil {
Expand All @@ -330,9 +396,45 @@ 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_configuration"] = 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{})
if v.BackendPort != nil {
output["backend_port"] = v.BackendPort
}
if v.TransportProtocol != nil {
output["transport_protocol"] = v.TransportProtocol
}
outputs = append(outputs, output)
}

return outputs
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@ resource "azurerm_dev_test_virtual_network" "test" {
subnet {
use_public_ip_address = "Deny"
use_in_virtual_machine_creation = "Allow"
shared_public_ip_address_configuration {
allowed_ports {
backend_port = 22
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_configuration` - (Optional) A `shared_public_ip_address_configuration` block as defined below.

---

A `shared_public_ip_address_configuration` 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 2d79a25

Please sign in to comment.