Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance client call code #1897

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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