|
@@ -1,18 +1,26 @@
|
|
|
package main
|
|
|
|
|
|
import (
|
|
|
+ "crypto/rand"
|
|
|
+ "crypto/rsa"
|
|
|
"crypto/sha256"
|
|
|
+ "crypto/tls"
|
|
|
+ "crypto/x509"
|
|
|
"database/sql"
|
|
|
"encoding/json"
|
|
|
+ "encoding/pem"
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
"github.com/dgrijalva/jwt-go"
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
|
"github.com/labstack/echo"
|
|
|
"io/ioutil"
|
|
|
+ mrand "math/rand"
|
|
|
"net/http"
|
|
|
+ "os"
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
+ "time"
|
|
|
)
|
|
|
|
|
|
type ovirt struct {
|
|
@@ -296,20 +304,35 @@ func (o ovirt) vmStatus(uuid string) string {
|
|
|
req, err := http.NewRequest(method, url, nil)
|
|
|
|
|
|
if err != nil {
|
|
|
- return "Null"
|
|
|
+ fmt.Println("Error in vmStatus ", err)
|
|
|
+ //return "Null"
|
|
|
}
|
|
|
req.Header.Add("Version", "4")
|
|
|
req.Header.Add("Accept", "application/json")
|
|
|
+ //fmt.Println("in vmStatus 1 " )
|
|
|
//TODO: replace BA
|
|
|
req.Header.Add("Authorization", "Basic YWRtaW5AaW50ZXJuYWw6a2VsYW5zaCBqMw==")
|
|
|
+ //fmt.Println("in vmStatus 1 2 " )
|
|
|
|
|
|
res, err := client.Do(req)
|
|
|
+ //fmt.Println("in vmStatus :",err )
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("Error in vmStatus ", err)
|
|
|
+ //return "Null"
|
|
|
+ }
|
|
|
defer res.Body.Close()
|
|
|
+ //fmt.Println("in vmStatus 3 " )
|
|
|
body, err := ioutil.ReadAll(res.Body)
|
|
|
-
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("Error in vmStatus ", err)
|
|
|
+ //return "Null"
|
|
|
+ }
|
|
|
//fmt.Println("full response: ",string(body))
|
|
|
+ //fmt.Println("in vmStatus 4 " )
|
|
|
+
|
|
|
_vmstatus := vmStatus{}
|
|
|
_err := json.Unmarshal(body, &_vmstatus)
|
|
|
+ //fmt.Println("in vmStatus 5 " )
|
|
|
if _err != nil {
|
|
|
fmt.Println("Error in vmStatus ", _err)
|
|
|
}
|
|
@@ -342,7 +365,7 @@ func (o ovirt) addvm(c echo.Context) error {
|
|
|
_BA := strings.Split(token, ";")
|
|
|
BA := _BA[len(_BA)-2]
|
|
|
var vmname, vmdescr, vmcomment, templatename, cpuSock, cpuCore, cpuThread, mem, Disk, nic, period, extraBW string
|
|
|
- vmname = login(BA).AuthenticatedUser.ID + "_" + c.FormValue("VmName")
|
|
|
+ vmname = login(BA).AuthenticatedUser.ID + "-_-" + c.FormValue("VmName")
|
|
|
vmdescr = c.FormValue("VmDescr")
|
|
|
vmcomment = c.FormValue("VmComment")
|
|
|
templatename = c.FormValue("VmTempl")
|
|
@@ -928,12 +951,16 @@ func (o ovirt) listVM(c echo.Context) error {
|
|
|
} `json:"reported_devices"`
|
|
|
} `json:"NIC"`
|
|
|
}{}
|
|
|
+ //fmt.Println("Customer UUID:",login(BA).AuthenticatedUser.ID)
|
|
|
for _, v := range _VMLists.VM {
|
|
|
y.ID = v.ID
|
|
|
y.Fqdn = v.Fqdn
|
|
|
y.StartTime = v.StartTime
|
|
|
y.Status = v.Status
|
|
|
y.Name = v.Name
|
|
|
+ if len(v.Name) > 35 {
|
|
|
+ y.Name = v.Name[len(login(BA).AuthenticatedUser.ID)+3:]
|
|
|
+ }
|
|
|
y.Description = v.Description
|
|
|
y.Comment = v.Comment
|
|
|
y.CPU.Topology.Cores = v.CPU.Topology.Cores
|
|
@@ -941,14 +968,14 @@ func (o ovirt) listVM(c echo.Context) error {
|
|
|
y.CPU.Topology.Threads = v.CPU.Topology.Threads
|
|
|
y.Memory = v.Memory
|
|
|
y.CreationTime = v.CreationTime
|
|
|
- y.NIC = listNIC(v.ID)
|
|
|
- y.Disk = listDisks(v.ID)
|
|
|
+ y.NIC = listNIC(v.ID, BA)
|
|
|
+ y.Disk = listDisks(v.ID, BA)
|
|
|
|
|
|
_Data = append(_Data, y)
|
|
|
}
|
|
|
_listVMResponse.Data = _Data
|
|
|
if err != nil {
|
|
|
- fmt.Println(err)
|
|
|
+ //fmt.Println("Error on ListVM",err)
|
|
|
//return CPUPrice, memPrice, StoragePrice, IPPrice, extraBWPrice
|
|
|
resp := _response{
|
|
|
Origin: "ovirt-listvms",
|
|
@@ -1028,16 +1055,27 @@ func (o ovirt) StopVM(c echo.Context) error {
|
|
|
var hashChannel = make(chan []byte, 1)
|
|
|
hashChannel <- sha256[:]
|
|
|
___json := encrypt(<-hashChannel, __json)
|
|
|
- startVM := addVMTask{
|
|
|
- URL: url,
|
|
|
- JSON: ___json,
|
|
|
- Method: method,
|
|
|
- Headers: nil,
|
|
|
+
|
|
|
+ payload := strings.NewReader(___json)
|
|
|
+ tr := &http.Transport{
|
|
|
+ TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
}
|
|
|
- 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})
|
|
|
+ client := &http.Client{Transport: tr}
|
|
|
+ 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)
|
|
|
+ res, err := client.Do(req)
|
|
|
+ if err != nil {
|
|
|
+ //return c.String(http.StatusBadRequest, "Error"+err.Error())
|
|
|
+ return c.JSON(http.StatusOK, "Error"+err.Error())
|
|
|
+ }
|
|
|
+ defer res.Body.Close()
|
|
|
+ body, err := ioutil.ReadAll(res.Body)
|
|
|
+ fmt.Println("Resp:" + string(body))
|
|
|
|
|
|
//req.Header.Add("Version", "4")
|
|
|
//req.Header.Add("Accept", "application/json")
|
|
@@ -1046,10 +1084,10 @@ func (o ovirt) StopVM(c echo.Context) error {
|
|
|
//
|
|
|
//res, err := client.Do(req)
|
|
|
//defer res.Body.Close()
|
|
|
- uuid, _ := uuidgen("APIGW-Ovirt")
|
|
|
- __startVM, _ := json.MarshalIndent(startVM, "", " ")
|
|
|
+ //uuid, _ := uuidgen("APIGW-Ovirt")
|
|
|
+ //__startVM, _ := json.MarshalIndent(req, "", " ")
|
|
|
//fmt.Println("StartVMHeaders: ", string(__startVMHeaders))
|
|
|
- addTask(uuid, string(__startVM), "", "APIGW", "VM Start", vmuuid, "1", "1")
|
|
|
+ //addTask(uuid, string(__startVM), "", "APIGW", "VM Start", vmuuid, "1", "1")
|
|
|
|
|
|
resp := _response{
|
|
|
Origin: "ovirt-StopVM",
|
|
@@ -1423,8 +1461,10 @@ func createVM(invoice_uuid string, apiJson string, uuid string) {
|
|
|
}
|
|
|
func VMInitialization(relatedUuid string, apiCall string, uuid string) {
|
|
|
iaas := &ovirt{}
|
|
|
+
|
|
|
+ //fmt.Println("VM :", relatedUuid)
|
|
|
status := iaas.vmStatus(relatedUuid)
|
|
|
- //fmt.Println("VM :", relatedUuid, " is now: ", status)
|
|
|
+ fmt.Println("VM :", relatedUuid, " is now: ", status)
|
|
|
if status == "down" {
|
|
|
//fmt.Println("APICall: ", apiCall)
|
|
|
startVM := addVMTask{}
|
|
@@ -1966,7 +2006,7 @@ func (o ovirt) ovirtPayment(c echo.Context) error {
|
|
|
//return c.String(http.StatusOK, string(b))
|
|
|
return c.JSON(http.StatusOK, resp)
|
|
|
}
|
|
|
-func listNIC(VMUUID string) []struct {
|
|
|
+func listNIC(VMUUID string, BA string) []struct {
|
|
|
Mac string `json:"mac"`
|
|
|
IPv4 string `json:"IPv4"`
|
|
|
IPv6 string `json:"IPv6"`
|
|
@@ -2047,30 +2087,33 @@ func listNIC(VMUUID string) []struct {
|
|
|
req, err := http.NewRequest(method, url, payload)
|
|
|
|
|
|
if err != nil {
|
|
|
- fmt.Println(err)
|
|
|
+ //fmt.Println(err)
|
|
|
return nil
|
|
|
}
|
|
|
req.Header.Add("Version", "4")
|
|
|
req.Header.Add("Accept", "application/json")
|
|
|
- req.Header.Add("Authorization", "Basic YXJnbzhASVBBOjEyMw==")
|
|
|
+ req.Header.Add("Authorization", BA)
|
|
|
|
|
|
res, err := client.Do(req)
|
|
|
if err != nil {
|
|
|
- fmt.Println(err)
|
|
|
+ //fmt.Println(err)
|
|
|
+ ///TODO: ErrorHandling
|
|
|
return nil
|
|
|
}
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
body, err := ioutil.ReadAll(res.Body)
|
|
|
if err != nil {
|
|
|
- fmt.Println(err)
|
|
|
+ //fmt.Println(err)
|
|
|
+ ///TODO: ErrorHandling
|
|
|
return nil
|
|
|
}
|
|
|
_VMNIC := VMNIC{}
|
|
|
err = json.Unmarshal(body, &_VMNIC)
|
|
|
+ //fmt.Println("ListNIC: res", string(body))
|
|
|
if err != nil {
|
|
|
///TODO: ErrorHandling
|
|
|
- fmt.Println(err)
|
|
|
+ //fmt.Println(err)
|
|
|
//return CPUPrice, memPrice, StoragePrice, IPPrice, extraBWPrice
|
|
|
}
|
|
|
_NICList := NICList{}
|
|
@@ -2098,7 +2141,7 @@ func listNIC(VMUUID string) []struct {
|
|
|
_NICList.NIC = _Data
|
|
|
return _NICList.NIC
|
|
|
}
|
|
|
-func listDisks(VMUUID string) []struct {
|
|
|
+func listDisks(VMUUID string, ba string) []struct {
|
|
|
Name string `json:"Name"`
|
|
|
TotalSize string `json:"TotalSize"`
|
|
|
ActualSize string `json:"ActualSize"`
|
|
@@ -2132,7 +2175,7 @@ func listDisks(VMUUID string) []struct {
|
|
|
ID string `json:"id"`
|
|
|
} `json:"disk_attachment"`
|
|
|
}
|
|
|
- url := "https://ovirt-cl.zi-tel.com/ovirt-engine/api/vms/bcb15dd8-d402-4b2e-99da-94496174ea6b/diskattachments"
|
|
|
+ url := "https://ovirt-cl.zi-tel.com/ovirt-engine/api/vms/" + VMUUID + "/diskattachments"
|
|
|
method := "GET"
|
|
|
|
|
|
payload := strings.NewReader(``)
|
|
@@ -2142,42 +2185,43 @@ func listDisks(VMUUID string) []struct {
|
|
|
req, err := http.NewRequest(method, url, payload)
|
|
|
|
|
|
if err != nil {
|
|
|
- fmt.Println(err)
|
|
|
+ //fmt.Println(err)
|
|
|
return nil
|
|
|
}
|
|
|
req.Header.Add("Version", "4")
|
|
|
req.Header.Add("Accept", "application/json")
|
|
|
- req.Header.Add("Authorization", "Basic YXJnbzhASVBBOjEyMw==")
|
|
|
+ req.Header.Add("Authorization", ba)
|
|
|
|
|
|
res, err := client.Do(req)
|
|
|
if err != nil {
|
|
|
- fmt.Println(err)
|
|
|
+ //fmt.Println(err)
|
|
|
return nil
|
|
|
}
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
body, err := ioutil.ReadAll(res.Body)
|
|
|
+ //fmt.Println("ListDisk: res", string(body))
|
|
|
if err != nil {
|
|
|
- fmt.Println(err)
|
|
|
+ //fmt.Println(err)
|
|
|
return nil
|
|
|
}
|
|
|
_ListDisks := ListDisks{}
|
|
|
err = json.Unmarshal(body, &_ListDisks)
|
|
|
if err != nil {
|
|
|
- fmt.Println(err)
|
|
|
+ //fmt.Println(err)
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
_DiskList := DiskList{}
|
|
|
for _, v := range _ListDisks.DiskAttachment {
|
|
|
- _DiskList.Disk = append(_DiskList.Disk, DiskDetails(v.Disk.ID))
|
|
|
+ _DiskList.Disk = append(_DiskList.Disk, DiskDetails(v.Disk.ID, ba))
|
|
|
|
|
|
}
|
|
|
|
|
|
return _DiskList.Disk
|
|
|
}
|
|
|
|
|
|
-func DiskDetails(id string) struct {
|
|
|
+func DiskDetails(id string, ba string) struct {
|
|
|
Name string `json:"Name"`
|
|
|
TotalSize string `json:"TotalSize"`
|
|
|
ActualSize string `json:"ActualSize"`
|
|
@@ -2227,7 +2271,8 @@ func DiskDetails(id string) struct {
|
|
|
Rel string `json:"rel"`
|
|
|
} `json:"link"`
|
|
|
}
|
|
|
- url := "https://ovirt-cl.zi-tel.com/ovirt-engine/api/disks/c9485030-687f-4987-9980-72e762bca829"
|
|
|
+ //fmt.Println("List Disk: ",id)
|
|
|
+ url := "https://ovirt-cl.zi-tel.com/ovirt-engine/api/disks/" + id
|
|
|
method := "GET"
|
|
|
|
|
|
payload := strings.NewReader(``)
|
|
@@ -2237,27 +2282,27 @@ func DiskDetails(id string) struct {
|
|
|
req, err := http.NewRequest(method, url, payload)
|
|
|
|
|
|
if err != nil {
|
|
|
- fmt.Println(err)
|
|
|
+ //fmt.Println(err)
|
|
|
}
|
|
|
req.Header.Add("Version", "4")
|
|
|
req.Header.Add("Accept", "application/json")
|
|
|
- req.Header.Add("Authorization", "Basic YXJnbzhASVBBOjEyMw==")
|
|
|
+ req.Header.Add("Authorization", ba)
|
|
|
|
|
|
res, err := client.Do(req)
|
|
|
if err != nil {
|
|
|
- fmt.Println(err)
|
|
|
+ //fmt.Println(err)
|
|
|
}
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
body, err := ioutil.ReadAll(res.Body)
|
|
|
if err != nil {
|
|
|
- fmt.Println(err)
|
|
|
+ //fmt.Println(err)
|
|
|
}
|
|
|
|
|
|
_DiskDetail := DiskDetail{}
|
|
|
err = json.Unmarshal(body, &_DiskDetail)
|
|
|
if err != nil {
|
|
|
- fmt.Println(err)
|
|
|
+ //fmt.Println(err)
|
|
|
}
|
|
|
|
|
|
Disk := struct {
|
|
@@ -2272,3 +2317,364 @@ func DiskDetails(id string) struct {
|
|
|
|
|
|
return Disk
|
|
|
}
|
|
|
+func (o ovirt) listTemplate(c echo.Context) error {
|
|
|
+ type templateList struct {
|
|
|
+ Template []struct {
|
|
|
+ Status string `json:"status"`
|
|
|
+ Version struct {
|
|
|
+ VersionName string `json:"version_name"`
|
|
|
+ VersionNumber string `json:"version_number"`
|
|
|
+ BaseTemplate struct {
|
|
|
+ Href string `json:"href"`
|
|
|
+ ID string `json:"id"`
|
|
|
+ } `json:"base_template"`
|
|
|
+ } `json:"version"`
|
|
|
+ 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"`
|
|
|
+ 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"`
|
|
|
+ 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 {
|
|
|
+ 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"`
|
|
|
+ } `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"`
|
|
|
+ Cluster struct {
|
|
|
+ Href string `json:"href"`
|
|
|
+ ID string `json:"id"`
|
|
|
+ } `json:"cluster,omitempty"`
|
|
|
+ TimeZone struct {
|
|
|
+ Name string `json:"name"`
|
|
|
+ } `json:"time_zone,omitempty"`
|
|
|
+ CPUProfile struct {
|
|
|
+ Href string `json:"href"`
|
|
|
+ ID string `json:"id"`
|
|
|
+ } `json:"cpu_profile,omitempty"`
|
|
|
+ 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,omitempty"`
|
|
|
+ } `json:"template"`
|
|
|
+ }
|
|
|
+ type templateListResponse struct {
|
|
|
+ Data []struct {
|
|
|
+ Name string `json:"name"`
|
|
|
+ } `json:"Data"`
|
|
|
+ Message string `json:"message"`
|
|
|
+ Origin string `json:"origin"`
|
|
|
+ Code int `json:"code"`
|
|
|
+ }
|
|
|
+ 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/templates?filter=true&search=Cloud"
|
|
|
+ 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)
|
|
|
+ _templateList := templateList{}
|
|
|
+ err = json.Unmarshal(body, &_templateList)
|
|
|
+ //fmt.Println("resp:",string(body))
|
|
|
+ _templateListResponse := templateListResponse{
|
|
|
+ Data: nil,
|
|
|
+ Message: "Done",
|
|
|
+ Origin: "ListTemplate",
|
|
|
+ Code: 1000,
|
|
|
+ }
|
|
|
+ _Data := _templateListResponse.Data
|
|
|
+ y := struct {
|
|
|
+ Name string `json:"name"`
|
|
|
+ }{}
|
|
|
+ for _, v := range _templateList.Template {
|
|
|
+ y.Name = v.Name
|
|
|
+ _Data = append(_Data, y)
|
|
|
+ }
|
|
|
+ _templateListResponse.Data = _Data
|
|
|
+ return c.JSON(http.StatusOK, _templateListResponse)
|
|
|
+ //return c.String(http.StatusOK, string(b))
|
|
|
+}
|
|
|
+
|
|
|
+func (o ovirt) vmHistory(c echo.Context) error {
|
|
|
+
|
|
|
+ type VMStatistics struct {
|
|
|
+ Statistic []struct {
|
|
|
+ Kind string `json:"kind"`
|
|
|
+ Type string `json:"type"`
|
|
|
+ Unit string `json:"unit"`
|
|
|
+ Values struct {
|
|
|
+ Value []struct {
|
|
|
+ Datum int64 `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"`
|
|
|
+ }
|
|
|
+ type VMStatisticsResponse struct {
|
|
|
+ Data []struct {
|
|
|
+ Kind string `json:"kind"`
|
|
|
+ Type string `json:"type"`
|
|
|
+ Unit string `json:"unit"`
|
|
|
+ Values struct {
|
|
|
+ Value []struct {
|
|
|
+ Datum int64 `json:"datum"`
|
|
|
+ } `json:"value"`
|
|
|
+ } `json:"values"`
|
|
|
+ Name string `json:"name"`
|
|
|
+ Description string `json:"description"`
|
|
|
+ //Href string `json:"href,omitempty"`
|
|
|
+ //ID string `json:"id"`
|
|
|
+ } `json:"statistic"`
|
|
|
+ Message string `json:"message"`
|
|
|
+ Origin string `json:"origin"`
|
|
|
+ Code int `json:"code"`
|
|
|
+ }
|
|
|
+ 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]
|
|
|
+ vmuuid := c.FormValue("VmUUID")
|
|
|
+ url := OvirtURL + "/ovirt-engine/api/vms/" + vmuuid + "/statistics"
|
|
|
+ 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)
|
|
|
+ _VMStatistics := VMStatistics{}
|
|
|
+ err = json.Unmarshal(body, &_VMStatistics)
|
|
|
+ _VMStatisticsResponse := VMStatisticsResponse{
|
|
|
+ Data: nil,
|
|
|
+ Message: "Done",
|
|
|
+ Origin: "vmHistory",
|
|
|
+ Code: 1000,
|
|
|
+ }
|
|
|
+ _Data := _VMStatisticsResponse.Data
|
|
|
+ y := struct {
|
|
|
+ Kind string `json:"kind"`
|
|
|
+ Type string `json:"type"`
|
|
|
+ Unit string `json:"unit"`
|
|
|
+ Values struct {
|
|
|
+ Value []struct {
|
|
|
+ Datum int64 `json:"datum"`
|
|
|
+ } `json:"value"`
|
|
|
+ } `json:"values"`
|
|
|
+ Name string `json:"name"`
|
|
|
+ Description string `json:"description"`
|
|
|
+ //Href string `json:"href,omitempty"`
|
|
|
+ //ID string `json:"id"`
|
|
|
+ }{}
|
|
|
+ for _, v := range _VMStatistics.Statistic {
|
|
|
+ y.Kind = v.Kind
|
|
|
+ y.Unit = v.Unit
|
|
|
+ y.Type = v.Type
|
|
|
+ y.Values = v.Values
|
|
|
+ y.Name = v.Name
|
|
|
+ y.Description = v.Description
|
|
|
+ _Data = append(_Data, y)
|
|
|
+ }
|
|
|
+ _VMStatisticsResponse.Data = _Data
|
|
|
+ return c.JSON(http.StatusOK, _VMStatisticsResponse)
|
|
|
+}
|
|
|
+
|
|
|
+func (o ovirt) SSHKeyGen(c echo.Context) error {
|
|
|
+ type SSHKeyGenResponse struct {
|
|
|
+ Message string `json:"message"`
|
|
|
+ Origin string `json:"origin"`
|
|
|
+ Code int `json:"code"`
|
|
|
+ }
|
|
|
+ user := c.Get("user").(*jwt.Token)
|
|
|
+ claims := user.Claims.(jwt.MapClaims)
|
|
|
+ email:=fmt.Sprintf("%s",claims["mail"])
|
|
|
+ fmt.Println(email)
|
|
|
+
|
|
|
+ mrand.Seed(time.Now().Unix())
|
|
|
+ var output strings.Builder
|
|
|
+ //Only lowercase
|
|
|
+ charSet := "abcdedfghijklmnopqrstABCDEFGHIJKLMNOP0123456789"
|
|
|
+ length := 20
|
|
|
+ for i := 0; i < length; i++ {
|
|
|
+ random := mrand.Intn(len(charSet))
|
|
|
+ randomChar := charSet[random]
|
|
|
+ output.WriteString(string(randomChar))
|
|
|
+ }
|
|
|
+
|
|
|
+ publicKeyBlock := &pem.Block{}
|
|
|
+ {
|
|
|
+ // generate key
|
|
|
+ privatekey, err := rsa.GenerateKey(rand.Reader, 1024)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Printf("Cannot generate RSA key\n")
|
|
|
+ //os.Exit(1)
|
|
|
+ }
|
|
|
+ publickey := &privatekey.PublicKey
|
|
|
+
|
|
|
+ // dump private key to file
|
|
|
+ var privateKeyBytes []byte = x509.MarshalPKCS1PrivateKey(privatekey)
|
|
|
+ privateKeyBlock := &pem.Block{
|
|
|
+ Type: "RSA PRIVATE KEY",
|
|
|
+ Bytes: privateKeyBytes,
|
|
|
+ }
|
|
|
+ privatePem, err := os.Create("/tmp/" + output.String() + "-private.pem")
|
|
|
+ if err != nil {
|
|
|
+ fmt.Printf("error when create private.pem: %s \n", err)
|
|
|
+ //os.Exit(1)
|
|
|
+ }
|
|
|
+ err = pem.Encode(privatePem, privateKeyBlock)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Printf("error when encode private pem: %s \n", err)
|
|
|
+ //os.Exit(1)
|
|
|
+ }
|
|
|
+
|
|
|
+ // dump public key to file
|
|
|
+ publicKeyBytes, err := x509.MarshalPKIXPublicKey(publickey)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Printf("error when dumping publickey: %s \n", err)
|
|
|
+ //os.Exit(1)
|
|
|
+ }
|
|
|
+ publicKeyBlock = &pem.Block{
|
|
|
+ Type: "PUBLIC KEY",
|
|
|
+ Bytes: publicKeyBytes,
|
|
|
+ }
|
|
|
+ publicPem, err := os.Create("/tmp/" + output.String() + "-public.pem")
|
|
|
+ if err != nil {
|
|
|
+ fmt.Printf("error when create public.pem: %s \n", err)
|
|
|
+ //os.Exit(1)
|
|
|
+ }
|
|
|
+
|
|
|
+ err = pem.Encode(publicPem, publicKeyBlock)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Printf("error when encode public pem: %s \n", err)
|
|
|
+ //os.Exit(1)
|
|
|
+ }
|
|
|
+ sendMail("Generated SSH Key pairs is attached to this email", "ZiCloud KeyPair", "inbox@sassan.co", []string{"/tmp/" + output.String() + "-public.pem", "/tmp/" + output.String() + "-private.pem"})
|
|
|
+ }
|
|
|
+ _SSHKeyGenResponse := SSHKeyGenResponse{
|
|
|
+ Message: string(pem.EncodeToMemory(publicKeyBlock)),
|
|
|
+ Origin: "SSHKeyGen",
|
|
|
+ Code: 1000,
|
|
|
+ }
|
|
|
+ return c.JSON(http.StatusOK, _SSHKeyGenResponse)
|
|
|
+}
|