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.6 kB

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