|
@@ -0,0 +1,883 @@
|
|
|
+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))
|
|
|
+
|
|
|
+}
|