Skip to content

Commit

Permalink
Implement CreateNetworkWithSubnet
Browse files Browse the repository at this point in the history
This method of Controller interface is being used a lot in
driver_test.go.

Change-Id: I594b5e60686c83be1960620e3e4af4d9ebaa174b
Partial-Bug: #1778671
  • Loading branch information
Michal Kostrzewa committed Jul 4, 2018
1 parent a61735f commit a6e2495
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
35 changes: 32 additions & 3 deletions adapters/secondary/controller_REST/controller.go
Expand Up @@ -21,7 +21,8 @@ import (
"regexp"
"strings"

"github.com/Juniper/contrail-go-api"
contrail "github.com/Juniper/contrail-go-api"
"github.com/Juniper/contrail-go-api/config"
"github.com/Juniper/contrail-go-api/types"
"github.com/Juniper/contrail-windows-docker-driver/common"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -49,8 +50,36 @@ func (c *ControllerAdapter) NewProject(domain, tenant string) (*types.Project, e
}

func (c *ControllerAdapter) CreateNetworkWithSubnet(tenantName, networkName, subnetCIDR string) (*types.VirtualNetwork, error) {
// TODO: possibly implement when fixing driver tests
return nil, errors.New("Not implemented - possibly TODO when fixing driver module tests.")
net, err := c.GetNetwork(tenantName, networkName)
if err == nil {
showDetails := true
details, err := config.NetworkShow(c.ApiClient, net.GetUuid(), showDetails)
if err != nil {
return nil, err
}
for _, existingCIDR := range details.Subnets {
if subnetCIDR == existingCIDR {
return net, nil
}
}
return nil, errors.New("such network already has a different subnet")
}
projectFQName := fmt.Sprintf("%s:%s", common.DomainName, tenantName)
project, err := c.ApiClient.FindByName("project", projectFQName)
if err != nil {
return nil, err
}

netUUID, err := config.CreateNetworkWithSubnet(c.ApiClient, project.GetUuid(), networkName, subnetCIDR)
if err != nil {
return nil, err
}

net, err = types.VirtualNetworkByUuid(c.ApiClient, netUUID)
if err != nil {
return nil, err
}
return net, nil
}

func (c *ControllerAdapter) GetNetwork(tenantName, networkName string) (*types.VirtualNetwork,
Expand Down
38 changes: 38 additions & 0 deletions adapters/secondary/controller_REST/controller_test.go
Expand Up @@ -159,6 +159,44 @@ var _ = Describe("ControllerAdapter", func() {
})
})

Describe("creating Contrail network with subnet", func() {
It("when network with the same subnet already exists in Contrail, returns it", func() {
testNetwork := CreateTestNetworkWithSubnet(client.ApiClient, networkName, subnetCIDR, project)

net, err := client.CreateNetworkWithSubnet(tenantName, networkName, subnetCIDR)

Expect(err).ToNot(HaveOccurred())
Expect(net).ToNot(BeNil())
Expect(net.GetNetworkIpamRefs()).To(HaveLen(1))
Expect(net.GetUuid()).To(Equal(testNetwork.GetUuid()))
})
It("when network doesn't exist in Contrail, it creates and returns it", func() {
net, err := client.CreateNetworkWithSubnet(tenantName, networkName, subnetCIDR)

Expect(err).ToNot(HaveOccurred())
Expect(net).ToNot(BeNil())
Expect(net.GetNetworkIpamRefs()).To(HaveLen(1))
})
It("when network with a different subnet already exists in Contrail, returns error", func() {
otherCIDR := "5.6.7.8/24"
Expect(subnetCIDR).ToNot(Equal(otherCIDR)) // sanity check
CreateTestNetworkWithSubnet(client.ApiClient, networkName, otherCIDR, project)

net, err := client.CreateNetworkWithSubnet(tenantName, networkName, subnetCIDR)

Expect(err).To(HaveOccurred())
Expect(net).To(BeNil())
})
It("when network without a subnet already exists in Contrail, returns error", func() {
CreateTestNetwork(client.ApiClient, networkName, project)

net, err := client.CreateNetworkWithSubnet(tenantName, networkName, subnetCIDR)

Expect(err).To(HaveOccurred())
Expect(net).To(BeNil())
})
})

Describe("getting Contrail subnet info", func() {
assertGettingSubnetFails := func(getTestedNet func() *types.VirtualNetwork,
CIDR string) func() {
Expand Down

0 comments on commit a6e2495

Please sign in to comment.