Skip to content

Commit

Permalink
Merge pull request #1 from monobilisim/rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
xeome authored Jul 18, 2023
2 parents 64e45be + 3a5de4e commit b727c4c
Show file tree
Hide file tree
Showing 22 changed files with 474 additions and 472 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.idea/
conf/config.yml
config.yml

*.exe
*.exe
1 change: 0 additions & 1 deletion conf/config.yml

This file was deleted.

1 change: 1 addition & 0 deletions config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
port: 8080
46 changes: 32 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,30 +1,48 @@
module wmi-rest

go 1.18
go 1.20

require (
github.com/StackExchange/wmi v1.2.1
github.com/gorilla/mux v1.8.0
github.com/bhendo/go-powershell v0.0.0-20190719160123-219e7fb4e41e
github.com/gin-gonic/gin v1.9.1
github.com/kardianos/service v1.2.2
github.com/spf13/viper v1.14.0
github.com/spf13/viper v1.16.0
)

require (
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/juju/errors v1.0.0 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/spf13/afero v1.9.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
golang.org/x/sys v0.0.0-20220908164124-27713097b956 // indirect
golang.org/x/text v0.4.0 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
123 changes: 90 additions & 33 deletions go.sum

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions hyperv/memory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package hyperv

import (
"net/http"
"wmi-rest/utilities"

"github.com/gin-gonic/gin"
)

func Memory(c *gin.Context) {
input := c.Param("machid")

if input == "" {
c.Data(returnResponse("No VM ID specified", http.StatusBadRequest, "failure", "error"))
return
}

output, err := utilities.CommandLine(`Get-VM -Id ` + input + ` | Get-VMMemory | ConvertTo-Json`)
if err != nil {
c.Data(returnResponse(err.Error(), http.StatusInternalServerError, "failure", "error"))
return
}
c.Data(returnResponse(output, http.StatusOK, "success", "Memory info is displayed in data field"))
}
24 changes: 24 additions & 0 deletions hyperv/network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package hyperv

import (
"net/http"
"wmi-rest/utilities"

"github.com/gin-gonic/gin"
)

func Network(c *gin.Context) {
input := c.Param("machid")

if input == "" {
c.Data(returnResponse("No VM ID specified", http.StatusBadRequest, "failure", "error"))
return
}

output, err := utilities.CommandLine(`Get-VM -Id ` + input + ` | Get-VMNetworkAdapter | ConvertTo-Json`)
if err != nil {
c.Data(returnResponse(err.Error(), http.StatusInternalServerError, "failure", "error"))
return
}
c.Data(returnResponse(output, http.StatusOK, "success", "Network info is displayed in data field"))
}
25 changes: 25 additions & 0 deletions hyperv/processor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package hyperv

import (
"net/http"
"wmi-rest/utilities"

"github.com/gin-gonic/gin"
)

func Processor(c *gin.Context) {
input := c.Param("machid")

if input == "" {
c.Data(returnResponse("No VM ID specified", http.StatusBadRequest, "failure", "error"))
return
}

output, err := utilities.CommandLine(`Get-VM -Id ` + input + ` | Get-VMProcessor | ConvertTo-Json`)
if err != nil {
c.Data(returnResponse(err.Error(), http.StatusInternalServerError, "failure", "error"))
return
}

c.Data(returnResponse(output, http.StatusOK, "success", "Processor info is displayed in data field."))
}
22 changes: 22 additions & 0 deletions hyperv/resp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package hyperv

import "encoding/json"

type response struct {
Result string `json:"result"`
Message string `json:"message"`
Data interface{} `json:"data"`
}

func returnResponse(respData interface{}, status int, result, message string) (int, string, []byte) {
if value, ok := respData.(string); ok {
respData, _ = json.Marshal(value)
}
resp := response{
Result: result,
Message: message,
Data: json.RawMessage(respData.([]byte)),
}
jsonResp, _ := json.MarshalIndent(resp, "", " ")
return status, "application/json", jsonResp
}
30 changes: 30 additions & 0 deletions hyperv/vhd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package hyperv

import (
"net/http"
"wmi-rest/utilities"

"github.com/gin-gonic/gin"
)

func VHD(c *gin.Context) {
input := c.Param("machid")

if input == "" {
c.Data(returnResponse("No VM ID specified", http.StatusBadRequest, "failure", "error"))
return
}

output, err := utilities.CommandLine(`Get-VHD -Id ` + input + ` | ConvertTo-Json`)
if err != nil {
c.Data(returnResponse(err.Error(), http.StatusInternalServerError, "failure", "error"))
return
}

if len(output) < 1 {
c.Data(returnResponse("No Disk found.", http.StatusOK, "failure", "error"))
return
}

c.Data(returnResponse(output, http.StatusOK, "success", "VHD info is displayed in data field"))
}
25 changes: 25 additions & 0 deletions hyperv/vm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package hyperv

import (
"net/http"
"wmi-rest/utilities"

"github.com/gin-gonic/gin"
)

func VMS(c *gin.Context) {
cmdline := `Get-WmiObject -namespace 'root\virtualization\v2' -class Msvm_ComputerSystem -Filter 'Caption="Virtual Machine"' | Select-Object -Property ElementName, InstallDate, Name, ProcessID | ConvertTo-Json`
output, err := utilities.CommandLine(cmdline)

if err != nil {
c.Data(returnResponse(err.Error(), http.StatusInternalServerError, "failure", "error"))
return
}

if len(output) == 0 {
c.Data(returnResponse("No VM found.", http.StatusOK, "failure", "error"))
return
}

c.Data(returnResponse(output, http.StatusOK, "success", "VMs displayed in data field"))
}
Loading

0 comments on commit b727c4c

Please sign in to comment.