From 1c86902b1f402c98eae2d5ca46d469355f4701ed Mon Sep 17 00:00:00 2001 From: Nikita Joshi Date: Thu, 25 Jul 2024 16:27:36 -0700 Subject: [PATCH] OS Repository APIs --- inttests/os_repository_test.go | 91 ++++++++++++++++++++++++++++++++++ os_repository.go | 86 ++++++++++++++++++++++++++++++++ types/v1/types.go | 35 +++++++++++++ 3 files changed, 212 insertions(+) create mode 100644 inttests/os_repository_test.go create mode 100644 os_repository.go diff --git a/inttests/os_repository_test.go b/inttests/os_repository_test.go new file mode 100644 index 0000000..75718fa --- /dev/null +++ b/inttests/os_repository_test.go @@ -0,0 +1,91 @@ +// 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 ( + "testing" + "time" + + types "github.com/dell/goscaleio/types/v1" + "github.com/stretchr/testify/assert" +) + +// TestOSRepositoryGetAll tests get All OS Repositories +func TestOSRepositoryGetAll(t *testing.T) { + system := getSystem() + osRepositories, err := system.GetAllOSRepositories() + assert.Nil(t, err) + assert.NotNil(t, osRepositories) +} + +// TestOSRepositoryGetByID tests Get OS Repository by Id +func TestOSRepositoryGetByID(t *testing.T) { + system := getSystem() + osRepository, err := system.GetOSRepositoryByID("8aaa80458fca6913018fce6449f50e81") + assert.Nil(t, err) + assert.NotNil(t, osRepository) +} + +// TestOSRepositoryGetByIDFail tests the negative case for Get OS Repository by Id +func TestOSRepositoryGetByIDFail(t *testing.T) { + system := getSystem() + _, err := system.GetOSRepositoryByID("Invalid") + assert.NotNil(t, err) +} + +// TestGetOSRepositoryByIDFail tests the negative case for Get OS Repository by Id +func TestOSRepositoryCreateFail(t *testing.T) { + system := getSystem() + _, err := system.CreateOSRepository(nil) + assert.NotNil(t, err) +} + +// TestOSRepositoryDeleteFail tests the negative case for Delete OS Repository +func TestOSRepositoryDeleteFail(t *testing.T) { + system := getSystem() + // Delete + err := system.RemoveOSRepository("invalid") + assert.NotNil(t, err) +} + +// TestOSRepositoryCreateAndDelete tests create and delete operations for OS Repository +func TestOSRepositoryCreateAndDelete(t *testing.T) { + system := getSystem() + createOSRepository := &types.OSRepository{ + Name: "Test-OS-Repository", + RepoType: "ISO", + SourcePath: "https://100.65.27.72/artifactory/vxfm-yum-release/pfmp20/RCM/Denver/RCMs/esxi/ESXi-8.0.0-20513097-3.8.0.0_Dell.iso", + ImageType: "vmware_esxi", + } + // Create + osRepository, err := system.CreateOSRepository(createOSRepository) + assert.Nil(t, err) + assert.NotNil(t, osRepository) + // We will wait for Repository to be unpacked and created + time.Sleep(240 * time.Second) + + var repoID string + osRepositories, err := system.GetAllOSRepositories() + assert.Nil(t, err) + assert.NotNil(t, osRepositories) + for _, repo := range osRepositories { + if repo.Name == "Test-OS-Repository" { + repoID = repo.ID + break + } + } + + // Delete + err = system.RemoveOSRepository(repoID) + assert.Nil(t, err) +} diff --git a/os_repository.go b/os_repository.go new file mode 100644 index 0000000..fe09f4f --- /dev/null +++ b/os_repository.go @@ -0,0 +1,86 @@ +// 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 ( + "fmt" + "net/http" + "time" + + types "github.com/dell/goscaleio/types/v1" +) + +const osRepoPath = "/api/v1/OSRepository" + +// GetAllOSRepositories Gets all OS Repositories +func (s *System) GetAllOSRepositories() ([]types.OSRepository, error) { + defer TimeSpent("GetAllOSRepositories", time.Now()) + + var osRepositories []types.OSRepository + err := s.client.getJSONWithRetry( + http.MethodGet, osRepoPath, nil, &osRepositories) + if err != nil { + return nil, err + } + return osRepositories, nil +} + +// GetOSRepositoryByID Gets OS Repository by ID +func (s *System) GetOSRepositoryByID(id string) (*types.OSRepository, error) { + defer TimeSpent("GetOSRepositoryByID", time.Now()) + + pathWithID := fmt.Sprintf("%v/%v", osRepoPath, id) + var osRepository types.OSRepository + err := s.client.getJSONWithRetry( + http.MethodGet, pathWithID, nil, &osRepository) + if err != nil { + return nil, err + } + + return &osRepository, nil +} + +// CreateOSRepository Creates OS Repository +func (s *System) CreateOSRepository(createOSRepository *types.OSRepository) (*types.OSRepository, error) { + defer TimeSpent("CreateOSRepository", time.Now()) + var createResponse types.OSRepository + if createOSRepository == nil { + return &createResponse, fmt.Errorf("createOSRepository cannot be nil") + } + bodyData := map[string]interface{}{ + "name": createOSRepository.Name, + "repoType": createOSRepository.RepoType, + "sourcePath": createOSRepository.SourcePath, + "imageType": createOSRepository.ImageType, + } + err := s.client.getJSONWithRetry( + http.MethodPost, osRepoPath, bodyData, &createResponse) + if err != nil { + return nil, err + } + + return &createResponse, nil +} + +// RemoveOSRepository Removes OS Repository +func (s *System) RemoveOSRepository(id string) error { + defer TimeSpent("RemoveOSRepository", time.Now()) + pathWithID := fmt.Sprintf("%v/%v", osRepoPath, id) + err := s.client.getJSONWithRetry( + http.MethodDelete, pathWithID, nil, nil) + if err != nil { + return err + } + + return nil +} diff --git a/types/v1/types.go b/types/v1/types.go index 3ade7dc..f715325 100644 --- a/types/v1/types.go +++ b/types/v1/types.go @@ -295,6 +295,41 @@ type Statistics struct { VolumeAddressSpaceInKb int `json:"volumeAddressSpaceInKb"` } +// OSRepository defines struct of OS Repository +type OSRepository struct { + CreatedDate string `json:"createdDate,omitempty"` + ImageType string `json:"imageType,omitempty"` + SourcePath string `json:"sourcePath,omitempty"` + RazorName string `json:"razorName,omitempty"` + InUse bool `json:"inUse,omitempty"` + UserName string `json:"username,omitempty"` + CreatedBy string `json:"createdBy,omitempty"` + Password string `json:"password,omitempty"` + Name string `json:"name,omitempty"` + ID string `json:"id,omitempty"` + State string `json:"state,omitempty"` + RepoType string `json:"repoType,omitempty"` + RCMPath string `json:"rcmPath,omitempty"` + FirmwareRepo FirmwareRepositoryDetails `json:"firmwareRepository,omitempty"` + Metadata OSRepositoryMetadata `json:"metadata,omitempty"` + BaseURL string `json:"baseUrl,omitempty"` + FromWeb bool `json:"fromWeb,omitempty"` +} + +// OSRepositoryMetadata defines struct of Metadata for OS Repository +type OSRepositoryMetadata struct { + Repos []OSRepositoryMetadataRepo `json:"repos,omitempty"` +} + +// OSRepositoryMetadataRepo defines struct of OSRepositoryMetadataRepo +type OSRepositoryMetadataRepo struct { + BasePath string `json:"base_path,omitempty"` + Description string `json:"description,omitempty"` + GPGKey string `json:"gpg_key,omitempty"` + Name string `json:"name,omitempty"` + OSPackages bool `json:"os_packages,omitempty"` +} + // CompatibilityManagement defines struct of CompatibilityManagement type CompatibilityManagement struct { ID string `json:"id,omitempty"`