Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display IP addresses when available #225

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ FROM golang:alpine AS builder

# Install git.
# Git is required for fetching the dependencies.
RUN apk update && apk add --no-cache git coreutils musl-utils ca-certificates
RUN apk update && apk add --no-cache git coreutils musl-utils ca-certificates curl

WORKDIR /go/src

Expand All @@ -20,13 +20,15 @@ RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-w -s" -o /go/bin/prl-devops-ser
############################
# STEP 2 build a small image
############################
FROM scratch
FROM alpine:latest

RUN apk update && apk add curl coreutils bash musl-utils ca-certificates
# Copy our static executable.
COPY --from=builder /bin/cat /bin/cat
COPY --from=builder /usr/bin/whoami /usr/bin/whoami
COPY --from=builder /usr/bin/getent /usr/bin/getent
COPY --from=builder /usr/bin/id /usr/bin/id
# COPY --from=builder /bin/cat /bin/cat
# COPY --from=builder /usr/bin/whoami /usr/bin/whoami
# COPY --from=builder /usr/bin/getent /usr/bin/getent
# COPY --from=builder /bin/uname /usr/bin/uname
# COPY --from=builder /usr/bin/id /usr/bin/id
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/

COPY --from=builder /etc/ssl/certs/ca-certificates.crt /tmp/
Expand Down
58 changes: 58 additions & 0 deletions src/controllers/machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,14 @@ func registerVirtualMachinesHandlers(ctx basecontext.ApiContext, version string)
WithHandler(ExecuteCommandOnVirtualMachineHandler()).
Register()

restapi.NewController().
WithMethod(restapi.POST).
WithVersion(version).
WithPath("/machines/{id}/upload").
WithRequiredClaim(constants.UPDATE_VM_CLAIM).
WithHandler(UploadFileToVirtualMachineHandler()).
Register()

restapi.NewController().
WithMethod(restapi.PUT).
WithVersion(version).
Expand Down Expand Up @@ -797,6 +805,56 @@ func ExecuteCommandOnVirtualMachineHandler() restapi.ControllerHandler {
}
}

// @Summary Uploads a file to a virtual machine
// @Description This endpoint executes a command on a virtual machine
// @Tags Machines
// @Produce json
// @Param id path string true "Machine ID"
// @Param executeRequest body models.VirtualMachineUploadRequest true "Machine Upload file Command Request"
// @Success 200 {object} models.VirtualMachineUploadResponse
// @Failure 400 {object} models.ApiErrorResponse
// @Failure 401 {object} models.OAuthErrorResponse
// @Security ApiKeyAuth
// @Security BearerAuth
// @Router /v1/machines/{id}/upload [post]
func UploadFileToVirtualMachineHandler() restapi.ControllerHandler {
return func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
ctx := GetBaseContext(r)
defer Recover(ctx, r, w)
var request models.VirtualMachineUploadRequest
provider := serviceprovider.Get()
svc := provider.ParallelsDesktopService
if err := http_helper.MapRequestBody(r, &request); err != nil {
ReturnApiError(ctx, w, models.ApiErrorResponse{
Message: "Invalid request body: " + err.Error(),
Code: http.StatusBadRequest,
})
return
}
if err := request.Validate(); err != nil {
ReturnApiError(ctx, w, models.ApiErrorResponse{
Message: "Invalid request body: " + err.Error(),
Code: http.StatusBadRequest,
})
return
}

params := mux.Vars(r)
id := params["id"]

if response, err := svc.LocalUploadToVm(ctx, id, &request); err != nil {
ReturnApiError(ctx, w, models.NewFromError(err))
return
} else {
w.WriteHeader(http.StatusOK)
defer r.Body.Close()
_ = json.NewEncoder(w).Encode(response)
ctx.LogInfof("Command executed on machine: %v", id)
}
}
}

// @Summary Renames a virtual machine
// @Description This endpoint Renames a virtual machine
// @Tags Machines
Expand Down
32 changes: 20 additions & 12 deletions src/data/models/host_resources.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
package models

type HostResourceOverviewResponseItem struct {
CpuType string `json:"cpu_type,omitempty"`
CpuBrand string `json:"cpu_brand,omitempty"`
Total HostResourceItem `json:"total,omitempty"`
TotalAvailable HostResourceItem `json:"total_available,omitempty"`
TotalInUse HostResourceItem `json:"total_in_use,omitempty"`
TotalReserved HostResourceItem `json:"total_reserved,omitempty"`
CpuType string `json:"cpu_type,omitempty"`
CpuBrand string `json:"cpu_brand,omitempty"`
DevOpsVersion string `json:"devops_version,omitempty"`
OSName string `json:"os_name,omitempty"`
OsVersion string `json:"os_version,omitempty"`
ExternalIpAddress string `json:"external_ip_address,omitempty"`
Total HostResourceItem `json:"total,omitempty"`
TotalAvailable HostResourceItem `json:"total_available,omitempty"`
TotalInUse HostResourceItem `json:"total_in_use,omitempty"`
TotalReserved HostResourceItem `json:"total_reserved,omitempty"`
}

type HostResources struct {
CpuType string `json:"cpu_type,omitempty"`
CpuBrand string `json:"cpu_brand,omitempty"`
Total HostResourceItem `json:"total,omitempty"`
TotalAvailable HostResourceItem `json:"total_available,omitempty"`
TotalInUse HostResourceItem `json:"total_in_use,omitempty"`
TotalReserved HostResourceItem `json:"total_reserved,omitempty"`
CpuType string `json:"cpu_type,omitempty"`
CpuBrand string `json:"cpu_brand,omitempty"`
DevOpsVersion string `json:"devops_version,omitempty"`
OsName string `json:"os_name,omitempty"`
OsVersion string `json:"os_version,omitempty"`
ExternalIpAddress string `json:"external_ip_address,omitempty"`
Total HostResourceItem `json:"total,omitempty"`
TotalAvailable HostResourceItem `json:"total_available,omitempty"`
TotalInUse HostResourceItem `json:"total_in_use,omitempty"`
TotalReserved HostResourceItem `json:"total_reserved,omitempty"`
}

func (c *HostResources) Diff(source HostResources) bool {
Expand Down
18 changes: 17 additions & 1 deletion src/data/models/orchestrator_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ type OrchestratorHost struct {
Enabled bool `json:"enabled"`
Host string `json:"host"`
Architecture string `json:"architecture"`
CpuModel string `json:"cpu_model"`
CpuModel string `json:"cpu_model,omitempty"`
OsVersion string `json:"os_version,omitempty"`
OsName string `json:"os_name,omitempty"`
ExternalIpAddress string `json:"external_ip_address,omitempty"`
DevOpsVersion string `json:"devops_version,omitempty"`
Description string `json:"description,omitempty"`
Tags []string `json:"tags,omitempty"`
Port string `json:"port,omitempty"`
Expand Down Expand Up @@ -87,6 +91,18 @@ func (o *OrchestratorHost) Diff(source OrchestratorHost) bool {
if o.Enabled != source.Enabled {
return true
}
if o.OsVersion != source.OsVersion {
return true
}
if o.OsName != source.OsName {
return true
}
if o.DevOpsVersion != source.DevOpsVersion {
return true
}
if o.ExternalIpAddress != source.ExternalIpAddress {
return true
}
if o.Architecture != source.Architecture {
return true
}
Expand Down
80 changes: 80 additions & 0 deletions src/data/models/virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ type VirtualMachine struct {
HostId string `json:"host_id,omitempty"`
HostState string `json:"host_state,omitempty"`
User string `json:"user,omitempty"`
HostExternalIpAddress string `json:"host_external_ip_address,omitempty"`
InternalIpAddress string `json:"internal_ip_address,omitempty"`
Host string `json:"host,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Expand Down Expand Up @@ -47,6 +49,7 @@ type VirtualMachine struct {
Advanced VirtualMachineAdvanced `json:"advanced,omitempty"`
PrintManagement VirtualMachinePrintManagement `json:"print _management,omitempty"`
GuestSharedFolders VirtualMachineGuestSharedFolders `json:"guest_shared_folders,omitempty"`
NetworkInformation VirtualMachineNetworkInformation `json:"network_information,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
}
Expand All @@ -58,6 +61,15 @@ func (m *VirtualMachine) Diff(source VirtualMachine) bool {
if m.HostId != source.HostId {
return true
}
if m.HostState != source.HostState {
return true
}
if m.HostExternalIpAddress != source.HostExternalIpAddress {
return true
}
if m.InternalIpAddress != source.InternalIpAddress {
return true
}
if m.User != source.User {
return true
}
Expand Down Expand Up @@ -193,6 +205,9 @@ func (m *VirtualMachine) Diff(source VirtualMachine) bool {
if m.GuestSharedFolders.Diff(source.GuestSharedFolders) {
return true
}
if m.NetworkInformation.Diff(source.NetworkInformation) {
return true
}

return false
}
Expand Down Expand Up @@ -928,3 +943,68 @@ func (m *VirtualMachineUSBAndBluetooth) Diff(source VirtualMachineUSBAndBluetoot

return false
}

type VirtualMachineNetworkInformation struct {
Conditioned string `json:"conditioned"`
Inbound VirtualMachineNetworkInformationBound `json:"inbound"`
Outbound VirtualMachineNetworkInformationBound `json:"outbound"`
IPAddresses []VirtualMachineNetworkInformationIPAddress `json:"ip_addresses"`
}

func (m *VirtualMachineNetworkInformation) Diff(source VirtualMachineNetworkInformation) bool {
if m.Conditioned != source.Conditioned {
return true
}

if m.Inbound.Diff(source.Inbound) {
return true
}

if m.Outbound.Diff(source.Outbound) {
return true
}

for i, v := range m.IPAddresses {
if v.Diff(source.IPAddresses[i]) {
return true
}
}

return false
}

type VirtualMachineNetworkInformationIPAddress struct {
Type string `json:"type"`
IP string `json:"ip"`
}

func (m *VirtualMachineNetworkInformationIPAddress) Diff(source VirtualMachineNetworkInformationIPAddress) bool {
if m.Type != source.Type {
return true
}
if m.IP != source.IP {
return true
}

return false
}

type VirtualMachineNetworkInformationBound struct {
Bandwidth string `json:"bandwidth"`
PacketLoss string `json:"packet_loss"`
Delay string `json:"delay"`
}

func (m *VirtualMachineNetworkInformationBound) Diff(source VirtualMachineNetworkInformationBound) bool {
if m.Bandwidth != source.Bandwidth {
return true
}
if m.PacketLoss != source.PacketLoss {
return true
}
if m.Delay != source.Delay {
return true
}

return false
}
4 changes: 4 additions & 0 deletions src/data/orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ func (j *JsonDatabase) UpdateOrchestratorHost(ctx basecontext.ApiContext, host *
j.data.OrchestratorHosts[index].Enabled = host.Enabled
j.data.OrchestratorHosts[index].UpdatedAt = helpers.GetUtcCurrentDateTime()
j.data.OrchestratorHosts[index].Host = host.Host
j.data.OrchestratorHosts[index].OsVersion = host.OsVersion
j.data.OrchestratorHosts[index].OsName = host.OsName
j.data.OrchestratorHosts[index].ExternalIpAddress = host.ExternalIpAddress
j.data.OrchestratorHosts[index].DevOpsVersion = host.DevOpsVersion
j.data.OrchestratorHosts[index].Architecture = host.Architecture
j.data.OrchestratorHosts[index].CpuModel = host.CpuModel
j.data.OrchestratorHosts[index].Port = host.Port
Expand Down
24 changes: 14 additions & 10 deletions src/mappers/orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@ import (

func DtoOrchestratorHostToApiResponse(dto data_models.OrchestratorHost) models.OrchestratorHostResponse {
result := models.OrchestratorHostResponse{
ID: dto.ID,
Enabled: dto.Enabled,
Host: dto.GetHost(),
Architecture: dto.Architecture,
CpuModel: dto.CpuModel,
Description: dto.Description,
Tags: dto.Tags,
RequiredClaims: dto.RequiredClaims,
RequiredRoles: dto.RequiredRoles,
State: dto.State,
ID: dto.ID,
Enabled: dto.Enabled,
Host: dto.GetHost(),
Architecture: dto.Architecture,
CpuModel: dto.CpuModel,
OsVersion: dto.OsVersion,
OsName: dto.OsName,
ExternalIpAddress: dto.ExternalIpAddress,
DevOpsVersion: dto.DevOpsVersion,
Description: dto.Description,
Tags: dto.Tags,
RequiredClaims: dto.RequiredClaims,
RequiredRoles: dto.RequiredRoles,
State: dto.State,
}

if dto.Resources != nil {
Expand Down
7 changes: 7 additions & 0 deletions src/mappers/virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ func MapDtoVirtualMachineFromApi(m models.ParallelsVM) data_models.VirtualMachin
mapped := data_models.VirtualMachine{
HostId: m.HostId,
Host: m.Host,
HostState: m.HostState,
HostExternalIpAddress: m.HostExternalIpAddress,
InternalIpAddress: m.InternalIpAddress,
User: m.User,
ID: m.ID,
Name: m.Name,
Expand Down Expand Up @@ -49,6 +52,7 @@ func MapDtoVirtualMachineFromApi(m models.ParallelsVM) data_models.VirtualMachin
Advanced: MapDtoVirtualMachineAdvancedFromApi(m.Advanced),
PrintManagement: MapDtoVirtualMachinePrintManagementFromApi(m.PrintManagement),
GuestSharedFolders: MapDtoVirtualMachineGuestSharedFoldersFromApi(m.GuestSharedFolders),
NetworkInformation: MapDtoVirtualMachineNetworkInformationFromApi(m.NetworkInformation),
}

return mapped
Expand All @@ -59,6 +63,8 @@ func MapDtoVirtualMachineToApi(m data_models.VirtualMachine) models.ParallelsVM
HostId: m.HostId,
Host: m.Host,
HostState: m.HostState,
HostExternalIpAddress: m.HostExternalIpAddress,
InternalIpAddress: m.InternalIpAddress,
User: m.User,
ID: m.ID,
Name: m.Name,
Expand Down Expand Up @@ -99,6 +105,7 @@ func MapDtoVirtualMachineToApi(m data_models.VirtualMachine) models.ParallelsVM
Advanced: MapDtoVirtualMachineAdvancedToApi(m.Advanced),
PrintManagement: MapDtoVirtualMachinePrintManagementToApi(m.PrintManagement),
GuestSharedFolders: MapDtoVirtualMachineGuestSharedFoldersToApi(m.GuestSharedFolders),
NetworkInformation: MapDtoVirtualNetworkInformationToApi(m.NetworkInformation),
}

return mapped
Expand Down
Loading
Loading