Skip to content
Draft
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
6 changes: 6 additions & 0 deletions internal/controller/http/v1/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type response struct {
func ErrorResponse(c *gin.Context, err error) {
var (
validatorErr validator.ValidationErrors
cancelledError dto.CanceledError
nfErr sqldb.NotFoundError
notValidErr dto.NotValidError
dbErr sqldb.DatabaseError
Expand All @@ -36,6 +37,8 @@ func ErrorResponse(c *gin.Context, err error) {
switch {
case errors.As(err, &netErr):
netErrorHandle(c, netErr)
case errors.As(err, &cancelledError):
cancelledErrorHandle(c, cancelledError)
case errors.As(err, &notValidErr):
notValidErrorHandle(c, notValidErr)
case errors.As(err, &validatorErr):
Expand All @@ -62,6 +65,9 @@ func ErrorResponse(c *gin.Context, err error) {
func netErrorHandle(c *gin.Context, netErr net.Error) {
c.AbortWithStatusJSON(http.StatusGatewayTimeout, response{netErr.Error()})
}
func cancelledErrorHandle(c *gin.Context, cancelError dto.CanceledError) {
c.AbortWithStatusJSON(http.StatusBadRequest, response{cancelError.Error()})
}

func notValidErrorHandle(c *gin.Context, err dto.NotValidError) {
c.AbortWithStatusJSON(http.StatusBadRequest, response{err.Console.FriendlyMessage()})
Expand Down
20 changes: 20 additions & 0 deletions internal/entity/dto/v1/canceled.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dto

import (
"github.com/open-amt-cloud-toolkit/console/pkg/consoleerrors"

Check failure on line 4 in internal/entity/dto/v1/canceled.go

View workflow job for this annotation

GitHub Actions / runner / build and tests (1.24.x, windows-2019)

no required module provides package github.com/open-amt-cloud-toolkit/console/pkg/consoleerrors; to add it:

Check failure on line 4 in internal/entity/dto/v1/canceled.go

View workflow job for this annotation

GitHub Actions / runner / build and tests (1.23.x, ubuntu-22.04)

no required module provides package github.com/open-amt-cloud-toolkit/console/pkg/consoleerrors; to add it:

Check failure on line 4 in internal/entity/dto/v1/canceled.go

View workflow job for this annotation

GitHub Actions / runner / build and tests (1.24.x, ubuntu-22.04)

no required module provides package github.com/open-amt-cloud-toolkit/console/pkg/consoleerrors; to add it:

Check failure on line 4 in internal/entity/dto/v1/canceled.go

View workflow job for this annotation

GitHub Actions / runner / golangci-lint

[golangci] reported by reviewdog 🐶 could not import github.com/open-amt-cloud-toolkit/console/pkg/consoleerrors (internal/entity/dto/v1/canceled.go:4:2: no required module provides package github.com/open-amt-cloud-toolkit/console/pkg/consoleerrors; to add it: Raw Output: internal/entity/dto/v1/canceled.go:4:2: could not import github.com/open-amt-cloud-toolkit/console/pkg/consoleerrors (internal/entity/dto/v1/canceled.go:4:2: no required module provides package github.com/open-amt-cloud-toolkit/console/pkg/consoleerrors; to add it: go get github.com/open-amt-cloud-toolkit/console/pkg/consoleerrors) (typecheck) "github.com/open-amt-cloud-toolkit/console/pkg/consoleerrors" ^ 1 issues: * typecheck: 1

Check failure on line 4 in internal/entity/dto/v1/canceled.go

View workflow job for this annotation

GitHub Actions / runner / build and tests (1.24.x, ubuntu-24.04)

no required module provides package github.com/open-amt-cloud-toolkit/console/pkg/consoleerrors; to add it:

Check failure on line 4 in internal/entity/dto/v1/canceled.go

View workflow job for this annotation

GitHub Actions / runner / build and tests (1.24.x, windows-2022)

no required module provides package github.com/open-amt-cloud-toolkit/console/pkg/consoleerrors; to add it:

Check failure on line 4 in internal/entity/dto/v1/canceled.go

View workflow job for this annotation

GitHub Actions / runner / build and tests (1.23.x, windows-2022)

no required module provides package github.com/open-amt-cloud-toolkit/console/pkg/consoleerrors; to add it:
)

type CanceledError struct {
Console consoleerrors.InternalError
}

func (e CanceledError) Error() string {
return e.Console.Error()
}

func (e CanceledError) Wrap(function, call string, err error) error {
_ = e.Console.Wrap(function, call, err)
e.Console.Message = "Canceled: " + err.Error()

return e
}
10 changes: 6 additions & 4 deletions internal/usecase/devices/alarms.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ func (uc *UseCase) GetAlarmOccurrences(c context.Context, guid string) ([]dto.Al
if err != nil {
return nil, err
}

if item == nil || item.GUID == "" {
return nil, ErrNotFound
}

device := uc.device.SetupWsmanClient(*item, false, true)
device := uc.device.SetupWsmanClient(c, *item, false, true)
if device == nil {
return nil, ErrCancelled
}

alarms, err := device.GetAlarmOccurrences()
if err != nil {
Expand Down Expand Up @@ -61,7 +63,7 @@ func (uc *UseCase) CreateAlarmOccurrences(c context.Context, guid string, alarm

alarm.InstanceID = alarm.ElementName

device := uc.device.SetupWsmanClient(*item, false, true)
device := uc.device.SetupWsmanClient(c, *item, false, true)

alarmReference, err := device.CreateAlarmOccurrences(alarm.InstanceID, alarm.StartTime, alarm.Interval, alarm.DeleteOnCompletion)
if err != nil {
Expand All @@ -83,7 +85,7 @@ func (uc *UseCase) DeleteAlarmOccurrences(c context.Context, guid, instanceID st
return ErrNotFound
}

device := uc.device.SetupWsmanClient(*item, false, true)
device := uc.device.SetupWsmanClient(c, *item, false, true)

err = device.DeleteAlarmOccurrences(instanceID)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/usecase/devices/certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func (uc *UseCase) GetCertificates(c context.Context, guid string) (dto.Security
return dto.SecuritySettings{}, ErrNotFound
}

device := uc.device.SetupWsmanClient(*item, false, true)
device := uc.device.SetupWsmanClient(c, *item, false, true)

response, err := device.GetCertificates()
if err != nil {
Expand Down Expand Up @@ -256,7 +256,7 @@ func (uc *UseCase) GetDeviceCertificate(c context.Context, guid string) (dto.Cer
return dto.Certificate{}, ErrNotFound
}

device := uc.device.SetupWsmanClient(*item, false, true)
device := uc.device.SetupWsmanClient(c, *item, false, true)

cert1, err := device.GetDeviceCertificate()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/usecase/devices/connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (uc *UseCase) GetTLSSettingData(c context.Context, guid string) ([]dto.Sett
return nil, ErrNotFound
}

device := uc.device.SetupWsmanClient(*item, false, true)
device := uc.device.SetupWsmanClient(c, *item, false, true)

response, err := device.GetTLSSettingData()
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions internal/usecase/devices/consent.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (uc *UseCase) CancelUserConsent(c context.Context, guid string) (dto.UserCo
return dto.UserConsentMessage{}, ErrNotFound
}

device := uc.device.SetupWsmanClient(*item, false, true)
device := uc.device.SetupWsmanClient(c, *item, false, true)

response, err := device.CancelUserConsentRequest()
if err != nil {
Expand All @@ -37,7 +37,7 @@ func (uc *UseCase) GetUserConsentCode(c context.Context, guid string) (dto.GetUs
return dto.GetUserConsentMessage{}, ErrNotFound
}

device := uc.device.SetupWsmanClient(*item, false, true)
device := uc.device.SetupWsmanClient(c, *item, false, true)

code, err := device.GetUserConsentCode()
if err != nil {
Expand All @@ -64,7 +64,7 @@ func (uc *UseCase) SendConsentCode(c context.Context, userConsent dto.UserConsen
return dto.UserConsentMessage{}, ErrNotFound
}

device := uc.device.SetupWsmanClient(*item, false, true)
device := uc.device.SetupWsmanClient(c, *item, false, true)

consentCode, _ := strconv.Atoi(userConsent.ConsentCode)

Expand Down
7 changes: 5 additions & 2 deletions internal/usecase/devices/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ func (uc *UseCase) GetFeatures(c context.Context, guid string) (settingsResults
return settingsResults, settingsResultsV2, ErrNotFound
}

device := uc.device.SetupWsmanClient(*item, false, true)
device := uc.device.SetupWsmanClient(c, *item, false, true)
if device == nil {
return dto.Features{}, dtov2.Features{}, ErrCancelled
}

// Get redirection settings from AMT
err = getRedirectionService(&settingsResultsV2, device)
Expand Down Expand Up @@ -189,7 +192,7 @@ func (uc *UseCase) SetFeatures(c context.Context, guid string, features dto.Feat
return settingsResults, settingsResultsV2, ErrNotFound
}

device := uc.device.SetupWsmanClient(*item, false, true)
device := uc.device.SetupWsmanClient(c, *item, false, true)

// redirection
state, listenerEnabled, err := redirectionRequestStateChange(features.EnableSOL, features.EnableIDER, &settingsResultsV2, device)
Expand Down
18 changes: 12 additions & 6 deletions internal/usecase/devices/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ func (uc *UseCase) GetVersion(c context.Context, guid string) (v1 dto.Version, v
return v1, v2, ErrNotFound
}

device := uc.device.SetupWsmanClient(*item, false, true)
device := uc.device.SetupWsmanClient(c, *item, false, true)
if device == nil {
return v1, v2, ErrCancelled
}

softwareIdentity, err := device.GetAMTVersion()
if err != nil {
Expand Down Expand Up @@ -69,7 +72,7 @@ func (uc *UseCase) GetHardwareInfo(c context.Context, guid string) (interface{},
return nil, ErrNotFound
}

device := uc.device.SetupWsmanClient(*item, false, true)
device := uc.device.SetupWsmanClient(c, *item, false, true)

hwInfo, err := device.GetHardwareInfo()
if err != nil {
Expand All @@ -89,7 +92,7 @@ func (uc *UseCase) GetDiskInfo(c context.Context, guid string) (interface{}, err
return nil, ErrNotFound
}

device := uc.device.SetupWsmanClient(*item, false, true)
device := uc.device.SetupWsmanClient(c, *item, false, true)

diskInfo, err := device.GetDiskInfo()
if err != nil {
Expand All @@ -109,7 +112,7 @@ func (uc *UseCase) GetAuditLog(c context.Context, startIndex int, guid string) (
return dto.AuditLog{}, ErrNotFound
}

device := uc.device.SetupWsmanClient(*item, false, true)
device := uc.device.SetupWsmanClient(c, *item, false, true)

response, err := device.GetAuditLog(startIndex)
if err != nil {
Expand All @@ -133,7 +136,7 @@ func (uc *UseCase) GetEventLog(c context.Context, startIndex, maxReadRecords int
return dto.EventLogs{}, ErrNotFound
}

device := uc.device.SetupWsmanClient(*item, false, true)
device := uc.device.SetupWsmanClient(c, *item, false, true)

eventLogs, err := device.GetEventLog(startIndex, maxReadRecords)
if err != nil {
Expand Down Expand Up @@ -184,7 +187,10 @@ func (uc *UseCase) GetGeneralSettings(c context.Context, guid string) (interface
return nil, ErrNotFound
}

device := uc.device.SetupWsmanClient(*item, false, true)
device := uc.device.SetupWsmanClient(c, *item, false, true)
if device == nil {
return nil, ErrCancelled
}

generalSettings, err := device.GetGeneralSettings()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/usecase/devices/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

type (
WSMAN interface {
SetupWsmanClient(device entity.Device, isRedirection, logMessages bool) wsmanAPI.Management
SetupWsmanClient(ctx context.Context, device entity.Device, isRedirection, logMessages bool) wsmanAPI.Management
DestroyWsmanClient(device dto.Device)
Worker()
}
Expand Down
2 changes: 1 addition & 1 deletion internal/usecase/devices/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (uc *UseCase) GetNetworkSettings(c context.Context, guid string) (dto.Netwo
return dto.NetworkSettings{}, ErrNotFound
}

device := uc.device.SetupWsmanClient(*item, false, true)
device := uc.device.SetupWsmanClient(c, *item, false, true)

response, err := device.GetNetworkSettings()
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions internal/usecase/devices/power.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (uc *UseCase) SendPowerAction(c context.Context, guid string, action int) (
return power.PowerActionResponse{}, ErrNotFound
}

device := uc.device.SetupWsmanClient(*item, false, true)
device := uc.device.SetupWsmanClient(c, *item, false, true)

if action == OsToFullPower || action == OsToPowerSaving {
response, err := handleOSPowerSavingStateChange(device, action)
Expand Down Expand Up @@ -127,7 +127,7 @@ func (uc *UseCase) GetPowerState(c context.Context, guid string) (dto.PowerState
return dto.PowerState{}, ErrNotFound
}

device := uc.device.SetupWsmanClient(*item, false, true)
device := uc.device.SetupWsmanClient(c, *item, false, true)

state, err := device.GetPowerState()
if err != nil {
Expand Down Expand Up @@ -158,7 +158,7 @@ func (uc *UseCase) GetPowerCapabilities(c context.Context, guid string) (dto.Pow
return dto.PowerCapabilities{}, ErrNotFound
}

device := uc.device.SetupWsmanClient(*item, false, true)
device := uc.device.SetupWsmanClient(c, *item, false, true)

version, err := device.GetAMTVersion()
if err != nil {
Expand Down Expand Up @@ -230,7 +230,7 @@ func (uc *UseCase) SetBootOptions(c context.Context, guid string, bootSetting dt
return power.PowerActionResponse{}, ErrNotFound
}

device := uc.device.SetupWsmanClient(*item, false, true)
device := uc.device.SetupWsmanClient(c, *item, false, true)

bootData, err := device.GetBootData()
if err != nil {
Expand Down
6 changes: 4 additions & 2 deletions internal/usecase/devices/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import (
)

var (
ErrDomainsUseCase = consoleerrors.CreateConsoleError("DevicesUseCase")
ErrDatabase = sqldb.DatabaseError{Console: consoleerrors.CreateConsoleError("DevicesUseCase")}
ErrNotFound = sqldb.NotFoundError{Console: consoleerrors.CreateConsoleError("DevicesUseCase")}
ErrCancelled = dto.CanceledError{Console: consoleerrors.CreateConsoleError("DevicesUseCase")}
ErrDeviceUseCase = consoleerrors.CreateConsoleError("DevicesUseCase")
ErrDatabase = sqldb.DatabaseError{Console: consoleerrors.CreateConsoleError("DevicesUseCase")}
ErrNotFound = sqldb.NotFoundError{Console: consoleerrors.CreateConsoleError("DevicesUseCase")}
)

// History - getting translate history from store.
Expand Down
10 changes: 8 additions & 2 deletions internal/usecase/devices/wsman/message.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package wsman

import (
"context"
gotls "crypto/tls"
"sync"
"time"
Expand Down Expand Up @@ -114,15 +115,20 @@ func (g GoWSMANMessages) Worker() {
}
}

func (g GoWSMANMessages) SetupWsmanClient(device entity.Device, isRedirection, logAMTMessages bool) Management {
func (g GoWSMANMessages) SetupWsmanClient(ctx context.Context, device entity.Device, isRedirection, logAMTMessages bool) Management {
resultChan := make(chan *ConnectionEntry)
// Queue the request
requestQueue <- func() {
device.Password, _ = g.safeRequirements.Decrypt(device.Password)
resultChan <- g.setupWsmanClientInternal(device, isRedirection, logAMTMessages)
}

return <-resultChan
select {
case entry := <-resultChan:
return entry
case <-ctx.Done():
return nil
}
}

func (g GoWSMANMessages) setupWsmanClientInternal(device entity.Device, isRedirection, logAMTMessages bool) *ConnectionEntry {
Expand Down
Loading