Skip to content

Commit

Permalink
added delete and testing connection functionality for FR (#126)
Browse files Browse the repository at this point in the history
* added delete and testing connection functionality for FR
  • Loading branch information
AnikaAgiwal2711 authored Jun 17, 2024
1 parent 3e3d60c commit 26a7b35
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
21 changes: 21 additions & 0 deletions inttests/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,24 @@ func TestApproveUnsignedFile(t *testing.T) {
err := GC.ApproveUnsignedFile(unsigned)
assert.Nil(t, err)
}

func TestDeleteFirmwareRepository(t *testing.T) {
var id string
if os.Getenv("GOSCALEIO_COMPLIANCE_FILE_ID_FOR_DELETE") != "" {
id = os.Getenv("GOSCALEIO_COMPLIANCE_FILE_ID_FOR_DELETE")
}
err := GC.DeleteFirmwareRepository(id)
assert.Nil(t, err)
}

func TestConnection(t *testing.T) {
var sourceLocation string
if os.Getenv("GOSCALEIO_COMPLIANCE_ENDPOINT") != "" {
sourceLocation = os.Getenv("GOSCALEIO_COMPLIANCE_ENDPOINT")
}
ucParam := &types.UploadComplianceParam{
SourceLocation: sourceLocation,
}
err := GC.TestConnection(ucParam)
assert.Nil(t, err)
}
69 changes: 69 additions & 0 deletions upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,3 +298,72 @@ func (gc *GatewayClient) GetFirmwareRepositoryDetailsUsingName(name string) (*ty
}
return frDetails, err
}

// DeleteFirmwareRepository deletes the particular firmware repository
func (gc *GatewayClient) DeleteFirmwareRepository(id string) error {
req, httpError := http.NewRequest(http.MethodDelete, gc.host+"/Api/V1/FirmwareRepository/"+id, nil)
if httpError != nil {
return httpError
}

req.Header.Set("Authorization", "Bearer "+gc.token)
setCookieError := setCookie(req.Header, gc.host)
if setCookieError != nil {
return fmt.Errorf("Error While Handling Cookie: %s", setCookieError)
}
req.Header.Set("Content-Type", "application/json")

client := gc.http
httpResp, httpRespError := client.Do(req)
if httpRespError != nil {
return httpRespError
}

if httpResp.StatusCode != http.StatusNoContent {
return fmt.Errorf("Error while deleting firmware repository")
}

err3 := storeCookie(httpResp.Header, gc.host)
if err3 != nil {
return fmt.Errorf("Error While Storing cookie: %s", err3)
}

return nil
}

// TestConnection tests the connection to the source location.
func (gc *GatewayClient) TestConnection(uploadComplianceParam *types.UploadComplianceParam) error {
jsonData, err := json.Marshal(uploadComplianceParam)
if err != nil {
return err
}

req, httpError := http.NewRequest(http.MethodPost, gc.host+"/Api/V1/FirmwareRepository/connection", bytes.NewBuffer(jsonData))
if httpError != nil {
return httpError
}

req.Header.Set("Authorization", "Bearer "+gc.token)
setCookieError := setCookie(req.Header, gc.host)
if setCookieError != nil {
return fmt.Errorf("Error While Handling Cookie: %s", setCookieError)
}
req.Header.Set("Content-Type", "application/json")

client := gc.http
httpResp, httpRespError := client.Do(req)
if httpRespError != nil {
return httpRespError
}

if httpResp.StatusCode != http.StatusNoContent {
return fmt.Errorf("Error while connecting to the source location. Please chack the credentials")
}

err3 := storeCookie(httpResp.Header, gc.host)
if err3 != nil {
return fmt.Errorf("Error While Storing cookie: %s", err3)
}

return nil
}

0 comments on commit 26a7b35

Please sign in to comment.