Skip to content

Commit

Permalink
Enhance client call code
Browse files Browse the repository at this point in the history
  • Loading branch information
seokho-son committed Oct 28, 2024
1 parent 23c485c commit 03812ad
Showing 1 changed file with 18 additions and 39 deletions.
57 changes: 18 additions & 39 deletions src/core/resource/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,52 +296,31 @@ func RegisterImageWithInfo(nsId string, content *model.TbImageInfo, update bool)
// LookupImageList accepts Spider conn config,
// lookups and returns the list of all images in the region of conn config
// in the form of the list of Spider image objects
func LookupImageList(connConfig string) (model.SpiderImageList, error) {

if connConfig == "" {
content := model.SpiderImageList{}
err := fmt.Errorf("LookupImage() called with empty connConfig.")
log.Error().Err(err).Msg("")
return content, err
}
func LookupImageList(connConfigName string) (model.SpiderImageList, error) {

var callResult model.SpiderImageList
client := resty.New()
url := model.SpiderRestUrl + "/vmimage"

// Create Req body
method := "GET"
requestBody := model.SpiderConnectionName{}
requestBody.ConnectionName = connConfig
requestBody.ConnectionName = connConfigName

client := resty.New().SetCloseConnection(true)
client.SetAllowGetMethodPayload(true)

resp, err := client.R().
SetHeader("Content-Type", "application/json").
SetBody(requestBody).
SetResult(&model.SpiderImageList{}). // or SetResult(AuthSuccess{}).
//SetError(&AuthError{}). // or SetError(AuthError{}).
Get(url)
err := common.ExecuteHttpRequest(
client,
method,
url,
nil,
common.SetUseBody(requestBody),
&requestBody,
&callResult,
common.ShortDuration,
)

if err != nil {
log.Error().Err(err).Msg("")
content := model.SpiderImageList{}
err := fmt.Errorf("an error occurred while requesting to CB-Spider")
return content, err
}

log.Debug().Msg(string(resp.Body()))

// fmt.Println("HTTP Status code: " + strconv.Itoa(resp.StatusCode()))
switch {
case resp.StatusCode() >= 400 || resp.StatusCode() < 200:
err := fmt.Errorf(string(resp.Body()))
log.Error().Err(err).Msg("")
content := model.SpiderImageList{}
return content, err
log.Err(err).Msg("Failed to Lookup Image List from Spider")
return callResult, err
}

temp := resp.Result().(*model.SpiderImageList)
return *temp, nil

return callResult, nil
}

// LookupImage accepts Spider conn config and CSP image ID, lookups and returns the Spider image object
Expand Down

0 comments on commit 03812ad

Please sign in to comment.