Skip to content

Commit

Permalink
feat: adds centralized wsman error struct and decode for boot and kvm
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-primrose committed Sep 10, 2024
1 parent 149638f commit bca1d5c
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 2 deletions.
19 changes: 19 additions & 0 deletions pkg/wsman/amt/boot/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

package boot

import (
"errors"
"strings"
)

// INPUTS Constants.
const (
AMTBootSettingData string = "AMT_BootSettingData"
Expand Down Expand Up @@ -55,3 +60,17 @@ func (i IDERBootDevice) String() string {

return ValueNotFound
}

func DecodeWSMANError(subCode string) error {
if len(subCode) > 0 {
modifiedString := strings.Split(subCode, ":")[1]
switch modifiedString {
case "AccessDenied":
return errors.New("User Consent is required for this action")
default:
return errors.New("unknown error")
}
}

return nil
}
29 changes: 28 additions & 1 deletion pkg/wsman/amt/boot/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

package boot

import "testing"
import (
"errors"
"testing"
)

func TestFirmwareVerbosity_String(t *testing.T) {
tests := []struct {
Expand Down Expand Up @@ -44,3 +47,27 @@ func TestIDERBootDevice_String(t *testing.T) {
}
}
}

func TestDecodeWSMANError(t *testing.T) {
tests := []struct {
input string
expected error
}{
{"e:AccessDenied", errors.New("User Consent is required for this action")},
{"b:UnknownError", errors.New("unknown error")},
{"", nil},
}

for _, test := range tests {
result := DecodeWSMANError(test.input)
if result == nil {
if test.expected != nil {
t.Errorf("Expected %s, but got nil", test.expected)
}
} else {
if result.Error() != test.expected.Error() {
t.Errorf("Expected %s, but got %s", test.expected, result)
}
}
}
}
10 changes: 10 additions & 0 deletions pkg/wsman/amt/boot/settingdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/open-amt-cloud-toolkit/go-wsman-messages/v2/internal/message"
"github.com/open-amt-cloud-toolkit/go-wsman-messages/v2/pkg/wsman/client"
"github.com/open-amt-cloud-toolkit/go-wsman-messages/v2/pkg/wsman/common"
)

// Instantiates a new Boot Setting Data service.
Expand Down Expand Up @@ -147,5 +148,14 @@ func (settingData SettingData) Put(bootSettingData BootSettingDataRequest) (resp
return response, err
}

checkForErrorResponse := common.ErrorResponse{}

err = xml.Unmarshal([]byte(response.XMLOutput), &checkForErrorResponse)
if err != nil {
return

Check failure on line 155 in pkg/wsman/amt/boot/settingdata.go

View workflow job for this annotation

GitHub Actions / runner / golangci-lint

[golangci] reported by reviewdog 🐶 naked return in func `Put` with 67 lines of code (nakedret) Raw Output: pkg/wsman/amt/boot/settingdata.go:155:3: naked return in func `Put` with 67 lines of code (nakedret) return ^
}

err = DecodeWSMANError(checkForErrorResponse.Body.Fault.Code.SubCode.Value)

Check failure on line 158 in pkg/wsman/amt/boot/settingdata.go

View workflow job for this annotation

GitHub Actions / runner / golangci-lint

[golangci] reported by reviewdog 🐶 ineffectual assignment to err (ineffassign) Raw Output: pkg/wsman/amt/boot/settingdata.go:158:2: ineffectual assignment to err (ineffassign) err = DecodeWSMANError(checkForErrorResponse.Body.Fault.Code.SubCode.Value) ^

return response, nil
}
19 changes: 19 additions & 0 deletions pkg/wsman/cim/kvm/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

package kvm

import (
"errors"
"strings"
)

const (
CIMKVMRedirectionSAP string = "CIM_KVMRedirectionSAP"
ValueNotFound string = "Value not found in map"
Expand Down Expand Up @@ -160,3 +165,17 @@ func (e RequestedState) String() string {

return ValueNotFound
}

func DecodeWSMANError(subCode string) error {
if len(subCode) > 0 {
modifiedString := strings.Split(subCode, ":")[1]
switch modifiedString {
case "DestinationUnreachable":
return errors.New("KVM is not available on this system")
default:
return errors.New("unknown error")
}
}

return nil
}
29 changes: 28 additions & 1 deletion pkg/wsman/cim/kvm/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

package kvm

import "testing"
import (
"errors"
"testing"
)

func TestKVMProtocol_String(t *testing.T) {
tests := []struct {
Expand Down Expand Up @@ -109,3 +112,27 @@ func TestRequestedState_String(t *testing.T) {
}
}
}

func TestDecodeWSMANError(t *testing.T) {
tests := []struct {
input string
expected error
}{
{"b:DestinationUnreachable", errors.New("KVM is not available on this system")},
{"e:UnknownError", errors.New("unknown error")},
{"", nil},
}

for _, test := range tests {
result := DecodeWSMANError(test.input)
if result == nil {
if test.expected != nil {
t.Errorf("Expected %s, but got nil", test.expected)
}
} else {
if result.Error() != test.expected.Error() {
t.Errorf("Expected %s, but got %s", test.expected, result)
}
}
}
}
10 changes: 10 additions & 0 deletions pkg/wsman/cim/kvm/redirectionsap.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/open-amt-cloud-toolkit/go-wsman-messages/v2/internal/message"
"github.com/open-amt-cloud-toolkit/go-wsman-messages/v2/pkg/wsman/cim/methods"
"github.com/open-amt-cloud-toolkit/go-wsman-messages/v2/pkg/wsman/client"
"github.com/open-amt-cloud-toolkit/go-wsman-messages/v2/pkg/wsman/common"
)

// NewKVMRedirectionSAP returns a new instance of the KVMRedirectionSAP struct.
Expand Down Expand Up @@ -102,5 +103,14 @@ func (redirectionSAP RedirectionSAP) Pull(enumerationContext string) (response R
return
}

checkForErrorResponse := common.ErrorResponse{}

err = xml.Unmarshal([]byte(response.XMLOutput), &checkForErrorResponse)
if err != nil {
return
}

err = DecodeWSMANError(checkForErrorResponse.Body.Fault.Code.SubCode.Value)

return
}
37 changes: 37 additions & 0 deletions pkg/wsman/common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,40 @@ type ReturnValue struct {
ReturnValue int `xml:"ReturnValue,omitempty"`
ReturnValueStr string `xml:"ReturnValueStr,omitempty"`
}

// WSMAN Error Response Types.
type (
ErrorResponse struct {
XMLName xml.Name `xml:"Envelope"`
Header message.Header `xml:"Header"`
Body ErrorBody `xml:"Body"`
}

ErrorBody struct {
XMLName xml.Name `xml:"Body"`
Fault Fault `xml:"Fault"`
}

Fault struct {
XMLName xml.Name `xml:"Fault"`
Code Code `xml:"Code"`
Reason Reason `xml:"Reason"`
Detail string `xml:"Detail"`
}

Code struct {
XMLName xml.Name `xml:"Code"`
Value string `xml:"Value"`
SubCode SubCode `xml:"Subcode"`
}

SubCode struct {
XMLName xml.Name `xml:"Subcode"`
Value string `xml:"Value"`
}

Reason struct {
XMLName xml.Name `xml:"Reason"`
Text string `xml:"Text"`
}
)

0 comments on commit bca1d5c

Please sign in to comment.