ovirt.go 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399
  1. package main
  2. import (
  3. "crypto/sha256"
  4. "database/sql"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/dgrijalva/jwt-go"
  8. _ "github.com/go-sql-driver/mysql"
  9. "github.com/labstack/echo"
  10. "io/ioutil"
  11. "net/http"
  12. "strconv"
  13. "strings"
  14. )
  15. type ovirt struct {
  16. }
  17. var MySQLUSER = "apigw"
  18. var MySQLPASS = "^_P+^7Q$bmPj+$xB"
  19. func login(BA string) OvirtObject {
  20. url := OvirtURL + "/ovirt-engine/api"
  21. method := "GET"
  22. client := &http.Client{
  23. }
  24. req, err := http.NewRequest(method, url, nil)
  25. if err != nil {
  26. fmt.Println(err)
  27. }
  28. req.Header.Add("Version", "4")
  29. req.Header.Add("Accept", "application/json")
  30. req.Header.Add("Authorization", BA)
  31. res, err := client.Do(req)
  32. defer res.Body.Close()
  33. body, err := ioutil.ReadAll(res.Body)
  34. object := OvirtObject{}
  35. err = json.Unmarshal(body, &object)
  36. return object
  37. }
  38. type OvirtObject struct {
  39. ProductInfo struct {
  40. Name string `json:"name"`
  41. Version struct {
  42. Build string `json:"build"`
  43. FullVersion string `json:"full_version"`
  44. Major string `json:"major"`
  45. Minor string `json:"minor"`
  46. Revision string `json:"revision"`
  47. } `json:"version"`
  48. } `json:"product_info"`
  49. SpecialObjects struct {
  50. BlankTemplate struct {
  51. Href string `json:"href"`
  52. ID string `json:"id"`
  53. } `json:"blank_template"`
  54. RootTag struct {
  55. Href string `json:"href"`
  56. ID string `json:"id"`
  57. } `json:"root_tag"`
  58. } `json:"special_objects"`
  59. Time int64 `json:"time"`
  60. AuthenticatedUser struct {
  61. Href string `json:"href"`
  62. ID string `json:"id"`
  63. } `json:"authenticated_user"`
  64. EffectiveUser struct {
  65. Href string `json:"href"`
  66. ID string `json:"id"`
  67. } `json:"effective_user"`
  68. Link []struct {
  69. Href string `json:"href"`
  70. Rel string `json:"rel"`
  71. } `json:"link"`
  72. }
  73. type TaskHeader struct {
  74. Name string `json:"Name,omitempty"`
  75. Value string `json:"Value,omitempty"`
  76. }
  77. type addVMTask struct {
  78. URL string `json:"url"`
  79. JSON string `json:"__json"`
  80. Method string `json:"method"`
  81. Headers []struct {
  82. Name string `json:"Name,omitempty"`
  83. Value string `json:"Value,omitempty"`
  84. } `json:"headers"`
  85. }
  86. type VMStatistics struct {
  87. Statistic []struct {
  88. Kind string `json:"kind"`
  89. Type string `json:"type"`
  90. Unit string `json:"unit"`
  91. Values struct {
  92. Value []struct {
  93. Datum float64 `json:"datum"`
  94. } `json:"value"`
  95. } `json:"values"`
  96. VM struct {
  97. Href string `json:"href"`
  98. ID string `json:"id"`
  99. } `json:"vm,omitempty"`
  100. Name string `json:"name"`
  101. Description string `json:"description"`
  102. Href string `json:"href,omitempty"`
  103. ID string `json:"id"`
  104. } `json:"statistic"`
  105. }
  106. func (o ovirt) vmStatus(uuid string) string {
  107. type vmStatus struct {
  108. NextRunConfigurationExists string `json:"next_run_configuration_exists"`
  109. NumaTuneMode string `json:"numa_tune_mode"`
  110. Status string `json:"status"`
  111. StopTime int64 `json:"stop_time"`
  112. OriginalTemplate struct {
  113. Href string `json:"href"`
  114. ID string `json:"id"`
  115. } `json:"original_template"`
  116. Template struct {
  117. Href string `json:"href"`
  118. ID string `json:"id"`
  119. } `json:"template"`
  120. Actions struct {
  121. Link []struct {
  122. Href string `json:"href"`
  123. Rel string `json:"rel"`
  124. } `json:"link"`
  125. } `json:"actions"`
  126. Name string `json:"name"`
  127. Description string `json:"description"`
  128. Comment string `json:"comment"`
  129. Href string `json:"href"`
  130. ID string `json:"id"`
  131. Bios struct {
  132. BootMenu struct {
  133. Enabled string `json:"enabled"`
  134. } `json:"boot_menu"`
  135. Type string `json:"type"`
  136. } `json:"bios"`
  137. CPU struct {
  138. Architecture string `json:"architecture"`
  139. Topology struct {
  140. Cores string `json:"cores"`
  141. Sockets string `json:"sockets"`
  142. Threads string `json:"threads"`
  143. } `json:"topology"`
  144. } `json:"cpu"`
  145. Display struct {
  146. AllowOverride string `json:"allow_override"`
  147. CopyPasteEnabled string `json:"copy_paste_enabled"`
  148. DisconnectAction string `json:"disconnect_action"`
  149. FileTransferEnabled string `json:"file_transfer_enabled"`
  150. Monitors string `json:"monitors"`
  151. SingleQxlPci string `json:"single_qxl_pci"`
  152. SmartcardEnabled string `json:"smartcard_enabled"`
  153. Type string `json:"type"`
  154. } `json:"display"`
  155. Initialization struct {
  156. AuthorizedSSHKeys string `json:"authorized_ssh_keys"`
  157. CloudInitNetworkProtocol string `json:"cloud_init_network_protocol"`
  158. CustomScript string `json:"custom_script"`
  159. HostName string `json:"host_name"`
  160. NicConfigurations struct {
  161. } `json:"nic_configurations"`
  162. RegenerateSSHKeys string `json:"regenerate_ssh_keys"`
  163. UserName string `json:"user_name"`
  164. } `json:"initialization"`
  165. Io struct {
  166. Threads string `json:"threads"`
  167. } `json:"io"`
  168. Memory string `json:"memory"`
  169. Migration struct {
  170. AutoConverge string `json:"auto_converge"`
  171. Compressed string `json:"compressed"`
  172. Encrypted string `json:"encrypted"`
  173. } `json:"migration"`
  174. Origin string `json:"origin"`
  175. Os struct {
  176. Boot struct {
  177. Devices struct {
  178. Device []string `json:"device"`
  179. } `json:"devices"`
  180. } `json:"boot"`
  181. Type string `json:"type"`
  182. } `json:"os"`
  183. Sso struct {
  184. Methods struct {
  185. Method []struct {
  186. ID string `json:"id"`
  187. } `json:"method"`
  188. } `json:"methods"`
  189. } `json:"sso"`
  190. Stateless string `json:"stateless"`
  191. Type string `json:"type"`
  192. Usb struct {
  193. Enabled string `json:"enabled"`
  194. } `json:"usb"`
  195. Cluster struct {
  196. Href string `json:"href"`
  197. ID string `json:"id"`
  198. } `json:"cluster"`
  199. Quota struct {
  200. ID string `json:"id"`
  201. } `json:"quota"`
  202. Link []struct {
  203. Href string `json:"href"`
  204. Rel string `json:"rel"`
  205. } `json:"link"`
  206. CPUShares string `json:"cpu_shares"`
  207. CreationTime int64 `json:"creation_time"`
  208. DeleteProtected string `json:"delete_protected"`
  209. HighAvailability struct {
  210. Enabled string `json:"enabled"`
  211. Priority string `json:"priority"`
  212. } `json:"high_availability"`
  213. LargeIcon struct {
  214. Href string `json:"href"`
  215. ID string `json:"id"`
  216. } `json:"large_icon"`
  217. MemoryPolicy struct {
  218. Ballooning string `json:"ballooning"`
  219. Guaranteed string `json:"guaranteed"`
  220. Max string `json:"max"`
  221. } `json:"memory_policy"`
  222. MigrationDowntime string `json:"migration_downtime"`
  223. MultiQueuesEnabled string `json:"multi_queues_enabled"`
  224. PlacementPolicy struct {
  225. Affinity string `json:"affinity"`
  226. Hosts struct {
  227. Host []struct {
  228. Href string `json:"href"`
  229. ID string `json:"id"`
  230. } `json:"host"`
  231. } `json:"hosts"`
  232. } `json:"placement_policy"`
  233. SmallIcon struct {
  234. Href string `json:"href"`
  235. ID string `json:"id"`
  236. } `json:"small_icon"`
  237. StartPaused string `json:"start_paused"`
  238. StorageErrorResumeBehaviour string `json:"storage_error_resume_behaviour"`
  239. TimeZone struct {
  240. Name string `json:"name"`
  241. } `json:"time_zone"`
  242. CPUProfile struct {
  243. Href string `json:"href"`
  244. ID string `json:"id"`
  245. } `json:"cpu_profile"`
  246. }
  247. //url := "https://ovirt-cl.zi-tel.com/ovirt-engine/api/vms/c1a44128-6c6c-406b-99d8-4a68a99380c9"
  248. url := OvirtURL + "/ovirt-engine/api/vms/" + uuid
  249. //fmt.Println("Url:" ,url)
  250. method := "GET"
  251. client := &http.Client{
  252. }
  253. req, err := http.NewRequest(method, url, nil)
  254. if err != nil {
  255. return "Null"
  256. }
  257. req.Header.Add("Version", "4")
  258. req.Header.Add("Accept", "application/json")
  259. req.Header.Add("Authorization", "Basic YWRtaW5AaW50ZXJuYWw6a2VsYW5zaCBqMw==")
  260. res, err := client.Do(req)
  261. defer res.Body.Close()
  262. body, err := ioutil.ReadAll(res.Body)
  263. //fmt.Println("full response: ",string(body))
  264. _vmstatus := vmStatus{}
  265. _err := json.Unmarshal(body, &_vmstatus)
  266. if _err != nil {
  267. fmt.Println("Error: ", _err)
  268. }
  269. return _vmstatus.Status
  270. }
  271. func (o ovirt) addvm(c echo.Context) error {
  272. type AddVMResponse struct {
  273. NextRunConfigurationExists string `json:"next_run_configuration_exists"`
  274. NumaTuneMode string `json:"numa_tune_mode"`
  275. Status string `json:"status"`
  276. StopTime int64 `json:"stop_time"`
  277. OriginalTemplate struct {
  278. Href string `json:"href"`
  279. ID string `json:"id"`
  280. } `json:"original_template"`
  281. Template struct {
  282. Href string `json:"href"`
  283. ID string `json:"id"`
  284. } `json:"template"`
  285. Actions struct {
  286. Link []struct {
  287. Href string `json:"href"`
  288. Rel string `json:"rel"`
  289. } `json:"link"`
  290. } `json:"actions"`
  291. Name string `json:"name"`
  292. Description string `json:"description"`
  293. Comment string `json:"comment"`
  294. Href string `json:"href"`
  295. ID string `json:"id"`
  296. Bios struct {
  297. BootMenu struct {
  298. Enabled string `json:"enabled"`
  299. } `json:"boot_menu"`
  300. Type string `json:"type"`
  301. } `json:"bios"`
  302. Console struct {
  303. Enabled string `json:"enabled"`
  304. } `json:"console"`
  305. CPU struct {
  306. Architecture string `json:"architecture"`
  307. Topology struct {
  308. Cores string `json:"cores"`
  309. Sockets string `json:"sockets"`
  310. Threads string `json:"threads"`
  311. } `json:"topology"`
  312. } `json:"cpu"`
  313. Display struct {
  314. AllowOverride string `json:"allow_override"`
  315. CopyPasteEnabled string `json:"copy_paste_enabled"`
  316. DisconnectAction string `json:"disconnect_action"`
  317. FileTransferEnabled string `json:"file_transfer_enabled"`
  318. Monitors string `json:"monitors"`
  319. SingleQxlPci string `json:"single_qxl_pci"`
  320. SmartcardEnabled string `json:"smartcard_enabled"`
  321. Type string `json:"type"`
  322. } `json:"display"`
  323. Initialization struct {
  324. AuthorizedSSHKeys string `json:"authorized_ssh_keys"`
  325. CloudInitNetworkProtocol string `json:"cloud_init_network_protocol"`
  326. CustomScript string `json:"custom_script"`
  327. HostName string `json:"host_name"`
  328. NicConfigurations struct {
  329. } `json:"nic_configurations"`
  330. RegenerateSSHKeys string `json:"regenerate_ssh_keys"`
  331. UserName string `json:"user_name"`
  332. } `json:"initialization"`
  333. Io struct {
  334. Threads string `json:"threads"`
  335. } `json:"io"`
  336. Memory string `json:"memory"`
  337. Migration struct {
  338. AutoConverge string `json:"auto_converge"`
  339. Compressed string `json:"compressed"`
  340. Encrypted string `json:"encrypted"`
  341. } `json:"migration"`
  342. Origin string `json:"origin"`
  343. Os struct {
  344. Boot struct {
  345. Devices struct {
  346. Device []string `json:"device"`
  347. } `json:"devices"`
  348. } `json:"boot"`
  349. Type string `json:"type"`
  350. } `json:"os"`
  351. Sso struct {
  352. Methods struct {
  353. Method []struct {
  354. ID string `json:"id"`
  355. } `json:"method"`
  356. } `json:"methods"`
  357. } `json:"sso"`
  358. Stateless string `json:"stateless"`
  359. Type string `json:"type"`
  360. Usb struct {
  361. Enabled string `json:"enabled"`
  362. } `json:"usb"`
  363. Cluster struct {
  364. Href string `json:"href"`
  365. ID string `json:"id"`
  366. } `json:"cluster"`
  367. Quota struct {
  368. ID string `json:"id"`
  369. } `json:"quota"`
  370. CreationStatus string `json:"creation_status"`
  371. Link []struct {
  372. Href string `json:"href"`
  373. Rel string `json:"rel"`
  374. } `json:"link"`
  375. CPUShares string `json:"cpu_shares"`
  376. CreationTime int64 `json:"creation_time"`
  377. DeleteProtected string `json:"delete_protected"`
  378. HighAvailability struct {
  379. Enabled string `json:"enabled"`
  380. Priority string `json:"priority"`
  381. } `json:"high_availability"`
  382. LargeIcon struct {
  383. Href string `json:"href"`
  384. ID string `json:"id"`
  385. } `json:"large_icon"`
  386. MemoryPolicy struct {
  387. Ballooning string `json:"ballooning"`
  388. Guaranteed string `json:"guaranteed"`
  389. Max string `json:"max"`
  390. } `json:"memory_policy"`
  391. MigrationDowntime string `json:"migration_downtime"`
  392. MultiQueuesEnabled string `json:"multi_queues_enabled"`
  393. RngDevice struct {
  394. Source string `json:"source"`
  395. } `json:"rng_device"`
  396. SmallIcon struct {
  397. Href string `json:"href"`
  398. ID string `json:"id"`
  399. } `json:"small_icon"`
  400. SoundcardEnabled string `json:"soundcard_enabled"`
  401. StartPaused string `json:"start_paused"`
  402. StorageErrorResumeBehaviour string `json:"storage_error_resume_behaviour"`
  403. TimeZone struct {
  404. Name string `json:"name"`
  405. } `json:"time_zone"`
  406. VirtioScsi struct {
  407. Enabled string `json:"enabled"`
  408. } `json:"virtio_scsi"`
  409. CPUProfile struct {
  410. Href string `json:"href"`
  411. ID string `json:"id"`
  412. } `json:"cpu_profile"`
  413. }
  414. user := c.Get("user").(*jwt.Token)
  415. claims := user.Claims.(jwt.MapClaims)
  416. _sha256 := sha256.Sum256([]byte(string(claims["name"].(string))))
  417. var hashChannel_ = make(chan []byte, 1)
  418. hashChannel_ <- _sha256[:]
  419. token := decrypt(<-hashChannel_, claims["IPAToken"].(string))
  420. _BA := strings.Split(token, ";")
  421. BA := _BA[len(_BA)-2]
  422. //fmt.Println("Token: ", token)
  423. //fmt.Println("BA: ", BA)
  424. var vmname, vmdescr, vmcomment, templatename, cpuSock, cpuCore, cpuThread, mem string
  425. vmname = c.FormValue("VmName")
  426. vmdescr = c.FormValue("VmDescr")
  427. vmcomment = c.FormValue("VmComment")
  428. templatename = c.FormValue("VmTempl")
  429. cpuThread = c.FormValue("VmCPU")
  430. sshKey := c.FormValue("sshKey")
  431. rootpass := c.FormValue("rootpass")
  432. cpuCore = "1"
  433. cpuSock = "1"
  434. mem = c.FormValue("VmMem")
  435. url := OvirtURL + "/ovirt-engine/api/vms"
  436. method := "POST"
  437. _json := fmt.Sprintf(`
  438. {
  439. "name": "%s",
  440. "description": "%s",
  441. "comment": "%s",
  442. "cluster": {
  443. "name": "Default"
  444. },
  445. "template": {
  446. "name": "%s"
  447. },
  448. "cpu": {
  449. "topology": {
  450. "sockets": "%s",
  451. "cores": "%s",
  452. "threads": "%s"
  453. }
  454. },
  455. "memory": "%s",
  456. "memory_policy": {
  457. "ballooning": "true",
  458. "guaranteed": "%s",
  459. "over_commit": {
  460. "percent": "10"
  461. }
  462. }
  463. }
  464. `, vmname, vmdescr, vmcomment, templatename, cpuSock, cpuCore, cpuThread, mem, mem)
  465. payload := strings.NewReader(_json)
  466. //tr := &http.Transport{
  467. // TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  468. //}
  469. client := &http.Client{}
  470. req, err := http.NewRequest(method, url, payload)
  471. if err != nil {
  472. fmt.Println(err)
  473. }
  474. req.Header.Add("Version", "4")
  475. req.Header.Add("Accept", "application/json")
  476. req.Header.Add("Authorization", BA)
  477. req.Header.Add("Content-Type", "application/json")
  478. res, err := client.Do(req)
  479. body, err := ioutil.ReadAll(res.Body)
  480. //fmt.Println("len: ", len(_json))
  481. //fmt.Println("res:", res)
  482. if err != nil || res.StatusCode != 202 {
  483. resp := _response{
  484. Origin: "ovirt-addvm",
  485. Message: res.Status,
  486. Code: 1001,
  487. }
  488. b, _ := json.MarshalIndent(resp, "", " ")
  489. return c.String(http.StatusBadRequest, string(b))
  490. }
  491. defer res.Body.Close()
  492. addvmresponse := AddVMResponse{}
  493. err = json.Unmarshal(body, &addvmresponse)
  494. //fmt.Println("User UID:", claims["IPAUid"].(string))
  495. {
  496. db, err := sql.Open("mysql", MySQLUSER+":"+MySQLPASS+"@tcp(127.0.0.1:3306)/zicloud")
  497. if err != nil {
  498. resp := _response{
  499. Origin: "ovirt-addvm",
  500. Message: "Error on connecting db for save add vm result",
  501. Code: 1001,
  502. }
  503. b, _ := json.MarshalIndent(resp, "", " ")
  504. return c.String(http.StatusBadRequest, string(b))
  505. }
  506. defer db.Close()
  507. insert, err := db.Query("INSERT INTO service_profile VALUES ( '" + addvmresponse.ID + "'," +
  508. "'" + claims["IPAUid"].(string) + "'," +
  509. "'" + "VM" + "'," +
  510. "'" + addvmresponse.ID + "'," +
  511. "'" + login(BA).AuthenticatedUser.ID + "'," +
  512. "NOW()" + "," +
  513. "NOW() ," +
  514. "true" + "," +
  515. "'" + addvmresponse.Name + "' )")
  516. defer insert.Close()
  517. }
  518. if err != nil {
  519. resp := _response{
  520. Origin: "ovirt-addvm",
  521. Message: "Error on pars AddVMResponse: " + err.Error(),
  522. Code: 1001,
  523. }
  524. //b, _ := json.MarshalIndent(resp, "", " ")
  525. return c.JSON(http.StatusBadRequest, resp)
  526. //return c.String(http.StatusBadRequest, string(b))
  527. }
  528. {
  529. url := OvirtURL + "/ovirt-engine/api/vms/" + addvmresponse.ID + "/start"
  530. method := "POST"
  531. nicAddress := "172.20.15.251"
  532. nicMask := "255.255.255.0"
  533. nicGW := "172.20.15.1"
  534. __json := fmt.Sprintf(`
  535. {
  536. "async":"true",
  537. "use_cloud_init": "true",
  538. "vm": {
  539. "initialization": {
  540. "authorized_ssh_keys": "%s",
  541. "host_name": "Sassan.local",
  542. "user_name": "root",
  543. "root_password": "%s",
  544. "nic_configurations": {
  545. "nic_configuration": [
  546. {
  547. "name": "eth0",
  548. "on_boot": "true",
  549. "boot_protocol": "static",
  550. "ip": {
  551. "address": "%s",
  552. "netmask": "%s",
  553. "gateway": "%s"
  554. }
  555. }
  556. ]
  557. },
  558. "dns_servers": "172.20.11.11"
  559. }
  560. }
  561. }
  562. `, sshKey, rootpass, nicAddress, nicMask, nicGW)
  563. //client := &http.Client{
  564. //}
  565. //payload := strings.NewReader(__json)
  566. //req, err := http.NewRequest(method, url, payload)
  567. //
  568. //if err != nil {
  569. // fmt.Println(err)
  570. //}
  571. //var startVMHeaders []TaskHeader
  572. sha256 := sha256.Sum256([]byte(addvmresponse.ID))
  573. var hashChannel = make(chan []byte, 1)
  574. hashChannel <- sha256[:]
  575. ___json := encrypt(<-hashChannel, __json)
  576. startVM := addVMTask{
  577. URL: url,
  578. JSON: ___json,
  579. Method: method,
  580. Headers: nil,
  581. }
  582. startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Version", Value: "4"})
  583. startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Accept", Value: "application/json"})
  584. startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Content-Type", Value: "application/json"})
  585. startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Authorization", Value: BA})
  586. //req.Header.Add("Version", "4")
  587. //req.Header.Add("Accept", "application/json")
  588. //req.Header.Add("Authorization", BA)
  589. //req.Header.Add("Content-Type", "application/json")
  590. //
  591. //res, err := client.Do(req)
  592. //defer res.Body.Close()
  593. uuid, _ := uuidgen("APIGW-Ovirt")
  594. __startVM, _ := json.MarshalIndent(startVM, "", " ")
  595. //fmt.Println("StartVMHeaders: ", string(__startVMHeaders))
  596. addTask(uuid, string(__startVM), "", "APIGW", "VM Initialization", addvmresponse.ID, "1", "1")
  597. }
  598. resp := _response{
  599. Origin: "ovirt-addvm",
  600. Message: "Done",
  601. Code: 1000,
  602. }
  603. //b, _ := json.MarshalIndent(resp, "", " ")
  604. return c.JSON(http.StatusOK, resp)
  605. //return c.String(http.StatusOK, string(b))
  606. }
  607. func (o ovirt) listVM(c echo.Context) error {
  608. user := c.Get("user").(*jwt.Token)
  609. claims := user.Claims.(jwt.MapClaims)
  610. _sha256 := sha256.Sum256([]byte(string(claims["name"].(string))))
  611. var hashChannel_ = make(chan []byte, 1)
  612. hashChannel_ <- _sha256[:]
  613. token := decrypt(<-hashChannel_, claims["IPAToken"].(string))
  614. _BA := strings.Split(token, ";")
  615. BA := _BA[len(_BA)-2]
  616. url := OvirtURL + "/ovirt-engine/api/vms"
  617. client := &http.Client{
  618. }
  619. req, err := http.NewRequest("GET", url, nil)
  620. if err != nil {
  621. fmt.Println(err)
  622. }
  623. req.Header.Add("Version", "4")
  624. req.Header.Add("Accept", "application/json")
  625. req.Header.Add("Authorization", BA)
  626. res, err := client.Do(req)
  627. defer res.Body.Close()
  628. body, err := ioutil.ReadAll(res.Body)
  629. //fmt.Printf("%s",body)
  630. //b, _ := json.MarshalIndent(body, "", " ")
  631. resp := _response{
  632. Origin: "ovirt-listvms",
  633. Message: string(body),
  634. Code: 1000,
  635. }
  636. //b, _ := json.MarshalIndent(resp, "", " ")
  637. return c.JSON(http.StatusOK, resp)
  638. //return c.String(http.StatusOK, string(b))
  639. }
  640. func (o ovirt) StartVM(c echo.Context) error {
  641. vmuuid := c.FormValue("VmUUID")
  642. user := c.Get("user").(*jwt.Token)
  643. claims := user.Claims.(jwt.MapClaims)
  644. _sha256 := sha256.Sum256([]byte(string(claims["name"].(string))))
  645. var hashChannel_ = make(chan []byte, 1)
  646. hashChannel_ <- _sha256[:]
  647. token := decrypt(<-hashChannel_, claims["IPAToken"].(string))
  648. _BA := strings.Split(token, ";")
  649. BA := _BA[len(_BA)-2]
  650. url := OvirtURL + "/ovirt-engine/api/vms/" + vmuuid + "/start"
  651. method := "POST"
  652. __json := "{\"async\":\"true\"}"
  653. sha256 := sha256.Sum256([]byte(vmuuid))
  654. var hashChannel = make(chan []byte, 1)
  655. hashChannel <- sha256[:]
  656. ___json := encrypt(<-hashChannel, __json)
  657. startVM := addVMTask{
  658. URL: url,
  659. JSON: ___json,
  660. Method: method,
  661. Headers: nil,
  662. }
  663. startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Version", Value: "4"})
  664. startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Accept", Value: "application/json"})
  665. startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Content-Type", Value: "application/json"})
  666. startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Authorization", Value: BA})
  667. //req.Header.Add("Version", "4")
  668. //req.Header.Add("Accept", "application/json")
  669. //req.Header.Add("Authorization", BA)
  670. //req.Header.Add("Content-Type", "application/json")
  671. //
  672. //res, err := client.Do(req)
  673. //defer res.Body.Close()
  674. uuid, _ := uuidgen("APIGW-Ovirt")
  675. __startVM, _ := json.MarshalIndent(startVM, "", " ")
  676. //fmt.Println("StartVMHeaders: ", string(__startVMHeaders))
  677. addTask(uuid, string(__startVM), "", "APIGW", "VM Start", vmuuid, "1", "1")
  678. resp := _response{
  679. Origin: "ovirt-StartVM",
  680. Message: "Done",
  681. Code: 1000,
  682. }
  683. //b, _ := json.MarshalIndent(resp, "", " ")
  684. return c.JSON(http.StatusOK, resp)
  685. //return c.String(http.StatusOK, string(b))
  686. }
  687. func (o ovirt) StopVM(c echo.Context) error {
  688. vmuuid := c.FormValue("VmUUID")
  689. user := c.Get("user").(*jwt.Token)
  690. claims := user.Claims.(jwt.MapClaims)
  691. _sha256 := sha256.Sum256([]byte(string(claims["name"].(string))))
  692. var hashChannel_ = make(chan []byte, 1)
  693. hashChannel_ <- _sha256[:]
  694. token := decrypt(<-hashChannel_, claims["IPAToken"].(string))
  695. _BA := strings.Split(token, ";")
  696. BA := _BA[len(_BA)-2]
  697. url := OvirtURL + "/ovirt-engine/api/vms/" + vmuuid + "/shutdown"
  698. method := "POST"
  699. __json := "{\"async\":\"true\"}"
  700. sha256 := sha256.Sum256([]byte(vmuuid))
  701. var hashChannel = make(chan []byte, 1)
  702. hashChannel <- sha256[:]
  703. ___json := encrypt(<-hashChannel, __json)
  704. startVM := addVMTask{
  705. URL: url,
  706. JSON: ___json,
  707. Method: method,
  708. Headers: nil,
  709. }
  710. startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Version", Value: "4"})
  711. startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Accept", Value: "application/json"})
  712. startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Content-Type", Value: "application/json"})
  713. startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Authorization", Value: BA})
  714. //req.Header.Add("Version", "4")
  715. //req.Header.Add("Accept", "application/json")
  716. //req.Header.Add("Authorization", BA)
  717. //req.Header.Add("Content-Type", "application/json")
  718. //
  719. //res, err := client.Do(req)
  720. //defer res.Body.Close()
  721. uuid, _ := uuidgen("APIGW-Ovirt")
  722. __startVM, _ := json.MarshalIndent(startVM, "", " ")
  723. //fmt.Println("StartVMHeaders: ", string(__startVMHeaders))
  724. addTask(uuid, string(__startVM), "", "APIGW", "VM Start", vmuuid, "1", "1")
  725. resp := _response{
  726. Origin: "ovirt-StartVM",
  727. Message: "Done",
  728. Code: 1000,
  729. }
  730. //b, _ := json.MarshalIndent(resp, "", " ")
  731. return c.JSON(http.StatusOK, resp)
  732. //return c.String(http.StatusOK, string(b))
  733. }
  734. func (o ovirt) RebootVM(c echo.Context) error {
  735. vmuuid := c.FormValue("VmUUID")
  736. user := c.Get("user").(*jwt.Token)
  737. claims := user.Claims.(jwt.MapClaims)
  738. _sha256 := sha256.Sum256([]byte(string(claims["name"].(string))))
  739. var hashChannel_ = make(chan []byte, 1)
  740. hashChannel_ <- _sha256[:]
  741. token := decrypt(<-hashChannel_, claims["IPAToken"].(string))
  742. _BA := strings.Split(token, ";")
  743. BA := _BA[len(_BA)-2]
  744. url := OvirtURL + "/ovirt-engine/api/vms/" + vmuuid + "/reboot"
  745. method := "POST"
  746. __json := "{\"async\":\"true\"}"
  747. sha256 := sha256.Sum256([]byte(vmuuid))
  748. var hashChannel = make(chan []byte, 1)
  749. hashChannel <- sha256[:]
  750. ___json := encrypt(<-hashChannel, __json)
  751. startVM := addVMTask{
  752. URL: url,
  753. JSON: ___json,
  754. Method: method,
  755. Headers: nil,
  756. }
  757. startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Version", Value: "4"})
  758. startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Accept", Value: "application/json"})
  759. startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Content-Type", Value: "application/json"})
  760. startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Authorization", Value: BA})
  761. //req.Header.Add("Version", "4")
  762. //req.Header.Add("Accept", "application/json")
  763. //req.Header.Add("Authorization", BA)
  764. //req.Header.Add("Content-Type", "application/json")
  765. //
  766. //res, err := client.Do(req)
  767. //defer res.Body.Close()
  768. uuid, _ := uuidgen("APIGW-Ovirt")
  769. __startVM, _ := json.MarshalIndent(startVM, "", " ")
  770. //fmt.Println("StartVMHeaders: ", string(__startVMHeaders))
  771. addTask(uuid, string(__startVM), "", "APIGW", "VM Start", vmuuid, "1", "1")
  772. resp := _response{
  773. Origin: "ovirt-StartVM",
  774. Message: "Done",
  775. Code: 1000,
  776. }
  777. //b, _ := json.MarshalIndent(resp, "", " ")
  778. //return c.String(http.StatusOK, string(b))
  779. return c.JSON(http.StatusOK, resp)
  780. }
  781. func (o ovirt) PowerOffVM(c echo.Context) error {
  782. vmuuid := c.FormValue("VmUUID")
  783. user := c.Get("user").(*jwt.Token)
  784. claims := user.Claims.(jwt.MapClaims)
  785. _sha256 := sha256.Sum256([]byte(string(claims["name"].(string))))
  786. var hashChannel_ = make(chan []byte, 1)
  787. hashChannel_ <- _sha256[:]
  788. token := decrypt(<-hashChannel_, claims["IPAToken"].(string))
  789. _BA := strings.Split(token, ";")
  790. BA := _BA[len(_BA)-2]
  791. url := OvirtURL + "/ovirt-engine/api/vms/" + vmuuid + "/stop"
  792. method := "POST"
  793. __json := "{\"async\":\"true\"}"
  794. sha256 := sha256.Sum256([]byte(vmuuid))
  795. var hashChannel = make(chan []byte, 1)
  796. hashChannel <- sha256[:]
  797. ___json := encrypt(<-hashChannel, __json)
  798. startVM := addVMTask{
  799. URL: url,
  800. JSON: ___json,
  801. Method: method,
  802. Headers: nil,
  803. }
  804. startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Version", Value: "4"})
  805. startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Accept", Value: "application/json"})
  806. startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Content-Type", Value: "application/json"})
  807. startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Authorization", Value: BA})
  808. //req.Header.Add("Version", "4")
  809. //req.Header.Add("Accept", "application/json")
  810. //req.Header.Add("Authorization", BA)
  811. //req.Header.Add("Content-Type", "application/json")
  812. //
  813. //res, err := client.Do(req)
  814. //defer res.Body.Close()
  815. uuid, _ := uuidgen("APIGW-Ovirt")
  816. __startVM, _ := json.MarshalIndent(startVM, "", " ")
  817. //fmt.Println("StartVMHeaders: ", string(__startVMHeaders))
  818. addTask(uuid, string(__startVM), "", "APIGW", "VM Start", vmuuid, "1", "1")
  819. resp := _response{
  820. Origin: "ovirt-StartVM",
  821. Message: "Done",
  822. Code: 1000,
  823. }
  824. //b, _ := json.MarshalIndent(resp, "", " ")
  825. //return c.String(http.StatusOK, string(b))
  826. return c.JSON(http.StatusOK, resp)
  827. }
  828. func (o ovirt) ResetVM(c echo.Context) error {
  829. resp := _response{
  830. Origin: "ovirt-ResetVM",
  831. Message: "not implemented",
  832. Code: 1003,
  833. }
  834. //b, _ := json.MarshalIndent(resp, "", " ")
  835. return c.JSON(http.StatusOK, resp)
  836. //return c.String(http.StatusOK, string(b))
  837. }
  838. func (o ovirt) AddNIC(c echo.Context) error {
  839. resp := _response{
  840. Origin: "ovirt-AddNIC",
  841. Message: "Done",
  842. Code: 1000,
  843. }
  844. //b, _ := json.MarshalIndent(resp, "", " ")
  845. //return c.String(http.StatusOK, string(b))
  846. return c.JSON(http.StatusOK, resp)
  847. }
  848. func (o ovirt) AddDisk(c echo.Context) error {
  849. resp := _response{
  850. Origin: "ovirt-AddDisk",
  851. Message: "Done",
  852. Code: 1000,
  853. }
  854. //b, _ := json.MarshalIndent(resp, "", " ")
  855. //return c.String(http.StatusOK, string(b))
  856. return c.JSON(http.StatusOK, resp)
  857. }
  858. func (o ovirt) EditVM(c echo.Context) error {
  859. resp := _response{
  860. Origin: "ovirt-EditVM",
  861. Message: "Done",
  862. Code: 1000,
  863. }
  864. //b, _ := json.MarshalIndent(resp, "", " ")
  865. //return c.String(http.StatusOK, string(b))
  866. return c.JSON(http.StatusOK, resp)
  867. }
  868. func (o ovirt) ResetPassword(c echo.Context) error {
  869. resp := _response{
  870. Origin: "ovirt-ResetPassword",
  871. Message: "not implemented",
  872. Code: 1003,
  873. }
  874. //b, _ := json.MarshalIndent(resp, "", " ")
  875. //return c.String(http.StatusOK, string(b))
  876. return c.JSON(http.StatusOK, resp)
  877. }
  878. func vmStatistics(BA string, VMUUID string) (string, float64, float64) {
  879. //ram,cpu,storage
  880. var _disk []string
  881. {
  882. type DiskAttachments struct {
  883. DiskAttachment []struct {
  884. Active string `json:"active"`
  885. Bootable string `json:"bootable"`
  886. Interface string `json:"interface"`
  887. LogicalName string `json:"logical_name"`
  888. PassDiscard string `json:"pass_discard"`
  889. ReadOnly string `json:"read_only"`
  890. UsesScsiReservation string `json:"uses_scsi_reservation"`
  891. Disk struct {
  892. Href string `json:"href"`
  893. ID string `json:"id"`
  894. } `json:"disk"`
  895. VM struct {
  896. Href string `json:"href"`
  897. ID string `json:"id"`
  898. } `json:"vm"`
  899. Href string `json:"href"`
  900. ID string `json:"id"`
  901. } `json:"disk_attachment"`
  902. }
  903. url := OvirtURL + "/ovirt-engine/api/vms/" + VMUUID + "/diskattachments"
  904. //fmt.Println("Url:" ,url)
  905. method := "GET"
  906. client := &http.Client{
  907. }
  908. req, err := http.NewRequest(method, url, nil)
  909. if err != nil {
  910. return "", -1, -1
  911. }
  912. req.Header.Add("Version", "4")
  913. req.Header.Add("Accept", "application/json")
  914. req.Header.Add("Authorization", BA)
  915. res, err := client.Do(req)
  916. defer res.Body.Close()
  917. body, err := ioutil.ReadAll(res.Body)
  918. diskattachments := DiskAttachments{}
  919. _err := json.Unmarshal(body, &diskattachments)
  920. if _err != nil {
  921. fmt.Println("Error: ", _err)
  922. }
  923. for _, v := range diskattachments.DiskAttachment {
  924. url := OvirtURL + "/ovirt-engine/api/disks/" + v.Disk.ID
  925. type DiskAttachment struct {
  926. ActualSize string `json:"actual_size"`
  927. Alias string `json:"alias"`
  928. Backup string `json:"backup"`
  929. ContentType string `json:"content_type"`
  930. Format string `json:"format"`
  931. ImageID string `json:"image_id"`
  932. PropagateErrors string `json:"propagate_errors"`
  933. ProvisionedSize string `json:"provisioned_size"`
  934. QcowVersion string `json:"qcow_version"`
  935. Shareable string `json:"shareable"`
  936. Sparse string `json:"sparse"`
  937. Status string `json:"status"`
  938. StorageType string `json:"storage_type"`
  939. TotalSize string `json:"total_size"`
  940. WipeAfterDelete string `json:"wipe_after_delete"`
  941. DiskProfile struct {
  942. Href string `json:"href"`
  943. ID string `json:"id"`
  944. } `json:"disk_profile"`
  945. Quota struct {
  946. Href string `json:"href"`
  947. ID string `json:"id"`
  948. } `json:"quota"`
  949. StorageDomains struct {
  950. StorageDomain []struct {
  951. Href string `json:"href"`
  952. ID string `json:"id"`
  953. } `json:"storage_domain"`
  954. } `json:"storage_domains"`
  955. Actions struct {
  956. Link []struct {
  957. Href string `json:"href"`
  958. Rel string `json:"rel"`
  959. } `json:"link"`
  960. } `json:"actions"`
  961. Name string `json:"name"`
  962. Description string `json:"description"`
  963. Href string `json:"href"`
  964. ID string `json:"id"`
  965. Link []struct {
  966. Href string `json:"href"`
  967. Rel string `json:"rel"`
  968. } `json:"link"`
  969. }
  970. //fmt.Println("Url:" ,url)
  971. method := "GET"
  972. client := &http.Client{
  973. }
  974. req, _ := http.NewRequest(method, url, nil)
  975. //if err != nil {
  976. // return "Null"
  977. //}
  978. req.Header.Add("Version", "4")
  979. req.Header.Add("Accept", "application/json")
  980. req.Header.Add("Authorization", BA)
  981. res, _ := client.Do(req)
  982. defer res.Body.Close()
  983. body, _ := ioutil.ReadAll(res.Body)
  984. //fmt.Println("full response: ",string(body))
  985. disk := DiskAttachment{}
  986. _err := json.Unmarshal(body, &disk)
  987. if _err != nil {
  988. fmt.Println("Error: ", _err)
  989. }
  990. _disk = append(_disk, disk.ProvisionedSize)
  991. //_disk = D_disk + "" + disk.ProvisionedSize+" "
  992. }
  993. }
  994. url := OvirtURL + "/ovirt-engine/api/vms/" + VMUUID + "/statistics"
  995. //fmt.Println("Url:" ,url)
  996. method := "GET"
  997. client := &http.Client{
  998. }
  999. req, err := http.NewRequest(method, url, nil)
  1000. if err != nil {
  1001. return "", -1, -1
  1002. }
  1003. req.Header.Add("Version", "4")
  1004. req.Header.Add("Accept", "application/json")
  1005. req.Header.Add("Authorization", BA)
  1006. res, err := client.Do(req)
  1007. defer res.Body.Close()
  1008. body, err := ioutil.ReadAll(res.Body)
  1009. //fmt.Println("full response: ",string(body))
  1010. _vmstatistics := VMStatistics{}
  1011. _err := json.Unmarshal(body, &_vmstatistics)
  1012. if _err != nil {
  1013. fmt.Println("Error: ", _err)
  1014. }
  1015. cpuUUID := _vmstatistics.Statistic[4].ID
  1016. memUsedUUID := _vmstatistics.Statistic[1].ID
  1017. memTotalUUID := _vmstatistics.Statistic[0].ID
  1018. //fmt.Println(memUsedUUID)
  1019. var cpu, memUsed, memTotal string
  1020. type ItemUsage struct {
  1021. Kind string `json:"kind"`
  1022. Type string `json:"type"`
  1023. Unit string `json:"unit"`
  1024. Values struct {
  1025. Value []struct {
  1026. Datum float64 `json:"datum"`
  1027. } `json:"value"`
  1028. } `json:"values"`
  1029. VM struct {
  1030. Href string `json:"href"`
  1031. ID string `json:"id"`
  1032. } `json:"vm"`
  1033. Name string `json:"name"`
  1034. Description string `json:"description"`
  1035. Href string `json:"href"`
  1036. ID string `json:"id"`
  1037. }
  1038. {
  1039. url := OvirtURL + "/ovirt-engine/api/vms/" + VMUUID + "/statistics/" + cpuUUID
  1040. //fmt.Println("Url:" ,url)
  1041. method := "GET"
  1042. client := &http.Client{
  1043. }
  1044. req, err := http.NewRequest(method, url, nil)
  1045. if err != nil {
  1046. return "", -1, -1
  1047. }
  1048. req.Header.Add("Version", "4")
  1049. req.Header.Add("Accept", "application/json")
  1050. req.Header.Add("Authorization", BA)
  1051. res, err := client.Do(req)
  1052. defer res.Body.Close()
  1053. body, err := ioutil.ReadAll(res.Body)
  1054. cpuUsage := ItemUsage{}
  1055. _err := json.Unmarshal(body, &cpuUsage)
  1056. if _err != nil {
  1057. fmt.Println("Error: ", _err)
  1058. }
  1059. cpu = fmt.Sprintf("%f", cpuUsage.Values.Value[0].Datum)
  1060. }
  1061. {
  1062. url := OvirtURL + "/ovirt-engine/api/vms/" + VMUUID + "/statistics/" + memUsedUUID
  1063. //fmt.Println("Url:" ,url)
  1064. method := "GET"
  1065. client := &http.Client{
  1066. }
  1067. req, err := http.NewRequest(method, url, nil)
  1068. if err != nil {
  1069. return "", -1, -1
  1070. }
  1071. req.Header.Add("Version", "4")
  1072. req.Header.Add("Accept", "application/json")
  1073. req.Header.Add("Authorization", BA)
  1074. res, err := client.Do(req)
  1075. defer res.Body.Close()
  1076. body, err := ioutil.ReadAll(res.Body)
  1077. memUsage := ItemUsage{}
  1078. _err := json.Unmarshal(body, &memUsage)
  1079. if _err != nil {
  1080. fmt.Println("Error: ", _err)
  1081. }
  1082. memUsed = fmt.Sprintf("%f", memUsage.Values.Value[0].Datum)
  1083. }
  1084. {
  1085. url := OvirtURL + "/ovirt-engine/api/vms/" + VMUUID + "/statistics/" + memTotalUUID
  1086. //fmt.Println("Url:" ,url)
  1087. method := "GET"
  1088. client := &http.Client{
  1089. }
  1090. req, err := http.NewRequest(method, url, nil)
  1091. if err != nil {
  1092. return "", -1, -1
  1093. }
  1094. req.Header.Add("Version", "4")
  1095. req.Header.Add("Accept", "application/json")
  1096. req.Header.Add("Authorization", BA)
  1097. res, err := client.Do(req)
  1098. defer res.Body.Close()
  1099. body, err := ioutil.ReadAll(res.Body)
  1100. memUsage := ItemUsage{}
  1101. _err := json.Unmarshal(body, &memUsage)
  1102. if _err != nil {
  1103. fmt.Println("Error: ", _err)
  1104. }
  1105. memTotal = fmt.Sprintf("%f", memUsage.Values.Value[0].Datum)
  1106. }
  1107. //fmt.Println("CPU:" ,cpu)
  1108. //fmt.Println("MEM:", memUsed)
  1109. //fmt.Println("MEM:", memTotal)
  1110. _memusage, _ := strconv.ParseFloat(memUsed, 32)
  1111. _memtotal, _ := strconv.ParseFloat(memTotal, 32)
  1112. mem := _memusage / _memtotal
  1113. _cpu, _ := strconv.ParseFloat(cpu, 64)
  1114. return strings.Join(_disk,", "), _cpu, mem
  1115. }
  1116. func (o ovirt) vmDetails(c echo.Context) error {
  1117. //name,...,traffic,ip,status
  1118. user := c.Get("user").(*jwt.Token)
  1119. claims := user.Claims.(jwt.MapClaims)
  1120. _sha256 := sha256.Sum256([]byte(string(claims["name"].(string))))
  1121. var hashChannel_ = make(chan []byte, 1)
  1122. hashChannel_ <- _sha256[:]
  1123. token := decrypt(<-hashChannel_, claims["IPAToken"].(string))
  1124. _BA := strings.Split(token, ";")
  1125. BA := _BA[len(_BA)-2]
  1126. uuid := c.FormValue("uuid")
  1127. if len(uuid) < 5 {
  1128. resp := _response{
  1129. Origin: "AddUser",
  1130. Message: "Missing UUID",
  1131. Code: 1001,
  1132. }
  1133. return c.JSON(http.StatusNotFound, resp)
  1134. }
  1135. type vmStatus struct {
  1136. NextRunConfigurationExists string `json:"next_run_configuration_exists"`
  1137. NumaTuneMode string `json:"numa_tune_mode"`
  1138. Status string `json:"status"`
  1139. StopTime int64 `json:"stop_time"`
  1140. OriginalTemplate struct {
  1141. Href string `json:"href"`
  1142. ID string `json:"id"`
  1143. } `json:"original_template"`
  1144. Template struct {
  1145. Href string `json:"href"`
  1146. ID string `json:"id"`
  1147. } `json:"template"`
  1148. Actions struct {
  1149. Link []struct {
  1150. Href string `json:"href"`
  1151. Rel string `json:"rel"`
  1152. } `json:"link"`
  1153. } `json:"actions"`
  1154. Name string `json:"name"`
  1155. Description string `json:"description"`
  1156. Comment string `json:"comment"`
  1157. Href string `json:"href"`
  1158. ID string `json:"id"`
  1159. Bios struct {
  1160. BootMenu struct {
  1161. Enabled string `json:"enabled"`
  1162. } `json:"boot_menu"`
  1163. Type string `json:"type"`
  1164. } `json:"bios"`
  1165. CPU struct {
  1166. Architecture string `json:"architecture"`
  1167. Topology struct {
  1168. Cores string `json:"cores"`
  1169. Sockets string `json:"sockets"`
  1170. Threads string `json:"threads"`
  1171. } `json:"topology"`
  1172. } `json:"cpu"`
  1173. Display struct {
  1174. AllowOverride string `json:"allow_override"`
  1175. CopyPasteEnabled string `json:"copy_paste_enabled"`
  1176. DisconnectAction string `json:"disconnect_action"`
  1177. FileTransferEnabled string `json:"file_transfer_enabled"`
  1178. Monitors string `json:"monitors"`
  1179. SingleQxlPci string `json:"single_qxl_pci"`
  1180. SmartcardEnabled string `json:"smartcard_enabled"`
  1181. Type string `json:"type"`
  1182. } `json:"display"`
  1183. Initialization struct {
  1184. AuthorizedSSHKeys string `json:"authorized_ssh_keys"`
  1185. CloudInitNetworkProtocol string `json:"cloud_init_network_protocol"`
  1186. CustomScript string `json:"custom_script"`
  1187. HostName string `json:"host_name"`
  1188. NicConfigurations struct {
  1189. } `json:"nic_configurations"`
  1190. RegenerateSSHKeys string `json:"regenerate_ssh_keys"`
  1191. UserName string `json:"user_name"`
  1192. } `json:"initialization"`
  1193. Io struct {
  1194. Threads string `json:"threads"`
  1195. } `json:"io"`
  1196. Memory string `json:"memory"`
  1197. Migration struct {
  1198. AutoConverge string `json:"auto_converge"`
  1199. Compressed string `json:"compressed"`
  1200. Encrypted string `json:"encrypted"`
  1201. } `json:"migration"`
  1202. Origin string `json:"origin"`
  1203. Os struct {
  1204. Boot struct {
  1205. Devices struct {
  1206. Device []string `json:"device"`
  1207. } `json:"devices"`
  1208. } `json:"boot"`
  1209. Type string `json:"type"`
  1210. } `json:"os"`
  1211. Sso struct {
  1212. Methods struct {
  1213. Method []struct {
  1214. ID string `json:"id"`
  1215. } `json:"method"`
  1216. } `json:"methods"`
  1217. } `json:"sso"`
  1218. Stateless string `json:"stateless"`
  1219. Type string `json:"type"`
  1220. Usb struct {
  1221. Enabled string `json:"enabled"`
  1222. } `json:"usb"`
  1223. Cluster struct {
  1224. Href string `json:"href"`
  1225. ID string `json:"id"`
  1226. } `json:"cluster"`
  1227. Quota struct {
  1228. ID string `json:"id"`
  1229. } `json:"quota"`
  1230. Link []struct {
  1231. Href string `json:"href"`
  1232. Rel string `json:"rel"`
  1233. } `json:"link"`
  1234. CPUShares string `json:"cpu_shares"`
  1235. CreationTime int64 `json:"creation_time"`
  1236. DeleteProtected string `json:"delete_protected"`
  1237. HighAvailability struct {
  1238. Enabled string `json:"enabled"`
  1239. Priority string `json:"priority"`
  1240. } `json:"high_availability"`
  1241. LargeIcon struct {
  1242. Href string `json:"href"`
  1243. ID string `json:"id"`
  1244. } `json:"large_icon"`
  1245. MemoryPolicy struct {
  1246. Ballooning string `json:"ballooning"`
  1247. Guaranteed string `json:"guaranteed"`
  1248. Max string `json:"max"`
  1249. } `json:"memory_policy"`
  1250. MigrationDowntime string `json:"migration_downtime"`
  1251. MultiQueuesEnabled string `json:"multi_queues_enabled"`
  1252. PlacementPolicy struct {
  1253. Affinity string `json:"affinity"`
  1254. Hosts struct {
  1255. Host []struct {
  1256. Href string `json:"href"`
  1257. ID string `json:"id"`
  1258. } `json:"host"`
  1259. } `json:"hosts"`
  1260. } `json:"placement_policy"`
  1261. SmallIcon struct {
  1262. Href string `json:"href"`
  1263. ID string `json:"id"`
  1264. } `json:"small_icon"`
  1265. StartPaused string `json:"start_paused"`
  1266. StorageErrorResumeBehaviour string `json:"storage_error_resume_behaviour"`
  1267. TimeZone struct {
  1268. Name string `json:"name"`
  1269. } `json:"time_zone"`
  1270. CPUProfile struct {
  1271. Href string `json:"href"`
  1272. ID string `json:"id"`
  1273. } `json:"cpu_profile"`
  1274. }
  1275. //url := "https://ovirt-cl.zi-tel.com/ovirt-engine/api/vms/c1a44128-6c6c-406b-99d8-4a68a99380c9"
  1276. url := OvirtURL + "/ovirt-engine/api/vms/" + uuid
  1277. //fmt.Println("Url:" ,url)
  1278. method := "GET"
  1279. client := &http.Client{
  1280. }
  1281. req, _ := http.NewRequest(method, url, nil)
  1282. //if err != nil {
  1283. // return "Null"
  1284. //}
  1285. req.Header.Add("Version", "4")
  1286. req.Header.Add("Accept", "application/json")
  1287. req.Header.Add("Authorization", BA)
  1288. res, _ := client.Do(req)
  1289. defer res.Body.Close()
  1290. body, _ := ioutil.ReadAll(res.Body)
  1291. //fmt.Println("full response: ",string(body))
  1292. _vmstatus := vmStatus{}
  1293. _err := json.Unmarshal(body, &_vmstatus)
  1294. if _err != nil {
  1295. fmt.Println("Error: ", _err)
  1296. }
  1297. disk, cpu, ram := vmStatistics(BA, uuid)
  1298. type AutoGenerated struct {
  1299. Message struct {
  1300. Disk string `json:"Disk"`
  1301. Memory string `json:"Memory"`
  1302. CPU string `json:"CPU"`
  1303. NetAddress string `json:"NetAddress"`
  1304. Traffic string `json:"Traffic"`
  1305. Status string `json:"Status"`
  1306. Name string `json:"Name"`
  1307. } `json:"message"`
  1308. Origin string `json:"origin"`
  1309. Code int `json:"code"`
  1310. }
  1311. resp := AutoGenerated{
  1312. Message: struct {
  1313. Disk string `json:"Disk"`
  1314. Memory string `json:"Memory"`
  1315. CPU string `json:"CPU"`
  1316. NetAddress string `json:"NetAddress"`
  1317. Traffic string `json:"Traffic"`
  1318. Status string `json:"Status"`
  1319. Name string `json:"Name"`
  1320. }{
  1321. Disk: disk,
  1322. Memory: fmt.Sprintf("%f", ram),
  1323. CPU: fmt.Sprintf("%f", cpu),
  1324. NetAddress: "",
  1325. Traffic: "",
  1326. Status: _vmstatus.Status,
  1327. Name: _vmstatus.Name,
  1328. },
  1329. Origin: "vmDetails",
  1330. Code: 1000,
  1331. }
  1332. return c.JSON(http.StatusOK, resp)
  1333. //return _vmstatus.Status, _vmstatus.Name, disk, cpu, ram
  1334. }