Skip to content

Commit

Permalink
Merge pull request #1071 from ninjarobot/subnet-depends-on
Browse files Browse the repository at this point in the history
Subnet: Support 'depends_on'
  • Loading branch information
ninjarobot authored Oct 26, 2023
2 parents a36d177 + 07ea288 commit 73511f3
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 3 deletions.
3 changes: 3 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Release Notes
=============

## 1.7.31
* Subnets: Support for `depends_on` when defining standalone subnets.

## 1.7.30
* Docker Images: Support parsing of tags with one or more colons in tag name.
* Private DNS Zones: Support linking a Private DNS Zone to a Virtual Network to provide DNS resolution within the vnet.
Expand Down
1 change: 1 addition & 0 deletions docs/content/api-overview/resources/vnet.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ The Virtual Network module contains four builders
| associate_service_endpoint_policies | Associates a subnet with an existing service policy. |
| allow_private_endpoints | Enable or disable support for private endpoints, default is `Disabled` |
| private_link_service_network_policies | Enable or disable support for private link service network polices, default is `Disabled` |
| depends_on | Add depdendencies on the deployment of another resource. |

##### Automatically build out an address space: `addressSpace`

Expand Down
5 changes: 3 additions & 2 deletions src/Farmer/Arm/Network.fs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ type Subnet =
AssociatedServiceEndpointPolicies: ResourceId list
PrivateEndpointNetworkPolicies: FeatureFlag option
PrivateLinkServiceNetworkPolicies: FeatureFlag option
Dependencies: ResourceId Set
}

member internal this.JsonModelProperties =
Expand Down Expand Up @@ -397,11 +398,11 @@ type Subnet =
member this.JsonModel =
match this.VirtualNetwork with
| Some (Managed vnet) ->
{| subnets.Create(vnet.Name / this.Name, dependsOn = [ vnet ]) with
{| subnets.Create(vnet.Name / this.Name, dependsOn = (this.Dependencies |> Set.add vnet)) with
properties = this.JsonModelProperties
|}
| Some (Unmanaged vnet) ->
{| subnets.Create(vnet.Name / this.Name) with
{| subnets.Create(vnet.Name / this.Name, dependsOn = this.Dependencies) with
properties = this.JsonModelProperties
|}
| None -> raiseFarmer "Subnet record must be linked to a virtual network to properly assign the resourceId."
Expand Down
1 change: 1 addition & 0 deletions src/Farmer/Builders/Builders.NetworkInterface.fs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ type NetworkInterfaceConfig =
AssociatedServiceEndpointPolicies = []
PrivateEndpointNetworkPolicies = None
PrivateLinkServiceNetworkPolicies = None
Dependencies = Set.empty
}

//ipConfig
Expand Down
1 change: 1 addition & 0 deletions src/Farmer/Builders/Builders.RouteServer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ type RouteServerConfig =
AssociatedServiceEndpointPolicies = []
PrivateEndpointNetworkPolicies = None
PrivateLinkServiceNetworkPolicies = None
Dependencies = Set.empty
}

//ip configuration
Expand Down
10 changes: 10 additions & 0 deletions src/Farmer/Builders/Builders.VirtualNetwork.fs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type SubnetConfig =
AssociatedServiceEndpointPolicies: ResourceId list
AllowPrivateEndpoints: FeatureFlag option
PrivateLinkServiceNetworkPolicies: FeatureFlag option
Dependencies: ResourceId Set
}

member internal this.AsSubnetResource =
Expand All @@ -45,6 +46,7 @@ type SubnetConfig =
// to ENable private endpoints we have to DISable PrivateEndpointNetworkPolicies
PrivateEndpointNetworkPolicies = this.AllowPrivateEndpoints |> Option.map FeatureFlag.invert
PrivateLinkServiceNetworkPolicies = this.PrivateLinkServiceNetworkPolicies
Dependencies = this.Dependencies
}

interface IBuilder with
Expand Down Expand Up @@ -72,6 +74,7 @@ type SubnetBuilder() =
AssociatedServiceEndpointPolicies = []
AllowPrivateEndpoints = None
PrivateLinkServiceNetworkPolicies = None
Dependencies = Set.empty
}

/// Sets the name of the subnet
Expand Down Expand Up @@ -220,6 +223,12 @@ type SubnetBuilder() =
PrivateLinkServiceNetworkPolicies = Some flag
}

interface IDependable<SubnetConfig> with
member _.Add state newDeps =
{ state with
Dependencies = state.Dependencies + newDeps
}

let subnet = SubnetBuilder()

/// Specification for a subnet to build from an address space.
Expand Down Expand Up @@ -665,6 +674,7 @@ type VirtualNetworkBuilder() =
AssociatedServiceEndpointPolicies = serviceEndpointPolicies
AllowPrivateEndpoints = allowPrivateEndpoints
PrivateLinkServiceNetworkPolicies = privateLinkServiceNetworkPolicies
Dependencies = Set.empty
}))

let newAddressSpaces =
Expand Down
1 change: 1 addition & 0 deletions src/Farmer/Builders/Builders.Vm.fs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ type VmConfig =
AssociatedServiceEndpointPolicies = []
PrivateEndpointNetworkPolicies = None
PrivateLinkServiceNetworkPolicies = None
Dependencies = Set.empty
}
]
Tags = this.Tags
Expand Down
35 changes: 34 additions & 1 deletion src/Tests/Network.fs
Original file line number Diff line number Diff line change
Expand Up @@ -590,13 +590,46 @@ let tests =
jobj.SelectToken "resources[?(@.type=='Microsoft.Network/virtualNetworks/subnets')].dependsOn"
:?> Newtonsoft.Json.Linq.JArray

Expect.isNull dependsOn "Linking to unmanaged vnet should have no dependencies"
Expect.isEmpty dependsOn "Linking to unmanaged vnet should have no dependencies"

let subnet =
jobj.SelectToken "resources[?(@.type=='Microsoft.Network/virtualNetworks/subnets')].name"

Expect.equal (string subnet) "my-vnet/services" "Incorrect name on subnet"
}
test "Add multiple subnets linked to existing (unmanaged) vnet with dependencies" {
let vnetName = "my-vnet"

let template =
arm {
add_resources
[
subnet {
name "subnet1"
link_to_unmanaged_vnet (virtualNetworks.resourceId vnetName)
prefix "10.28.0.0/24"
}
subnet {
name "subnet2"
link_to_unmanaged_vnet (virtualNetworks.resourceId vnetName)
prefix "10.28.1.0/24"
depends_on (subnets.resourceId (ResourceName vnetName / ResourceName "subnet1"))
}
]
}

let jobj = template.Template |> Writer.toJson |> Newtonsoft.Json.Linq.JObject.Parse

let dependsOn =
jobj.SelectToken "resources[?(@.name=='my-vnet/subnet2')].dependsOn" :?> Newtonsoft.Json.Linq.JArray

Expect.hasLength dependsOn 1 "subnet2 should have a single dependency"

Expect.equal
(string dependsOn[0])
"[resourceId('Microsoft.Network/virtualNetworks/subnets', 'my-vnet', 'subnet1')]"
"subnet2 should have a dependency on subnet1"
}
test "Standalone subnet without linked vnet not allowed" {
Expect.throws
(fun _ ->
Expand Down

0 comments on commit 73511f3

Please sign in to comment.