Skip to content
Merged
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
15 changes: 15 additions & 0 deletions tbclient/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,3 +391,18 @@ type IpNatRule struct {
Target IpNatTarget `json:"target"`
Topology *Topology `json:"topology"`
}

// VM NAT Rule

type VmNatTarget struct {
IpAddress string `json:"ipAddress,omitempty"`
Name string `json:"name,omitempty"`
VmNic *VmNic `json:"targetItem"`
}

type VmNatRule struct {
Uid string `json:"uid,omitempty"`
EastWest bool `json:"eastWest"`
Target VmNatTarget `json:"target"`
Topology *Topology `json:"topology"`
}
90 changes: 86 additions & 4 deletions tbclient/natRule.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ type natRule struct {
Topology *Topology `json:"topology"`
EastWest bool `json:"eastWest"`
Target struct {
Type string `json:"type"`
TargetItem interface{} `json:"targetItem"`
Name string `json:"name"`
IpAddress string `json:"ipAddress"`
Type string `json:"type"`
VmNic *VmNic `json:"targetItem"`
Name string `json:"name"`
IpAddress string `json:"ipAddress"`
} `json:"target"`
}

Expand Down Expand Up @@ -112,3 +112,85 @@ func (c *Client) DeleteIpNatRule(uid string) error {

return executeDelete(c.createRestClient(), c.Token, url)
}

func (c *Client) GetAllVmNatRules(topologyUid string) ([]VmNatRule, error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was not possible to create a generic service for this due to the real instantiation of either VM Nat Rule or IP Nat Rule, which can't be implemented in generic code in Go


url := fmt.Sprintf("%s/topologies/%s/nat-rules", c.HostURL, topologyUid)

resp, err := executeGet(c.createRestClient(), c.Token, url, collection[natRuleCollection]{})
if err != nil {
return nil, err
}

natRules := resp.Result().(*collection[natRuleCollection]).Embedded.getData()
vmNatRules := make([]VmNatRule, 0)

for _, natRule := range natRules {
if natRule.Target.Type == "VM_NIC" {
vmNatRule := VmNatRule{
Uid: natRule.Uid,
EastWest: natRule.EastWest,
Target: VmNatTarget{
VmNic: natRule.Target.VmNic,
IpAddress: natRule.Target.IpAddress,
Name: natRule.Target.Name,
},
Topology: natRule.Topology,
}
vmNatRules = append(vmNatRules, vmNatRule)
}
}

return vmNatRules, nil
}

func (c *Client) GetVmNatRule(uid string) (*VmNatRule, error) {

url := fmt.Sprintf("%s/nat-rules/%s", c.HostURL, uid)

resp, err := executeGet(c.createRestClient(), c.Token, url, natRule{})
if err != nil {
return nil, err
}

natRule := resp.Result().(natRule)
if natRule.Target.Type != "VM_NIC" {
return nil, errors.New("VM NAT Rule Not Found")
}

return &VmNatRule{
Uid: natRule.Uid,
EastWest: natRule.EastWest,
Target: VmNatTarget{
VmNic: natRule.Target.VmNic,
IpAddress: natRule.Target.IpAddress,
Name: natRule.Target.Name,
},
Topology: natRule.Topology,
}, nil
}

func (c *Client) CreateVmNatRule(vmNatRule VmNatRule) (*VmNatRule, error) {

rest := c.createRestClient()
url := fmt.Sprintf("%s/vm-nic-nat-rules", c.HostURL)

resp, err := executePost(rest, c.Token, url, vmNatRule, VmNatRule{})
if err != nil {
return nil, err
}

return resp.Result().(*VmNatRule), nil
}

func (c *Client) DeleteVmNatRule(uid string) error {

_, err := c.GetVmNatRule(uid)
if err != nil {
return err
}

url := fmt.Sprintf("%s/nat-rules/%s", c.HostURL, uid)

return executeDelete(c.createRestClient(), c.Token, url)
}
45 changes: 45 additions & 0 deletions tbclient/tbclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,17 @@ var ipNatRule = IpNatRule{
Topology: &Topology{Uid: lonTopology.Uid},
}

var vmNatRule = VmNatRule{
Uid: "lonvmnatrule1",
EastWest: false,
Target: VmNatTarget{
VmNic: &VmNic{Uid: "lonvm1natnic"},
IpAddress: "198.18.131.201",
Name: "Mail Server 1",
},
Topology: &Topology{Uid: lonTopology.Uid},
}

func (suite *ContractTestSuite) SetupSuite() {
suite.docker = startWiremock(suite)
suite.tbClient = createTbClient(suite)
Expand Down Expand Up @@ -991,6 +1002,40 @@ func (suite *ContractTestSuite) TestCreateIpNatRule() {

// ToDo "Delete" Tests when Contracts are updated

// VM Nat Rule

func (suite *ContractTestSuite) TestGetAllVmNatRules() {

// When
vmNatRules, err := suite.tbClient.GetAllVmNatRules(lonTopology.Uid)
suite.handleError(err)

// Then
suite.Equal(1, len(vmNatRules))
suite.Contains(vmNatRules, vmNatRule)
}

// TODO - "Get One" Tests when Contracts are updated

func (suite *ContractTestSuite) TestCreateVmNatRule() {

// Given
expectedVmNatRule := vmNatRule

// When
actualVmNatRule, err := suite.tbClient.CreateVmNatRule(expectedVmNatRule)
suite.handleError(err)

// Then
// Use Contract Values
expectedVmNatRule.Uid = "newvmnat"
expectedVmNatRule.Target.Name = "Collab DB"
expectedVmNatRule.Target.IpAddress = "198.18.133.111"
suite.Equal(expectedVmNatRule, *actualVmNatRule)
}

// ToDo "Delete" Tests when Contracts are updated to include "Get One"

// Inventory Tests

func (suite *ContractTestSuite) TestGetAllOsFamilies() {
Expand Down