Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azurerm_dev_test_virtual_network: add support for shared_public_ip_address_configuration #26299

Merged
merged 3 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
Comment on lines +207 to +210
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Multiple can be specified, can we please add another one to the test as well as test the update of these?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will add.

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
Loading