ovirt.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  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. "strings"
  13. )
  14. type ovirt struct {
  15. }
  16. var MySQLUSER = "apigw"
  17. var MySQLPASS = "^_P+^7Q$bmPj+$xB"
  18. func login(BA string) OvirtObject {
  19. url := OvirtURL + "/ovirt-engine/api"
  20. method := "GET"
  21. client := &http.Client{
  22. }
  23. req, err := http.NewRequest(method, url, nil)
  24. if err != nil {
  25. fmt.Println(err)
  26. }
  27. req.Header.Add("Version", "4")
  28. req.Header.Add("Accept", "application/json")
  29. req.Header.Add("Authorization", BA)
  30. res, err := client.Do(req)
  31. defer res.Body.Close()
  32. body, err := ioutil.ReadAll(res.Body)
  33. object := OvirtObject{}
  34. err = json.Unmarshal(body, &object)
  35. return object
  36. }
  37. type OvirtObject struct {
  38. ProductInfo struct {
  39. Name string `json:"name"`
  40. Version struct {
  41. Build string `json:"build"`
  42. FullVersion string `json:"full_version"`
  43. Major string `json:"major"`
  44. Minor string `json:"minor"`
  45. Revision string `json:"revision"`
  46. } `json:"version"`
  47. } `json:"product_info"`
  48. SpecialObjects struct {
  49. BlankTemplate struct {
  50. Href string `json:"href"`
  51. ID string `json:"id"`
  52. } `json:"blank_template"`
  53. RootTag struct {
  54. Href string `json:"href"`
  55. ID string `json:"id"`
  56. } `json:"root_tag"`
  57. } `json:"special_objects"`
  58. Time int64 `json:"time"`
  59. AuthenticatedUser struct {
  60. Href string `json:"href"`
  61. ID string `json:"id"`
  62. } `json:"authenticated_user"`
  63. EffectiveUser struct {
  64. Href string `json:"href"`
  65. ID string `json:"id"`
  66. } `json:"effective_user"`
  67. Link []struct {
  68. Href string `json:"href"`
  69. Rel string `json:"rel"`
  70. } `json:"link"`
  71. }
  72. type TaskHeader struct {
  73. Name string `json:"Name,omitempty"`
  74. Value string `json:"Value,omitempty"`
  75. }
  76. type addVMTask struct {
  77. URL string `json:"url"`
  78. JSON string `json:"__json"`
  79. Method string `json:"method"`
  80. Headers []struct {
  81. Name string `json:"Name,omitempty"`
  82. Value string `json:"Value,omitempty"`
  83. } `json:"headers"`
  84. }
  85. func (o ovirt) vmStatus(uuid string) string {
  86. type vmStatus struct {
  87. NextRunConfigurationExists string `json:"next_run_configuration_exists"`
  88. NumaTuneMode string `json:"numa_tune_mode"`
  89. Status string `json:"status"`
  90. StopTime int64 `json:"stop_time"`
  91. OriginalTemplate struct {
  92. Href string `json:"href"`
  93. ID string `json:"id"`
  94. } `json:"original_template"`
  95. Template struct {
  96. Href string `json:"href"`
  97. ID string `json:"id"`
  98. } `json:"template"`
  99. Actions struct {
  100. Link []struct {
  101. Href string `json:"href"`
  102. Rel string `json:"rel"`
  103. } `json:"link"`
  104. } `json:"actions"`
  105. Name string `json:"name"`
  106. Description string `json:"description"`
  107. Comment string `json:"comment"`
  108. Href string `json:"href"`
  109. ID string `json:"id"`
  110. Bios struct {
  111. BootMenu struct {
  112. Enabled string `json:"enabled"`
  113. } `json:"boot_menu"`
  114. Type string `json:"type"`
  115. } `json:"bios"`
  116. CPU struct {
  117. Architecture string `json:"architecture"`
  118. Topology struct {
  119. Cores string `json:"cores"`
  120. Sockets string `json:"sockets"`
  121. Threads string `json:"threads"`
  122. } `json:"topology"`
  123. } `json:"cpu"`
  124. Display struct {
  125. AllowOverride string `json:"allow_override"`
  126. CopyPasteEnabled string `json:"copy_paste_enabled"`
  127. DisconnectAction string `json:"disconnect_action"`
  128. FileTransferEnabled string `json:"file_transfer_enabled"`
  129. Monitors string `json:"monitors"`
  130. SingleQxlPci string `json:"single_qxl_pci"`
  131. SmartcardEnabled string `json:"smartcard_enabled"`
  132. Type string `json:"type"`
  133. } `json:"display"`
  134. Initialization struct {
  135. AuthorizedSSHKeys string `json:"authorized_ssh_keys"`
  136. CloudInitNetworkProtocol string `json:"cloud_init_network_protocol"`
  137. CustomScript string `json:"custom_script"`
  138. HostName string `json:"host_name"`
  139. NicConfigurations struct {
  140. } `json:"nic_configurations"`
  141. RegenerateSSHKeys string `json:"regenerate_ssh_keys"`
  142. UserName string `json:"user_name"`
  143. } `json:"initialization"`
  144. Io struct {
  145. Threads string `json:"threads"`
  146. } `json:"io"`
  147. Memory string `json:"memory"`
  148. Migration struct {
  149. AutoConverge string `json:"auto_converge"`
  150. Compressed string `json:"compressed"`
  151. Encrypted string `json:"encrypted"`
  152. } `json:"migration"`
  153. Origin string `json:"origin"`
  154. Os struct {
  155. Boot struct {
  156. Devices struct {
  157. Device []string `json:"device"`
  158. } `json:"devices"`
  159. } `json:"boot"`
  160. Type string `json:"type"`
  161. } `json:"os"`
  162. Sso struct {
  163. Methods struct {
  164. Method []struct {
  165. ID string `json:"id"`
  166. } `json:"method"`
  167. } `json:"methods"`
  168. } `json:"sso"`
  169. Stateless string `json:"stateless"`
  170. Type string `json:"type"`
  171. Usb struct {
  172. Enabled string `json:"enabled"`
  173. } `json:"usb"`
  174. Cluster struct {
  175. Href string `json:"href"`
  176. ID string `json:"id"`
  177. } `json:"cluster"`
  178. Quota struct {
  179. ID string `json:"id"`
  180. } `json:"quota"`
  181. Link []struct {
  182. Href string `json:"href"`
  183. Rel string `json:"rel"`
  184. } `json:"link"`
  185. CPUShares string `json:"cpu_shares"`
  186. CreationTime int64 `json:"creation_time"`
  187. DeleteProtected string `json:"delete_protected"`
  188. HighAvailability struct {
  189. Enabled string `json:"enabled"`
  190. Priority string `json:"priority"`
  191. } `json:"high_availability"`
  192. LargeIcon struct {
  193. Href string `json:"href"`
  194. ID string `json:"id"`
  195. } `json:"large_icon"`
  196. MemoryPolicy struct {
  197. Ballooning string `json:"ballooning"`
  198. Guaranteed string `json:"guaranteed"`
  199. Max string `json:"max"`
  200. } `json:"memory_policy"`
  201. MigrationDowntime string `json:"migration_downtime"`
  202. MultiQueuesEnabled string `json:"multi_queues_enabled"`
  203. PlacementPolicy struct {
  204. Affinity string `json:"affinity"`
  205. Hosts struct {
  206. Host []struct {
  207. Href string `json:"href"`
  208. ID string `json:"id"`
  209. } `json:"host"`
  210. } `json:"hosts"`
  211. } `json:"placement_policy"`
  212. SmallIcon struct {
  213. Href string `json:"href"`
  214. ID string `json:"id"`
  215. } `json:"small_icon"`
  216. StartPaused string `json:"start_paused"`
  217. StorageErrorResumeBehaviour string `json:"storage_error_resume_behaviour"`
  218. TimeZone struct {
  219. Name string `json:"name"`
  220. } `json:"time_zone"`
  221. CPUProfile struct {
  222. Href string `json:"href"`
  223. ID string `json:"id"`
  224. } `json:"cpu_profile"`
  225. }
  226. //url := "https://ovirt-cl.zi-tel.com/ovirt-engine/api/vms/c1a44128-6c6c-406b-99d8-4a68a99380c9"
  227. url := OvirtURL + "/ovirt-engine/api/vms/" + uuid
  228. //fmt.Println("Url:" ,url)
  229. method := "GET"
  230. client := &http.Client{
  231. }
  232. req, err := http.NewRequest(method, url, nil)
  233. if err != nil {
  234. return "Null"
  235. }
  236. req.Header.Add("Version", "4")
  237. req.Header.Add("Accept", "application/json")
  238. req.Header.Add("Authorization", "Basic YWRtaW5AaW50ZXJuYWw6a2VsYW5zaCBqMw==")
  239. res, err := client.Do(req)
  240. defer res.Body.Close()
  241. body, err := ioutil.ReadAll(res.Body)
  242. //fmt.Println("full response: ",string(body))
  243. _vmstatus := vmStatus{}
  244. _err := json.Unmarshal(body, &_vmstatus)
  245. if _err != nil {
  246. fmt.Println("Error: ", _err)
  247. }
  248. return _vmstatus.Status
  249. }
  250. func (o ovirt) addvm(c echo.Context) error {
  251. type AddVMResponse struct {
  252. NextRunConfigurationExists string `json:"next_run_configuration_exists"`
  253. NumaTuneMode string `json:"numa_tune_mode"`
  254. Status string `json:"status"`
  255. StopTime int64 `json:"stop_time"`
  256. OriginalTemplate struct {
  257. Href string `json:"href"`
  258. ID string `json:"id"`
  259. } `json:"original_template"`
  260. Template struct {
  261. Href string `json:"href"`
  262. ID string `json:"id"`
  263. } `json:"template"`
  264. Actions struct {
  265. Link []struct {
  266. Href string `json:"href"`
  267. Rel string `json:"rel"`
  268. } `json:"link"`
  269. } `json:"actions"`
  270. Name string `json:"name"`
  271. Description string `json:"description"`
  272. Comment string `json:"comment"`
  273. Href string `json:"href"`
  274. ID string `json:"id"`
  275. Bios struct {
  276. BootMenu struct {
  277. Enabled string `json:"enabled"`
  278. } `json:"boot_menu"`
  279. Type string `json:"type"`
  280. } `json:"bios"`
  281. Console struct {
  282. Enabled string `json:"enabled"`
  283. } `json:"console"`
  284. CPU struct {
  285. Architecture string `json:"architecture"`
  286. Topology struct {
  287. Cores string `json:"cores"`
  288. Sockets string `json:"sockets"`
  289. Threads string `json:"threads"`
  290. } `json:"topology"`
  291. } `json:"cpu"`
  292. Display struct {
  293. AllowOverride string `json:"allow_override"`
  294. CopyPasteEnabled string `json:"copy_paste_enabled"`
  295. DisconnectAction string `json:"disconnect_action"`
  296. FileTransferEnabled string `json:"file_transfer_enabled"`
  297. Monitors string `json:"monitors"`
  298. SingleQxlPci string `json:"single_qxl_pci"`
  299. SmartcardEnabled string `json:"smartcard_enabled"`
  300. Type string `json:"type"`
  301. } `json:"display"`
  302. Initialization struct {
  303. AuthorizedSSHKeys string `json:"authorized_ssh_keys"`
  304. CloudInitNetworkProtocol string `json:"cloud_init_network_protocol"`
  305. CustomScript string `json:"custom_script"`
  306. HostName string `json:"host_name"`
  307. NicConfigurations struct {
  308. } `json:"nic_configurations"`
  309. RegenerateSSHKeys string `json:"regenerate_ssh_keys"`
  310. UserName string `json:"user_name"`
  311. } `json:"initialization"`
  312. Io struct {
  313. Threads string `json:"threads"`
  314. } `json:"io"`
  315. Memory string `json:"memory"`
  316. Migration struct {
  317. AutoConverge string `json:"auto_converge"`
  318. Compressed string `json:"compressed"`
  319. Encrypted string `json:"encrypted"`
  320. } `json:"migration"`
  321. Origin string `json:"origin"`
  322. Os struct {
  323. Boot struct {
  324. Devices struct {
  325. Device []string `json:"device"`
  326. } `json:"devices"`
  327. } `json:"boot"`
  328. Type string `json:"type"`
  329. } `json:"os"`
  330. Sso struct {
  331. Methods struct {
  332. Method []struct {
  333. ID string `json:"id"`
  334. } `json:"method"`
  335. } `json:"methods"`
  336. } `json:"sso"`
  337. Stateless string `json:"stateless"`
  338. Type string `json:"type"`
  339. Usb struct {
  340. Enabled string `json:"enabled"`
  341. } `json:"usb"`
  342. Cluster struct {
  343. Href string `json:"href"`
  344. ID string `json:"id"`
  345. } `json:"cluster"`
  346. Quota struct {
  347. ID string `json:"id"`
  348. } `json:"quota"`
  349. CreationStatus string `json:"creation_status"`
  350. Link []struct {
  351. Href string `json:"href"`
  352. Rel string `json:"rel"`
  353. } `json:"link"`
  354. CPUShares string `json:"cpu_shares"`
  355. CreationTime int64 `json:"creation_time"`
  356. DeleteProtected string `json:"delete_protected"`
  357. HighAvailability struct {
  358. Enabled string `json:"enabled"`
  359. Priority string `json:"priority"`
  360. } `json:"high_availability"`
  361. LargeIcon struct {
  362. Href string `json:"href"`
  363. ID string `json:"id"`
  364. } `json:"large_icon"`
  365. MemoryPolicy struct {
  366. Ballooning string `json:"ballooning"`
  367. Guaranteed string `json:"guaranteed"`
  368. Max string `json:"max"`
  369. } `json:"memory_policy"`
  370. MigrationDowntime string `json:"migration_downtime"`
  371. MultiQueuesEnabled string `json:"multi_queues_enabled"`
  372. RngDevice struct {
  373. Source string `json:"source"`
  374. } `json:"rng_device"`
  375. SmallIcon struct {
  376. Href string `json:"href"`
  377. ID string `json:"id"`
  378. } `json:"small_icon"`
  379. SoundcardEnabled string `json:"soundcard_enabled"`
  380. StartPaused string `json:"start_paused"`
  381. StorageErrorResumeBehaviour string `json:"storage_error_resume_behaviour"`
  382. TimeZone struct {
  383. Name string `json:"name"`
  384. } `json:"time_zone"`
  385. VirtioScsi struct {
  386. Enabled string `json:"enabled"`
  387. } `json:"virtio_scsi"`
  388. CPUProfile struct {
  389. Href string `json:"href"`
  390. ID string `json:"id"`
  391. } `json:"cpu_profile"`
  392. }
  393. user := c.Get("user").(*jwt.Token)
  394. claims := user.Claims.(jwt.MapClaims)
  395. _sha256 := sha256.Sum256([]byte(string(claims["name"].(string))))
  396. var hashChannel_ = make(chan []byte, 1)
  397. hashChannel_ <- _sha256[:]
  398. token := decrypt(<-hashChannel_, claims["IPAToken"].(string))
  399. _BA := strings.Split(token, ";")
  400. BA := _BA[len(_BA)-2]
  401. //fmt.Println("Token: ", token)
  402. //fmt.Println("BA: ", BA)
  403. var vmname, vmdescr, vmcomment, templatename, cpuSock, cpuCore, cpuThread, mem string
  404. vmname = c.FormValue("VmName")
  405. vmdescr = c.FormValue("VmDescr")
  406. vmcomment = c.FormValue("VmComment")
  407. templatename = c.FormValue("VmTempl")
  408. cpuThread = c.FormValue("VmCPU")
  409. sshKey := c.FormValue("sshKey")
  410. rootpass := c.FormValue("rootpass")
  411. cpuCore = "1"
  412. cpuSock = "1"
  413. mem = c.FormValue("VmMem")
  414. url := OvirtURL + "/ovirt-engine/api/vms"
  415. method := "POST"
  416. _json := fmt.Sprintf(`
  417. {
  418. "name": "%s",
  419. "description": "%s",
  420. "comment": "%s",
  421. "cluster": {
  422. "name": "Default"
  423. },
  424. "template": {
  425. "name": "%s"
  426. },
  427. "cpu": {
  428. "topology": {
  429. "sockets": "%s",
  430. "cores": "%s",
  431. "threads": "%s"
  432. }
  433. },
  434. "memory": "%s",
  435. "memory_policy": {
  436. "ballooning": "true",
  437. "guaranteed": "%s",
  438. "over_commit": {
  439. "percent": "10"
  440. }
  441. }
  442. }
  443. `, vmname, vmdescr, vmcomment, templatename, cpuSock, cpuCore, cpuThread, mem, mem)
  444. payload := strings.NewReader(_json)
  445. //tr := &http.Transport{
  446. // TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  447. //}
  448. client := &http.Client{}
  449. req, err := http.NewRequest(method, url, payload)
  450. if err != nil {
  451. fmt.Println(err)
  452. }
  453. req.Header.Add("Version", "4")
  454. req.Header.Add("Accept", "application/json")
  455. req.Header.Add("Authorization", BA)
  456. req.Header.Add("Content-Type", "application/json")
  457. res, err := client.Do(req)
  458. body, err := ioutil.ReadAll(res.Body)
  459. //fmt.Println("len: ", len(_json))
  460. //fmt.Println("res:", res)
  461. if err != nil || res.StatusCode != 202 {
  462. resp := _response{
  463. Origin: "ovirt-addvm",
  464. Message: res.Status,
  465. Code: 1001,
  466. }
  467. b, _ := json.MarshalIndent(resp, "", " ")
  468. return c.String(http.StatusBadRequest, string(b))
  469. }
  470. defer res.Body.Close()
  471. addvmresponse := AddVMResponse{}
  472. err = json.Unmarshal(body, &addvmresponse)
  473. //fmt.Println("User UID:", claims["IPAUid"].(string))
  474. {
  475. db, err := sql.Open("mysql", MySQLUSER+":"+MySQLPASS+"@tcp(127.0.0.1:3306)/zicloud")
  476. if err != nil {
  477. resp := _response{
  478. Origin: "ovirt-addvm",
  479. Message: "Error on connecting db for save add vm result",
  480. Code: 1001,
  481. }
  482. b, _ := json.MarshalIndent(resp, "", " ")
  483. return c.String(http.StatusBadRequest, string(b))
  484. }
  485. defer db.Close()
  486. insert, err := db.Query("INSERT INTO service_profile VALUES ( '" + addvmresponse.ID + "'," +
  487. "'" + claims["IPAUid"].(string) + "'," +
  488. "'" + "VM" + "'," +
  489. "'" + addvmresponse.ID + "'," +
  490. "'" + login(BA).AuthenticatedUser.ID + "'," +
  491. "NOW()" + "," +
  492. "NOW() ," +
  493. "true" + "," +
  494. "'" + addvmresponse.Name + "' )")
  495. defer insert.Close()
  496. }
  497. if err != nil {
  498. resp := _response{
  499. Origin: "ovirt-addvm",
  500. Message: "Error on pars AddVMResponse",
  501. Code: 1001,
  502. }
  503. b, _ := json.MarshalIndent(resp, "", " ")
  504. return c.String(http.StatusBadRequest, string(b))
  505. }
  506. {
  507. url := OvirtURL + "/ovirt-engine/api/vms/" + addvmresponse.ID + "/start"
  508. method := "POST"
  509. nicAddress := "172.20.15.251"
  510. nicMask := "255.255.255.0"
  511. nicGW := "172.20.15.1"
  512. __json := fmt.Sprintf(`
  513. {
  514. "async":"true",
  515. "use_cloud_init": "true",
  516. "vm": {
  517. "initialization": {
  518. "authorized_ssh_keys": "%s",
  519. "host_name": "Sassan.local",
  520. "user_name": "root",
  521. "root_password": "%s",
  522. "nic_configurations": {
  523. "nic_configuration": [
  524. {
  525. "name": "eth0",
  526. "on_boot": "true",
  527. "boot_protocol": "static",
  528. "ip": {
  529. "address": "%s",
  530. "netmask": "%s",
  531. "gateway": "%s"
  532. }
  533. }
  534. ]
  535. },
  536. "dns_servers": "172.20.11.11"
  537. }
  538. }
  539. }
  540. `, sshKey, rootpass, nicAddress, nicMask, nicGW)
  541. //client := &http.Client{
  542. //}
  543. //payload := strings.NewReader(__json)
  544. //req, err := http.NewRequest(method, url, payload)
  545. //
  546. //if err != nil {
  547. // fmt.Println(err)
  548. //}
  549. //var startVMHeaders []TaskHeader
  550. sha256 := sha256.Sum256([]byte(addvmresponse.ID))
  551. var hashChannel = make(chan []byte, 1)
  552. hashChannel <- sha256[:]
  553. ___json := encrypt(<-hashChannel, __json)
  554. startVM := addVMTask{
  555. URL: url,
  556. JSON: ___json,
  557. Method: method,
  558. Headers: nil,
  559. }
  560. startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Version", Value: "4"})
  561. startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Accept", Value: "application/json"})
  562. startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Content-Type", Value: "application/json"})
  563. startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Authorization", Value: BA})
  564. //req.Header.Add("Version", "4")
  565. //req.Header.Add("Accept", "application/json")
  566. //req.Header.Add("Authorization", BA)
  567. //req.Header.Add("Content-Type", "application/json")
  568. //
  569. //res, err := client.Do(req)
  570. //defer res.Body.Close()
  571. uuid, _ := uuidgen("APIGW-Ovirt")
  572. __startVM, _ := json.MarshalIndent(startVM, "", " ")
  573. //fmt.Println("StartVMHeaders: ", string(__startVMHeaders))
  574. addTask(uuid, string(__startVM), "", "APIGW", "VM Initialization", addvmresponse.ID, "1", "1")
  575. }
  576. resp := _response{
  577. Origin: "ovirt-addvm",
  578. Message: "Done",
  579. Code: 1000,
  580. }
  581. b, _ := json.MarshalIndent(resp, "", " ")
  582. return c.String(http.StatusOK, string(b))
  583. }
  584. func (o ovirt) listVM(c echo.Context) error {
  585. user := c.Get("user").(*jwt.Token)
  586. claims := user.Claims.(jwt.MapClaims)
  587. _sha256 := sha256.Sum256([]byte(string(claims["name"].(string))))
  588. var hashChannel_ = make(chan []byte, 1)
  589. hashChannel_ <- _sha256[:]
  590. token := decrypt(<-hashChannel_, claims["IPAToken"].(string))
  591. _BA := strings.Split(token, ";")
  592. BA := _BA[len(_BA)-2]
  593. url := OvirtURL + "/ovirt-engine/api/vms"
  594. client := &http.Client{
  595. }
  596. req, err := http.NewRequest("GET", url, nil)
  597. if err != nil {
  598. fmt.Println(err)
  599. }
  600. req.Header.Add("Version", "4")
  601. req.Header.Add("Accept", "application/json")
  602. req.Header.Add("Authorization", BA)
  603. res, err := client.Do(req)
  604. defer res.Body.Close()
  605. body, err := ioutil.ReadAll(res.Body)
  606. //fmt.Printf("%s",body)
  607. //b, _ := json.MarshalIndent(body, "", " ")
  608. resp := _response{
  609. Origin: "ovirt-listvms",
  610. Message: string(body),
  611. Code: 1000,
  612. }
  613. b, _ := json.MarshalIndent(resp, "", " ")
  614. return c.String(http.StatusOK, string(b))
  615. }
  616. func (o ovirt) StartVM(c echo.Context) error {
  617. vmuuid := c.FormValue("VmUUID")
  618. user := c.Get("user").(*jwt.Token)
  619. claims := user.Claims.(jwt.MapClaims)
  620. _sha256 := sha256.Sum256([]byte(string(claims["name"].(string))))
  621. var hashChannel_ = make(chan []byte, 1)
  622. hashChannel_ <- _sha256[:]
  623. token := decrypt(<-hashChannel_, claims["IPAToken"].(string))
  624. _BA := strings.Split(token, ";")
  625. BA := _BA[len(_BA)-2]
  626. url := OvirtURL + "/ovirt-engine/api/vms/" + vmuuid + "/start"
  627. method := "POST"
  628. __json:="{\"async\":\"true\"}"
  629. sha256 := sha256.Sum256([]byte(vmuuid))
  630. var hashChannel = make(chan []byte, 1)
  631. hashChannel <- sha256[:]
  632. ___json := encrypt(<-hashChannel, __json)
  633. startVM := addVMTask{
  634. URL: url,
  635. JSON: ___json,
  636. Method: method,
  637. Headers: nil,
  638. }
  639. startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Version", Value: "4"})
  640. startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Accept", Value: "application/json"})
  641. startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Content-Type", Value: "application/json"})
  642. startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Authorization", Value: BA})
  643. //req.Header.Add("Version", "4")
  644. //req.Header.Add("Accept", "application/json")
  645. //req.Header.Add("Authorization", BA)
  646. //req.Header.Add("Content-Type", "application/json")
  647. //
  648. //res, err := client.Do(req)
  649. //defer res.Body.Close()
  650. uuid, _ := uuidgen("APIGW-Ovirt")
  651. __startVM, _ := json.MarshalIndent(startVM, "", " ")
  652. //fmt.Println("StartVMHeaders: ", string(__startVMHeaders))
  653. addTask(uuid, string(__startVM), "", "APIGW", "VM Start", vmuuid, "1", "1")
  654. resp := _response{
  655. Origin: "ovirt-StartVM",
  656. Message: "Done",
  657. Code: 1000,
  658. }
  659. b, _ := json.MarshalIndent(resp, "", " ")
  660. return c.String(http.StatusOK, string(b))
  661. }
  662. func (o ovirt) StopVM(c echo.Context) error {
  663. vmuuid := c.FormValue("VmUUID")
  664. user := c.Get("user").(*jwt.Token)
  665. claims := user.Claims.(jwt.MapClaims)
  666. _sha256 := sha256.Sum256([]byte(string(claims["name"].(string))))
  667. var hashChannel_ = make(chan []byte, 1)
  668. hashChannel_ <- _sha256[:]
  669. token := decrypt(<-hashChannel_, claims["IPAToken"].(string))
  670. _BA := strings.Split(token, ";")
  671. BA := _BA[len(_BA)-2]
  672. url := OvirtURL + "/ovirt-engine/api/vms/" + vmuuid + "/shutdown"
  673. method := "POST"
  674. __json:="{\"async\":\"true\"}"
  675. sha256 := sha256.Sum256([]byte(vmuuid))
  676. var hashChannel = make(chan []byte, 1)
  677. hashChannel <- sha256[:]
  678. ___json := encrypt(<-hashChannel, __json)
  679. startVM := addVMTask{
  680. URL: url,
  681. JSON: ___json,
  682. Method: method,
  683. Headers: nil,
  684. }
  685. startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Version", Value: "4"})
  686. startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Accept", Value: "application/json"})
  687. startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Content-Type", Value: "application/json"})
  688. startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Authorization", Value: BA})
  689. //req.Header.Add("Version", "4")
  690. //req.Header.Add("Accept", "application/json")
  691. //req.Header.Add("Authorization", BA)
  692. //req.Header.Add("Content-Type", "application/json")
  693. //
  694. //res, err := client.Do(req)
  695. //defer res.Body.Close()
  696. uuid, _ := uuidgen("APIGW-Ovirt")
  697. __startVM, _ := json.MarshalIndent(startVM, "", " ")
  698. //fmt.Println("StartVMHeaders: ", string(__startVMHeaders))
  699. addTask(uuid, string(__startVM), "", "APIGW", "VM Start", vmuuid, "1", "1")
  700. resp := _response{
  701. Origin: "ovirt-StartVM",
  702. Message: "Done",
  703. Code: 1000,
  704. }
  705. b, _ := json.MarshalIndent(resp, "", " ")
  706. return c.String(http.StatusOK, string(b))
  707. }
  708. func (o ovirt) RebootVM(c echo.Context) error {
  709. vmuuid := c.FormValue("VmUUID")
  710. user := c.Get("user").(*jwt.Token)
  711. claims := user.Claims.(jwt.MapClaims)
  712. _sha256 := sha256.Sum256([]byte(string(claims["name"].(string))))
  713. var hashChannel_ = make(chan []byte, 1)
  714. hashChannel_ <- _sha256[:]
  715. token := decrypt(<-hashChannel_, claims["IPAToken"].(string))
  716. _BA := strings.Split(token, ";")
  717. BA := _BA[len(_BA)-2]
  718. url := OvirtURL + "/ovirt-engine/api/vms/" + vmuuid + "/reboot"
  719. method := "POST"
  720. __json:="{\"async\":\"true\"}"
  721. sha256 := sha256.Sum256([]byte(vmuuid))
  722. var hashChannel = make(chan []byte, 1)
  723. hashChannel <- sha256[:]
  724. ___json := encrypt(<-hashChannel, __json)
  725. startVM := addVMTask{
  726. URL: url,
  727. JSON: ___json,
  728. Method: method,
  729. Headers: nil,
  730. }
  731. startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Version", Value: "4"})
  732. startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Accept", Value: "application/json"})
  733. startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Content-Type", Value: "application/json"})
  734. startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Authorization", Value: BA})
  735. //req.Header.Add("Version", "4")
  736. //req.Header.Add("Accept", "application/json")
  737. //req.Header.Add("Authorization", BA)
  738. //req.Header.Add("Content-Type", "application/json")
  739. //
  740. //res, err := client.Do(req)
  741. //defer res.Body.Close()
  742. uuid, _ := uuidgen("APIGW-Ovirt")
  743. __startVM, _ := json.MarshalIndent(startVM, "", " ")
  744. //fmt.Println("StartVMHeaders: ", string(__startVMHeaders))
  745. addTask(uuid, string(__startVM), "", "APIGW", "VM Start", vmuuid, "1", "1")
  746. resp := _response{
  747. Origin: "ovirt-StartVM",
  748. Message: "Done",
  749. Code: 1000,
  750. }
  751. b, _ := json.MarshalIndent(resp, "", " ")
  752. return c.String(http.StatusOK, string(b))
  753. }
  754. func (o ovirt) PowerOffVM(c echo.Context) error {
  755. vmuuid := c.FormValue("VmUUID")
  756. user := c.Get("user").(*jwt.Token)
  757. claims := user.Claims.(jwt.MapClaims)
  758. _sha256 := sha256.Sum256([]byte(string(claims["name"].(string))))
  759. var hashChannel_ = make(chan []byte, 1)
  760. hashChannel_ <- _sha256[:]
  761. token := decrypt(<-hashChannel_, claims["IPAToken"].(string))
  762. _BA := strings.Split(token, ";")
  763. BA := _BA[len(_BA)-2]
  764. url := OvirtURL + "/ovirt-engine/api/vms/" + vmuuid + "/stop"
  765. method := "POST"
  766. __json:="{\"async\":\"true\"}"
  767. sha256 := sha256.Sum256([]byte(vmuuid))
  768. var hashChannel = make(chan []byte, 1)
  769. hashChannel <- sha256[:]
  770. ___json := encrypt(<-hashChannel, __json)
  771. startVM := addVMTask{
  772. URL: url,
  773. JSON: ___json,
  774. Method: method,
  775. Headers: nil,
  776. }
  777. startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Version", Value: "4"})
  778. startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Accept", Value: "application/json"})
  779. startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Content-Type", Value: "application/json"})
  780. startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Authorization", Value: BA})
  781. //req.Header.Add("Version", "4")
  782. //req.Header.Add("Accept", "application/json")
  783. //req.Header.Add("Authorization", BA)
  784. //req.Header.Add("Content-Type", "application/json")
  785. //
  786. //res, err := client.Do(req)
  787. //defer res.Body.Close()
  788. uuid, _ := uuidgen("APIGW-Ovirt")
  789. __startVM, _ := json.MarshalIndent(startVM, "", " ")
  790. //fmt.Println("StartVMHeaders: ", string(__startVMHeaders))
  791. addTask(uuid, string(__startVM), "", "APIGW", "VM Start", vmuuid, "1", "1")
  792. resp := _response{
  793. Origin: "ovirt-StartVM",
  794. Message: "Done",
  795. Code: 1000,
  796. }
  797. b, _ := json.MarshalIndent(resp, "", " ")
  798. return c.String(http.StatusOK, string(b))
  799. }
  800. func (o ovirt) ResetVM(c echo.Context) error {
  801. resp := _response{
  802. Origin: "ovirt-ResetVM",
  803. Message: "not implemented",
  804. Code: 1003,
  805. }
  806. b, _ := json.MarshalIndent(resp, "", " ")
  807. return c.String(http.StatusOK, string(b))
  808. }
  809. func (o ovirt) AddNIC(c echo.Context) error {
  810. resp := _response{
  811. Origin: "ovirt-AddNIC",
  812. Message: "Done",
  813. Code: 1000,
  814. }
  815. b, _ := json.MarshalIndent(resp, "", " ")
  816. return c.String(http.StatusOK, string(b))
  817. }
  818. func (o ovirt) AddDisk(c echo.Context) error {
  819. resp := _response{
  820. Origin: "ovirt-AddDisk",
  821. Message: "Done",
  822. Code: 1000,
  823. }
  824. b, _ := json.MarshalIndent(resp, "", " ")
  825. return c.String(http.StatusOK, string(b))
  826. }
  827. func (o ovirt) EditVM(c echo.Context) error {
  828. resp := _response{
  829. Origin: "ovirt-EditVM",
  830. Message: "Done",
  831. Code: 1000,
  832. }
  833. b, _ := json.MarshalIndent(resp, "", " ")
  834. return c.String(http.StatusOK, string(b))
  835. }
  836. func (o ovirt) ResetPassword(c echo.Context) error {
  837. resp := _response{
  838. Origin: "ovirt-ResetPassword",
  839. Message: "not implemented",
  840. Code: 1003,
  841. }
  842. b, _ := json.MarshalIndent(resp, "", " ")
  843. return c.String(http.StatusOK, string(b))
  844. }