Skip to content

Instantly share code, notes, and snippets.

@clstokes
Last active April 14, 2021 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clstokes/01260b82dca3240c5d4b6207870f2d96 to your computer and use it in GitHub Desktop.
Save clstokes/01260b82dca3240c5d4b6207870f2d96 to your computer and use it in GitHub Desktop.
import { Input } from "@pulumi/pulumi"
import * as pulumi from "@pulumi/pulumi";
import * as network from "@pulumi/azure-native/network";
import * as resources from "@pulumi/azure-native/resources";
interface NetworkArgs {
resourceGroup: resources.ResourceGroup;
cidrBlock: Input<string>;
subnetCidrBlocks: Input<string>[];
tags?: pulumi.Input<{
[key: string]: pulumi.Input<string>;
}>;
};
export class Network extends pulumi.ComponentResource {
public readonly network: network.VirtualNetwork;
public readonly subnets: network.Subnet[];
constructor(name: string, args: NetworkArgs, opts?: pulumi.ComponentResourceOptions) {
super("custom:x:Network", name, args, opts);
this.network = new network.VirtualNetwork(`${name}-vnet`, {
resourceGroupName: args.resourceGroup.name,
virtualNetworkName: `${name}-vnet`,
addressSpace: { addressPrefixes: [args.cidrBlock] },
tags: args.tags,
}, { parent: this });
this.subnets = [];
for (let i = 0; i < args.subnetCidrBlocks.length; i++) {
const subnet = new network.Subnet(`${name}-subnet-${i}`, {
resourceGroupName: args.resourceGroup.name,
virtualNetworkName: this.network.name,
subnetName: `${name}-subnet-${i}`,
addressPrefix: args.subnetCidrBlocks[i],
}, { parent: this.network, });
this.subnets.push(subnet);
}
this.registerOutputs({
network: this.network,
subnets: this.subnets,
});
}
}
import * as pulumi from "@pulumi/pulumi";
import * as resources from "@pulumi/azure-native/resources";
import * as components from "./components";
const resourceGroup = new resources.ResourceGroup("main");
const network = new components.Network("main", {
resourceGroup: resourceGroup,
cidrBlock: "10.0.0.0/22",
subnetCidrBlocks: ["10.0.0.0/24", "10.0.1.0/24"],
tags: {
project: pulumi.getProject(),
stack: pulumi.getStack(),
},
}, { parent: resourceGroup });
export const networkId = network.network.id;
export const subnetIds = network.subnets.map(it => it.id);
{
"name": "p-azure-subnet",
"devDependencies": {
"@types/node": "^10.0.0"
},
"dependencies": {
"@pulumi/azure-native": "^0.7.0",
"@pulumi/pulumi": "^2.0.0"
}
}
name: p-azure-subnet
runtime: nodejs
description: A minimal Azure Native TypeScript Pulumi program
{
"compilerOptions": {
"strict": true,
"outDir": "bin",
"target": "es2016",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"experimentalDecorators": true,
"pretty": true,
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.ts"
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment