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 authored Sep 10, 2024
1 parent 92fcb4a commit b4ea5d3
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 3 deletions.
4 changes: 3 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,9 @@

package boot

import "testing"
import (
"testing"
)

func TestFirmwareVerbosity_String(t *testing.T) {
tests := []struct {
Expand Down
12 changes: 11 additions & 1 deletion 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
}

return response, nil
checkForErrorResponse := common.ErrorResponse{}

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

err = common.DecodeAMTError(checkForErrorResponse)

return response, err
}
4 changes: 3 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,9 @@

package kvm

import "testing"
import (
"testing"
)

func TestKVMProtocol_String(t *testing.T) {
tests := []struct {
Expand Down
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 = common.DecodeAMTError(checkForErrorResponse)

return
}
24 changes: 24 additions & 0 deletions pkg/wsman/common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

package common

import (
"fmt"
)

// TODO: Review if this file is still necessary.

const ValueNotFound string = "Value not found in map"
Expand Down Expand Up @@ -231,3 +235,23 @@ func ConvertPackageTypeToString(value int) string {

return ValueNotFound
}

func (e *AMTError) Error() string {
return fmt.Sprintf("Error [SubCode: %s] Message: %s, Detail: %s", e.SubCode, e.Message, e.Detail)
}

func NewAMTError(subCode, message, detail string) *AMTError {
return &AMTError{
SubCode: subCode,
Message: message,
Detail: detail,
}
}

func DecodeAMTError(er ErrorResponse) error {
if er.Body.Fault.Code.SubCode.Value != "" {
return NewAMTError(er.Body.Fault.Code.SubCode.Value, er.Body.Fault.Reason.Text, er.Body.Fault.Detail)
}

return nil
}
73 changes: 73 additions & 0 deletions pkg/wsman/common/constants_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*********************************************************************
* Copyright (c) Intel Corporation 2024
* SPDX-License-Identifier: Apache-2.0
**********************************************************************/

package common

import (
"testing"
)

func TestDecodeWSMANError(t *testing.T) {
tests := []struct {
input ErrorResponse
expected error
}{
{
ErrorResponse{
Body: ErrorBody{
Fault: Fault{
Code: Code{
SubCode: SubCode{
Value: "b:AccessDenied",
},
},
Reason: Reason{
Text: "The sender was not authorized to access the resource.",
},
Detail: "",
},
},
},
&AMTError{
SubCode: "b:AccessDenied", Message: "The sender was not authorized to access the resource.", Detail: "",
},
}, {
ErrorResponse{
Body: ErrorBody{
Fault: Fault{
Code: Code{
SubCode: SubCode{
Value: "e:DestinationUnreachable",
},
},
Reason: Reason{
Text: "No route can be determined to reach the destination role defined by the WSAddressing To.",
},
Detail: "",
},
},
},
&AMTError{
SubCode: "e:DestinationUnreachable", Message: "No route can be determined to reach the destination role defined by the WSAddressing To.", Detail: "",
},
}, {
ErrorResponse{},
nil,
},
}

for _, test := range tests {
result := DecodeAMTError(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)
}
}
}
}
43 changes: 43 additions & 0 deletions pkg/wsman/common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,46 @@ type ReturnValue struct {
ReturnValue int `xml:"ReturnValue,omitempty"`
ReturnValueStr string `xml:"ReturnValueStr,omitempty"`
}

// AMT WSMAN Error Response Types.
type (
AMTError struct {
SubCode string
Message string
Detail string
}

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 b4ea5d3

Please sign in to comment.