-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
157 lines (137 loc) · 4.34 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"context"
"encoding/json"
"io/ioutil"
"log"
"os"
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2020-03-01/network"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2019-05-01/resources"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/Azure/go-autorest/autorest/azure/auth"
"github.com/Azure/go-autorest/autorest/to"
)
const (
resourceGroupName = "edge_nsg"
resourceGroupLocation = "westus2"
deploymentName = "VMDeployEdge"
templateFile = "vm-quickstart-template.json"
parametersFile = "vm-quickstart-params.json"
)
// Information loaded from the authorization file to identify the client
type clientInfo struct {
SubscriptionID string
VMPassword string
}
var (
ctx = context.Background()
clientData clientInfo
authorizer autorest.Authorizer
)
// Authenticate with the Azure services using file-based authentication
func init() {
var err error
authorizer, err = auth.NewAuthorizerFromFile(azure.PublicCloud.ResourceManagerEndpoint)
if err != nil {
log.Fatalf("Failed to get OAuth config: %v", err)
}
authInfo, err := readJSON(os.Getenv("AZURE_AUTH_LOCATION"))
if err != nil {
log.Fatalf("Failed to read JSON: %+v", err)
}
clientData.SubscriptionID = (*authInfo)["subscriptionId"].(string)
clientData.VMPassword = (*authInfo)["clientSecret"].(string)
}
func main() {
// group, err := createGroup()
// if err != nil {
// log.Fatalf("failed to create group: %v", err)
// }
// log.Printf("Created group: %v", *group.Name)
log.Printf("Starting deployment: %s", deploymentName)
result, err := createDeployment()
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)
}
getLogin()
}
// Create a resource group for the deployment.
func createGroup() (group resources.Group, err error) {
groupsClient := resources.NewGroupsClient(clientData.SubscriptionID)
groupsClient.Authorizer = authorizer
return groupsClient.CreateOrUpdate(
ctx,
resourceGroupName,
resources.Group{
Location: to.StringPtr(resourceGroupLocation)})
}
// Create the deployment
func createDeployment() (deployment resources.DeploymentExtended, err error) {
template, err := readJSON(templateFile)
if err != nil {
return
}
params, err := readJSON(parametersFile)
if err != nil {
return
}
(*params)["vm_password"] = map[string]string{
"value": clientData.VMPassword,
}
deploymentsClient := resources.NewDeploymentsClient(clientData.SubscriptionID)
deploymentsClient.Authorizer = authorizer
deploymentFuture, err := deploymentsClient.CreateOrUpdate(
ctx,
resourceGroupName,
deploymentName,
resources.Deployment{
Properties: &resources.DeploymentProperties{
Template: template,
Parameters: params,
Mode: resources.Incremental,
},
},
)
if err != nil {
return
}
err = deploymentFuture.Future.WaitForCompletionRef(ctx, deploymentsClient.BaseClient.Client)
if err != nil {
return
}
return deploymentFuture.Result(deploymentsClient)
}
// Get login information by querying the deployed public IP resource.
func getLogin() {
params, err := readJSON(parametersFile)
if err != nil {
log.Fatalf("Unable to read parameters. Get login information with `az network public-ip list -g %s", resourceGroupName)
}
addressClient := network.NewPublicIPAddressesClient(clientData.SubscriptionID)
addressClient.Authorizer = authorizer
ipName := (*params)["publicIPAddresses_QuickstartVM_ip_name"].(map[string]interface{})
ipAddress, err := addressClient.Get(ctx, resourceGroupName, ipName["value"].(string), "")
if err != nil {
log.Fatalf("Unable to get IP information. Try using `az network public-ip list -g %s", resourceGroupName)
}
vmUser := (*params)["vm_user"].(map[string]interface{})
log.Printf("Log in with ssh: %s@%s, password: %s",
vmUser["value"].(string),
*ipAddress.PublicIPAddressPropertiesFormat.IPAddress,
clientData.VMPassword)
}
func readJSON(path string) (*map[string]interface{}, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
log.Fatalf("failed to read file: %v", err)
}
contents := make(map[string]interface{})
_ = json.Unmarshal(data, &contents)
return &contents, nil
}