forked from pulumi/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
65 lines (56 loc) · 1.9 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2016-2019, Pulumi Corporation. All rights reserved.
package main
import (
"fmt"
"github.com/pulumi/pulumi-azure/sdk/go/azure/core"
"github.com/pulumi/pulumi-azure/sdk/go/azure/network"
"github.com/pulumi/pulumi/sdk/go/pulumi"
"github.com/pulumi/pulumi/sdk/go/pulumi/config"
)
// Get the desired username and password for our webserver VMs.
func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
config := config.New(ctx, "")
count := config.GetInt("count")
if count == 0 {
count = 2
}
username := config.Require("username")
password := config.Require("password")
// All resources will share a resource group.
rg, err := core.NewResourceGroup(ctx, "server-rg", nil)
if err != nil {
return err
}
// Create a network and subnet for all VMs.
network, err := network.NewVirtualNetwork(ctx, "server-network", &network.VirtualNetworkArgs{
ResourceGroupName: rg.Name,
AddressSpaces: pulumi.StringArray{pulumi.String("10.0.0.0/16")},
Subnets: network.VirtualNetworkSubnetArray{
network.VirtualNetworkSubnetArgs{
Name: pulumi.String("default"),
AddressPrefix: pulumi.String("10.0.1.0/24"),
},
},
})
// Now, allocate a few websever VMs -- by default, just 2, but this is configurable.
var ipAddresses pulumi.StringArray
for i := 0; i < count; i++ {
server, err := NewWebserver(ctx, fmt.Sprintf("ws-%v", i), &WebserverArgs{
Username: pulumi.String(username),
Password: pulumi.String(password),
BootScript: pulumi.String(fmt.Sprintf(`#!/bin/bash
echo "Hello, from Server %v!" > index.html
nohup python -m SimpleHTTPServer 80 &`, i)),
ResourceGroupName: rg.Name,
SubnetID: network.Subnets.Index(pulumi.Int(0)).Id().ToStringPtrOutput().Elem(),
})
if err != nil {
return err
}
ipAddresses = append(ipAddresses, server.GetIPAddress(ctx))
}
ctx.Export("ipAddresses", ipAddresses)
return nil
})
}