-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvmUtilsExample.go
More file actions
86 lines (77 loc) · 2.43 KB
/
Copy pathvmUtilsExample.go
File metadata and controls
86 lines (77 loc) · 2.43 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"encoding/base64"
"fmt"
"math/rand"
"github.com/Azure/azure-sdk-for-go/management"
"github.com/Azure/azure-sdk-for-go/management/hostedservice"
"github.com/Azure/azure-sdk-for-go/management/storageservice"
"github.com/Azure/azure-sdk-for-go/management/virtualmachine"
"github.com/Azure/azure-sdk-for-go/management/vmutils"
)
func main() {
dnsName := "test-vm-from-go"
storageAccount := fmt.Sprintf("stoacc%v", randomString(5))
location := "central us"
vmSize := "Small"
vmImage := "b39f27a8b8c64d52b05eac6a62ebad85__Ubuntu-14_04-LTS-amd64-server-20140724-en-us-30GB"
userName := "testuser"
userPassword := "Test123"
fmt.Println("Create client...")
client, err := management.ClientFromPublishSettingsFile("credentials.publishsettings", "")
if err != nil {
panic(err)
}
fmt.Println("Create hosted service...")
// create hosted service
if err := hostedservice.NewClient(client).CreateHostedService(hostedservice.CreateHostedServiceParameters{
ServiceName: dnsName,
Location: location,
Label: base64.StdEncoding.EncodeToString([]byte(dnsName))}); err != nil {
panic(err)
}
fmt.Println("Create storage account...")
_, err = storageservice.NewClient(client).CreateStorageService(storageservice.StorageAccountCreateParameters{
ServiceName: storageAccount,
Label: base64.URLEncoding.EncodeToString([]byte(storageAccount)),
Location: location,
AccountType: storageservice.AccountTypeStandardLRS})
if err != nil {
panic(err)
}
fmt.Println("Create VM...")
// create virtual machine
role := vmutils.NewVMConfiguration(dnsName, vmSize)
vmutils.ConfigureDeploymentFromPlatformImage(
&role,
vmImage,
fmt.Sprintf("http://%s.blob.core.windows.net/sdktest/%s.vhd", storageAccount, dnsName),
"")
vmutils.ConfigureForLinux(&role, dnsName, userName, userPassword)
vmutils.ConfigureWithPublicSSH(&role)
fmt.Println("Deploy...")
operationID, err := virtualmachine.NewClient(client).
CreateDeployment(role, dnsName, virtualmachine.CreateDeploymentOptions{})
if err != nil {
panic(err)
}
fmt.Println("Waiting for deployment to finish...")
if err := client.WaitForOperation(operationID, nil); err != nil {
panic(err)
}
}
func randomString(strLen int) string {
ran := 'z' - 'a' + 10
text := make([]byte, strLen)
for i := range text {
char := rand.Int()
char %= int(ran)
if char <= 9 {
char += '0'
} else {
char += 'a' - 10
}
text[i] = byte(char)
}
return string(text)
}