diff --git a/adapters/secondary/controller_REST/controller.go b/adapters/secondary/controller_REST/controller.go index 4c848da..cfa9238 100644 --- a/adapters/secondary/controller_REST/controller.go +++ b/adapters/secondary/controller_REST/controller.go @@ -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" @@ -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, diff --git a/adapters/secondary/controller_REST/controller_test.go b/adapters/secondary/controller_REST/controller_test.go index 8286046..fc7c0ea 100644 --- a/adapters/secondary/controller_REST/controller_test.go +++ b/adapters/secondary/controller_REST/controller_test.go @@ -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() {