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

added delete and testing connection functionality for FR #126

Merged
merged 3 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
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
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
}