From 970807afd3086b311982b42b87bd893900be4a75 Mon Sep 17 00:00:00 2001 From: Shendge Date: Thu, 26 Oct 2023 18:58:56 +0530 Subject: [PATCH 1/2] added methods for fault set data source --- fault_set.go | 51 ++++++++++++++++++++++++++++- fault_set_test.go | 66 ++++++++++++++++++++++++++++++++++++++ inttests/fault_set_test.go | 42 ++++++++++++++++++++++++ types/v1/types.go | 7 ++-- 4 files changed, 162 insertions(+), 4 deletions(-) diff --git a/fault_set.go b/fault_set.go index aeba2a9..06b8e8e 100644 --- a/fault_set.go +++ b/fault_set.go @@ -13,9 +13,12 @@ package goscaleio import ( + "errors" "fmt" - types "github.com/dell/goscaleio/types/v1" "net/http" + "time" + + types "github.com/dell/goscaleio/types/v1" ) // CreateFaultSet creates a fault set @@ -83,3 +86,49 @@ func (system *System) GetFaultSetByID(id string) (*types.FaultSet, error) { } return fs, nil } + +// GetAllFaultSets returns all fault sets on the system +func (sys *System) GetAllFaultSets() ([]types.FaultSet, error) { + defer TimeSpent("FaultSet", time.Now()) + path := "/api/types/FaultSet/instances" + + var faultsets []types.FaultSet + err := sys.client.getJSONWithRetry( + http.MethodGet, path, nil, &faultsets) + if err != nil { + return nil, err + } + + return faultsets, nil +} + +// GetAllFaultSetsSds returns SDS details associated with fault set +func (sys *System) GetAllFaultSetsSds(faultsetid string) ([]types.Sds, error) { + defer TimeSpent("FaultSet", time.Now()) + path := fmt.Sprintf("/api/instances/FaultSet::%v/relationships/Sds", faultsetid) + + var faultsets []types.Sds + err := sys.client.getJSONWithRetry( + http.MethodGet, path, nil, &faultsets) + if err != nil { + return nil, err + } + + return faultsets, nil +} + +// GetFaultSetByName will read the fault set using the name +func (sys *System) GetFaultSetByName(name string) (*types.FaultSet, error) { + allFaultSets, err := sys.GetAllFaultSets() + if err != nil { + return nil, err + } + + for _, faultset := range allFaultSets { + if faultset.Name == name { + return &faultset, nil + } + } + + return nil, errors.New("couldn't find faultset by name") +} diff --git a/fault_set_test.go b/fault_set_test.go index 69f172c..6720345 100644 --- a/fault_set_test.go +++ b/fault_set_test.go @@ -20,6 +20,7 @@ import ( "testing" types "github.com/dell/goscaleio/types/v1" + "github.com/stretchr/testify/assert" ) var ( @@ -266,3 +267,68 @@ func TestDeleteFaultSet(t *testing.T) { }) } } + +func TestGetAllFaultSets(t *testing.T) { + svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusNoContent) + })) + defer svr.Close() + + client, err := NewClientWithArgs(svr.URL, "", math.MaxInt64, true, false) + if err != nil { + t.Fatal(err) + } + + s := System{ + client: client, + } + + faultsets, err := s.GetAllFaultSets() + assert.Equal(t, len(faultsets), 0) + assert.Nil(t, err) +} + +func TestGetAllFaultSetsSds(t *testing.T) { + type testCase struct { + id string + expected error + } + cases := []testCase{ + { + id: "6b2b5ce800000000", + expected: nil, + }, + { + id: "6b2b5ce800000001", + expected: errors.New("Error in get relationship Sds"), + }, + } + + svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + })) + defer svr.Close() + + for _, tc := range cases { + tc := tc + t.Run("", func(ts *testing.T) { + client, err := NewClientWithArgs(svr.URL, "", math.MaxInt64, true, false) + if err != nil { + t.Fatal(err) + } + + s := System{ + client: client, + } + _, err2 := s.GetAllFaultSetsSds(tc.id) + if err2 != nil { + if tc.expected == nil { + t.Errorf("Getting sds related with fault set did not work as expected, \n\tgot: %s \n\twant: %v", err2, tc.expected) + } else { + if err2.Error() != tc.expected.Error() { + t.Errorf("Getting sds related with fault set did not work as expected, \n\tgot: %s \n\twant: %s", err2, tc.expected) + } + } + } + }) + } +} diff --git a/inttests/fault_set_test.go b/inttests/fault_set_test.go index 52bcf32..739e4bd 100644 --- a/inttests/fault_set_test.go +++ b/inttests/fault_set_test.go @@ -64,3 +64,45 @@ func TestCreateModifyDeleteFaultSet(t *testing.T) { err3 := domain.DeleteFaultSet(invalidIdentifier) assert.NotNil(t, err3) } + +func TestGetAllFaultSets(t *testing.T) { + system := getSystem() + assert.NotNil(t, system) + + _, err := system.GetAllFaultSets() + assert.Nil(t, err) +} + +func TestGetSdsFaultSet(t *testing.T) { + system := getSystem() + assert.NotNil(t, system) + + faultsets, err := system.GetAllFaultSets() + assert.Nil(t, err) + + if len(faultsets) > 0 { + _, err = system.GetAllFaultSetsSds(faultsets[0].ID) + assert.Nil(t, err) + } +} + +func TestGetFaultSetByName(t *testing.T) { + domain := getProtectionDomain(t) + system := getSystem() + assert.NotNil(t, domain) + assert.NotNil(t, system) + fsName := fmt.Sprintf("%s-%s", testPrefix, "FaultSet") + + fs := &types.FaultSetParam{ + Name: fsName, + ProtectionDomainID: domain.ProtectionDomain.ID, + } + + // create the fault set + _, err := domain.CreateFaultSet(fs) + assert.Nil(t, err) + + fsDetails, err := system.GetFaultSetByName(fsName) + assert.NotNil(t, fsDetails) + assert.Nil(t, err) +} diff --git a/types/v1/types.go b/types/v1/types.go index 2461054..0130a5b 100644 --- a/types/v1/types.go +++ b/types/v1/types.go @@ -1767,9 +1767,10 @@ type FaultSetResp struct { // FaultSet defines struct for reading the fault set type FaultSet struct { - ID string `json:"id"` - Name string `json:"name"` - ProtectionDomainId string `json:"protectionDomainId"` + ID string `json:"id"` + Name string `json:"name"` + ProtectionDomainId string `json:"protectionDomainId"` + Links []*Link `json:"links"` } // FaultSetRename defines struct for renaming the fault set From 9733bddc445b6e516d2cef1bef688b3c94e87371 Mon Sep 17 00:00:00 2001 From: Shendge Date: Mon, 30 Oct 2023 16:52:24 +0530 Subject: [PATCH 2/2] addressed comments --- fault_set.go | 4 ++-- fault_set_test.go | 2 +- inttests/fault_set_test.go | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/fault_set.go b/fault_set.go index 06b8e8e..01c178d 100644 --- a/fault_set.go +++ b/fault_set.go @@ -102,8 +102,8 @@ func (sys *System) GetAllFaultSets() ([]types.FaultSet, error) { return faultsets, nil } -// GetAllFaultSetsSds returns SDS details associated with fault set -func (sys *System) GetAllFaultSetsSds(faultsetid string) ([]types.Sds, error) { +// GetAllSDSByFaultSetID returns SDS details associated with fault set +func (sys *System) GetAllSDSByFaultSetID(faultsetid string) ([]types.Sds, error) { defer TimeSpent("FaultSet", time.Now()) path := fmt.Sprintf("/api/instances/FaultSet::%v/relationships/Sds", faultsetid) diff --git a/fault_set_test.go b/fault_set_test.go index 6720345..4f54f64 100644 --- a/fault_set_test.go +++ b/fault_set_test.go @@ -319,7 +319,7 @@ func TestGetAllFaultSetsSds(t *testing.T) { s := System{ client: client, } - _, err2 := s.GetAllFaultSetsSds(tc.id) + _, err2 := s.GetAllSDSByFaultSetID(tc.id) if err2 != nil { if tc.expected == nil { t.Errorf("Getting sds related with fault set did not work as expected, \n\tgot: %s \n\twant: %v", err2, tc.expected) diff --git a/inttests/fault_set_test.go b/inttests/fault_set_test.go index 739e4bd..328a493 100644 --- a/inttests/fault_set_test.go +++ b/inttests/fault_set_test.go @@ -81,7 +81,7 @@ func TestGetSdsFaultSet(t *testing.T) { assert.Nil(t, err) if len(faultsets) > 0 { - _, err = system.GetAllFaultSetsSds(faultsets[0].ID) + _, err = system.GetAllSDSByFaultSetID(faultsets[0].ID) assert.Nil(t, err) } }