You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

unifyops.go 6.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. package unifyops
  2. import (
  3. "fmt"
  4. "net/url"
  5. "strings"
  6. myhttp "gitlink.org.cn/cloudream/common/utils/http"
  7. "gitlink.org.cn/cloudream/common/utils/serder"
  8. )
  9. const CORRECT_CODE int = 200
  10. type SlwNode struct {
  11. ID int64 `json:"ID"`
  12. Name string `json:"name"`
  13. SlwRegionID int64 `json:"slwRegionID"`
  14. }
  15. func (c *Client) GetSlwNodeInfo() (*[]SlwNode, error) {
  16. url, err := url.JoinPath(c.baseURL, "/cmdb/resApi/getSlwNodeInfo")
  17. if err != nil {
  18. return nil, err
  19. }
  20. resp, err := myhttp.GetJSON(url, myhttp.RequestParam{})
  21. if err != nil {
  22. return nil, err
  23. }
  24. contType := resp.Header.Get("Content-Type")
  25. if strings.Contains(contType, myhttp.ContentTypeJSON) {
  26. var codeResp response[[]SlwNode]
  27. if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
  28. return nil, fmt.Errorf("parsing response: %w", err)
  29. }
  30. if codeResp.Code == CORRECT_CODE {
  31. return &codeResp.Data, nil
  32. }
  33. return nil, codeResp.ToError()
  34. }
  35. return nil, fmt.Errorf("unknow response content type: %s", contType)
  36. }
  37. type Node struct {
  38. NodeId int64 `json:"nodeId"`
  39. }
  40. type ResourceData[T any] struct {
  41. Name string `json:"name"`
  42. Total DetailType[T] `json:"total"`
  43. Available DetailType[T] `json:"available"`
  44. }
  45. type DetailType[T any] struct {
  46. Unit string `json:"unit"`
  47. Value T `json:"value"`
  48. }
  49. func (c *Client) GetCPUData(node Node) (*ResourceData[int64], error) {
  50. //TODO 整合成一个接口,参数增加一个resourceType,根据type调用不同的接口
  51. url, err := url.JoinPath(c.baseURL, "/cmdb/resApi/getCPUData")
  52. if err != nil {
  53. return nil, err
  54. }
  55. resp, err := myhttp.PostJSON(url, myhttp.RequestParam{
  56. Body: node,
  57. })
  58. if err != nil {
  59. return nil, err
  60. }
  61. contType := resp.Header.Get("Content-Type")
  62. if strings.Contains(contType, myhttp.ContentTypeJSON) {
  63. var codeResp response[ResourceData[int64]]
  64. if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
  65. return nil, fmt.Errorf("parsing response: %w", err)
  66. }
  67. if codeResp.Code == CORRECT_CODE {
  68. return &codeResp.Data, nil
  69. }
  70. return nil, codeResp.ToError()
  71. }
  72. return nil, fmt.Errorf("unknow response content type: %s", contType)
  73. }
  74. func (c *Client) GetNPUData(node Node) (*ResourceData[int64], error) {
  75. url, err := url.JoinPath(c.baseURL, "/cmdb/resApi/getNPUData")
  76. if err != nil {
  77. return nil, err
  78. }
  79. resp, err := myhttp.PostJSON(url, myhttp.RequestParam{
  80. Body: node,
  81. })
  82. if err != nil {
  83. return nil, err
  84. }
  85. contType := resp.Header.Get("Content-Type")
  86. if strings.Contains(contType, myhttp.ContentTypeJSON) {
  87. var codeResp response[ResourceData[int64]]
  88. if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
  89. return nil, fmt.Errorf("parsing response: %w", err)
  90. }
  91. if codeResp.Code == CORRECT_CODE {
  92. return &codeResp.Data, nil
  93. }
  94. return nil, codeResp.ToError()
  95. }
  96. return nil, fmt.Errorf("unknow response content type: %s", contType)
  97. }
  98. func (c *Client) GetGPUData(node Node) (*ResourceData[int64], error) {
  99. url, err := url.JoinPath(c.baseURL, "/cmdb/resApi/getGPUData")
  100. if err != nil {
  101. return nil, err
  102. }
  103. resp, err := myhttp.PostJSON(url, myhttp.RequestParam{
  104. Body: node,
  105. })
  106. if err != nil {
  107. return nil, err
  108. }
  109. contType := resp.Header.Get("Content-Type")
  110. if strings.Contains(contType, myhttp.ContentTypeJSON) {
  111. var codeResp response[ResourceData[int64]]
  112. if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
  113. return nil, fmt.Errorf("parsing response: %w", err)
  114. }
  115. if codeResp.Code == CORRECT_CODE {
  116. return &codeResp.Data, nil
  117. }
  118. return nil, codeResp.ToError()
  119. }
  120. return nil, fmt.Errorf("unknow response content type: %s", contType)
  121. }
  122. func (c *Client) GetMLUData(node Node) (*ResourceData[int64], error) {
  123. url, err := url.JoinPath(c.baseURL, "/cmdb/resApi/getMLUData")
  124. if err != nil {
  125. return nil, err
  126. }
  127. resp, err := myhttp.PostJSON(url, myhttp.RequestParam{
  128. Body: node,
  129. })
  130. if err != nil {
  131. return nil, err
  132. }
  133. contType := resp.Header.Get("Content-Type")
  134. if strings.Contains(contType, myhttp.ContentTypeJSON) {
  135. var codeResp response[ResourceData[int64]]
  136. if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
  137. return nil, fmt.Errorf("parsing response: %w", err)
  138. }
  139. if codeResp.Code == CORRECT_CODE {
  140. return &codeResp.Data, nil
  141. }
  142. return nil, codeResp.ToError()
  143. }
  144. return nil, fmt.Errorf("unknow response content type: %s", contType)
  145. }
  146. func (c *Client) GetStorageData(node Node) (*ResourceData[float64], error) {
  147. url, err := url.JoinPath(c.baseURL, "/cmdb/resApi/getStorageData")
  148. if err != nil {
  149. return nil, err
  150. }
  151. resp, err := myhttp.PostJSON(url, myhttp.RequestParam{
  152. Body: node,
  153. })
  154. if err != nil {
  155. return nil, err
  156. }
  157. contType := resp.Header.Get("Content-Type")
  158. if strings.Contains(contType, myhttp.ContentTypeJSON) {
  159. var codeResp response[ResourceData[float64]]
  160. if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
  161. return nil, fmt.Errorf("parsing response: %w", err)
  162. }
  163. if codeResp.Code == CORRECT_CODE {
  164. return &codeResp.Data, nil
  165. }
  166. return nil, codeResp.ToError()
  167. }
  168. return nil, fmt.Errorf("unknow response content type: %s", contType)
  169. }
  170. func (c *Client) GetMemoryData(node Node) (*ResourceData[float64], error) {
  171. url, err := url.JoinPath(c.baseURL, "/cmdb/resApi/getMemoryData")
  172. if err != nil {
  173. return nil, err
  174. }
  175. resp, err := myhttp.PostJSON(url, myhttp.RequestParam{
  176. Body: node,
  177. })
  178. if err != nil {
  179. return nil, err
  180. }
  181. contType := resp.Header.Get("Content-Type")
  182. if strings.Contains(contType, myhttp.ContentTypeJSON) {
  183. var codeResp response[ResourceData[float64]]
  184. if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
  185. return nil, fmt.Errorf("parsing response: %w", err)
  186. }
  187. if codeResp.Code == CORRECT_CODE {
  188. return &codeResp.Data, nil
  189. }
  190. return nil, codeResp.ToError()
  191. }
  192. return nil, fmt.Errorf("unknow response content type: %s", contType)
  193. }
  194. func (c *Client) GetIndicatorData(node Node) (*[]ResourceData[any], error) {
  195. url, err := url.JoinPath(c.baseURL, "/cmdb/resApi/getIndicatorData")
  196. if err != nil {
  197. return nil, err
  198. }
  199. resp, err := myhttp.PostJSON(url, myhttp.RequestParam{
  200. Body: node,
  201. })
  202. if err != nil {
  203. return nil, err
  204. }
  205. contType := resp.Header.Get("Content-Type")
  206. if strings.Contains(contType, myhttp.ContentTypeJSON) {
  207. var codeResp response[[]ResourceData[any]]
  208. if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
  209. return nil, fmt.Errorf("parsing response: %w", err)
  210. }
  211. if codeResp.Code == CORRECT_CODE {
  212. return &codeResp.Data, nil
  213. }
  214. return nil, codeResp.ToError()
  215. }
  216. return nil, fmt.Errorf("unknow response content type: %s", contType)
  217. }