123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883 |
- 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"
- "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"`
- }
- 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",
- Code: 1001,
- }
- b, _ := json.MarshalIndent(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.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.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.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.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))
- }
- 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))
- }
- func (o ovirt) ResetVM(c echo.Context) error {
- resp := _response{
- Origin: "ovirt-ResetVM",
- Message: "not implemented",
- Code: 1003,
- }
- b, _ := json.MarshalIndent(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))
- }
- 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))
- }
- 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))
- }
- 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))
- }
|