Skip to content

Commit

Permalink
Service Resource
Browse files Browse the repository at this point in the history
  • Loading branch information
Krunal-Thakkar committed Feb 20, 2024
1 parent 9229ae9 commit cde5ee8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 28 deletions.
24 changes: 12 additions & 12 deletions inttests/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@ import (
)

func TestDeployeService(t *testing.T) {
templates, err := GC.DeployService("Test-Create", "Test", "8150d563-639d-464e-80c4-a435ed10f132", "8aaaee208c8c467e018cd37813250614")
assert.Nil(t, err)
assert.NotNil(t, templates)
deployments, err := GC.DeployService("Test-Create", "Test", "8150d563-639d-464e-80c4-a435ed10f132", "8aaaee208c8c467e018cd37813250614")
assert.NotNil(t, err)
assert.Nil(t, deployments)
}

func TestGetAllDeployeService(t *testing.T) {
templates, err := GC.GetAllServiceDetails()
deployments, err := GC.GetAllServiceDetails()
assert.Nil(t, err)
assert.NotNil(t, templates)
assert.NotNil(t, deployments)

if len(templates) > 0 {
template, err := GC.GetServiceDetailsByID(templates[0].ID, false)
if len(deployments) > 0 {
template, err := GC.GetServiceDetailsByID(deployments[0].ID, false)
assert.Nil(t, err)
assert.NotNil(t, template)

_, err = GC.UpdateService(templates[0].ID, "Test-Update - Krunal", "Test-Update - Krunal", 1)
_, err = GC.UpdateService(deployments[0].ID, "Test-Update-K", "Test-Update-K", 1)
assert.Nil(t, err)
}

Expand All @@ -44,12 +44,12 @@ func TestGetAllDeployeService(t *testing.T) {
}

func TestGetDeployeServiceByName(t *testing.T) {
templates, err := GC.GetAllServiceDetails()
deployments, err := GC.GetAllServiceDetails()
assert.Nil(t, err)
assert.NotNil(t, templates)
assert.NotNil(t, deployments)

if len(templates) > 0 {
template, err := GC.GetServiceDetailsByFilter(templates[0].DeploymentName, "name")
if len(deployments) > 0 {
template, err := GC.GetServiceDetailsByFilter(deployments[0].DeploymentName, "name")
assert.Nil(t, err)
assert.NotNil(t, template)
}
Expand Down
35 changes: 19 additions & 16 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,20 @@ func (gc *GatewayClient) UpdateService(deploymentID, deploymentName, deploymentD

if httpResp.StatusCode == 200 {

var deploymentResponse types.ServiceResponse

parseError := json.Unmarshal([]byte(responseString), &deploymentResponse)
if parseError != nil {
return nil, fmt.Errorf("Error While Parsing Response Data For Deployment: %s", parseError)
}

deployedNodes := deploymentResponse.ServiceTemplate.ServerCount

var deploymentPayloadJson []byte

if nodes > 0 {
nodeDiff := nodes - deployedNodes

if nodeDiff > 0 && nodeDiff == 1 {

var deploymentData map[string]interface{}

Expand All @@ -184,14 +195,12 @@ func (gc *GatewayClient) UpdateService(deploymentID, deploymentName, deploymentD
// Access the "components" field
serviceTemplate, ok := deploymentData["serviceTemplate"].(map[string]interface{})
if !ok {
fmt.Println("Error: serviceTemplate field not found or not a map[string]interface{}")
return nil, fmt.Errorf("Error While Parsing Response Data For Deployment: %s", ok)
return nil, fmt.Errorf("Error While Parsing Response Data For Deployment")
}

components, ok := serviceTemplate["components"].([]interface{})
if !ok {
fmt.Println("Error: components field not found or not a []interface{}")
return nil, fmt.Errorf("Error While Parsing Response Data For Deployment: %s", ok)
return nil, fmt.Errorf("Error While Parsing Response Data For Deployment")
}

// Find the component with type "SERVER"
Expand Down Expand Up @@ -225,8 +234,7 @@ func (gc *GatewayClient) UpdateService(deploymentID, deploymentName, deploymentD
// Deep copy resources
resources, ok := clonedComponent["resources"].([]interface{})
if !ok {
fmt.Println("Error: resources field not found or not a []interface{}")
return nil, fmt.Errorf("Error While Parsing Response Data For Deployment: %s", ok)
return nil, fmt.Errorf("Error While Parsing Response Data For Deployment")
}

clonedResources := make([]interface{}, len(resources))
Expand Down Expand Up @@ -257,8 +265,7 @@ func (gc *GatewayClient) UpdateService(deploymentID, deploymentName, deploymentD

parameters, ok := comp["parameters"].([]interface{})
if !ok {
fmt.Println("Error: components field not found or not a []interface{}")
return nil, fmt.Errorf("Error While Parsing Response Data For Deployment: %s", ok)
return nil, fmt.Errorf("Error While Parsing Response Data For Deployment")
}

clonedParams := make([]interface{}, len(parameters))
Expand Down Expand Up @@ -306,7 +313,7 @@ func (gc *GatewayClient) UpdateService(deploymentID, deploymentName, deploymentD
// Marshal deploymentData to JSON
deploymentPayloadJson, _ = json.Marshal(deploymentData)

} else {
} else if nodeDiff == 0 {

deploymentResponse, jsonParseError := jsonToMap(responseString)
if jsonParseError != nil {
Expand All @@ -318,12 +325,10 @@ func (gc *GatewayClient) UpdateService(deploymentID, deploymentName, deploymentD
deploymentResponse["deploymentDescription"] = deploymentDesc

deploymentPayloadJson, _ = json.Marshal(deploymentResponse)
} else if nodeDiff > 1 || nodeDiff < 0 {
return nil, fmt.Errorf("node difference is more than 1")
}

fmt.Println("==================================")

fmt.Println(string(deploymentPayloadJson))

req, httpError := http.NewRequest("PUT", gc.host+"/Api/V1/Deployment/"+deploymentID, bytes.NewBuffer(deploymentPayloadJson))
if httpError != nil {
return nil, httpError
Expand Down Expand Up @@ -376,8 +381,6 @@ func (gc *GatewayClient) UpdateService(deploymentID, deploymentName, deploymentD
return nil, fmt.Errorf("Error While Parsing Response Data For Deployment: %s", deploymentResponse.Messages[0].DisplayMessage)
}

//return nil, fmt.Errorf("Error While Parsing Response Data For Deployment:")

} else {
var deploymentResponse types.ServiceFailedResponse

Expand Down

0 comments on commit cde5ee8

Please sign in to comment.