diff --git a/inttests/upgrade_test.go b/inttests/upgrade_test.go new file mode 100644 index 0000000..8fe5fc0 --- /dev/null +++ b/inttests/upgrade_test.go @@ -0,0 +1,51 @@ +// Copyright © 2024 Dell Inc. or its subsidiaries. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package inttests + +import ( + "os" + "testing" + "time" + + types "github.com/dell/goscaleio/types/v1" + "github.com/stretchr/testify/assert" +) + +// TestDeployUploadPackage function to test upload packge with dummy path of packages +func TestUploadCompliance(t *testing.T) { + var sourceLocation string + if os.Getenv("GOSCALEIO_COMPLIANCE_ENDPOINT") != "" { + sourceLocation = os.Getenv("GOSCALEIO_COMPLIANCE_ENDPOINT") + } + ucParam := &types.UploadComplianceParam{ + SourceLocation: sourceLocation, + } + details, err := GC.UploadCompliance(ucParam) + assert.Nil(t, err) + assert.NotNil(t, details.ID) + assert.NotNil(t, details.State) + time.Sleep(5 * time.Second) + indepthDetails, err := GC.GetUploadComplianceDetails(details.ID) + assert.Nil(t, err) + assert.NotEmpty(t, indepthDetails.ID) + assert.NotEmpty(t, indepthDetails.State) +} + +func TestApproveUnsignedFile(t *testing.T) { + var unsigned string + if os.Getenv("GOSCALEIO_UNSIGNED_COMPLIANCE_FILE_ID") != "" { + unsigned = os.Getenv("GOSCALEIO_UNSIGNED_COMPLIANCE_FILE_ID") + } + err := GC.ApproveUnsignedFile(unsigned) + assert.Nil(t, err) +} diff --git a/types/v1/types.go b/types/v1/types.go index b546a53..2284a62 100644 --- a/types/v1/types.go +++ b/types/v1/types.go @@ -1823,3 +1823,23 @@ type FaultSet struct { type FaultSetRename struct { NewName string `json:"newName"` } + +// UploadComplianceParam defines struct for uploading the compliance file +type UploadComplianceParam struct { + SourceLocation string `json:"sourceLocation"` + Username string `json:"username,omitempty"` + Password string `json:"password,omitempty"` +} + +// UploadComplianceTopologyDetails defines struct which will hold the details of the compliance file upload +type UploadComplianceTopologyDetails struct { + ID string `json:"id"` + Name string `json:"name"` + SourceLocation string `json:"sourceLocation"` + DiskLocation string `json:"diskLocation"` + Filename string `json:"filename"` + Username string `json:"username"` + Password string `json:"password"` + DefaultCatalog bool `json:"defaultCatalog"` + State string `json:"state"` +} diff --git a/upgrade.go b/upgrade.go new file mode 100644 index 0000000..dc71aec --- /dev/null +++ b/upgrade.go @@ -0,0 +1,156 @@ +// Copyright © 2024 Dell Inc. or its subsidiaries. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package goscaleio + +import ( + "bytes" + "encoding/json" + "fmt" + "net/http" + + types "github.com/dell/goscaleio/types/v1" +) + +// UploadCompliance function is used for uploading the compliance file. +func (gc *GatewayClient) UploadCompliance(uploadComplianceParam *types.UploadComplianceParam) (*types.UploadComplianceTopologyDetails, error) { + var uploadResponse types.UploadComplianceTopologyDetails + jsonData, err := json.Marshal(uploadComplianceParam) + if err != nil { + return &uploadResponse, err + } + + req, httpError := http.NewRequest("POST", gc.host+"/Api/V1/FirmwareRepository", bytes.NewBuffer(jsonData)) + if httpError != nil { + return &uploadResponse, httpError + } + + req.Header.Set("Authorization", "Bearer "+gc.token) + setCookieError := setCookie(req.Header, gc.host) + if setCookieError != nil { + return nil, 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 &uploadResponse, httpRespError + } + + responseString, err := extractString(httpResp) + if err != nil { + return &uploadResponse, fmt.Errorf("Error Extracting Response: %s", err) + } + + if httpResp.StatusCode != http.StatusCreated { + return &uploadResponse, fmt.Errorf("Error while uploading Compliance File") + } + + if responseString == "" { + return &uploadResponse, fmt.Errorf("Error while uploading Compliance File") + } + + err = storeCookie(httpResp.Header, gc.host) + if err != nil { + return &uploadResponse, fmt.Errorf("Error While Storing cookie: %s", err) + } + + err = json.Unmarshal([]byte(responseString), &uploadResponse) + if err != nil { + return &uploadResponse, fmt.Errorf("Error getting upload compliance details: %s", err) + } + + return &uploadResponse, nil +} + +// GetUploadComplianceDetails function is used for getting the details of the compliance upload +func (gc *GatewayClient) GetUploadComplianceDetails(id string) (*types.UploadComplianceTopologyDetails, error) { + var getUploadCompResponse types.UploadComplianceTopologyDetails + + req, httpError := http.NewRequest("GET", gc.host+"/Api/V1/FirmwareRepository/"+id, nil) + if httpError != nil { + return &getUploadCompResponse, httpError + } + + req.Header.Set("Authorization", "Bearer "+gc.token) + setCookieError := setCookie(req.Header, gc.host) + if setCookieError != nil { + return nil, 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 &getUploadCompResponse, httpRespError + } + + responseString, err := extractString(httpResp) + if err != nil { + return &getUploadCompResponse, fmt.Errorf("Error Extracting Response: %s", err) + } + + if httpResp.StatusCode != http.StatusOK { + return &getUploadCompResponse, fmt.Errorf("Error while getting Compliance details") + } + + if responseString == "" { + return &getUploadCompResponse, fmt.Errorf("Error Getting Compliance Details") + } + + err3 := storeCookie(httpResp.Header, gc.host) + if err3 != nil { + return &getUploadCompResponse, fmt.Errorf("Error While Storing cookie: %s", err3) + } + + err = json.Unmarshal([]byte(responseString), &getUploadCompResponse) + if err != nil { + return &getUploadCompResponse, fmt.Errorf("Error getting upload compliance details: %s", err) + } + + return &getUploadCompResponse, nil +} + +// ApproveUnsignedFile is used for approving the unsigned file to upload +func (gc *GatewayClient) ApproveUnsignedFile(id string) error { + jsonData := []byte(`{}`) + + req, httpError := http.NewRequest("PUT", gc.host+"/Api/V1/FirmwareRepository/"+id+"/allowunsignedfile", 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 approving the unsigned Compliance file") + } + + err3 := storeCookie(httpResp.Header, gc.host) + if err3 != nil { + return fmt.Errorf("Error While Storing cookie: %s", err3) + } + + return nil +} diff --git a/upgrade_test.go b/upgrade_test.go new file mode 100644 index 0000000..dbea945 --- /dev/null +++ b/upgrade_test.go @@ -0,0 +1,63 @@ +// Copyright © 2024 Dell Inc. or its subsidiaries. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package goscaleio + +import ( + "net/http" + "net/http/httptest" + "testing" + + types "github.com/dell/goscaleio/types/v1" +) + +// This test can be checked when NewGateway() function is fixed +func TestUploadCompliance(t *testing.T) { + t.Skip("Skipping this test case") + type testCase struct { + ucParam *types.UploadComplianceParam + expected error + } + cases := []testCase{ + { + ucParam: &types.UploadComplianceParam{ + SourceLocation: "https://10.10.10.1/artifactory/pfmp20/RCM/Denver/RCMs/SoftwareOnly/PowerFlex_Software_4.5.0.0_287_r1.zip", + }, + expected: nil, + }, + } + svr := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) { + })) + defer svr.Close() + + for _, tc := range cases { + tc := tc + + t.Run("", func(_ *testing.T) { + GC, err := NewGateway(svr.URL, "", "", true, true) + if err != nil { + t.Fatal(err) + } + + _, errFs = GC.UploadCompliance(tc.ucParam) + if errFs != nil { + if tc.expected == nil { + t.Errorf("Uploading Compliance File did not work as expected, \n\tgot: %s \n\twant: %v", errFs, tc.expected) + } else { + if errFs.Error() != tc.expected.Error() { + t.Errorf("Uploading Compliance File did not work as expected, \n\tgot: %s \n\twant: %s", errFs, tc.expected) + } + } + } + }) + } +}