diff --git a/inttests/system_limit_test.go b/inttests/system_limit_test.go new file mode 100644 index 0000000..5759d32 --- /dev/null +++ b/inttests/system_limit_test.go @@ -0,0 +1,37 @@ +// Copyright © 2021 - 2023 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 ( + "fmt" + "testing" + + "github.com/stretchr/testify/assert" +) + +// TestGetSystemLimits gets the list of system limits +func TestGetSystemLimits(t *testing.T) { + resp, err := C.GetSystemLimits() + fmt.Println("systemlimit", resp) + assert.NotNil(t, resp) + assert.Nil(t, err) +} + +// TestGetMaxVol get the max volume +func TestGetMaxVol(t *testing.T) { + maxvolsize, err := C.GetMaxVol() + fmt.Println("max vol size", maxvolsize) + assert.NotNil(t, maxvolsize) + assert.Nil(t, err) + +} diff --git a/system_limit.go b/system_limit.go new file mode 100644 index 0000000..b83f2ee --- /dev/null +++ b/system_limit.go @@ -0,0 +1,54 @@ +// Copyright © 2019 - 2023 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 ( + "errors" + "net/http" + "time" + + types "github.com/dell/goscaleio/types/v1" +) + +// GetSystemLimits gets list of sytem limits +func (c *Client) GetSystemLimits() (systemLimits *types.QuerySystemLimitsResponse, err error) { + defer TimeSpent("GetSystemLimits", time.Now()) + var body types.QuerySystemLimitsParam + path := "/api/instances/System/action/querySystemLimits" + err = c.getJSONWithRetry( + http.MethodPost, path, body, &systemLimits) + if err != nil { + return nil, err + } + + return systemLimits, nil +} + +// GetMaxVol returns max volume size in GB +func (c *Client) GetMaxVol() (MaxVolumeSize string, err error) { + defer TimeSpent("GetMaxVol", time.Now()) + sysLimit, err := c.GetSystemLimits() + + if err != nil { + return "", err + } + + for _, systemLimit := range sysLimit.SystemLimitEntryList { + + if systemLimit.Type == "volumeSizeGb" { + return systemLimit.MaxVal, nil + } + + } + return "", errors.New("couldn't get max vol size") +} diff --git a/system_limit_test.go b/system_limit_test.go new file mode 100644 index 0000000..1ecc384 --- /dev/null +++ b/system_limit_test.go @@ -0,0 +1,145 @@ +// Copyright © 2021 - 2023 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 ( + "encoding/json" + "fmt" + "math" + "net/http" + "net/http/httptest" + "testing" + + types "github.com/dell/goscaleio/types/v1" + "github.com/stretchr/testify/assert" +) + +func TestGetSystemLimits(t *testing.T) { + type checkFn func(*testing.T, *types.QuerySystemLimitsResponse, error) + check := func(fns ...checkFn) []checkFn { return fns } + + hasNoError := func(t *testing.T, syslimit *types.QuerySystemLimitsResponse, err error) { + if err != nil { + t.Fatalf("expected no error") + } + } + + hasError := func(t *testing.T, syslimit *types.QuerySystemLimitsResponse, err error) { + if err == nil { + t.Fatalf("expected error") + } + } + + checkLimitType := func(expectedType string) func(t *testing.T, syslimit *types.QuerySystemLimitsResponse, err error) { + return func(t *testing.T, syslimit *types.QuerySystemLimitsResponse, err error) { + if err == nil { + // Add your custom assertions here to check the syslimit.Type. + assert.Equal(t, expectedType, syslimit.SystemLimitEntryList[0].Type) + } + } + } + + checkLimitDescription := func(expectedDescription string) func(t *testing.T, syslimit *types.QuerySystemLimitsResponse, err error) { + return func(t *testing.T, syslimit *types.QuerySystemLimitsResponse, err error) { + if err == nil { + // Add your custom assertions here to check the syslimit.Description. + assert.Equal(t, expectedDescription, syslimit.SystemLimitEntryList[0].Description) + } + } + } + + checkLimitMaxVal := func(expectedMaxVal string) func(t *testing.T, syslimit *types.QuerySystemLimitsResponse, err error) { + return func(t *testing.T, syslimit *types.QuerySystemLimitsResponse, err error) { + if err == nil { + // Add your custom assertions here to check the syslimit.MaxVal. + assert.Equal(t, expectedMaxVal, syslimit.SystemLimitEntryList[0].MaxVal) + } + } + } + + tests := map[string]func(t *testing.T) (*httptest.Server, []checkFn){ + "success": func(t *testing.T) (*httptest.Server, []checkFn) { + href := "/api/instances/System/action/querySystemLimits" + + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + t.Fatal(fmt.Errorf("wrong method. Expected %s; but got %s", http.MethodPost, r.Method)) + } + + if r.URL.Path != href { + t.Fatal(fmt.Errorf("wrong path. Expected %s; but got %s", href, r.URL.Path)) + } + + // Simulate a successful response for GetSystemLimits. + resp := types.QuerySystemLimitsResponse{ + SystemLimitEntryList: []types.SystemLimits{ + { + Type: "volumeSizeGb", + Description: "Maximum volume size in GB", + MaxVal: "1024", + }, + }, + } + + respData, err := json.Marshal(resp) + if err != nil { + t.Fatal(err) + } + fmt.Fprintln(w, string(respData)) + })) + + return ts, check(hasNoError, checkLimitType("volumeSizeGb"), checkLimitDescription("Maximum volume size in GB"), checkLimitMaxVal("1024")) + }, + "not found": func(t *testing.T) (*httptest.Server, []checkFn) { + href := "/api/instances/System/action/querySystemLimits" + + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + t.Fatal(fmt.Errorf("wrong method. Expected %s; but got %s", http.MethodPost, r.Method)) + } + + if r.URL.Path != href { + t.Fatal(fmt.Errorf("wrong path. Expected %s; but got %s", href, r.URL.Path)) + } + + http.Error(w, "nas not found", http.StatusNotFound) + })) + + return ts, check(hasError) + }, + } + + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + ts, checkFns := tc(t) + defer ts.Close() + + // Create a test client and call GetSystemLimits. + //client := NewTestClient(ts.URL) // Replace with your own client creation logic. + client, err := NewClientWithArgs(ts.URL, "", math.MaxInt64, true, false) + client.configConnect.Version = "4.0" + if err != nil { + t.Fatal(err) + } + + sys := System{ + client: client, + } + + resp, err := sys.client.GetSystemLimits() + for _, checkFn := range checkFns { + checkFn(t, resp, err) + } + }) + } +} diff --git a/types/v1/types.go b/types/v1/types.go index 659c5f7..6ec8a3b 100644 --- a/types/v1/types.go +++ b/types/v1/types.go @@ -1738,3 +1738,18 @@ type MDMQueueCommandDetails struct { TargetEntityIdentifier string `json:"targetEntityIdentifier,omitempty"` AllowedPhase string `json:"allowedPhase,omitempty"` } + +// SystemLimits defines struct for system limits +type SystemLimits struct { + Type string `json:"type,omitempty"` + Description string `json:"description,omitempty"` + MaxVal string `json:"maxVal,omitempty"` +} + +// SystemLimitEntryList defines struct for system limit entryList +type QuerySystemLimitsResponse struct { + SystemLimitEntryList []SystemLimits `json:"systemLimitEntryList"` +} + +type QuerySystemLimitsParam struct { +}