From e6a8df7a25e54e9796d625492a4e76a22eba4aa3 Mon Sep 17 00:00:00 2001 From: zaidusmani26 <106174728+zaidusmani26@users.noreply.github.com> Date: Mon, 30 Oct 2023 16:23:55 -0700 Subject: [PATCH] feat(amt): add execute support for redirection (#103) Co-authored-by: Mike --- pkg/amt/redirection/service.go | 114 +++++++++++++++--- pkg/amt/redirection/service_test.go | 105 +++++++++++++--- .../responses/amt/redirection/enumerate.xml | 22 ++++ .../responses/amt/redirection/get.xml | 5 +- 4 files changed, 210 insertions(+), 36 deletions(-) create mode 100644 pkg/wsmantesting/responses/amt/redirection/enumerate.xml diff --git a/pkg/amt/redirection/service.go b/pkg/amt/redirection/service.go index bd46326e..554c9764 100644 --- a/pkg/amt/redirection/service.go +++ b/pkg/amt/redirection/service.go @@ -6,10 +6,37 @@ package redirection import ( + "encoding/json" + "encoding/xml" + "github.com/open-amt-cloud-toolkit/go-wsman-messages/internal/message" - "github.com/open-amt-cloud-toolkit/go-wsman-messages/pkg/amt/actions" + "github.com/open-amt-cloud-toolkit/go-wsman-messages/pkg/common" + "github.com/open-amt-cloud-toolkit/go-wsman-messages/pkg/wsman" ) +type ( + Response struct { + *wsman.Message + XMLName xml.Name `xml:"Envelope"` + Header message.Header `xml:"Header"` + Body Body `xml:"Body"` + } + Body struct { + XMLName xml.Name `xml:"Body"` + Redirection Redirection `xml:"AMT_RedirectionService"` + + EnumerateResponse common.EnumerateResponse + } + Redirection struct { + CreationClassName string + ElementName string + EnabledState int + ListenerEnabled bool + Name string + SystemCreationClassName string + SystemName string + } +) type RedirectionResponse struct { AMT_RedirectionService RedirectionService } @@ -58,7 +85,16 @@ const ( ) type Service struct { - base message.Base + base message.Base + client wsman.WSManClient +} + +func (w *Response) JSON() string { + jsonOutput, err := json.Marshal(w.Body) + if err != nil { + return "" + } + return string(jsonOutput) } func NewRedirectionService(wsmanMessageCreator *message.WSManMessageCreator) Service { @@ -67,27 +103,69 @@ func NewRedirectionService(wsmanMessageCreator *message.WSManMessageCreator) Ser } } +func NewRedirectionServiceWithClient(wsmanMessageCreator *message.WSManMessageCreator, client wsman.WSManClient) Service { + return Service{ + base: message.NewBaseWithClient(wsmanMessageCreator, AMT_RedirectionService, client), + client: client, + } +} + // Get retrieves the representation of the instance -func (RedirectionService Service) Get() string { - return RedirectionService.base.Get(nil) +func (s Service) Get() (response Response, err error) { + response = Response{ + Message: &wsman.Message{ + XMLInput: s.base.Get(nil), + }, + } + + // send the message to AMT + err = s.base.Execute(response.Message) + if err != nil { + return + } + + // put the xml response into the go struct + err = xml.Unmarshal([]byte(response.XMLOutput), &response) + if err != nil { + return + } + + return } // Enumerates the instances of this class -func (RedirectionService Service) Enumerate() string { - return RedirectionService.base.Enumerate() -} +func (s Service) Enumerate() (response Response, err error) { + response = Response{ + Message: &wsman.Message{ + XMLInput: s.base.Enumerate(), + }, + } + // send the message to AMT + err = s.base.Execute(response.Message) + if err != nil { + return + } -// Pulls instances of this class, following an Enumerate operation -func (RedirectionService Service) Pull(enumerationContext string) string { - return RedirectionService.base.Pull(enumerationContext) -} + // put the xml response into the go struct + err = xml.Unmarshal([]byte(response.XMLOutput), &response) + if err != nil { + return + } -// Put will change properties of the selected instance -func (RedirectionService Service) Put(redirectionService RedirectionService) string { - return RedirectionService.base.Put(redirectionService, false, nil) + return } -// RequestStateChange requests that the state of the element be changed to the value specified in the RequestedState parameter . . . -func (RedirectionService Service) RequestStateChange(requestedState RequestedState) string { - return RedirectionService.base.RequestStateChange(actions.RequestStateChange(AMT_RedirectionService), int(requestedState)) -} +// Pulls instances of this class, following an Enumerate operation +// func (RedirectionService Service) Pull(enumerationContext string) string { +// return RedirectionService.base.Pull(enumerationContext) +// } + +// // Put will change properties of the selected instance +// func (RedirectionService Service) Put(redirectionService RedirectionService) string { +// return RedirectionService.base.Put(redirectionService, false, nil) +// } + +// // RequestStateChange requests that the state of the element be changed to the value specified in the RequestedState parameter . . . +// func (RedirectionService Service) RequestStateChange(requestedState RequestedState) string { +// return RedirectionService.base.RequestStateChange(actions.RequestStateChange(AMT_RedirectionService), int(requestedState)) +// } diff --git a/pkg/amt/redirection/service_test.go b/pkg/amt/redirection/service_test.go index 66515879..64a79d94 100644 --- a/pkg/amt/redirection/service_test.go +++ b/pkg/amt/redirection/service_test.go @@ -6,45 +6,120 @@ package redirection import ( + "encoding/xml" + "fmt" + "io" + "os" + "strings" "testing" "github.com/stretchr/testify/assert" "github.com/open-amt-cloud-toolkit/go-wsman-messages/internal/message" + "github.com/open-amt-cloud-toolkit/go-wsman-messages/pkg/common" "github.com/open-amt-cloud-toolkit/go-wsman-messages/pkg/wsmantesting" ) +type MockClient struct { +} + +const ( + EnvelopeResponse = `http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous0` + GetBody = `AMT_RedirectionServiceIntel(r) AMT Redirection ServiceIntel(r) AMT Redirection ServiceCIM_ComputerSystemManagedSystem + + + http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous + 0 + http://schemas.xmlsoap.org/ws/2004/09/enumeration/EnumerateResponse + uuid:00000000-8086-8086-8086-000000000322 + http://intel.com/wbem/wscim/1/amt-schema/1/AMT_RedirectionService + + + + D3000000-0000-0000-0000-000000000000 + + + \ No newline at end of file diff --git a/pkg/wsmantesting/responses/amt/redirection/get.xml b/pkg/wsmantesting/responses/amt/redirection/get.xml index f4f9e942..86e8b968 100644 --- a/pkg/wsmantesting/responses/amt/redirection/get.xml +++ b/pkg/wsmantesting/responses/amt/redirection/get.xml @@ -1,4 +1,4 @@ - + @@ -14,8 +14,7 @@ Intel(r) AMT Redirection Service 32771 true - Intel(r) AMT Redirection Service - + Intel(r) AMT Redirection Service CIM_ComputerSystem Intel(r) AMT