Skip to content

Commit

Permalink
feat(amt): adds execute support to remoteaccess
Browse files Browse the repository at this point in the history
  • Loading branch information
zaidusmani26 committed Oct 11, 2023
1 parent 5ec768a commit 6fca57c
Show file tree
Hide file tree
Showing 9 changed files with 532 additions and 111 deletions.
80 changes: 75 additions & 5 deletions pkg/amt/remoteaccess/policyappliestomps.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,33 @@ package remoteaccess

import (
"encoding/xml"
"encoding/json"

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

const AMT_RemoteAccessPolicyAppliesToMPS = "AMT_RemoteAccessPolicyAppliesToMPS"

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"`
PolicyApplies PolicyApplies `xml:"AMT_RemoteAccessPolicyAppliesToMPS"`

EnumerateResponse common.EnumerateResponse
}
PolicyApplies struct{

}
)
type RemoteAccessPolicyAppliesToMPS struct {
XMLName xml.Name `xml:"h:AMT_RemoteAccessPolicyAppliesToMPS"`
H string `xml:"xmlns:h,attr"`
Expand Down Expand Up @@ -60,6 +80,22 @@ const (

type PolicyAppliesToMPS struct {
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 NewRemoteAccessPolicyAppliesToMPSWithClient(wsmanMessageCreator *message.WSManMessageCreator, client wsman.WSManClient) PolicyAppliesToMPS {
return PolicyAppliesToMPS{
base: message.NewBaseWithClient(wsmanMessageCreator, AMT_RemoteAccessPolicyAppliesToMPS, client),
client: client,
}
}

func NewRemoteAccessPolicyAppliesToMPS(wsmanMessageCreator *message.WSManMessageCreator) PolicyAppliesToMPS {
Expand All @@ -68,16 +104,50 @@ func NewRemoteAccessPolicyAppliesToMPS(wsmanMessageCreator *message.WSManMessage
}
}


// Get retrieves the representation of the instance
func (RemoteAccessPolicyAppliesToMPS PolicyAppliesToMPS) Get() string {
return RemoteAccessPolicyAppliesToMPS.base.Get(nil)
func (RemoteAccessPolicyAppliesToMPS PolicyAppliesToMPS) Get() (response Response, err error) {
response = Response{
Message: &wsman.Message{
XMLInput: RemoteAccessPolicyAppliesToMPS.base.Get(nil),
},
}
// send the message to AMT
err = RemoteAccessPolicyAppliesToMPS.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 (RemoteAccessPolicyAppliesToMPS PolicyAppliesToMPS) Enumerate() string {
return RemoteAccessPolicyAppliesToMPS.base.Enumerate()
}
func (RemoteAccessPolicyAppliesToMPS PolicyAppliesToMPS) Enumerate() (response Response, err error) {
response = Response{
Message: &wsman.Message{
XMLInput: RemoteAccessPolicyAppliesToMPS.base.Enumerate(),
},
}
// send the message to AMT
err = RemoteAccessPolicyAppliesToMPS.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
}
// Pulls instances of this class, following an Enumerate operation
func (RemoteAccessPolicyAppliesToMPS PolicyAppliesToMPS) Pull(enumerationContext string) string {
return RemoteAccessPolicyAppliesToMPS.base.Pull(enumerationContext)
Expand Down
121 changes: 77 additions & 44 deletions pkg/amt/remoteaccess/policyappliestomps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,79 +6,112 @@
package remoteaccess

import (
"encoding/xml"
"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/cim/models"
//"github.com/open-amt-cloud-toolkit/go-wsman-messages/pkg/cim/models"
"github.com/open-amt-cloud-toolkit/go-wsman-messages/pkg/wsman"
"github.com/open-amt-cloud-toolkit/go-wsman-messages/pkg/wsmantesting"
)

func TestAMT_RemoteAccessPolicyAppliesToMPS(t *testing.T) {
messageID := 0
resourceUriBase := "http://intel.com/wbem/wscim/1/amt-schema/1/"
wsmanMessageCreator := message.NewWSManMessageCreator(resourceUriBase)
elementUnderTest := NewRemoteAccessPolicyAppliesToMPS(wsmanMessageCreator)

// client := wsmantesting.MockClient{
// PackageUnderTest: "amt/general",
// }
client := wsman.NewClient("http://localhost:16992/wsman", "admin", "Intel123!", true)

elementUnderTest := NewRemoteAccessPolicyAppliesToMPSWithClient(wsmanMessageCreator, client)
t.Run("amt_* Tests", func(t *testing.T) {
tests := []struct {
name string
method string
action string
body string
extraHeader string
responseFunc func() string
responseFunc func() (Response, error)
expectedResponse interface{}
}{
//GETS
{"should create a valid AMT_RemoteAccessPolicyAppliesToMPS Get wsman message", "AMT_RemoteAccessPolicyAppliesToMPS", wsmantesting.GET, "", "", elementUnderTest.Get},
{
"should create a valid AMT_RemoteAccessPolicyAppliesToMPS Get wsman message",
"AMT_RemoteAccessPolicyAppliesToMPS",
wsmantesting.GET,
"",
"",
func() (Response, error) {
//client.CurrentMessage = "Get"
return elementUnderTest.Get()
},
Body{
XMLName: xml.Name{Space: "http://www.w3.org/2003/05/soap-envelope", Local: "Body"},

},
},
//ENUMERATES
{"should create a valid AMT_RemoteAccessPolicyAppliesToMPS Enumerate wsman message", "AMT_RemoteAccessPolicyAppliesToMPS", wsmantesting.ENUMERATE, wsmantesting.ENUMERATE_BODY, "", elementUnderTest.Enumerate},
/*{
"should create a valid AMT_RemoteAccessPolicyAppliesToMPS Enumerate wsman message",
"AMT_RemoteAccessPolicyAppliesToMPS",
wsmantesting.ENUMERATE,
wsmantesting.ENUMERATE_BODY,
"",
func() (Response, error) {
//client.CurrentMessage = "Enumerate"
return elementUnderTest.Enumerate()
},
Body{},
},*/
//PULLS
{"should create a valid AMT_RemoteAccessPolicyAppliesToMPS Pull wsman message", "AMT_RemoteAccessPolicyAppliesToMPS", wsmantesting.PULL, wsmantesting.PULL_BODY, "", func() string { return elementUnderTest.Pull(wsmantesting.EnumerationContext) }},
{"should create a valid AMT_RemoteAccessPolicyAppliesToMPS Put wsman message", "AMT_RemoteAccessPolicyAppliesToMPS", wsmantesting.PUT, `<h:AMT_RemoteAccessPolicyAppliesToMPS xmlns:h="http://intel.com/wbem/wscim/1/amt-schema/1/AMT_RemoteAccessPolicyAppliesToMPS"><h:PolicySet><h:Caption>test</h:Caption><h:Description>test</h:Description><h:ElementName>test</h:ElementName><h:CommonName>test</h:CommonName><h:PolicyKeywords>test</h:PolicyKeywords><h:PolicyDecisionStrategy>1</h:PolicyDecisionStrategy><h:PolicyRoles>test</h:PolicyRoles><h:Enabled>1</h:Enabled></h:PolicySet><h:ManagedElement><h:Caption>test</h:Caption><h:Description>test</h:Description><h:ElementName>test</h:ElementName></h:ManagedElement><h:OrderOfAccess>0</h:OrderOfAccess><h:MpsType>2</h:MpsType></h:AMT_RemoteAccessPolicyAppliesToMPS>`, "", func() string {
rapatmps := RemoteAccessPolicyAppliesToMPS{
PolicySetAppliesToElement: PolicySetAppliesToElement{
ManagedElement: models.ManagedElement{
Caption: "test",
Description: "test",
ElementName: "test",
},
PolicySet: PolicySet{
Enabled: 1,
PolicyDecisionStrategy: PolicyDecisionStrategyFirstMatching,
PolicyRoles: []string{"test"},
Policy: Policy{
ManagedElement: models.ManagedElement{
Caption: "test",
Description: "test",
ElementName: "test",
},
CommonName: "test",
PolicyKeywords: []string{"test"},
},
},
},
MPSType: BothMPS,
OrderOfAccess: 0,
}
// {"should create a valid AMT_RemoteAccessPolicyAppliesToMPS Pull wsman message", "AMT_RemoteAccessPolicyAppliesToMPS", wsmantesting.PULL, wsmantesting.PULL_BODY, "", func() string { return elementUnderTest.Pull(wsmantesting.EnumerationContext) }},
// {"should create a valid AMT_RemoteAccessPolicyAppliesToMPS Put wsman message", "AMT_RemoteAccessPolicyAppliesToMPS", wsmantesting.PUT, `<h:AMT_RemoteAccessPolicyAppliesToMPS xmlns:h="http://intel.com/wbem/wscim/1/amt-schema/1/AMT_RemoteAccessPolicyAppliesToMPS"><h:PolicySet><h:Caption>test</h:Caption><h:Description>test</h:Description><h:ElementName>test</h:ElementName><h:CommonName>test</h:CommonName><h:PolicyKeywords>test</h:PolicyKeywords><h:PolicyDecisionStrategy>1</h:PolicyDecisionStrategy><h:PolicyRoles>test</h:PolicyRoles><h:Enabled>1</h:Enabled></h:PolicySet><h:ManagedElement><h:Caption>test</h:Caption><h:Description>test</h:Description><h:ElementName>test</h:ElementName></h:ManagedElement><h:OrderOfAccess>0</h:OrderOfAccess><h:MpsType>2</h:MpsType></h:AMT_RemoteAccessPolicyAppliesToMPS>`, "", func() string {
// rapatmps := RemoteAccessPolicyAppliesToMPS{
// PolicySetAppliesToElement: PolicySetAppliesToElement{
// ManagedElement: models.ManagedElement{
// Caption: "test",
// Description: "test",
// ElementName: "test",
// },
// PolicySet: PolicySet{
// Enabled: 1,
// PolicyDecisionStrategy: PolicyDecisionStrategyFirstMatching,
// PolicyRoles: []string{"test"},
// Policy: Policy{
// ManagedElement: models.ManagedElement{
// Caption: "test",
// Description: "test",
// ElementName: "test",
// },
// CommonName: "test",
// PolicyKeywords: []string{"test"},
// },
// },
// },
// MPSType: BothMPS,
// OrderOfAccess: 0,
// }

return elementUnderTest.Put(&rapatmps)
}},
//{"should create a valid AMT_RemoteAccessPolicyAppliesToMPS Create wsman message", "AMT_RemoteAccessPolicyAppliesToMPS", wsmantesting.PULL, wsmantesting.PULL_BODY, "", func() string { return elementUnderTest.Pull(wsmantesting.EnumerationContext) }},
{"should create a valid AMT_RemoteAccessPolicyAppliesToMPS Delete wsman message", "AMT_RemoteAccessPolicyAppliesToMPS", wsmantesting.DELETE, "", "<w:SelectorSet><w:Selector Name=\"Name\">Instance</w:Selector></w:SelectorSet>", func() string {
return elementUnderTest.Delete("Instance")
}},
// return elementUnderTest.Put(&rapatmps)
// }},
// //{"should create a valid AMT_RemoteAccessPolicyAppliesToMPS Create wsman message", "AMT_RemoteAccessPolicyAppliesToMPS", wsmantesting.PULL, wsmantesting.PULL_BODY, "", func() string { return elementUnderTest.Pull(wsmantesting.EnumerationContext) }},
// {"should create a valid AMT_RemoteAccessPolicyAppliesToMPS Delete wsman message", "AMT_RemoteAccessPolicyAppliesToMPS", wsmantesting.DELETE, "", "<w:SelectorSet><w:Selector Name=\"Name\">Instance</w:Selector></w:SelectorSet>", func() string {
// return elementUnderTest.Delete("Instance")
// }},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
correctResponse := wsmantesting.ExpectedResponse(messageID, resourceUriBase, test.method, test.action, test.extraHeader, test.body)
expectedXMLInput := wsmantesting.ExpectedResponse(messageID, resourceUriBase, test.method, test.action, "", test.body)
messageID++
response := test.responseFunc()
if response != correctResponse {
assert.Equal(t, correctResponse, response)
}
response, err := test.responseFunc()
println(response.XMLOutput)
assert.NoError(t, err)
assert.Equal(t, expectedXMLInput, response.XMLInput)
assert.Equal(t, test.expectedResponse, response.Body)
})
}
})
Expand Down
83 changes: 79 additions & 4 deletions pkg/amt/remoteaccess/policyrule.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,35 @@
package remoteaccess

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/cim/models"
"github.com/open-amt-cloud-toolkit/go-wsman-messages/pkg/common"
"github.com/open-amt-cloud-toolkit/go-wsman-messages/pkg/wsman"
)

const AMT_RemoteAccessPolicyRule = "AMT_RemoteAccessPolicyRule"

type (
ResponseRule struct {
*wsman.Message
XMLName xml.Name `xml:"Envelope"`
Header message.Header `xml:"Header"`
BodyRule BodyRule `xml:"BodyRule"`
}
BodyRule struct {
XMLName xml.Name `xml:"BodyRule"`
RemotePolicyRule RemotePolicyRule `xml:"AMT_RemoteAccessPolicyRule"`

EnumerateResponse common.EnumerateResponse
}
RemotePolicyRule struct {

}
)

type RemoteAccessPolicyRule struct {
Trigger Trigger
TunnelLifeTime int
Expand All @@ -28,6 +52,22 @@ const (

type PolicyRule struct {
base message.Base
client wsman.WSManClient
}

func (w *ResponseRule) JSONRule() string {
jsonOutput, err := json.Marshal(w.BodyRule)
if err != nil {
return ""
}
return string(jsonOutput)
}

func NewPolicyRuleWithClient(wsmanMessageCreator *message.WSManMessageCreator, client wsman.WSManClient) PolicyRule {
return PolicyRule{
base: message.NewBaseWithClient(wsmanMessageCreator, AMT_RemoteAccessPolicyRule, client),
client: client,
}
}

func NewRemoteAccessPolicyRule(wsmanMessageCreator *message.WSManMessageCreator) PolicyRule {
Expand All @@ -37,15 +77,50 @@ func NewRemoteAccessPolicyRule(wsmanMessageCreator *message.WSManMessageCreator)
}

// Get retrieves the representation of the instance
func (RemoteAccessPolicyRule PolicyRule) Get() string {
return RemoteAccessPolicyRule.base.Get(nil)
func (RemoteAccessPolicyRule PolicyRule) Get() (response ResponseRule, err error) {
response = ResponseRule{
Message: &wsman.Message{
XMLInput: RemoteAccessPolicyRule.base.Get(nil),
},
}
// send the message to AMT
err = RemoteAccessPolicyRule.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 (RemoteAccessPolicyRule PolicyRule) Enumerate() string {
return RemoteAccessPolicyRule.base.Enumerate()
func (RemoteAccessPolicyRule PolicyRule) Enumerate() (response ResponseRule, err error) {
response = ResponseRule{
Message: &wsman.Message{
XMLInput: RemoteAccessPolicyRule.base.Enumerate(),
},
}
// send the message to AMT
err = RemoteAccessPolicyRule.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
}


// Pulls instances of this class, following an Enumerate operation
func (RemoteAccessPolicyRule PolicyRule) Pull(enumerationContext string) string {
return RemoteAccessPolicyRule.base.Pull(enumerationContext)
Expand Down
Loading

0 comments on commit 6fca57c

Please sign in to comment.