From 46ccbe9c74eb72cfc8ef2c3faef6f369e99b09d2 Mon Sep 17 00:00:00 2001 From: Sydonian <794346190@qq.com> Date: Thu, 2 Nov 2023 15:53:06 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dunifyops=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sdks/unifyops/models.go | 52 +++++++++++++++++----------------- sdks/unifyops/unifyops.go | 10 ++----- sdks/unifyops/unifyops_test.go | 14 ++++----- 3 files changed, 36 insertions(+), 40 deletions(-) diff --git a/sdks/unifyops/models.go b/sdks/unifyops/models.go index d3304a2..8f8c2b9 100644 --- a/sdks/unifyops/models.go +++ b/sdks/unifyops/models.go @@ -19,7 +19,7 @@ const ( ) type SlwNode struct { - ID schsdk.SlwNodeID `json:"ID"` + ID schsdk.SlwNodeID `json:"id"` Name string `json:"name"` SlwRegionID int64 `json:"slwRegionID"` StgNodeID int64 `json:"stgNodeID"` @@ -45,19 +45,19 @@ type ResourceDataBase struct{} func (d *ResourceDataBase) Noop() {} -type DetailType[T any] struct { +type UnitValue[T any] struct { Unit string `json:"unit"` Value T `json:"value"` } type CPUResourceData struct { ResourceDataBase - Name ResourceType `json:"name" union:"CPU"` - Total DetailType[int64] `json:"total"` - Available DetailType[int64] `json:"available"` + Name ResourceType `json:"name" union:"CPU"` + Total UnitValue[int64] `json:"total"` + Available UnitValue[int64] `json:"available"` } -func NewCPUResourceData(total DetailType[int64], available DetailType[int64]) *CPUResourceData { +func NewCPUResourceData(total UnitValue[int64], available UnitValue[int64]) *CPUResourceData { return &CPUResourceData{ Name: ResourceTypeCPU, Total: total, @@ -67,12 +67,12 @@ func NewCPUResourceData(total DetailType[int64], available DetailType[int64]) *C type NPUResourceData struct { ResourceDataBase - Name ResourceType `json:"name" union:"NPU"` - Total DetailType[int64] `json:"total"` - Available DetailType[int64] `json:"available"` + Name ResourceType `json:"name" union:"NPU"` + Total UnitValue[int64] `json:"total"` + Available UnitValue[int64] `json:"available"` } -func NewNPUResourceData(total DetailType[int64], available DetailType[int64]) *NPUResourceData { +func NewNPUResourceData(total UnitValue[int64], available UnitValue[int64]) *NPUResourceData { return &NPUResourceData{ Name: ResourceTypeNPU, Total: total, @@ -82,12 +82,12 @@ func NewNPUResourceData(total DetailType[int64], available DetailType[int64]) *N type GPUResourceData struct { ResourceDataBase - Name ResourceType `json:"name" union:"GPU"` - Total DetailType[int64] `json:"total"` - Available DetailType[int64] `json:"available"` + Name ResourceType `json:"name" union:"GPU"` + Total UnitValue[int64] `json:"total"` + Available UnitValue[int64] `json:"available"` } -func NewGPUResourceData(total DetailType[int64], available DetailType[int64]) *GPUResourceData { +func NewGPUResourceData(total UnitValue[int64], available UnitValue[int64]) *GPUResourceData { return &GPUResourceData{ Name: ResourceTypeGPU, Total: total, @@ -97,12 +97,12 @@ func NewGPUResourceData(total DetailType[int64], available DetailType[int64]) *G type MLUResourceData struct { ResourceDataBase - Name ResourceType `json:"name" union:"MLU"` - Total DetailType[int64] `json:"total"` - Available DetailType[int64] `json:"available"` + Name ResourceType `json:"name" union:"MLU"` + Total UnitValue[int64] `json:"total"` + Available UnitValue[int64] `json:"available"` } -func NewMLUResourceData(total DetailType[int64], available DetailType[int64]) *MLUResourceData { +func NewMLUResourceData(total UnitValue[int64], available UnitValue[int64]) *MLUResourceData { return &MLUResourceData{ Name: ResourceTypeMLU, Total: total, @@ -112,12 +112,12 @@ func NewMLUResourceData(total DetailType[int64], available DetailType[int64]) *M type StorageResourceData struct { ResourceDataBase - Name ResourceType `json:"name" union:"STORAGE"` - Total DetailType[float64] `json:"total"` - Available DetailType[float64] `json:"available"` + Name ResourceType `json:"name" union:"STORAGE"` + Total UnitValue[float64] `json:"total"` + Available UnitValue[float64] `json:"available"` } -func NewStorageResourceData(total DetailType[float64], available DetailType[float64]) *StorageResourceData { +func NewStorageResourceData(total UnitValue[float64], available UnitValue[float64]) *StorageResourceData { return &StorageResourceData{ Name: ResourceTypeStorage, Total: total, @@ -127,12 +127,12 @@ func NewStorageResourceData(total DetailType[float64], available DetailType[floa type MemoryResourceData struct { ResourceDataBase - Name ResourceType `json:"name" union:"MEMORY"` - Total DetailType[float64] `json:"total"` - Available DetailType[float64] `json:"available"` + Name ResourceType `json:"name" union:"MEMORY"` + Total UnitValue[float64] `json:"total"` + Available UnitValue[float64] `json:"available"` } -func NewMemoryResourceData(total DetailType[float64], available DetailType[float64]) *MemoryResourceData { +func NewMemoryResourceData(total UnitValue[float64], available UnitValue[float64]) *MemoryResourceData { return &MemoryResourceData{ Name: ResourceTypeMemory, Total: total, diff --git a/sdks/unifyops/unifyops.go b/sdks/unifyops/unifyops.go index 019857d..75a8649 100644 --- a/sdks/unifyops/unifyops.go +++ b/sdks/unifyops/unifyops.go @@ -12,11 +12,7 @@ import ( const CORRECT_CODE int = 200 -type GetAllSlwNodeInfoResp struct { - Nodes []SlwNode `json:"nodes"` -} - -func (c *Client) GetAllSlwNodeInfo() (*GetAllSlwNodeInfoResp, error) { +func (c *Client) GetAllSlwNodeInfo() ([]SlwNode, error) { url, err := url.JoinPath(c.baseURL, "/cmdb/resApi/getSlwNodeInfo") if err != nil { return nil, err @@ -28,13 +24,13 @@ func (c *Client) GetAllSlwNodeInfo() (*GetAllSlwNodeInfoResp, error) { contType := resp.Header.Get("Content-Type") if strings.Contains(contType, myhttp.ContentTypeJSON) { - var codeResp response[GetAllSlwNodeInfoResp] + var codeResp response[[]SlwNode] if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil { return nil, fmt.Errorf("parsing response: %w", err) } if codeResp.Code == CORRECT_CODE { - return &codeResp.Data, nil + return codeResp.Data, nil } return nil, codeResp.ToError() diff --git a/sdks/unifyops/unifyops_test.go b/sdks/unifyops/unifyops_test.go index c770c78..16dc52b 100644 --- a/sdks/unifyops/unifyops_test.go +++ b/sdks/unifyops/unifyops_test.go @@ -17,43 +17,43 @@ func Test_UnifyOps(t *testing.T) { So(err, ShouldBeNil) cpuData, err := cli.GetCPUData(GetOneResourceDataReq{ - SlwNodeID: slwNodeInfos.Nodes[0].ID, + SlwNodeID: slwNodeInfos[0].ID, }) So(err, ShouldBeNil) fmt.Printf("cpuData: %v\n", cpuData) gpuData, err := cli.GetGPUData(GetOneResourceDataReq{ - SlwNodeID: slwNodeInfos.Nodes[0].ID, + SlwNodeID: slwNodeInfos[0].ID, }) So(err, ShouldBeNil) fmt.Printf("gpuData: %v\n", gpuData) npuData, err := cli.GetNPUData(GetOneResourceDataReq{ - SlwNodeID: slwNodeInfos.Nodes[0].ID, + SlwNodeID: slwNodeInfos[0].ID, }) So(err, ShouldBeNil) fmt.Printf("npuData: %v\n", npuData) mluData, err := cli.GetMLUData(GetOneResourceDataReq{ - SlwNodeID: slwNodeInfos.Nodes[0].ID, + SlwNodeID: slwNodeInfos[0].ID, }) So(err, ShouldBeNil) fmt.Printf("mluData: %v\n", mluData) storageData, err := cli.GetStorageData(GetOneResourceDataReq{ - SlwNodeID: slwNodeInfos.Nodes[0].ID, + SlwNodeID: slwNodeInfos[0].ID, }) So(err, ShouldBeNil) fmt.Printf("storageData: %v\n", storageData) memoryData, err := cli.GetMemoryData(GetOneResourceDataReq{ - SlwNodeID: slwNodeInfos.Nodes[0].ID, + SlwNodeID: slwNodeInfos[0].ID, }) So(err, ShouldBeNil) fmt.Printf("memoryData: %v\n", memoryData) indicatorData, err := cli.GetIndicatorData(GetOneResourceDataReq{ - SlwNodeID: slwNodeInfos.Nodes[0].ID, + SlwNodeID: slwNodeInfos[0].ID, }) So(err, ShouldBeNil) fmt.Printf("indicatorData: %v\n", indicatorData)