-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
104 lines (90 loc) · 3 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"context"
"log"
"time"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2020-10-01/resources"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/azure/auth"
"github.com/Azure/go-autorest/autorest/to"
)
const (
tenantID = "[tenantID]"
subscriptionID = "[subscriptionID]"
clientID = "[clientID]"
clientSecret = "[clientSecret]"
)
var (
ctx = context.Background()
authorizer autorest.Authorizer
)
func init() {
var err error
authorizer, err = auth.NewClientCredentialsConfig(clientID, clientSecret, tenantID).Authorizer()
if err != nil {
log.Fatalf("Failed to get OAuth config: %v", err)
}
}
func main() {
//创建资源组
resourceGroupName := "TestBatchVMGroup1"
resourceGroupLocation := "eastus"
group, err := CreateGroup(resourceGroupName, resourceGroupLocation)
if err != nil {
log.Fatalf("failed to create group: %v", err)
}
log.Printf("Created group: %v", *group.Name)
//创建部署
templateURL := "https://raw.githubusercontent.com/SnowOpen/AzureARMTemplate/main/BatchCreateVMs-Template.json"
deploymentName := "deploy-" + time.Now().UTC().Format("20060102150405")
log.Printf("Starting deployment: %s", deploymentName)
result, err := CreateDeployment(deploymentName, resourceGroupName, &templateURL)
if err != nil {
log.Fatalf("Failed to deploy: %v", err)
}
if result.Name != nil {
log.Printf("Completed deployment %v: %v", deploymentName, result.Properties.ProvisioningState)
} else {
log.Printf("Completed deployment %v (no data returned to SDK)", deploymentName)
}
}
//CreateGroup Create the Resource Group
func CreateGroup(resourceGroupName string, resourceGroupLocation string) (group resources.Group, err error) {
groupsClient := resources.NewGroupsClient(subscriptionID)
groupsClient.Authorizer = authorizer
return groupsClient.CreateOrUpdate(
ctx,
resourceGroupName,
resources.Group{
Location: to.StringPtr(resourceGroupLocation)})
}
//CreateDeployment Create the deployment
func CreateDeployment(deploymentName string, resourceGroupName string, templateURL *string) (deployment resources.DeploymentExtended, err error) {
params := make(map[string]interface{})
params["adminPassword"] = map[string]string{
"value": "password123!@#",
}
templateLink := resources.TemplateLink{URI: templateURL}
deploymentsClient := resources.NewDeploymentsClient(subscriptionID)
deploymentsClient.Authorizer = authorizer
deploymentFuture, err := deploymentsClient.CreateOrUpdate(
ctx,
resourceGroupName,
deploymentName,
resources.Deployment{
Properties: &resources.DeploymentProperties{
TemplateLink: &templateLink,
Parameters: params,
Mode: resources.Incremental,
},
},
)
if err != nil {
return
}
err = deploymentFuture.WaitForCompletionRef(ctx, deploymentsClient.BaseClient.Client)
if err != nil {
return
}
return deploymentFuture.Result(deploymentsClient)
}