Skip to content

Commit 9229ae9

Browse files
Service Resource
1 parent 4a71c80 commit 9229ae9

File tree

1 file changed

+57
-9
lines changed

1 file changed

+57
-9
lines changed

service.go

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -195,16 +195,22 @@ func (gc *GatewayClient) UpdateService(deploymentID, deploymentName, deploymentD
195195
}
196196

197197
// Find the component with type "SERVER"
198-
var clonedComponent map[string]interface{}
198+
var serverComponent map[string]interface{}
199199

200200
for _, comp := range components {
201201
comp := comp.(map[string]interface{})
202202
if comp["type"].(string) == "SERVER" {
203-
clonedComponent = comp
203+
serverComponent = comp
204204
break
205205
}
206206
}
207207

208+
// Deep copy the component
209+
clonedComponent := make(map[string]interface{})
210+
for key, value := range serverComponent {
211+
clonedComponent[key] = value
212+
}
213+
208214
// Modify ID and GUID of the cloned component
209215
clonedComponent["id"] = uuid
210216
clonedComponent["name"] = uuid
@@ -216,12 +222,24 @@ func (gc *GatewayClient) UpdateService(deploymentID, deploymentName, deploymentD
216222
clonedComponent["osPuppetCertName"] = nil
217223
clonedComponent["managementIpAddress"] = nil
218224

225+
// Deep copy resources
219226
resources, ok := clonedComponent["resources"].([]interface{})
220227
if !ok {
221-
fmt.Println("Error: components field not found or not a []interface{}")
228+
fmt.Println("Error: resources field not found or not a []interface{}")
222229
return nil, fmt.Errorf("Error While Parsing Response Data For Deployment: %s", ok)
223230
}
224231

232+
clonedResources := make([]interface{}, len(resources))
233+
for i, res := range resources {
234+
resCopy := make(map[string]interface{})
235+
for k, v := range res.(map[string]interface{}) {
236+
resCopy[k] = v
237+
}
238+
clonedResources[i] = resCopy
239+
}
240+
clonedComponent["resources"] = clonedResources
241+
242+
// Exclude list of parameters to skip
225243
excludeList := map[string]bool{
226244
"razor_image": true,
227245
"scaleio_enabled": true,
@@ -230,36 +248,62 @@ func (gc *GatewayClient) UpdateService(deploymentID, deploymentName, deploymentD
230248
"replication_enabled": true,
231249
}
232250

233-
for _, comp := range resources {
251+
// Iterate over resources to modify parameters
252+
for _, comp := range clonedResources {
234253
comp := comp.(map[string]interface{})
235254
if comp["id"].(string) == "asm::server" {
236255

256+
comp["guid"] = nil
257+
237258
parameters, ok := comp["parameters"].([]interface{})
238259
if !ok {
239260
fmt.Println("Error: components field not found or not a []interface{}")
240261
return nil, fmt.Errorf("Error While Parsing Response Data For Deployment: %s", ok)
241262
}
242263

243-
for _, parameter := range parameters {
264+
clonedParams := make([]interface{}, len(parameters))
265+
for i, param := range parameters {
266+
paramCopy := make(map[string]interface{})
267+
for k, v := range param.(map[string]interface{}) {
268+
paramCopy[k] = v
269+
}
270+
clonedParams[i] = paramCopy
271+
}
272+
273+
for _, parameter := range clonedParams {
244274
parameter := parameter.(map[string]interface{})
245275
if !excludeList[parameter["id"].(string)] {
246-
parameter["guid"] = nil
247-
parameter["value"] = nil
276+
277+
if parameter["id"].(string) == "scaleio_mdm_role" {
278+
parameter["guid"] = nil
279+
parameter["value"] = "standby_mdm"
280+
} else {
281+
parameter["guid"] = nil
282+
parameter["value"] = nil
283+
}
284+
248285
}
249286
}
287+
288+
// Update parameters in the component
289+
comp["parameters"] = clonedParams
250290
}
251291
}
252292

253-
clonedComponent["resources"] = resources
254-
255293
// Append the cloned component back to the components array
256294
components = append(components, clonedComponent)
257295

296+
// Update serviceTemplate with modified components
258297
serviceTemplate["components"] = components
259298

299+
// Update deploymentData with modified serviceTemplate
300+
deploymentData["serviceTemplate"] = serviceTemplate
301+
302+
// Update other fields as needed
260303
deploymentData["scaleUp"] = true
261304
deploymentData["retry"] = true
262305

306+
// Marshal deploymentData to JSON
263307
deploymentPayloadJson, _ = json.Marshal(deploymentData)
264308

265309
} else {
@@ -276,6 +320,10 @@ func (gc *GatewayClient) UpdateService(deploymentID, deploymentName, deploymentD
276320
deploymentPayloadJson, _ = json.Marshal(deploymentResponse)
277321
}
278322

323+
fmt.Println("==================================")
324+
325+
fmt.Println(string(deploymentPayloadJson))
326+
279327
req, httpError := http.NewRequest("PUT", gc.host+"/Api/V1/Deployment/"+deploymentID, bytes.NewBuffer(deploymentPayloadJson))
280328
if httpError != nil {
281329
return nil, httpError

0 commit comments

Comments
 (0)