|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- package unifyops
-
- import (
- "fmt"
- "net/url"
- "strings"
-
- myhttp "gitlink.org.cn/cloudream/common/utils/http"
- "gitlink.org.cn/cloudream/common/utils/serder"
- )
-
- const CORRECT_CODE int = 200
-
- type SlwNode struct {
- ID int64 `json:"ID"`
- Name string `json:"name"`
- SlwRegionID int64 `json:"slwRegionID"`
- }
-
- func (c *Client) GetSlwNodeInfo() (*[]SlwNode, error) {
- url, err := url.JoinPath(c.baseURL, "/cmdb/resApi/getSlwNodeInfo")
- if err != nil {
- return nil, err
- }
- resp, err := myhttp.GetJSON(url, myhttp.RequestParam{})
- if err != nil {
- return nil, err
- }
- contType := resp.Header.Get("Content-Type")
- if strings.Contains(contType, myhttp.ContentTypeJSON) {
-
- 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 nil, codeResp.ToError()
- }
-
- return nil, fmt.Errorf("unknow response content type: %s", contType)
- }
-
- type Node struct {
- NodeId int64 `json:"nodeId"`
- }
-
- type ResourceData[T any] struct {
- Name string `json:"name"`
- Total DetailType[T] `json:"total"`
- Available DetailType[T] `json:"available"`
- }
-
- type DetailType[T any] struct {
- Unit string `json:"unit"`
- Value T `json:"value"`
- }
-
- func (c *Client) GetCPUData(node Node) (*ResourceData[int64], error) {
- //TODO 整合成一个接口,参数增加一个resourceType,根据type调用不同的接口
- url, err := url.JoinPath(c.baseURL, "/cmdb/resApi/getCPUData")
- if err != nil {
- return nil, err
- }
- resp, err := myhttp.PostJSON(url, myhttp.RequestParam{
- Body: node,
- })
- if err != nil {
- return nil, err
- }
-
- contType := resp.Header.Get("Content-Type")
- if strings.Contains(contType, myhttp.ContentTypeJSON) {
-
- var codeResp response[ResourceData[int64]]
- 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 nil, codeResp.ToError()
- }
-
- return nil, fmt.Errorf("unknow response content type: %s", contType)
- }
-
- func (c *Client) GetNPUData(node Node) (*ResourceData[int64], error) {
- url, err := url.JoinPath(c.baseURL, "/cmdb/resApi/getNPUData")
- if err != nil {
- return nil, err
- }
- resp, err := myhttp.PostJSON(url, myhttp.RequestParam{
- Body: node,
- })
- if err != nil {
- return nil, err
- }
-
- contType := resp.Header.Get("Content-Type")
- if strings.Contains(contType, myhttp.ContentTypeJSON) {
-
- var codeResp response[ResourceData[int64]]
- 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 nil, codeResp.ToError()
- }
-
- return nil, fmt.Errorf("unknow response content type: %s", contType)
- }
-
- func (c *Client) GetGPUData(node Node) (*ResourceData[int64], error) {
- url, err := url.JoinPath(c.baseURL, "/cmdb/resApi/getGPUData")
- if err != nil {
- return nil, err
- }
- resp, err := myhttp.PostJSON(url, myhttp.RequestParam{
- Body: node,
- })
- if err != nil {
- return nil, err
- }
-
- contType := resp.Header.Get("Content-Type")
- if strings.Contains(contType, myhttp.ContentTypeJSON) {
-
- var codeResp response[ResourceData[int64]]
- 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 nil, codeResp.ToError()
- }
-
- return nil, fmt.Errorf("unknow response content type: %s", contType)
- }
-
- func (c *Client) GetMLUData(node Node) (*ResourceData[int64], error) {
- url, err := url.JoinPath(c.baseURL, "/cmdb/resApi/getMLUData")
- if err != nil {
- return nil, err
- }
- resp, err := myhttp.PostJSON(url, myhttp.RequestParam{
- Body: node,
- })
- if err != nil {
- return nil, err
- }
-
- contType := resp.Header.Get("Content-Type")
- if strings.Contains(contType, myhttp.ContentTypeJSON) {
-
- var codeResp response[ResourceData[int64]]
- 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 nil, codeResp.ToError()
- }
-
- return nil, fmt.Errorf("unknow response content type: %s", contType)
- }
-
- func (c *Client) GetStorageData(node Node) (*ResourceData[float64], error) {
- url, err := url.JoinPath(c.baseURL, "/cmdb/resApi/getStorageData")
- if err != nil {
- return nil, err
- }
- resp, err := myhttp.PostJSON(url, myhttp.RequestParam{
- Body: node,
- })
- if err != nil {
- return nil, err
- }
-
- contType := resp.Header.Get("Content-Type")
- if strings.Contains(contType, myhttp.ContentTypeJSON) {
-
- var codeResp response[ResourceData[float64]]
- 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 nil, codeResp.ToError()
- }
-
- return nil, fmt.Errorf("unknow response content type: %s", contType)
- }
-
- func (c *Client) GetMemoryData(node Node) (*ResourceData[float64], error) {
- url, err := url.JoinPath(c.baseURL, "/cmdb/resApi/getMemoryData")
- if err != nil {
- return nil, err
- }
- resp, err := myhttp.PostJSON(url, myhttp.RequestParam{
- Body: node,
- })
- if err != nil {
- return nil, err
- }
-
- contType := resp.Header.Get("Content-Type")
- if strings.Contains(contType, myhttp.ContentTypeJSON) {
-
- var codeResp response[ResourceData[float64]]
- 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 nil, codeResp.ToError()
- }
-
- return nil, fmt.Errorf("unknow response content type: %s", contType)
- }
-
- func (c *Client) GetIndicatorData(node Node) (*[]ResourceData[any], error) {
- url, err := url.JoinPath(c.baseURL, "/cmdb/resApi/getIndicatorData")
- if err != nil {
- return nil, err
- }
- resp, err := myhttp.PostJSON(url, myhttp.RequestParam{
- Body: node,
- })
- if err != nil {
- return nil, err
- }
-
- contType := resp.Header.Get("Content-Type")
- if strings.Contains(contType, myhttp.ContentTypeJSON) {
-
- var codeResp response[[]ResourceData[any]]
- 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 nil, codeResp.ToError()
- }
-
- return nil, fmt.Errorf("unknow response content type: %s", contType)
- }
|