-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathoccm_azure.go
212 lines (176 loc) · 5.88 KB
/
occm_azure.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package cloudmanager
import (
"encoding/json"
"fmt"
"log"
"strings"
"time"
"github.com/fatih/structs"
)
func (c *Client) getCustomData(registerAgentTOService registerAgentTOServiceRequest, proxyCertificates []string, clientID string) (string, string, error) {
accesTokenResult, err := c.getAccessToken()
if err != nil {
return "", "", err
}
log.Print("getAccessToken ", accesTokenResult.Token)
c.Token = accesTokenResult.Token
if c.AccountID == "" {
accountID, err := c.getAccount(clientID)
if err != nil {
return "", "", err
}
log.Print("getAccount ", accountID)
registerAgentTOService.AccountID = accountID
} else {
registerAgentTOService.AccountID = c.AccountID
}
userDataRespone, err := c.registerAgentTOServiceForAzure(registerAgentTOService, clientID)
if err != nil {
return "", "", err
}
newClientID := userDataRespone.ClientID
c.AccountID = userDataRespone.AccountID
userDataRespone.ProxySettings.ProxyCertificates = proxyCertificates
rawUserData, _ := json.MarshalIndent(userDataRespone, "", "\t")
userData := string(rawUserData)
log.Printf("getCustomData: userData %#v %s", userData, newClientID)
return userData, newClientID, nil
}
func (c *Client) registerAgentTOServiceForAzure(registerAgentTOServiceRequest registerAgentTOServiceRequest, clientID string) (createUserData, error) {
baseURL := "/agents-mgmt/connector-setup"
hostType := "CloudManagerHost"
registerAgentTOServiceRequest.Placement.Provider = "AZURE"
params := structs.Map(registerAgentTOServiceRequest)
statusCode, response, _, err := c.CallAPIMethod("POST", baseURL, params, c.Token, hostType, clientID)
if err != nil {
log.Print("registerAgentTOService request failed ", statusCode)
return createUserData{}, err
}
responseError := apiResponseChecker(statusCode, response, "registerAgentTOService")
if responseError != nil {
return createUserData{}, responseError
}
var result createUserData
if err := json.Unmarshal(response, &result); err != nil {
log.Print("Failed to unmarshall response from registerAgentTOService ", err)
return createUserData{}, err
}
result.IgnoreUpgrade = true
return result, nil
}
func (c *Client) createOCCMAzure(occmDetails createOCCMDetails, proxyCertificates []string, clientID string) (OCCMMResult, error) {
log.Print("createOCCMAzure")
var registerAgentTOService registerAgentTOServiceRequest
registerAgentTOService.Name = occmDetails.Name
registerAgentTOService.Placement.Region = occmDetails.Location
registerAgentTOService.Company = occmDetails.Company
if occmDetails.ProxyURL != "" {
registerAgentTOService.Extra.Proxy.ProxyURL = occmDetails.ProxyURL
}
if occmDetails.ProxyUserName != "" {
registerAgentTOService.Extra.Proxy.ProxyUserName = occmDetails.ProxyUserName
}
if occmDetails.ProxyPassword != "" {
registerAgentTOService.Extra.Proxy.ProxyPassword = occmDetails.ProxyPassword
}
if !strings.Contains(occmDetails.VnetID, "/") {
log.Print("Compose vnetID...")
resourceGroup := occmDetails.ResourceGroup
if occmDetails.VnetResourceGroup != "" {
resourceGroup = occmDetails.VnetResourceGroup
}
registerAgentTOService.Placement.Network = fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/virtualNetworks/%s", occmDetails.SubscriptionID, resourceGroup, occmDetails.VnetID)
} else {
registerAgentTOService.Placement.Network = occmDetails.VnetID
}
log.Print("createOCCMAzure VnetID: ")
log.Print(registerAgentTOService.Placement.Network)
if !strings.Contains(occmDetails.SubnetID, "/") {
log.Print("Compose subnetID...")
registerAgentTOService.Placement.Subnet = fmt.Sprintf("%s/subnets/%s", registerAgentTOService.Placement.Network, occmDetails.SubnetID)
} else {
registerAgentTOService.Placement.Subnet = occmDetails.SubnetID
}
log.Print("createOCCMAzure SubnetID: ")
log.Print(registerAgentTOService.Placement.Subnet)
userData, newClientID, err := c.getCustomData(registerAgentTOService, proxyCertificates, clientID)
if err != nil {
return OCCMMResult{}, err
}
log.Print(userData)
log.Printf("deployAzureVM %s client_id %s", occmDetails.Name, newClientID)
c.UserData = userData
var result OCCMMResult
result.ClientID = newClientID
result.AccountID = c.AccountID
principalID, err := c.CallDeployAzureVM(occmDetails)
if err != nil {
return OCCMMResult{}, err
}
result.PrincipalID = principalID
log.Print("Sleep for 2 minutes")
time.Sleep(time.Duration(120) * time.Second)
retries := 26
for {
occmResp, err := c.checkOCCMStatus(newClientID)
if err != nil {
return OCCMMResult{}, err
}
if occmResp.Status == "active" {
break
} else {
if retries == 0 {
log.Print("Taking too long for status to be active")
return OCCMMResult{}, fmt.Errorf("Taking too long for OCCM agent to be active or not properly setup")
}
time.Sleep(time.Duration(30) * time.Second)
retries--
}
}
return result, nil
}
func (c *Client) getdeployAzureVM(occmDetails createOCCMDetails, id string) (string, error) {
log.Print("getdeployAzureVM")
res, err := c.CallGetAzureVM(occmDetails)
if err != nil {
return "", err
}
if res == id {
return res, nil
}
return "", nil
}
func (c *Client) deleteOCCMAzure(request deleteOCCMDetails, clientID string) error {
err := c.CallDeleteAzureVM(request)
if err != nil {
return err
}
log.Print("Sleep for 30 seconds")
time.Sleep(time.Duration(30) * time.Second)
accesTokenResult, err := c.getAccessToken()
c.Token = accesTokenResult.Token
if err != nil {
return err
}
retries := 30
for {
occmResp, err := c.checkOCCMStatus(clientID)
if err != nil {
return err
}
if occmResp.Status != "active" {
break
} else {
if retries == 0 {
log.Print("Taking too long for instance to finish terminating")
return fmt.Errorf("Taking too long for instance to finish terminating")
}
time.Sleep(time.Duration(10) * time.Second)
retries--
}
}
if err := c.callOCCMDelete(clientID); err != nil {
return err
}
return nil
}