123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274 |
- package main
- import (
- "crypto/sha256"
- "database/sql"
- "encoding/json"
- "errors"
- "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 AddVMRequest struct {
- URL string `json:"url"`
- JSON struct {
- Name string `json:"name"`
- Description string `json:"description"`
- Comment string `json:"comment"`
- Cluster struct {
- Name string `json:"name"`
- } `json:"cluster"`
- Template struct {
- Name string `json:"name"`
- } `json:"template"`
- CPU struct {
- Topology struct {
- Sockets string `json:"sockets"`
- Cores string `json:"cores"`
- Threads string `json:"threads"`
- } `json:"topology"`
- } `json:"cpu"`
- Memory string `json:"memory"`
- MemoryPolicy struct {
- Ballooning string `json:"ballooning"`
- Guaranteed string `json:"guaranteed"`
- OverCommit struct {
- Percent string `json:"percent"`
- } `json:"over_commit"`
- } `json:"memory_policy"`
- } `json:"JSON"`
- Method string `json:"method"`
- Headers []struct {
- Name string `json:"Name"`
- Value string `json:"Value"`
- } `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("VMSTATUS UUID:" ,uuid)
- 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")
- //TODO: replace BA
- 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 in vmStatus ", _err)
- }
- return _vmstatus.Status
- }
- func (o ovirt) addvm(c echo.Context) error {
- type AddVMPayment struct {
- Data struct {
- Price struct {
- CPU float64 `json:"cpu"`
- Mem float64 `json:"mem"`
- Disk float64 `json:"disk"`
- Nic float64 `json:"nic"`
- BW float64 `json:"bw"`
- SUM float64 `json:"sum"`
- Period string `json:"period"`
- } `json:"price"`
- InvoiceUUID string `json:"invoiceUUID"`
- } `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]
- var vmname, vmdescr, vmcomment, templatename, cpuSock, cpuCore, cpuThread, mem, Disk, nic, period, extraBW string
- vmname = login(BA).AuthenticatedUser.ID + "_" + c.FormValue("VmName")
- vmdescr = c.FormValue("VmDescr")
- vmcomment = c.FormValue("VmComment")
- templatename = c.FormValue("VmTempl")
- cpuThread = c.FormValue("VmCPU")
- Disk = c.FormValue("VmDisk")
- period = c.FormValue("VmPeriod")
- nic = c.FormValue("VmNIC")
- extraBW = c.FormValue("extraBW")
- sshKey := c.FormValue("sshKey")
- rootpass := c.FormValue("rootpass")
- cpuCore = "1"
- cpuSock = "1"
- mem = c.FormValue("VmMem")
- url := OvirtURL + "/ovirt-engine/api/vms"
- method := "POST"
- //TODO: Encrypt this Json
- // _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)
- // Change as task
- //change to task// payload := strings.NewReader(_json)
- //change to task// //tr := &http.Transport{
- //change to task// // TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
- //change to task// //}
- //change to task// client := &http.Client{}
- //change to task// req, err := http.NewRequest(method, url, payload)
- //change to task// if err != nil {
- //change to task// fmt.Println(err)
- //change to task// }
- //change to task// req.Header.Add("Version", "4")
- //change to task// req.Header.Add("Accept", "application/json")
- //change to task// req.Header.Add("Authorization", BA)
- //change to task// req.Header.Add("Content-Type", "application/json")
- //change to task// res, err := client.Do(req)
- //change to task// body, err := ioutil.ReadAll(res.Body)
- //change to task// //fmt.Println("len: ", len(_json))
- //change to task// //fmt.Println("res:", res)
- //change to task// if err != nil || res.StatusCode != 202 {
- //change to task// resp := _response{
- //change to task// Origin: "ovirt-addvm",
- //change to task// Message: res.Status,
- //change to task// Code: 1001,
- //change to task// }
- //change to task// b, _ := json.MarshalIndent(resp, "", " ")
- //change to task// return c.String(http.StatusBadRequest, string(b))
- //change to task// }
- //change to task// defer res.Body.Close()
- //addvmresponse := AddVMResponse{}O
- newvm := AddVMRequest{
- URL: url,
- JSON: struct {
- Name string `json:"name"`
- Description string `json:"description"`
- Comment string `json:"comment"`
- Cluster struct {
- Name string `json:"name"`
- } `json:"cluster"`
- Template struct {
- Name string `json:"name"`
- } `json:"template"`
- CPU struct {
- Topology struct {
- Sockets string `json:"sockets"`
- Cores string `json:"cores"`
- Threads string `json:"threads"`
- } `json:"topology"`
- } `json:"cpu"`
- Memory string `json:"memory"`
- MemoryPolicy struct {
- Ballooning string `json:"ballooning"`
- Guaranteed string `json:"guaranteed"`
- OverCommit struct {
- Percent string `json:"percent"`
- } `json:"over_commit"`
- } `json:"memory_policy"`
- }{
- Name: vmname,
- Description: vmdescr,
- Comment: vmcomment,
- Cluster: struct {
- Name string `json:"name"`
- }{Name: "Default"},
- Template: struct {
- Name string `json:"name"`
- }{Name: templatename},
- CPU: struct {
- Topology struct {
- Sockets string `json:"sockets"`
- Cores string `json:"cores"`
- Threads string `json:"threads"`
- } `json:"topology"`
- }{
- struct {
- Sockets string `json:"sockets"`
- Cores string `json:"cores"`
- Threads string `json:"threads"`
- }{
- Sockets: cpuSock,
- Cores: cpuCore,
- Threads: cpuThread,
- },
- },
- Memory: mem,
- MemoryPolicy: struct {
- Ballooning string `json:"ballooning"`
- Guaranteed string `json:"guaranteed"`
- OverCommit struct {
- Percent string `json:"percent"`
- } `json:"over_commit"`
- }{
- Ballooning: "true",
- Guaranteed: mem,
- OverCommit: struct {
- Percent string `json:"percent"`
- }{Percent: "10"},
- },
- },
- Method: method,
- Headers: nil,
- }
- newvm.Headers = append(newvm.Headers, struct {
- Name string `json:"Name"`
- Value string `json:"Value"`
- }{
- Name: "Accept",
- Value: "application/json",
- })
- newvm.Headers = append(newvm.Headers, struct {
- Name string `json:"Name"`
- Value string `json:"Value"`
- }{
- Name: "Authorization",
- Value: BA,
- })
- newvm.Headers = append(newvm.Headers, struct {
- Name string `json:"Name"`
- Value string `json:"Value"`
- }{
- Name: "Content-Type",
- Value: "application/json",
- })
- uuid, _ := uuidgen("APIGW-Ovirt-addVMTask")
- //invoiceUUID, _ := uuidgen("APIGW-FakeInvoice")
- CPUPrice, memPrice, StoragePrice, IPPrice, extraBWPrice, SUM, invoiceUUID := IaaSCreate(login(BA).AuthenticatedUser.ID, period, cpuThread, mem, Disk, nic, extraBW)
- responseInvoiece := AddVMPayment{
- Data: struct {
- Price struct {
- CPU float64 `json:"cpu"`
- Mem float64 `json:"mem"`
- Disk float64 `json:"disk"`
- Nic float64 `json:"nic"`
- BW float64 `json:"bw"`
- SUM float64 `json:"sum"`
- Period string `json:"period"`
- } `json:"price"`
- InvoiceUUID string `json:"invoiceUUID"`
- }{
- Price: struct {
- CPU float64 `json:"cpu"`
- Mem float64 `json:"mem"`
- Disk float64 `json:"disk"`
- Nic float64 `json:"nic"`
- BW float64 `json:"bw"`
- SUM float64 `json:"sum"`
- Period string `json:"period"`
- }{
- CPU: CPUPrice,
- Mem: memPrice,
- Disk: StoragePrice,
- Nic: IPPrice,
- BW: extraBWPrice,
- SUM: SUM,
- Period: period,
- },
- InvoiceUUID: invoiceUUID,
- },
- Message: "Done",
- Origin: "AddVM",
- Code: 1000,
- }
- if len(invoiceUUID) < 5 {
- resp := _response{
- Origin: "ovirt-addvm",
- Message: "Error on Invoice Calculation",
- Code: 1001,
- }
- b, _ := json.MarshalIndent(resp, "", " ")
- return c.String(http.StatusBadRequest, string(b))
- }
- //fmt.Println("Invoice: ",invoiceUUID)
- __createVM, _ := json.MarshalIndent(newvm, "", " ")
- // Add VM Task
- addTask(uuid, string(__createVM), "", "APIGW", "VM Creation for "+claims["name"].(string), invoiceUUID, "0", "0")
- {
- 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 ( '" + invoiceUUID + "'," +
- "'" + claims["IPAUid"].(string) + "'," +
- "'" + "VM" + "'," +
- "'" + "" + "'," +
- "'" + login(BA).AuthenticatedUser.ID + "'," +
- "NOW()" + "," +
- "NOW() ," +
- "-1" + "," +
- "'" + vmname + "'," +
- "'" + invoiceUUID + "' )")
- defer insert.Close()
- if err != nil {
- fmt.Println("Error in addVM: ", err.Error())
- }
- }
- // Start Task
- {
- url := OvirtURL + "/ovirt-engine/api/vms/" + invoiceUUID + "/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)
- sha256 := sha256.Sum256([]byte(invoiceUUID))
- 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})
- uuid, _ := uuidgen("APIGW-Ovirt-CloudInit")
- __startVM, _ := json.MarshalIndent(startVM, "", " ")
- //fmt.Println("StartVMHeaders: ", string(__startVMHeaders))
- addTask(uuid, string(__startVM), "", "APIGW", "VM Initialization", invoiceUUID, "1", "0")
- }
- //resp := _response{
- // Origin: "ovirt-addvm",
- // Message: resposeInvoice
- // Code: 1000,
- //}
- return c.JSON(http.StatusOK, responseInvoiece)
- }
- func (o ovirt) listVM(c echo.Context) error {
- type listVMs struct {
- VM []struct {
- Fqdn string `json:"fqdn,omitempty"`
- GuestOperatingSystem struct {
- Architecture string `json:"architecture"`
- Codename string `json:"codename"`
- Distribution string `json:"distribution"`
- Family string `json:"family"`
- Kernel struct {
- 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:"kernel"`
- Version struct {
- FullVersion string `json:"full_version"`
- Major string `json:"major"`
- } `json:"version"`
- } `json:"guest_operating_system,omitempty"`
- GuestTimeZone struct {
- Name string `json:"name"`
- UtcOffset string `json:"utc_offset"`
- } `json:"guest_time_zone,omitempty"`
- NextRunConfigurationExists string `json:"next_run_configuration_exists"`
- NumaTuneMode string `json:"numa_tune_mode"`
- RunOnce string `json:"run_once,omitempty"`
- StartTime int64 `json:"start_time,omitempty"`
- 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 {
- Address string `json:"address"`
- AllowOverride string `json:"allow_override"`
- Certificate struct {
- Content string `json:"content"`
- Organization string `json:"organization"`
- Subject string `json:"subject"`
- } `json:"certificate"`
- CopyPasteEnabled string `json:"copy_paste_enabled"`
- DisconnectAction string `json:"disconnect_action"`
- FileTransferEnabled string `json:"file_transfer_enabled"`
- Monitors string `json:"monitors"`
- Port string `json:"port"`
- SecurePort string `json:"secure_port"`
- 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 {
- Guaranteed string `json:"guaranteed"`
- Max string `json:"max"`
- } `json:"memory_policy"`
- MigrationDowntime string `json:"migration_downtime"`
- MultiQueuesEnabled string `json:"multi_queues_enabled"`
- 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"`
- StopReason string `json:"stop_reason,omitempty"`
- } `json:"vm"`
- }
- type listVMResponse struct {
- Data []struct {
- Fqdn string `json:"fqdn"`
- StartTime int64 `json:"start_time"`
- Status string `json:"status"`
- Name string `json:"name"`
- Description string `json:"description"`
- Comment string `json:"comment"`
- ID string `json:"id"`
- CPU struct {
- Topology struct {
- Cores string `json:"cores"`
- Sockets string `json:"sockets"`
- Threads string `json:"threads"`
- } `json:"topology"`
- } `json:"cpu"`
- Memory string `json:"memory"`
- CreationTime int64 `json:"creation_time"`
- Disk []struct {
- Name string `json:"Name"`
- TotalSize string `json:"TotalSize"`
- ActualSize string `json:"ActualSize"`
- } `json:"Disk"`
- NIC []struct {
- Mac string `json:"mac"`
- IPv4 string `json:"IPv4"`
- IPv6 string `json:"IPv6"`
- ReportedDevices struct {
- Mac string `json:"mac"`
- IPv4 string `json:"IPv4"`
- IPv6 string `json:"IPv6"`
- } `json:"reported_devices"`
- } `json:"NIC"`
- } `json:"vm"`
- 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/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, "", " ")
- _VMLists := listVMs{}
- err = json.Unmarshal(body, &_VMLists)
- _listVMResponse := listVMResponse{
- Data: nil,
- Message: "Done",
- Origin: "ListVMs",
- Code: 1000,
- }
- _Data := _listVMResponse.Data
- y := struct {
- Fqdn string `json:"fqdn"`
- StartTime int64 `json:"start_time"`
- Status string `json:"status"`
- Name string `json:"name"`
- Description string `json:"description"`
- Comment string `json:"comment"`
- ID string `json:"id"`
- CPU struct {
- Topology struct {
- Cores string `json:"cores"`
- Sockets string `json:"sockets"`
- Threads string `json:"threads"`
- } `json:"topology"`
- } `json:"cpu"`
- Memory string `json:"memory"`
- CreationTime int64 `json:"creation_time"`
- Disk []struct {
- Name string `json:"Name"`
- TotalSize string `json:"TotalSize"`
- ActualSize string `json:"ActualSize"`
- } `json:"Disk"`
- NIC []struct {
- Mac string `json:"mac"`
- IPv4 string `json:"IPv4"`
- IPv6 string `json:"IPv6"`
- ReportedDevices struct {
- Mac string `json:"mac"`
- IPv4 string `json:"IPv4"`
- IPv6 string `json:"IPv6"`
- } `json:"reported_devices"`
- } `json:"NIC"`
- }{}
- 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
- y.Description = v.Description
- y.Comment = v.Comment
- y.CPU.Topology.Cores = v.CPU.Topology.Cores
- y.CPU.Topology.Sockets = v.CPU.Topology.Sockets
- 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)
- _Data = append(_Data, y)
- }
- _listVMResponse.Data = _Data
- if err != nil {
- fmt.Println(err)
- //return CPUPrice, memPrice, StoragePrice, IPPrice, extraBWPrice
- resp := _response{
- Origin: "ovirt-listvms",
- Message: "Error in Parsing listVMs",
- Code: 1001,
- }
- return c.JSON(http.StatusInternalServerError, resp)
- }
- //fmt.Printf("%s", body)
- //b, _ := json.MarshalIndent(resp, "", " ")
- return c.JSON(http.StatusOK, _listVMResponse)
- //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-StopVM",
- 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-RebootVM",
- 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-PowerOffVM",
- 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 createVM(invoice_uuid string, apiJson string, uuid string) {
- 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"`
- }
- _createVM := AddVMRequest{}
- //fmt.Println(apiJson)
- _err := json.Unmarshal([]byte(apiJson), &_createVM)
- if _err != nil {
- fmt.Println("Error in VMCreation step 1: ", _err.Error())
- }
- addvmresponse := AddVMResponse{}
- var body []byte
- {
- url := _createVM.URL
- method := _createVM.Method
- __json := _createVM.JSON
- client := &http.Client{}
- _json, _ := json.Marshal(__json)
- payload := strings.NewReader(string(_json))
- //payload := __json
- req, err := http.NewRequest(method, url, payload)
- if err != nil {
- fmt.Println("Error in VMCreation step 2: ", err.Error())
- }
- _headers := _createVM.Headers
- for _, v := range _headers {
- req.Header.Add(v.Name, v.Value)
- }
- if err != nil {
- fmt.Println("Error in VMCreation step 3: ", err.Error())
- }
- res, err := client.Do(req)
- defer res.Body.Close()
- body, _ = ioutil.ReadAll(res.Body)
- }
- //body := runAPICall(_createVM)
- err := json.Unmarshal(body, &addvmresponse)
- if err != nil {
- fmt.Println("Error in VMCreation step 4: ", err.Error())
- }
- toggleTask(uuid, 0)
- // find related VMInitialization Task
- //ruuid := ""
- {
- db, err := sql.Open("mysql", MySQLUSER+":"+MySQLPASS+"@tcp(127.0.0.1:3306)/zicloud")
- if err != nil {
- fmt.Println("Error in VMCreation step 3: ", _err.Error())
- }
- //defer db.Close()
- //results, err := db.Query("SELECT uuid as UUID ,task_apiCall as TaskAPICall , cron_expression as CronExpression , related_uuid as Ruuid, type FROM scheduler where related_invoice=" + invoice_uuid + " and type=1")
- //type Task struct {
- // UUID string `json:"uuid"`
- // TaskAPICall string `json:"taskapicall"`
- // Ruuid string `json:"ruuid"`
- // CronExpression string `json:"cronexpression"`
- // Type string `json:"type"`
- //}
- //for results.Next() {
- // var res Task
- // err = results.Scan(&res.UUID, &res.TaskAPICall, &res.CronExpression, &res.Ruuid, &res.Type)
- // ruuid = res.UUID
- // break
- //}
- //update UUID and status
- update2, err := db.Query("update service_profile set uuid='" + addvmresponse.ID + "' , active=1 where related_invoice='" + invoice_uuid + "'")
- update, err := db.Query("update scheduler set related_uuid='" + addvmresponse.ID + "' , active=1 where related_uuid='" + invoice_uuid + "' and type=1")
- defer update.Close()
- defer update2.Close()
- //toggleTask(ruuid, 1)
- }
- }
- func VMInitialization(relatedUuid string, apiCall string, uuid string) {
- iaas := &ovirt{}
- status := iaas.vmStatus(relatedUuid)
- //fmt.Println("VM :", relatedUuid, " is now: ", status)
- if status == "down" {
- //fmt.Println("APICall: ", apiCall)
- startVM := addVMTask{}
- //b, _ := json.Marshal(apiCall)
- _err := json.Unmarshal([]byte(apiCall), &startVM)
- if _err != nil {
- fmt.Println("Error in VMInitialization: ", _err.Error())
- }
- //fmt.Println("calling initialization for VM :", relatedUuid)
- words := strings.Split(startVM.URL, "/")
- //fmt.Println("Invoice : ", words[6])
- _sha256 := sha256.Sum256([]byte(words[6]))
- var hashChannel_ = make(chan []byte, 1)
- hashChannel_ <- _sha256[:]
- //fmt.Println(" startVM.JSON: ", startVM.JSON)
- startVM.JSON = decrypt(<-hashChannel_, startVM.JSON)
- //fmt.Println("decrypted json: ",startVM.JSON)
- startVM.URL = OvirtURL + "/ovirt-engine/api/vms/" + relatedUuid + "/start"
- //fmt.Println("Start URL: ", startVM.URL)
- //fmt.Println("Initialize JSON: ", decrypt(<-hashChannel_, startVM.JSON))
- runAPICall(startVM)
- toggleTask(uuid, 0)
- }
- }
- func vmStatistics(BA string, VMUUID string) (string, float64, float64, error) {
- //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, errors.New("unexpected Error")
- }
- 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 in vmStatistics 1: ", _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 in vmStatistics 2 : ", _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, errors.New("unexpected Error")
- }
- 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("Code: ", res.StatusCode)
- if err != nil || res.StatusCode == 404 {
- return "", -1, -1, errors.New("unexpected Error")
- }
- //fmt.Println("full response: ", string(body))
- _vmstatistics := VMStatistics{}
- _err := json.Unmarshal(body, &_vmstatistics)
- if _err != nil {
- fmt.Println("Error in vmStatistics 3: ", _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, errors.New("unexpected Error")
- }
- 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 in vmStatistics 4 ", _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, errors.New("unexpected Error")
- }
- 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 in vmStatistics 5: ", _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, errors.New("unexpected Error")
- }
- 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 in vmStatistics 6: ", _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, nil
- }
- 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: "vmDetails",
- 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 {
- resp := _response{
- Origin: "vmDetails",
- Message: "Not Found",
- Code: 1001,
- }
- return c.JSON(http.StatusNotFound, resp)
- }
- disk, cpu, ram, err := vmStatistics(BA, uuid)
- if err != nil {
- fmt.Println("Error in vmDetails 2 : ", err)
- resp := _response{
- Origin: "vmDetails",
- Message: "Unexpected Error",
- Code: 1001,
- }
- return c.JSON(http.StatusInternalServerError, resp)
- }
- 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
- }
- func (o ovirt) ovirtPayment(c echo.Context) error {
- invoiceUuid := c.FormValue("invoiceUuid")
- db, err := sql.Open("mysql", MySQLUSER+":"+MySQLPASS+"@tcp(127.0.0.1:3306)/zicloud")
- //results, err := db.Query("SELECT uuid as UUID ,task_apiCall as TaskAPICall , cron_expression as CronExpression , related_uuid as Ruuid, type FROM scheduler where active=1")
- ///TODO: check duplicate payment confirmation by Invoice table
- if err != nil {
- }
- update, err := db.Query("update scheduler set active='1' where related_uuid='" + invoiceUuid + "' and type=0")
- defer db.Close()
- if err != nil {
- }
- defer update.Close()
- resp := _response{
- Origin: "ovirt-ovirtPayment",
- Message: "Done",
- Code: 1000,
- }
- //b, _ := json.MarshalIndent(resp, "", " ")
- //return c.String(http.StatusOK, string(b))
- return c.JSON(http.StatusOK, resp)
- }
- func listNIC(VMUUID string) []struct {
- Mac string `json:"mac"`
- IPv4 string `json:"IPv4"`
- IPv6 string `json:"IPv6"`
- ReportedDevices struct {
- Mac string `json:"mac"`
- IPv4 string `json:"IPv4"`
- IPv6 string `json:"IPv6"`
- } `json:"reported_devices"`
- } {
- type NICList struct {
- NIC []struct {
- Mac string `json:"mac"`
- IPv4 string `json:"IPv4"`
- IPv6 string `json:"IPv6"`
- ReportedDevices struct {
- Mac string `json:"mac"`
- IPv4 string `json:"IPv4"`
- IPv6 string `json:"IPv6"`
- } `json:"reported_devices"`
- } `json:"NIC"`
- }
- type VMNIC struct {
- Nic []struct {
- Interface string `json:"interface"`
- Linked string `json:"linked"`
- Mac struct {
- Address string `json:"address"`
- } `json:"mac"`
- Plugged string `json:"plugged"`
- ReportedDevices struct {
- ReportedDevice []struct {
- Ips struct {
- IP []struct {
- Address string `json:"address"`
- Version string `json:"version"`
- } `json:"ip"`
- } `json:"ips"`
- Mac struct {
- Address string `json:"address"`
- } `json:"mac"`
- Type string `json:"type"`
- Name string `json:"name"`
- Description string `json:"description"`
- Href string `json:"href"`
- ID string `json:"id"`
- } `json:"reported_device"`
- } `json:"reported_devices"`
- VnicProfile struct {
- Href string `json:"href"`
- ID string `json:"id"`
- } `json:"vnic_profile"`
- Actions struct {
- Link []struct {
- Href string `json:"href"`
- Rel string `json:"rel"`
- } `json:"link"`
- } `json:"actions"`
- Name string `json:"name"`
- Href string `json:"href"`
- ID string `json:"id"`
- VM struct {
- Href string `json:"href"`
- ID string `json:"id"`
- } `json:"vm"`
- Link []struct {
- Href string `json:"href"`
- Rel string `json:"rel"`
- } `json:"link"`
- } `json:"nic"`
- }
- url := "https://ovirt-cl.zi-tel.com/ovirt-engine/api/vms/" + VMUUID + "/nics"
- method := "GET"
- payload := strings.NewReader(``)
- client := &http.Client{
- }
- req, err := http.NewRequest(method, url, payload)
- if err != nil {
- fmt.Println(err)
- return nil
- }
- req.Header.Add("Version", "4")
- req.Header.Add("Accept", "application/json")
- req.Header.Add("Authorization", "Basic YXJnbzhASVBBOjEyMw==")
- res, err := client.Do(req)
- if err != nil {
- fmt.Println(err)
- return nil
- }
- defer res.Body.Close()
- body, err := ioutil.ReadAll(res.Body)
- if err != nil {
- fmt.Println(err)
- return nil
- }
- _VMNIC := VMNIC{}
- err = json.Unmarshal(body, &_VMNIC)
- if err != nil {
- ///TODO: ErrorHandling
- fmt.Println(err)
- //return CPUPrice, memPrice, StoragePrice, IPPrice, extraBWPrice
- }
- _NICList := NICList{}
- _Data := _NICList.NIC
- x := struct {
- Mac string `json:"mac"`
- IPv4 string `json:"IPv4"`
- IPv6 string `json:"IPv6"`
- ReportedDevices struct {
- Mac string `json:"mac"`
- IPv4 string `json:"IPv4"`
- IPv6 string `json:"IPv6"`
- } `json:"reported_devices"`
- }{}
- for _, i := range _VMNIC.Nic {
- x.IPv4 = "1.1.1.1"
- ///TODO:GetAssignedIP
- x.IPv6 = "Null"
- x.Mac = i.Mac.Address
- x.ReportedDevices.Mac = i.ReportedDevices.ReportedDevice[0].Mac.Address
- x.ReportedDevices.IPv4 = i.ReportedDevices.ReportedDevice[0].Ips.IP[0].Address
- x.ReportedDevices.IPv6 = i.ReportedDevices.ReportedDevice[0].Ips.IP[1].Address
- _Data = append(_Data, x)
- }
- _NICList.NIC = _Data
- return _NICList.NIC
- }
- func listDisks(VMUUID string) []struct {
- Name string `json:"Name"`
- TotalSize string `json:"TotalSize"`
- ActualSize string `json:"ActualSize"`
- } {
- type DiskList struct {
- Disk []struct {
- Name string `json:"Name"`
- TotalSize string `json:"TotalSize"`
- ActualSize string `json:"ActualSize"`
- } `json:"Disk"`
- }
- type ListDisks 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 := "https://ovirt-cl.zi-tel.com/ovirt-engine/api/vms/bcb15dd8-d402-4b2e-99da-94496174ea6b/diskattachments"
- method := "GET"
- payload := strings.NewReader(``)
- client := &http.Client{
- }
- req, err := http.NewRequest(method, url, payload)
- if err != nil {
- fmt.Println(err)
- return nil
- }
- req.Header.Add("Version", "4")
- req.Header.Add("Accept", "application/json")
- req.Header.Add("Authorization", "Basic YXJnbzhASVBBOjEyMw==")
- res, err := client.Do(req)
- if err != nil {
- fmt.Println(err)
- return nil
- }
- defer res.Body.Close()
- body, err := ioutil.ReadAll(res.Body)
- if err != nil {
- fmt.Println(err)
- return nil
- }
- _ListDisks := ListDisks{}
- err = json.Unmarshal(body, &_ListDisks)
- if err != nil {
- fmt.Println(err)
- return nil
- }
- _DiskList := DiskList{}
- for _, v := range _ListDisks.DiskAttachment {
- _DiskList.Disk = append(_DiskList.Disk, DiskDetails(v.Disk.ID))
- }
- return _DiskList.Disk
- }
- func DiskDetails(id string) struct {
- Name string `json:"Name"`
- TotalSize string `json:"TotalSize"`
- ActualSize string `json:"ActualSize"`
- } {
- type DiskDetail 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"`
- }
- url := "https://ovirt-cl.zi-tel.com/ovirt-engine/api/disks/c9485030-687f-4987-9980-72e762bca829"
- method := "GET"
- payload := strings.NewReader(``)
- 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", "Basic YXJnbzhASVBBOjEyMw==")
- res, err := client.Do(req)
- if err != nil {
- fmt.Println(err)
- }
- defer res.Body.Close()
- body, err := ioutil.ReadAll(res.Body)
- if err != nil {
- fmt.Println(err)
- }
- _DiskDetail := DiskDetail{}
- err = json.Unmarshal(body, &_DiskDetail)
- if err != nil {
- fmt.Println(err)
- }
- Disk := struct {
- Name string `json:"Name"`
- TotalSize string `json:"TotalSize"`
- ActualSize string `json:"ActualSize"`
- }{
- Name: _DiskDetail.Alias,
- TotalSize: _DiskDetail.TotalSize,
- ActualSize: _DiskDetail.ActualSize,
- }
- return Disk
- }
|