1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399 |
- package main
- import (
- "crypto/sha256"
- "database/sql"
- "encoding/json"
- "fmt"
- "github.com/dgrijalva/jwt-go"
- _ "github.com/go-sql-driver/mysql"
- "github.com/labstack/echo"
- "io/ioutil"
- "net/http"
- "strconv"
- "strings"
- )
- type ovirt struct {
- }
- var MySQLUSER = "apigw"
- var MySQLPASS = "^_P+^7Q$bmPj+$xB"
- func login(BA string) OvirtObject {
- url := OvirtURL + "/ovirt-engine/api"
- method := "GET"
- client := &http.Client{
- }
- req, err := http.NewRequest(method, url, nil)
- if err != nil {
- fmt.Println(err)
- }
- req.Header.Add("Version", "4")
- req.Header.Add("Accept", "application/json")
- req.Header.Add("Authorization", BA)
- res, err := client.Do(req)
- defer res.Body.Close()
- body, err := ioutil.ReadAll(res.Body)
- object := OvirtObject{}
- err = json.Unmarshal(body, &object)
- return object
- }
- type OvirtObject struct {
- ProductInfo struct {
- Name string `json:"name"`
- Version struct {
- Build string `json:"build"`
- FullVersion string `json:"full_version"`
- Major string `json:"major"`
- Minor string `json:"minor"`
- Revision string `json:"revision"`
- } `json:"version"`
- } `json:"product_info"`
- SpecialObjects struct {
- BlankTemplate struct {
- Href string `json:"href"`
- ID string `json:"id"`
- } `json:"blank_template"`
- RootTag struct {
- Href string `json:"href"`
- ID string `json:"id"`
- } `json:"root_tag"`
- } `json:"special_objects"`
- Time int64 `json:"time"`
- AuthenticatedUser struct {
- Href string `json:"href"`
- ID string `json:"id"`
- } `json:"authenticated_user"`
- EffectiveUser struct {
- Href string `json:"href"`
- ID string `json:"id"`
- } `json:"effective_user"`
- Link []struct {
- Href string `json:"href"`
- Rel string `json:"rel"`
- } `json:"link"`
- }
- type TaskHeader struct {
- Name string `json:"Name,omitempty"`
- Value string `json:"Value,omitempty"`
- }
- type addVMTask struct {
- URL string `json:"url"`
- JSON string `json:"__json"`
- Method string `json:"method"`
- Headers []struct {
- Name string `json:"Name,omitempty"`
- Value string `json:"Value,omitempty"`
- } `json:"headers"`
- }
- type VMStatistics struct {
- Statistic []struct {
- Kind string `json:"kind"`
- Type string `json:"type"`
- Unit string `json:"unit"`
- Values struct {
- Value []struct {
- Datum float64 `json:"datum"`
- } `json:"value"`
- } `json:"values"`
- VM struct {
- Href string `json:"href"`
- ID string `json:"id"`
- } `json:"vm,omitempty"`
- Name string `json:"name"`
- Description string `json:"description"`
- Href string `json:"href,omitempty"`
- ID string `json:"id"`
- } `json:"statistic"`
- }
- func (o ovirt) vmStatus(uuid string) string {
- type vmStatus struct {
- NextRunConfigurationExists string `json:"next_run_configuration_exists"`
- NumaTuneMode string `json:"numa_tune_mode"`
- Status string `json:"status"`
- StopTime int64 `json:"stop_time"`
- OriginalTemplate struct {
- Href string `json:"href"`
- ID string `json:"id"`
- } `json:"original_template"`
- Template struct {
- Href string `json:"href"`
- ID string `json:"id"`
- } `json:"template"`
- Actions struct {
- Link []struct {
- Href string `json:"href"`
- Rel string `json:"rel"`
- } `json:"link"`
- } `json:"actions"`
- Name string `json:"name"`
- Description string `json:"description"`
- Comment string `json:"comment"`
- Href string `json:"href"`
- ID string `json:"id"`
- Bios struct {
- BootMenu struct {
- Enabled string `json:"enabled"`
- } `json:"boot_menu"`
- Type string `json:"type"`
- } `json:"bios"`
- CPU struct {
- Architecture string `json:"architecture"`
- Topology struct {
- Cores string `json:"cores"`
- Sockets string `json:"sockets"`
- Threads string `json:"threads"`
- } `json:"topology"`
- } `json:"cpu"`
- Display struct {
- AllowOverride string `json:"allow_override"`
- CopyPasteEnabled string `json:"copy_paste_enabled"`
- DisconnectAction string `json:"disconnect_action"`
- FileTransferEnabled string `json:"file_transfer_enabled"`
- Monitors string `json:"monitors"`
- SingleQxlPci string `json:"single_qxl_pci"`
- SmartcardEnabled string `json:"smartcard_enabled"`
- Type string `json:"type"`
- } `json:"display"`
- Initialization struct {
- AuthorizedSSHKeys string `json:"authorized_ssh_keys"`
- CloudInitNetworkProtocol string `json:"cloud_init_network_protocol"`
- CustomScript string `json:"custom_script"`
- HostName string `json:"host_name"`
- NicConfigurations struct {
- } `json:"nic_configurations"`
- RegenerateSSHKeys string `json:"regenerate_ssh_keys"`
- UserName string `json:"user_name"`
- } `json:"initialization"`
- Io struct {
- Threads string `json:"threads"`
- } `json:"io"`
- Memory string `json:"memory"`
- Migration struct {
- AutoConverge string `json:"auto_converge"`
- Compressed string `json:"compressed"`
- Encrypted string `json:"encrypted"`
- } `json:"migration"`
- Origin string `json:"origin"`
- Os struct {
- Boot struct {
- Devices struct {
- Device []string `json:"device"`
- } `json:"devices"`
- } `json:"boot"`
- Type string `json:"type"`
- } `json:"os"`
- Sso struct {
- Methods struct {
- Method []struct {
- ID string `json:"id"`
- } `json:"method"`
- } `json:"methods"`
- } `json:"sso"`
- Stateless string `json:"stateless"`
- Type string `json:"type"`
- Usb struct {
- Enabled string `json:"enabled"`
- } `json:"usb"`
- Cluster struct {
- Href string `json:"href"`
- ID string `json:"id"`
- } `json:"cluster"`
- Quota struct {
- ID string `json:"id"`
- } `json:"quota"`
- Link []struct {
- Href string `json:"href"`
- Rel string `json:"rel"`
- } `json:"link"`
- CPUShares string `json:"cpu_shares"`
- CreationTime int64 `json:"creation_time"`
- DeleteProtected string `json:"delete_protected"`
- HighAvailability struct {
- Enabled string `json:"enabled"`
- Priority string `json:"priority"`
- } `json:"high_availability"`
- LargeIcon struct {
- Href string `json:"href"`
- ID string `json:"id"`
- } `json:"large_icon"`
- MemoryPolicy struct {
- Ballooning string `json:"ballooning"`
- Guaranteed string `json:"guaranteed"`
- Max string `json:"max"`
- } `json:"memory_policy"`
- MigrationDowntime string `json:"migration_downtime"`
- MultiQueuesEnabled string `json:"multi_queues_enabled"`
- PlacementPolicy struct {
- Affinity string `json:"affinity"`
- Hosts struct {
- Host []struct {
- Href string `json:"href"`
- ID string `json:"id"`
- } `json:"host"`
- } `json:"hosts"`
- } `json:"placement_policy"`
- SmallIcon struct {
- Href string `json:"href"`
- ID string `json:"id"`
- } `json:"small_icon"`
- StartPaused string `json:"start_paused"`
- StorageErrorResumeBehaviour string `json:"storage_error_resume_behaviour"`
- TimeZone struct {
- Name string `json:"name"`
- } `json:"time_zone"`
- CPUProfile struct {
- Href string `json:"href"`
- ID string `json:"id"`
- } `json:"cpu_profile"`
- }
- //url := "https://ovirt-cl.zi-tel.com/ovirt-engine/api/vms/c1a44128-6c6c-406b-99d8-4a68a99380c9"
- url := OvirtURL + "/ovirt-engine/api/vms/" + uuid
- //fmt.Println("Url:" ,url)
- method := "GET"
- client := &http.Client{
- }
- req, err := http.NewRequest(method, url, nil)
- if err != nil {
- return "Null"
- }
- req.Header.Add("Version", "4")
- req.Header.Add("Accept", "application/json")
- req.Header.Add("Authorization", "Basic YWRtaW5AaW50ZXJuYWw6a2VsYW5zaCBqMw==")
- res, err := client.Do(req)
- defer res.Body.Close()
- body, err := ioutil.ReadAll(res.Body)
- //fmt.Println("full response: ",string(body))
- _vmstatus := vmStatus{}
- _err := json.Unmarshal(body, &_vmstatus)
- if _err != nil {
- fmt.Println("Error: ", _err)
- }
- return _vmstatus.Status
- }
- func (o ovirt) addvm(c echo.Context) error {
- type AddVMResponse struct {
- NextRunConfigurationExists string `json:"next_run_configuration_exists"`
- NumaTuneMode string `json:"numa_tune_mode"`
- Status string `json:"status"`
- StopTime int64 `json:"stop_time"`
- OriginalTemplate struct {
- Href string `json:"href"`
- ID string `json:"id"`
- } `json:"original_template"`
- Template struct {
- Href string `json:"href"`
- ID string `json:"id"`
- } `json:"template"`
- Actions struct {
- Link []struct {
- Href string `json:"href"`
- Rel string `json:"rel"`
- } `json:"link"`
- } `json:"actions"`
- Name string `json:"name"`
- Description string `json:"description"`
- Comment string `json:"comment"`
- Href string `json:"href"`
- ID string `json:"id"`
- Bios struct {
- BootMenu struct {
- Enabled string `json:"enabled"`
- } `json:"boot_menu"`
- Type string `json:"type"`
- } `json:"bios"`
- Console struct {
- Enabled string `json:"enabled"`
- } `json:"console"`
- CPU struct {
- Architecture string `json:"architecture"`
- Topology struct {
- Cores string `json:"cores"`
- Sockets string `json:"sockets"`
- Threads string `json:"threads"`
- } `json:"topology"`
- } `json:"cpu"`
- Display struct {
- AllowOverride string `json:"allow_override"`
- CopyPasteEnabled string `json:"copy_paste_enabled"`
- DisconnectAction string `json:"disconnect_action"`
- FileTransferEnabled string `json:"file_transfer_enabled"`
- Monitors string `json:"monitors"`
- SingleQxlPci string `json:"single_qxl_pci"`
- SmartcardEnabled string `json:"smartcard_enabled"`
- Type string `json:"type"`
- } `json:"display"`
- Initialization struct {
- AuthorizedSSHKeys string `json:"authorized_ssh_keys"`
- CloudInitNetworkProtocol string `json:"cloud_init_network_protocol"`
- CustomScript string `json:"custom_script"`
- HostName string `json:"host_name"`
- NicConfigurations struct {
- } `json:"nic_configurations"`
- RegenerateSSHKeys string `json:"regenerate_ssh_keys"`
- UserName string `json:"user_name"`
- } `json:"initialization"`
- Io struct {
- Threads string `json:"threads"`
- } `json:"io"`
- Memory string `json:"memory"`
- Migration struct {
- AutoConverge string `json:"auto_converge"`
- Compressed string `json:"compressed"`
- Encrypted string `json:"encrypted"`
- } `json:"migration"`
- Origin string `json:"origin"`
- Os struct {
- Boot struct {
- Devices struct {
- Device []string `json:"device"`
- } `json:"devices"`
- } `json:"boot"`
- Type string `json:"type"`
- } `json:"os"`
- Sso struct {
- Methods struct {
- Method []struct {
- ID string `json:"id"`
- } `json:"method"`
- } `json:"methods"`
- } `json:"sso"`
- Stateless string `json:"stateless"`
- Type string `json:"type"`
- Usb struct {
- Enabled string `json:"enabled"`
- } `json:"usb"`
- Cluster struct {
- Href string `json:"href"`
- ID string `json:"id"`
- } `json:"cluster"`
- Quota struct {
- ID string `json:"id"`
- } `json:"quota"`
- CreationStatus string `json:"creation_status"`
- Link []struct {
- Href string `json:"href"`
- Rel string `json:"rel"`
- } `json:"link"`
- CPUShares string `json:"cpu_shares"`
- CreationTime int64 `json:"creation_time"`
- DeleteProtected string `json:"delete_protected"`
- HighAvailability struct {
- Enabled string `json:"enabled"`
- Priority string `json:"priority"`
- } `json:"high_availability"`
- LargeIcon struct {
- Href string `json:"href"`
- ID string `json:"id"`
- } `json:"large_icon"`
- MemoryPolicy struct {
- Ballooning string `json:"ballooning"`
- Guaranteed string `json:"guaranteed"`
- Max string `json:"max"`
- } `json:"memory_policy"`
- MigrationDowntime string `json:"migration_downtime"`
- MultiQueuesEnabled string `json:"multi_queues_enabled"`
- RngDevice struct {
- Source string `json:"source"`
- } `json:"rng_device"`
- SmallIcon struct {
- Href string `json:"href"`
- ID string `json:"id"`
- } `json:"small_icon"`
- SoundcardEnabled string `json:"soundcard_enabled"`
- StartPaused string `json:"start_paused"`
- StorageErrorResumeBehaviour string `json:"storage_error_resume_behaviour"`
- TimeZone struct {
- Name string `json:"name"`
- } `json:"time_zone"`
- VirtioScsi struct {
- Enabled string `json:"enabled"`
- } `json:"virtio_scsi"`
- CPUProfile struct {
- Href string `json:"href"`
- ID string `json:"id"`
- } `json:"cpu_profile"`
- }
- user := c.Get("user").(*jwt.Token)
- claims := user.Claims.(jwt.MapClaims)
- _sha256 := sha256.Sum256([]byte(string(claims["name"].(string))))
- var hashChannel_ = make(chan []byte, 1)
- hashChannel_ <- _sha256[:]
- token := decrypt(<-hashChannel_, claims["IPAToken"].(string))
- _BA := strings.Split(token, ";")
- BA := _BA[len(_BA)-2]
- //fmt.Println("Token: ", token)
- //fmt.Println("BA: ", BA)
- var vmname, vmdescr, vmcomment, templatename, cpuSock, cpuCore, cpuThread, mem string
- vmname = c.FormValue("VmName")
- vmdescr = c.FormValue("VmDescr")
- vmcomment = c.FormValue("VmComment")
- templatename = c.FormValue("VmTempl")
- cpuThread = c.FormValue("VmCPU")
- sshKey := c.FormValue("sshKey")
- rootpass := c.FormValue("rootpass")
- cpuCore = "1"
- cpuSock = "1"
- mem = c.FormValue("VmMem")
- url := OvirtURL + "/ovirt-engine/api/vms"
- method := "POST"
- _json := fmt.Sprintf(`
- {
- "name": "%s",
- "description": "%s",
- "comment": "%s",
- "cluster": {
- "name": "Default"
- },
- "template": {
- "name": "%s"
- },
- "cpu": {
- "topology": {
- "sockets": "%s",
- "cores": "%s",
- "threads": "%s"
- }
- },
- "memory": "%s",
- "memory_policy": {
- "ballooning": "true",
- "guaranteed": "%s",
- "over_commit": {
- "percent": "10"
- }
- }
- }
- `, vmname, vmdescr, vmcomment, templatename, cpuSock, cpuCore, cpuThread, mem, mem)
- payload := strings.NewReader(_json)
- //tr := &http.Transport{
- // TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
- //}
- client := &http.Client{}
- req, err := http.NewRequest(method, url, payload)
- if err != nil {
- fmt.Println(err)
- }
- req.Header.Add("Version", "4")
- req.Header.Add("Accept", "application/json")
- req.Header.Add("Authorization", BA)
- req.Header.Add("Content-Type", "application/json")
- res, err := client.Do(req)
- body, err := ioutil.ReadAll(res.Body)
- //fmt.Println("len: ", len(_json))
- //fmt.Println("res:", res)
- if err != nil || res.StatusCode != 202 {
- resp := _response{
- Origin: "ovirt-addvm",
- Message: res.Status,
- Code: 1001,
- }
- b, _ := json.MarshalIndent(resp, "", " ")
- return c.String(http.StatusBadRequest, string(b))
- }
- defer res.Body.Close()
- addvmresponse := AddVMResponse{}
- err = json.Unmarshal(body, &addvmresponse)
- //fmt.Println("User UID:", claims["IPAUid"].(string))
- {
- db, err := sql.Open("mysql", MySQLUSER+":"+MySQLPASS+"@tcp(127.0.0.1:3306)/zicloud")
- if err != nil {
- resp := _response{
- Origin: "ovirt-addvm",
- Message: "Error on connecting db for save add vm result",
- Code: 1001,
- }
- b, _ := json.MarshalIndent(resp, "", " ")
- return c.String(http.StatusBadRequest, string(b))
- }
- defer db.Close()
- insert, err := db.Query("INSERT INTO service_profile VALUES ( '" + addvmresponse.ID + "'," +
- "'" + claims["IPAUid"].(string) + "'," +
- "'" + "VM" + "'," +
- "'" + addvmresponse.ID + "'," +
- "'" + login(BA).AuthenticatedUser.ID + "'," +
- "NOW()" + "," +
- "NOW() ," +
- "true" + "," +
- "'" + addvmresponse.Name + "' )")
- defer insert.Close()
- }
- if err != nil {
- resp := _response{
- Origin: "ovirt-addvm",
- Message: "Error on pars AddVMResponse: " + err.Error(),
- Code: 1001,
- }
- //b, _ := json.MarshalIndent(resp, "", " ")
- return c.JSON(http.StatusBadRequest, resp)
- //return c.String(http.StatusBadRequest, string(b))
- }
- {
- url := OvirtURL + "/ovirt-engine/api/vms/" + addvmresponse.ID + "/start"
- method := "POST"
- nicAddress := "172.20.15.251"
- nicMask := "255.255.255.0"
- nicGW := "172.20.15.1"
- __json := fmt.Sprintf(`
- {
- "async":"true",
- "use_cloud_init": "true",
- "vm": {
- "initialization": {
- "authorized_ssh_keys": "%s",
- "host_name": "Sassan.local",
- "user_name": "root",
- "root_password": "%s",
- "nic_configurations": {
- "nic_configuration": [
- {
- "name": "eth0",
- "on_boot": "true",
- "boot_protocol": "static",
- "ip": {
- "address": "%s",
- "netmask": "%s",
- "gateway": "%s"
- }
- }
- ]
- },
- "dns_servers": "172.20.11.11"
- }
- }
- }
- `, sshKey, rootpass, nicAddress, nicMask, nicGW)
- //client := &http.Client{
- //}
- //payload := strings.NewReader(__json)
- //req, err := http.NewRequest(method, url, payload)
- //
- //if err != nil {
- // fmt.Println(err)
- //}
- //var startVMHeaders []TaskHeader
- sha256 := sha256.Sum256([]byte(addvmresponse.ID))
- var hashChannel = make(chan []byte, 1)
- hashChannel <- sha256[:]
- ___json := encrypt(<-hashChannel, __json)
- startVM := addVMTask{
- URL: url,
- JSON: ___json,
- Method: method,
- Headers: nil,
- }
- startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Version", Value: "4"})
- startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Accept", Value: "application/json"})
- startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Content-Type", Value: "application/json"})
- startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Authorization", Value: BA})
- //req.Header.Add("Version", "4")
- //req.Header.Add("Accept", "application/json")
- //req.Header.Add("Authorization", BA)
- //req.Header.Add("Content-Type", "application/json")
- //
- //res, err := client.Do(req)
- //defer res.Body.Close()
- uuid, _ := uuidgen("APIGW-Ovirt")
- __startVM, _ := json.MarshalIndent(startVM, "", " ")
- //fmt.Println("StartVMHeaders: ", string(__startVMHeaders))
- addTask(uuid, string(__startVM), "", "APIGW", "VM Initialization", addvmresponse.ID, "1", "1")
- }
- resp := _response{
- Origin: "ovirt-addvm",
- Message: "Done",
- Code: 1000,
- }
- //b, _ := json.MarshalIndent(resp, "", " ")
- return c.JSON(http.StatusOK, resp)
- //return c.String(http.StatusOK, string(b))
- }
- func (o ovirt) listVM(c echo.Context) error {
- user := c.Get("user").(*jwt.Token)
- claims := user.Claims.(jwt.MapClaims)
- _sha256 := sha256.Sum256([]byte(string(claims["name"].(string))))
- var hashChannel_ = make(chan []byte, 1)
- hashChannel_ <- _sha256[:]
- token := decrypt(<-hashChannel_, claims["IPAToken"].(string))
- _BA := strings.Split(token, ";")
- BA := _BA[len(_BA)-2]
- url := OvirtURL + "/ovirt-engine/api/vms"
- client := &http.Client{
- }
- req, err := http.NewRequest("GET", url, nil)
- if err != nil {
- fmt.Println(err)
- }
- req.Header.Add("Version", "4")
- req.Header.Add("Accept", "application/json")
- req.Header.Add("Authorization", BA)
- res, err := client.Do(req)
- defer res.Body.Close()
- body, err := ioutil.ReadAll(res.Body)
- //fmt.Printf("%s",body)
- //b, _ := json.MarshalIndent(body, "", " ")
- resp := _response{
- Origin: "ovirt-listvms",
- Message: string(body),
- Code: 1000,
- }
- //b, _ := json.MarshalIndent(resp, "", " ")
- return c.JSON(http.StatusOK, resp)
- //return c.String(http.StatusOK, string(b))
- }
- func (o ovirt) StartVM(c echo.Context) error {
- vmuuid := c.FormValue("VmUUID")
- user := c.Get("user").(*jwt.Token)
- claims := user.Claims.(jwt.MapClaims)
- _sha256 := sha256.Sum256([]byte(string(claims["name"].(string))))
- var hashChannel_ = make(chan []byte, 1)
- hashChannel_ <- _sha256[:]
- token := decrypt(<-hashChannel_, claims["IPAToken"].(string))
- _BA := strings.Split(token, ";")
- BA := _BA[len(_BA)-2]
- url := OvirtURL + "/ovirt-engine/api/vms/" + vmuuid + "/start"
- method := "POST"
- __json := "{\"async\":\"true\"}"
- sha256 := sha256.Sum256([]byte(vmuuid))
- var hashChannel = make(chan []byte, 1)
- hashChannel <- sha256[:]
- ___json := encrypt(<-hashChannel, __json)
- startVM := addVMTask{
- URL: url,
- JSON: ___json,
- Method: method,
- Headers: nil,
- }
- startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Version", Value: "4"})
- startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Accept", Value: "application/json"})
- startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Content-Type", Value: "application/json"})
- startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Authorization", Value: BA})
- //req.Header.Add("Version", "4")
- //req.Header.Add("Accept", "application/json")
- //req.Header.Add("Authorization", BA)
- //req.Header.Add("Content-Type", "application/json")
- //
- //res, err := client.Do(req)
- //defer res.Body.Close()
- uuid, _ := uuidgen("APIGW-Ovirt")
- __startVM, _ := json.MarshalIndent(startVM, "", " ")
- //fmt.Println("StartVMHeaders: ", string(__startVMHeaders))
- addTask(uuid, string(__startVM), "", "APIGW", "VM Start", vmuuid, "1", "1")
- resp := _response{
- Origin: "ovirt-StartVM",
- Message: "Done",
- Code: 1000,
- }
- //b, _ := json.MarshalIndent(resp, "", " ")
- return c.JSON(http.StatusOK, resp)
- //return c.String(http.StatusOK, string(b))
- }
- func (o ovirt) StopVM(c echo.Context) error {
- vmuuid := c.FormValue("VmUUID")
- user := c.Get("user").(*jwt.Token)
- claims := user.Claims.(jwt.MapClaims)
- _sha256 := sha256.Sum256([]byte(string(claims["name"].(string))))
- var hashChannel_ = make(chan []byte, 1)
- hashChannel_ <- _sha256[:]
- token := decrypt(<-hashChannel_, claims["IPAToken"].(string))
- _BA := strings.Split(token, ";")
- BA := _BA[len(_BA)-2]
- url := OvirtURL + "/ovirt-engine/api/vms/" + vmuuid + "/shutdown"
- method := "POST"
- __json := "{\"async\":\"true\"}"
- sha256 := sha256.Sum256([]byte(vmuuid))
- var hashChannel = make(chan []byte, 1)
- hashChannel <- sha256[:]
- ___json := encrypt(<-hashChannel, __json)
- startVM := addVMTask{
- URL: url,
- JSON: ___json,
- Method: method,
- Headers: nil,
- }
- startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Version", Value: "4"})
- startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Accept", Value: "application/json"})
- startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Content-Type", Value: "application/json"})
- startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Authorization", Value: BA})
- //req.Header.Add("Version", "4")
- //req.Header.Add("Accept", "application/json")
- //req.Header.Add("Authorization", BA)
- //req.Header.Add("Content-Type", "application/json")
- //
- //res, err := client.Do(req)
- //defer res.Body.Close()
- uuid, _ := uuidgen("APIGW-Ovirt")
- __startVM, _ := json.MarshalIndent(startVM, "", " ")
- //fmt.Println("StartVMHeaders: ", string(__startVMHeaders))
- addTask(uuid, string(__startVM), "", "APIGW", "VM Start", vmuuid, "1", "1")
- resp := _response{
- Origin: "ovirt-StartVM",
- Message: "Done",
- Code: 1000,
- }
- //b, _ := json.MarshalIndent(resp, "", " ")
- return c.JSON(http.StatusOK, resp)
- //return c.String(http.StatusOK, string(b))
- }
- func (o ovirt) RebootVM(c echo.Context) error {
- vmuuid := c.FormValue("VmUUID")
- user := c.Get("user").(*jwt.Token)
- claims := user.Claims.(jwt.MapClaims)
- _sha256 := sha256.Sum256([]byte(string(claims["name"].(string))))
- var hashChannel_ = make(chan []byte, 1)
- hashChannel_ <- _sha256[:]
- token := decrypt(<-hashChannel_, claims["IPAToken"].(string))
- _BA := strings.Split(token, ";")
- BA := _BA[len(_BA)-2]
- url := OvirtURL + "/ovirt-engine/api/vms/" + vmuuid + "/reboot"
- method := "POST"
- __json := "{\"async\":\"true\"}"
- sha256 := sha256.Sum256([]byte(vmuuid))
- var hashChannel = make(chan []byte, 1)
- hashChannel <- sha256[:]
- ___json := encrypt(<-hashChannel, __json)
- startVM := addVMTask{
- URL: url,
- JSON: ___json,
- Method: method,
- Headers: nil,
- }
- startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Version", Value: "4"})
- startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Accept", Value: "application/json"})
- startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Content-Type", Value: "application/json"})
- startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Authorization", Value: BA})
- //req.Header.Add("Version", "4")
- //req.Header.Add("Accept", "application/json")
- //req.Header.Add("Authorization", BA)
- //req.Header.Add("Content-Type", "application/json")
- //
- //res, err := client.Do(req)
- //defer res.Body.Close()
- uuid, _ := uuidgen("APIGW-Ovirt")
- __startVM, _ := json.MarshalIndent(startVM, "", " ")
- //fmt.Println("StartVMHeaders: ", string(__startVMHeaders))
- addTask(uuid, string(__startVM), "", "APIGW", "VM Start", vmuuid, "1", "1")
- resp := _response{
- Origin: "ovirt-StartVM",
- Message: "Done",
- Code: 1000,
- }
- //b, _ := json.MarshalIndent(resp, "", " ")
- //return c.String(http.StatusOK, string(b))
- return c.JSON(http.StatusOK, resp)
- }
- func (o ovirt) PowerOffVM(c echo.Context) error {
- vmuuid := c.FormValue("VmUUID")
- user := c.Get("user").(*jwt.Token)
- claims := user.Claims.(jwt.MapClaims)
- _sha256 := sha256.Sum256([]byte(string(claims["name"].(string))))
- var hashChannel_ = make(chan []byte, 1)
- hashChannel_ <- _sha256[:]
- token := decrypt(<-hashChannel_, claims["IPAToken"].(string))
- _BA := strings.Split(token, ";")
- BA := _BA[len(_BA)-2]
- url := OvirtURL + "/ovirt-engine/api/vms/" + vmuuid + "/stop"
- method := "POST"
- __json := "{\"async\":\"true\"}"
- sha256 := sha256.Sum256([]byte(vmuuid))
- var hashChannel = make(chan []byte, 1)
- hashChannel <- sha256[:]
- ___json := encrypt(<-hashChannel, __json)
- startVM := addVMTask{
- URL: url,
- JSON: ___json,
- Method: method,
- Headers: nil,
- }
- startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Version", Value: "4"})
- startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Accept", Value: "application/json"})
- startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Content-Type", Value: "application/json"})
- startVM.Headers = append(startVM.Headers, TaskHeader{Name: "Authorization", Value: BA})
- //req.Header.Add("Version", "4")
- //req.Header.Add("Accept", "application/json")
- //req.Header.Add("Authorization", BA)
- //req.Header.Add("Content-Type", "application/json")
- //
- //res, err := client.Do(req)
- //defer res.Body.Close()
- uuid, _ := uuidgen("APIGW-Ovirt")
- __startVM, _ := json.MarshalIndent(startVM, "", " ")
- //fmt.Println("StartVMHeaders: ", string(__startVMHeaders))
- addTask(uuid, string(__startVM), "", "APIGW", "VM Start", vmuuid, "1", "1")
- resp := _response{
- Origin: "ovirt-StartVM",
- Message: "Done",
- Code: 1000,
- }
- //b, _ := json.MarshalIndent(resp, "", " ")
- //return c.String(http.StatusOK, string(b))
- return c.JSON(http.StatusOK, resp)
- }
- func (o ovirt) ResetVM(c echo.Context) error {
- resp := _response{
- Origin: "ovirt-ResetVM",
- Message: "not implemented",
- Code: 1003,
- }
- //b, _ := json.MarshalIndent(resp, "", " ")
- return c.JSON(http.StatusOK, resp)
- //return c.String(http.StatusOK, string(b))
- }
- func (o ovirt) AddNIC(c echo.Context) error {
- resp := _response{
- Origin: "ovirt-AddNIC",
- Message: "Done",
- Code: 1000,
- }
- //b, _ := json.MarshalIndent(resp, "", " ")
- //return c.String(http.StatusOK, string(b))
- return c.JSON(http.StatusOK, resp)
- }
- func (o ovirt) AddDisk(c echo.Context) error {
- resp := _response{
- Origin: "ovirt-AddDisk",
- Message: "Done",
- Code: 1000,
- }
- //b, _ := json.MarshalIndent(resp, "", " ")
- //return c.String(http.StatusOK, string(b))
- return c.JSON(http.StatusOK, resp)
- }
- func (o ovirt) EditVM(c echo.Context) error {
- resp := _response{
- Origin: "ovirt-EditVM",
- Message: "Done",
- Code: 1000,
- }
- //b, _ := json.MarshalIndent(resp, "", " ")
- //return c.String(http.StatusOK, string(b))
- return c.JSON(http.StatusOK, resp)
- }
- func (o ovirt) ResetPassword(c echo.Context) error {
- resp := _response{
- Origin: "ovirt-ResetPassword",
- Message: "not implemented",
- Code: 1003,
- }
- //b, _ := json.MarshalIndent(resp, "", " ")
- //return c.String(http.StatusOK, string(b))
- return c.JSON(http.StatusOK, resp)
- }
- func vmStatistics(BA string, VMUUID string) (string, float64, float64) {
- //ram,cpu,storage
- var _disk []string
- {
- type DiskAttachments struct {
- DiskAttachment []struct {
- Active string `json:"active"`
- Bootable string `json:"bootable"`
- Interface string `json:"interface"`
- LogicalName string `json:"logical_name"`
- PassDiscard string `json:"pass_discard"`
- ReadOnly string `json:"read_only"`
- UsesScsiReservation string `json:"uses_scsi_reservation"`
- Disk struct {
- Href string `json:"href"`
- ID string `json:"id"`
- } `json:"disk"`
- VM struct {
- Href string `json:"href"`
- ID string `json:"id"`
- } `json:"vm"`
- Href string `json:"href"`
- ID string `json:"id"`
- } `json:"disk_attachment"`
- }
- url := OvirtURL + "/ovirt-engine/api/vms/" + VMUUID + "/diskattachments"
- //fmt.Println("Url:" ,url)
- method := "GET"
- client := &http.Client{
- }
- req, err := http.NewRequest(method, url, nil)
- if err != nil {
- return "", -1, -1
- }
- req.Header.Add("Version", "4")
- req.Header.Add("Accept", "application/json")
- req.Header.Add("Authorization", BA)
- res, err := client.Do(req)
- defer res.Body.Close()
- body, err := ioutil.ReadAll(res.Body)
- diskattachments := DiskAttachments{}
- _err := json.Unmarshal(body, &diskattachments)
- if _err != nil {
- fmt.Println("Error: ", _err)
- }
- for _, v := range diskattachments.DiskAttachment {
- url := OvirtURL + "/ovirt-engine/api/disks/" + v.Disk.ID
- type DiskAttachment struct {
- ActualSize string `json:"actual_size"`
- Alias string `json:"alias"`
- Backup string `json:"backup"`
- ContentType string `json:"content_type"`
- Format string `json:"format"`
- ImageID string `json:"image_id"`
- PropagateErrors string `json:"propagate_errors"`
- ProvisionedSize string `json:"provisioned_size"`
- QcowVersion string `json:"qcow_version"`
- Shareable string `json:"shareable"`
- Sparse string `json:"sparse"`
- Status string `json:"status"`
- StorageType string `json:"storage_type"`
- TotalSize string `json:"total_size"`
- WipeAfterDelete string `json:"wipe_after_delete"`
- DiskProfile struct {
- Href string `json:"href"`
- ID string `json:"id"`
- } `json:"disk_profile"`
- Quota struct {
- Href string `json:"href"`
- ID string `json:"id"`
- } `json:"quota"`
- StorageDomains struct {
- StorageDomain []struct {
- Href string `json:"href"`
- ID string `json:"id"`
- } `json:"storage_domain"`
- } `json:"storage_domains"`
- Actions struct {
- Link []struct {
- Href string `json:"href"`
- Rel string `json:"rel"`
- } `json:"link"`
- } `json:"actions"`
- Name string `json:"name"`
- Description string `json:"description"`
- Href string `json:"href"`
- ID string `json:"id"`
- Link []struct {
- Href string `json:"href"`
- Rel string `json:"rel"`
- } `json:"link"`
- }
- //fmt.Println("Url:" ,url)
- method := "GET"
- client := &http.Client{
- }
- req, _ := http.NewRequest(method, url, nil)
- //if err != nil {
- // return "Null"
- //}
- req.Header.Add("Version", "4")
- req.Header.Add("Accept", "application/json")
- req.Header.Add("Authorization", BA)
- res, _ := client.Do(req)
- defer res.Body.Close()
- body, _ := ioutil.ReadAll(res.Body)
- //fmt.Println("full response: ",string(body))
- disk := DiskAttachment{}
- _err := json.Unmarshal(body, &disk)
- if _err != nil {
- fmt.Println("Error: ", _err)
- }
- _disk = append(_disk, disk.ProvisionedSize)
- //_disk = D_disk + "" + disk.ProvisionedSize+" "
- }
- }
- url := OvirtURL + "/ovirt-engine/api/vms/" + VMUUID + "/statistics"
- //fmt.Println("Url:" ,url)
- method := "GET"
- client := &http.Client{
- }
- req, err := http.NewRequest(method, url, nil)
- if err != nil {
- return "", -1, -1
- }
- req.Header.Add("Version", "4")
- req.Header.Add("Accept", "application/json")
- req.Header.Add("Authorization", BA)
- res, err := client.Do(req)
- defer res.Body.Close()
- body, err := ioutil.ReadAll(res.Body)
- //fmt.Println("full response: ",string(body))
- _vmstatistics := VMStatistics{}
- _err := json.Unmarshal(body, &_vmstatistics)
- if _err != nil {
- fmt.Println("Error: ", _err)
- }
- cpuUUID := _vmstatistics.Statistic[4].ID
- memUsedUUID := _vmstatistics.Statistic[1].ID
- memTotalUUID := _vmstatistics.Statistic[0].ID
- //fmt.Println(memUsedUUID)
- var cpu, memUsed, memTotal string
- type ItemUsage struct {
- Kind string `json:"kind"`
- Type string `json:"type"`
- Unit string `json:"unit"`
- Values struct {
- Value []struct {
- Datum float64 `json:"datum"`
- } `json:"value"`
- } `json:"values"`
- VM struct {
- Href string `json:"href"`
- ID string `json:"id"`
- } `json:"vm"`
- Name string `json:"name"`
- Description string `json:"description"`
- Href string `json:"href"`
- ID string `json:"id"`
- }
- {
- url := OvirtURL + "/ovirt-engine/api/vms/" + VMUUID + "/statistics/" + cpuUUID
- //fmt.Println("Url:" ,url)
- method := "GET"
- client := &http.Client{
- }
- req, err := http.NewRequest(method, url, nil)
- if err != nil {
- return "", -1, -1
- }
- req.Header.Add("Version", "4")
- req.Header.Add("Accept", "application/json")
- req.Header.Add("Authorization", BA)
- res, err := client.Do(req)
- defer res.Body.Close()
- body, err := ioutil.ReadAll(res.Body)
- cpuUsage := ItemUsage{}
- _err := json.Unmarshal(body, &cpuUsage)
- if _err != nil {
- fmt.Println("Error: ", _err)
- }
- cpu = fmt.Sprintf("%f", cpuUsage.Values.Value[0].Datum)
- }
- {
- url := OvirtURL + "/ovirt-engine/api/vms/" + VMUUID + "/statistics/" + memUsedUUID
- //fmt.Println("Url:" ,url)
- method := "GET"
- client := &http.Client{
- }
- req, err := http.NewRequest(method, url, nil)
- if err != nil {
- return "", -1, -1
- }
- req.Header.Add("Version", "4")
- req.Header.Add("Accept", "application/json")
- req.Header.Add("Authorization", BA)
- res, err := client.Do(req)
- defer res.Body.Close()
- body, err := ioutil.ReadAll(res.Body)
- memUsage := ItemUsage{}
- _err := json.Unmarshal(body, &memUsage)
- if _err != nil {
- fmt.Println("Error: ", _err)
- }
- memUsed = fmt.Sprintf("%f", memUsage.Values.Value[0].Datum)
- }
- {
- url := OvirtURL + "/ovirt-engine/api/vms/" + VMUUID + "/statistics/" + memTotalUUID
- //fmt.Println("Url:" ,url)
- method := "GET"
- client := &http.Client{
- }
- req, err := http.NewRequest(method, url, nil)
- if err != nil {
- return "", -1, -1
- }
- req.Header.Add("Version", "4")
- req.Header.Add("Accept", "application/json")
- req.Header.Add("Authorization", BA)
- res, err := client.Do(req)
- defer res.Body.Close()
- body, err := ioutil.ReadAll(res.Body)
- memUsage := ItemUsage{}
- _err := json.Unmarshal(body, &memUsage)
- if _err != nil {
- fmt.Println("Error: ", _err)
- }
- memTotal = fmt.Sprintf("%f", memUsage.Values.Value[0].Datum)
- }
- //fmt.Println("CPU:" ,cpu)
- //fmt.Println("MEM:", memUsed)
- //fmt.Println("MEM:", memTotal)
- _memusage, _ := strconv.ParseFloat(memUsed, 32)
- _memtotal, _ := strconv.ParseFloat(memTotal, 32)
- mem := _memusage / _memtotal
- _cpu, _ := strconv.ParseFloat(cpu, 64)
- return strings.Join(_disk,", "), _cpu, mem
- }
- func (o ovirt) vmDetails(c echo.Context) error {
- //name,...,traffic,ip,status
- user := c.Get("user").(*jwt.Token)
- claims := user.Claims.(jwt.MapClaims)
- _sha256 := sha256.Sum256([]byte(string(claims["name"].(string))))
- var hashChannel_ = make(chan []byte, 1)
- hashChannel_ <- _sha256[:]
- token := decrypt(<-hashChannel_, claims["IPAToken"].(string))
- _BA := strings.Split(token, ";")
- BA := _BA[len(_BA)-2]
- uuid := c.FormValue("uuid")
- if len(uuid) < 5 {
- resp := _response{
- Origin: "AddUser",
- Message: "Missing UUID",
- Code: 1001,
- }
- return c.JSON(http.StatusNotFound, resp)
- }
- type vmStatus struct {
- NextRunConfigurationExists string `json:"next_run_configuration_exists"`
- NumaTuneMode string `json:"numa_tune_mode"`
- Status string `json:"status"`
- StopTime int64 `json:"stop_time"`
- OriginalTemplate struct {
- Href string `json:"href"`
- ID string `json:"id"`
- } `json:"original_template"`
- Template struct {
- Href string `json:"href"`
- ID string `json:"id"`
- } `json:"template"`
- Actions struct {
- Link []struct {
- Href string `json:"href"`
- Rel string `json:"rel"`
- } `json:"link"`
- } `json:"actions"`
- Name string `json:"name"`
- Description string `json:"description"`
- Comment string `json:"comment"`
- Href string `json:"href"`
- ID string `json:"id"`
- Bios struct {
- BootMenu struct {
- Enabled string `json:"enabled"`
- } `json:"boot_menu"`
- Type string `json:"type"`
- } `json:"bios"`
- CPU struct {
- Architecture string `json:"architecture"`
- Topology struct {
- Cores string `json:"cores"`
- Sockets string `json:"sockets"`
- Threads string `json:"threads"`
- } `json:"topology"`
- } `json:"cpu"`
- Display struct {
- AllowOverride string `json:"allow_override"`
- CopyPasteEnabled string `json:"copy_paste_enabled"`
- DisconnectAction string `json:"disconnect_action"`
- FileTransferEnabled string `json:"file_transfer_enabled"`
- Monitors string `json:"monitors"`
- SingleQxlPci string `json:"single_qxl_pci"`
- SmartcardEnabled string `json:"smartcard_enabled"`
- Type string `json:"type"`
- } `json:"display"`
- Initialization struct {
- AuthorizedSSHKeys string `json:"authorized_ssh_keys"`
- CloudInitNetworkProtocol string `json:"cloud_init_network_protocol"`
- CustomScript string `json:"custom_script"`
- HostName string `json:"host_name"`
- NicConfigurations struct {
- } `json:"nic_configurations"`
- RegenerateSSHKeys string `json:"regenerate_ssh_keys"`
- UserName string `json:"user_name"`
- } `json:"initialization"`
- Io struct {
- Threads string `json:"threads"`
- } `json:"io"`
- Memory string `json:"memory"`
- Migration struct {
- AutoConverge string `json:"auto_converge"`
- Compressed string `json:"compressed"`
- Encrypted string `json:"encrypted"`
- } `json:"migration"`
- Origin string `json:"origin"`
- Os struct {
- Boot struct {
- Devices struct {
- Device []string `json:"device"`
- } `json:"devices"`
- } `json:"boot"`
- Type string `json:"type"`
- } `json:"os"`
- Sso struct {
- Methods struct {
- Method []struct {
- ID string `json:"id"`
- } `json:"method"`
- } `json:"methods"`
- } `json:"sso"`
- Stateless string `json:"stateless"`
- Type string `json:"type"`
- Usb struct {
- Enabled string `json:"enabled"`
- } `json:"usb"`
- Cluster struct {
- Href string `json:"href"`
- ID string `json:"id"`
- } `json:"cluster"`
- Quota struct {
- ID string `json:"id"`
- } `json:"quota"`
- Link []struct {
- Href string `json:"href"`
- Rel string `json:"rel"`
- } `json:"link"`
- CPUShares string `json:"cpu_shares"`
- CreationTime int64 `json:"creation_time"`
- DeleteProtected string `json:"delete_protected"`
- HighAvailability struct {
- Enabled string `json:"enabled"`
- Priority string `json:"priority"`
- } `json:"high_availability"`
- LargeIcon struct {
- Href string `json:"href"`
- ID string `json:"id"`
- } `json:"large_icon"`
- MemoryPolicy struct {
- Ballooning string `json:"ballooning"`
- Guaranteed string `json:"guaranteed"`
- Max string `json:"max"`
- } `json:"memory_policy"`
- MigrationDowntime string `json:"migration_downtime"`
- MultiQueuesEnabled string `json:"multi_queues_enabled"`
- PlacementPolicy struct {
- Affinity string `json:"affinity"`
- Hosts struct {
- Host []struct {
- Href string `json:"href"`
- ID string `json:"id"`
- } `json:"host"`
- } `json:"hosts"`
- } `json:"placement_policy"`
- SmallIcon struct {
- Href string `json:"href"`
- ID string `json:"id"`
- } `json:"small_icon"`
- StartPaused string `json:"start_paused"`
- StorageErrorResumeBehaviour string `json:"storage_error_resume_behaviour"`
- TimeZone struct {
- Name string `json:"name"`
- } `json:"time_zone"`
- CPUProfile struct {
- Href string `json:"href"`
- ID string `json:"id"`
- } `json:"cpu_profile"`
- }
- //url := "https://ovirt-cl.zi-tel.com/ovirt-engine/api/vms/c1a44128-6c6c-406b-99d8-4a68a99380c9"
- url := OvirtURL + "/ovirt-engine/api/vms/" + uuid
- //fmt.Println("Url:" ,url)
- method := "GET"
- client := &http.Client{
- }
- req, _ := http.NewRequest(method, url, nil)
- //if err != nil {
- // return "Null"
- //}
- req.Header.Add("Version", "4")
- req.Header.Add("Accept", "application/json")
- req.Header.Add("Authorization", BA)
- res, _ := client.Do(req)
- defer res.Body.Close()
- body, _ := ioutil.ReadAll(res.Body)
- //fmt.Println("full response: ",string(body))
- _vmstatus := vmStatus{}
- _err := json.Unmarshal(body, &_vmstatus)
- if _err != nil {
- fmt.Println("Error: ", _err)
- }
- disk, cpu, ram := vmStatistics(BA, uuid)
- type AutoGenerated struct {
- Message struct {
- Disk string `json:"Disk"`
- Memory string `json:"Memory"`
- CPU string `json:"CPU"`
- NetAddress string `json:"NetAddress"`
- Traffic string `json:"Traffic"`
- Status string `json:"Status"`
- Name string `json:"Name"`
- } `json:"message"`
- Origin string `json:"origin"`
- Code int `json:"code"`
- }
- resp := AutoGenerated{
- Message: struct {
- Disk string `json:"Disk"`
- Memory string `json:"Memory"`
- CPU string `json:"CPU"`
- NetAddress string `json:"NetAddress"`
- Traffic string `json:"Traffic"`
- Status string `json:"Status"`
- Name string `json:"Name"`
- }{
- Disk: disk,
- Memory: fmt.Sprintf("%f", ram),
- CPU: fmt.Sprintf("%f", cpu),
- NetAddress: "",
- Traffic: "",
- Status: _vmstatus.Status,
- Name: _vmstatus.Name,
- },
- Origin: "vmDetails",
- Code: 1000,
- }
- return c.JSON(http.StatusOK, resp)
- //return _vmstatus.Status, _vmstatus.Name, disk, cpu, ram
- }
|