From 305ceda830cadc623c6b99cacdfa874af1eb26f6 Mon Sep 17 00:00:00 2001 From: songjc <969378911@qq.com> Date: Tue, 22 Aug 2023 10:39:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=BB=BC=E5=90=88=E8=BF=90?= =?UTF-8?q?=E6=8E=A7=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/unifyops/client.go | 16 --------- api/unifyops/unifyops.go | 65 ++++++++++++----------------------- api/unifyops/unifyops_test.go | 16 ++++----- 3 files changed, 30 insertions(+), 67 deletions(-) diff --git a/api/unifyops/client.go b/api/unifyops/client.go index 3a27048..9d239fd 100644 --- a/api/unifyops/client.go +++ b/api/unifyops/client.go @@ -28,19 +28,3 @@ func NewClient(baseURL string) *Client { baseURL: baseURL, } } - -// func (c *Client) SendRequest(path string, method string, body []byte) (*http.Response, error) { -// url := c.BaseURL + path -// req, err := http.NewRequest(method, url, bytes.NewBuffer(body)) -// if err != nil { -// return nil, err -// } - -// client := &http.Client{} -// resp, err := client.Do(req) -// if err != nil { -// return nil, err -// } - -// return resp, nil -// } diff --git a/api/unifyops/unifyops.go b/api/unifyops/unifyops.go index 8e17fd2..51e6dd3 100644 --- a/api/unifyops/unifyops.go +++ b/api/unifyops/unifyops.go @@ -17,7 +17,7 @@ type SlwNode struct { SlwRegionID int64 `json:"slwRegionID"` } -func (c *Client) getSlwNodeInfo() (*[]SlwNode, error) { +func (c *Client) GetSlwNodeInfo() (*[]SlwNode, error) { url, err := url.JoinPath(c.baseURL, "/cmdb/resApi/getSlwNodeInfo") if err != nil { return nil, err @@ -40,6 +40,7 @@ func (c *Client) getSlwNodeInfo() (*[]SlwNode, error) { return nil, codeResp.ToError() } + return nil, fmt.Errorf("unknow response content type: %s", contType) } @@ -47,18 +48,18 @@ type Node struct { NodeId int64 `json:"nodeId"` } -type CalResourceData struct { +type ResourceData[T any] struct { Name string `json:"name"` - Total calDetailType `json:"total"` - Available calDetailType `json:"available"` + Total DetailType[T] `json:"total"` + Available DetailType[T] `json:"available"` } -type calDetailType struct { - Unit string - Value int64 +type DetailType[T any] struct { + Unit string `json:"unit"` + Value T `json:"value"` } -func (c *Client) getCPUData(node Node) (*CalResourceData, error) { +func (c *Client) GetCPUData(node Node) (*ResourceData[int64], error) { //TODO 整合成一个接口,参数增加一个resourceType,根据type调用不同的接口 url, err := url.JoinPath(c.baseURL, "/cmdb/resApi/getCPUData") if err != nil { @@ -74,7 +75,7 @@ func (c *Client) getCPUData(node Node) (*CalResourceData, error) { contType := resp.Header.Get("Content-Type") if strings.Contains(contType, myhttp.ContentTypeJSON) { - var codeResp response[CalResourceData] + var codeResp response[ResourceData[int64]] if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil { return nil, fmt.Errorf("parsing response: %w", err) } @@ -89,7 +90,7 @@ func (c *Client) getCPUData(node Node) (*CalResourceData, error) { return nil, fmt.Errorf("unknow response content type: %s", contType) } -func (c *Client) getNPUData(node Node) (*CalResourceData, error) { +func (c *Client) GetNPUData(node Node) (*ResourceData[int64], error) { url, err := url.JoinPath(c.baseURL, "/cmdb/resApi/getNPUData") if err != nil { return nil, err @@ -104,7 +105,7 @@ func (c *Client) getNPUData(node Node) (*CalResourceData, error) { contType := resp.Header.Get("Content-Type") if strings.Contains(contType, myhttp.ContentTypeJSON) { - var codeResp response[CalResourceData] + var codeResp response[ResourceData[int64]] if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil { return nil, fmt.Errorf("parsing response: %w", err) } @@ -119,7 +120,7 @@ func (c *Client) getNPUData(node Node) (*CalResourceData, error) { return nil, fmt.Errorf("unknow response content type: %s", contType) } -func (c *Client) getGPUData(node Node) (*CalResourceData, error) { +func (c *Client) GetGPUData(node Node) (*ResourceData[int64], error) { url, err := url.JoinPath(c.baseURL, "/cmdb/resApi/getGPUData") if err != nil { return nil, err @@ -134,7 +135,7 @@ func (c *Client) getGPUData(node Node) (*CalResourceData, error) { contType := resp.Header.Get("Content-Type") if strings.Contains(contType, myhttp.ContentTypeJSON) { - var codeResp response[CalResourceData] + var codeResp response[ResourceData[int64]] if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil { return nil, fmt.Errorf("parsing response: %w", err) } @@ -149,7 +150,7 @@ func (c *Client) getGPUData(node Node) (*CalResourceData, error) { return nil, fmt.Errorf("unknow response content type: %s", contType) } -func (c *Client) getMLUData(node Node) (*CalResourceData, error) { +func (c *Client) GetMLUData(node Node) (*ResourceData[int64], error) { url, err := url.JoinPath(c.baseURL, "/cmdb/resApi/getMLUData") if err != nil { return nil, err @@ -164,7 +165,7 @@ func (c *Client) getMLUData(node Node) (*CalResourceData, error) { contType := resp.Header.Get("Content-Type") if strings.Contains(contType, myhttp.ContentTypeJSON) { - var codeResp response[CalResourceData] + var codeResp response[ResourceData[int64]] if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil { return nil, fmt.Errorf("parsing response: %w", err) } @@ -179,18 +180,7 @@ func (c *Client) getMLUData(node Node) (*CalResourceData, error) { return nil, fmt.Errorf("unknow response content type: %s", contType) } -type StrResourceData struct { - Name string `json:"name"` - Total strDetailType `json:"total"` - Available strDetailType `json:"available"` -} - -type strDetailType struct { - Unit string - Value float64 -} - -func (c *Client) getStorageData(node Node) (*StrResourceData, error) { +func (c *Client) GetStorageData(node Node) (*ResourceData[float64], error) { url, err := url.JoinPath(c.baseURL, "/cmdb/resApi/getStorageData") if err != nil { return nil, err @@ -205,7 +195,7 @@ func (c *Client) getStorageData(node Node) (*StrResourceData, error) { contType := resp.Header.Get("Content-Type") if strings.Contains(contType, myhttp.ContentTypeJSON) { - var codeResp response[StrResourceData] + var codeResp response[ResourceData[float64]] if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil { return nil, fmt.Errorf("parsing response: %w", err) } @@ -220,18 +210,7 @@ func (c *Client) getStorageData(node Node) (*StrResourceData, error) { return nil, fmt.Errorf("unknow response content type: %s", contType) } -type TotalResourceData struct { - Name string `json:"name"` - Total totalDetailType `json:"total"` - Available totalDetailType `json:"available"` -} - -type totalDetailType struct { - Unit string - Value interface{} -} - -func (c *Client) getMemoryData(node Node) (*StrResourceData, error) { +func (c *Client) GetMemoryData(node Node) (*ResourceData[float64], error) { url, err := url.JoinPath(c.baseURL, "/cmdb/resApi/getMemoryData") if err != nil { return nil, err @@ -246,7 +225,7 @@ func (c *Client) getMemoryData(node Node) (*StrResourceData, error) { contType := resp.Header.Get("Content-Type") if strings.Contains(contType, myhttp.ContentTypeJSON) { - var codeResp response[StrResourceData] + var codeResp response[ResourceData[float64]] if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil { return nil, fmt.Errorf("parsing response: %w", err) } @@ -261,7 +240,7 @@ func (c *Client) getMemoryData(node Node) (*StrResourceData, error) { return nil, fmt.Errorf("unknow response content type: %s", contType) } -func (c *Client) getIndicatorData(node Node) (*[]TotalResourceData, error) { +func (c *Client) GetIndicatorData(node Node) (*[]ResourceData[any], error) { url, err := url.JoinPath(c.baseURL, "/cmdb/resApi/getIndicatorData") if err != nil { return nil, err @@ -276,7 +255,7 @@ func (c *Client) getIndicatorData(node Node) (*[]TotalResourceData, error) { contType := resp.Header.Get("Content-Type") if strings.Contains(contType, myhttp.ContentTypeJSON) { - var codeResp response[[]TotalResourceData] + var codeResp response[[]ResourceData[any]] if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil { return nil, fmt.Errorf("parsing response: %w", err) } diff --git a/api/unifyops/unifyops_test.go b/api/unifyops/unifyops_test.go index 2aaecc5..ed57fce 100644 --- a/api/unifyops/unifyops_test.go +++ b/api/unifyops/unifyops_test.go @@ -11,47 +11,47 @@ func Test_Unify_Ops(t *testing.T) { Convey("测试获取SlwNode信息", t, func() { cli := NewClient("http://101.201.215.165:6000") - slwNodeInfo, err := cli.getSlwNodeInfo() + slwNodeInfo, err := cli.GetSlwNodeInfo() So(err, ShouldBeNil) sNodes := *slwNodeInfo - cpuData, err := cli.getCPUData(Node{ + cpuData, err := cli.GetCPUData(Node{ NodeId: sNodes[0].ID, }) So(err, ShouldBeNil) fmt.Printf("cpuData: %v\n", cpuData) - gpuData, err := cli.getGPUData(Node{ + gpuData, err := cli.GetGPUData(Node{ NodeId: sNodes[0].ID, }) So(err, ShouldBeNil) fmt.Printf("gpuData: %v\n", gpuData) - npuData, err := cli.getNPUData(Node{ + npuData, err := cli.GetNPUData(Node{ NodeId: sNodes[0].ID, }) So(err, ShouldBeNil) fmt.Printf("npuData: %v\n", npuData) - mluData, err := cli.getMLUData(Node{ + mluData, err := cli.GetMLUData(Node{ NodeId: sNodes[0].ID, }) So(err, ShouldBeNil) fmt.Printf("mluData: %v\n", mluData) - storageData, err := cli.getStorageData(Node{ + storageData, err := cli.GetStorageData(Node{ NodeId: sNodes[0].ID, }) So(err, ShouldBeNil) fmt.Printf("storageData: %v\n", storageData) - memoryData, err := cli.getMemoryData(Node{ + memoryData, err := cli.GetMemoryData(Node{ NodeId: sNodes[0].ID, }) So(err, ShouldBeNil) fmt.Printf("memoryData: %v\n", memoryData) - indicatorData, err := cli.getIndicatorData(Node{ + indicatorData, err := cli.GetIndicatorData(Node{ NodeId: sNodes[0].ID, }) So(err, ShouldBeNil)