Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Associate existing subnet GUIDs when configuring director networks #559

Closed
Closed
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
23 changes: 22 additions & 1 deletion api/director_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,15 @@ type Networks struct {
}

type Network struct {
GUID string `yaml:"guid,omitempty"`
Name string `yaml:"name"`
Subnets []Subnet `yaml:"subnets,omitempty"`
Fields map[string]interface{} `yaml:",inline"`
}

type Subnet struct {
GUID string `yaml:"guid,omitempty"`
Name string `yaml:"name"`
CIDR string `yaml:"cidr"`
Fields map[string]interface{} `yaml:",inline"`
}

Expand Down Expand Up @@ -254,6 +261,7 @@ func (a Api) addGUIDToExistingNetworks(networks Networks) (Networks, error) {
for _, existingNetwork := range existingNetworks.Networks {
if network.Name == existingNetwork.Name {
network.GUID = existingNetwork.GUID
network.Subnets = a.addGUIDToExistingSubnet(network, existingNetwork)
break
}
}
Expand All @@ -262,6 +270,19 @@ func (a Api) addGUIDToExistingNetworks(networks Networks) (Networks, error) {
return networks, nil
}

func (a Api) addGUIDToExistingSubnet(network *Network, existingNetwork *Network) []Subnet {
for k, subnet := range network.Subnets {
for _, existingSubnet := range existingNetwork.Subnets {
if subnet.CIDR == existingSubnet.CIDR {
Copy link
Author

Choose a reason for hiding this comment

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

Let me know if the comparison should be made on more fields. In theory it does not really matter since the other field would be updated.

network.Subnets[k].GUID = existingSubnet.GUID
break
}
}
}

return network.Subnets
}

type IAASConfigurationsInput json.RawMessage

type IAASConfigurationsAPIPayload struct {
Expand Down
46 changes: 45 additions & 1 deletion api/director_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,8 @@ var _ = Describe("Director", func() {
ghttp.RespondWith(http.StatusOK, `{
"networks": [{
"guid": "existing-network-guid",
"name": "existing-network"
"name": "existing-network",
"subnets": [{"guid": "existing-subnet-guid", "cidr":"10.0.0.0/24"}]
}]
}`),
),
Expand Down Expand Up @@ -533,6 +534,49 @@ var _ = Describe("Director", func() {
})
Expect(err).ToNot(HaveOccurred())
})

It("configures subnet and associates existing guids", func() {
server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("PUT", "/api/v0/staged/director/networks"),
ghttp.VerifyJSON(`{
"icmp_checks_enabled":false,
"networks": [{
"name": "existing-network",
"guid": "existing-network-guid",
"subnets": [{"guid": "existing-subnet-guid", "cidr":"10.0.0.0/24"}]
}]
}`),
),
)

err := service.UpdateStagedDirectorNetworks(api.NetworkInput{
Networks: json.RawMessage(`{"icmp_checks_enabled":false, "networks": [{"name":"existing-network", "subnets": [{"guid": "existing-subnet-guid", "cidr":"10.0.0.0/24"}]}]}`),
})
Expect(err).ToNot(HaveOccurred())

})

It("configures subnets and associates existing guids and no guid for new subnet", func() {
server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("PUT", "/api/v0/staged/director/networks"),
ghttp.VerifyJSON(`{
"icmp_checks_enabled":false,
"networks": [{
"name": "existing-network",
"guid": "existing-network-guid",
"subnets": [{"guid": "existing-subnet-guid", "cidr":"10.0.0.0/24"}, {"cidr": "172.16.0.0/12"}]
}]
}`),
),
)

err := service.UpdateStagedDirectorNetworks(api.NetworkInput{
Networks: json.RawMessage(`{"icmp_checks_enabled":false, "networks": [{"name":"existing-network","subnets":[{"guid":"existing-subnet-guid","cidr":"10.0.0.0/24"},{"cidr":"172.16.0.0/12"}]}]}`),
})
Expect(err).ToNot(HaveOccurred())
})
})

Context("failure cases", func() {
Expand Down