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 methods for fault set data source #88

Merged
merged 2 commits into from
Oct 30, 2023
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
51 changes: 50 additions & 1 deletion fault_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

// 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)

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")
}
66 changes: 66 additions & 0 deletions fault_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"testing"

types "github.com/dell/goscaleio/types/v1"
"github.com/stretchr/testify/assert"
)

var (
Expand Down Expand Up @@ -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.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)
} 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)
}
}
}
})
}
}
42 changes: 42 additions & 0 deletions inttests/fault_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.GetAllSDSByFaultSetID(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)
}
7 changes: 4 additions & 3 deletions types/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading