Skip to content

Commit

Permalink
fix(driver): Allow duplicate subnet names
Browse files Browse the repository at this point in the history
Duplicate subnet names are okay as long as subnets with duplicate
names exist on separate clusters. We can always filter subnets
to find the one matching the cluster.
  • Loading branch information
thunderboltsid committed Sep 26, 2024
1 parent 65e093d commit ae99550
Showing 1 changed file with 33 additions and 26 deletions.
59 changes: 33 additions & 26 deletions builder/nutanix/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func findSubnetByUUID(ctx context.Context, conn *v3.Client, uuid string) (*v3.Su
return conn.V3.GetSubnet(ctx, uuid)
}

func findSubnetByName(ctx context.Context, conn *v3.Client, name string) (*v3.SubnetIntentResponse, error) {
func findSubnetByName(ctx context.Context, conn *v3.Client, name string) ([]*v3.SubnetIntentResponse, error) {
filter := fmt.Sprintf("name==%s", name)
resp, err := conn.V3.ListAllSubnet(ctx, filter, getEmptyClientSideFilter())
if err != nil {
Expand All @@ -137,22 +137,18 @@ func findSubnetByName(ctx context.Context, conn *v3.Client, name string) (*v3.Su

entities := resp.Entities

found := make([]*v3.SubnetIntentResponse, 0)
subnets := make([]*v3.SubnetIntentResponse, 0)
for _, v := range entities {
if *v.Spec.Name == name {
found = append(found, v)
subnets = append(subnets, v)
}
}

if len(found) > 1 {
return nil, fmt.Errorf("your query returned more than one result. Please use subnet_uuid argument or use additional filters instead")
}

if len(found) == 0 {
if len(subnets) == 0 {
return nil, fmt.Errorf("subnet with the given name, not found")
}

return found[0], nil
return subnets, nil
}

func findGPUByName(ctx context.Context, conn *v3.Client, name string) (*v3.VMGpu, error) {
Expand Down Expand Up @@ -452,6 +448,19 @@ func (d *NutanixDriver) CreateRequest(ctx context.Context, vm VmConfig, state mu

state.Put("image_to_delete", imageToDelete)

cluster := &v3.ClusterIntentResponse{}
if vm.ClusterUUID != "" {
cluster, err = conn.V3.GetCluster(ctx, vm.ClusterUUID)
if err != nil {
return nil, fmt.Errorf("error while GetCluster, %s", err.Error())
}
} else {
cluster, err = findClusterByName(ctx, conn, vm.ClusterName)
if err != nil {
return nil, fmt.Errorf("error while findClusterByName, %s", err.Error())
}
}

NICList := []*v3.VMNic{}
for _, nic := range vm.VmNICs {
subnet := &v3.SubnetIntentResponse{}
Expand All @@ -461,10 +470,21 @@ func (d *NutanixDriver) CreateRequest(ctx context.Context, vm VmConfig, state mu
return nil, fmt.Errorf("error while findSubnetByUUID, %s", err.Error())
}
} else if nic.SubnetName != "" {
subnet, err = findSubnetByName(ctx, conn, nic.SubnetName)
subnets, err := findSubnetByName(ctx, conn, nic.SubnetName)
if err != nil {
return nil, fmt.Errorf("error while findSubnetByName, %s", err.Error())
}

for _, s := range subnets {
if s.Spec.ClusterReference != nil && *s.Spec.ClusterReference.UUID == *cluster.Metadata.UUID {
subnet = s
break
}
}
}

if subnet == nil {
return nil, fmt.Errorf("subnet not found")
}

isConnected := true
Expand All @@ -474,6 +494,7 @@ func (d *NutanixDriver) CreateRequest(ctx context.Context, vm VmConfig, state mu
}
NICList = append(NICList, &newNIC)
}

GPUList := make([]*v3.VMGpu, 0, len(vm.GPU))
for _, gpu := range vm.GPU {
vmGPU, err := findGPUByName(ctx, conn, gpu.Name)
Expand All @@ -483,29 +504,15 @@ func (d *NutanixDriver) CreateRequest(ctx context.Context, vm VmConfig, state mu
GPUList = append(GPUList, vmGPU)
}

PowerStateOn := "ON"

cluster := &v3.ClusterIntentResponse{}
if vm.ClusterUUID != "" {
cluster, err = conn.V3.GetCluster(ctx, vm.ClusterUUID)
if err != nil {
return nil, fmt.Errorf("error while GetCluster, %s", err.Error())
}
} else {
cluster, err = findClusterByName(ctx, conn, vm.ClusterName)
if err != nil {
return nil, fmt.Errorf("error while findClusterByName, %s", err.Error())
}
}

powerStateOn := "ON"
req := &v3.VMIntentInput{
Spec: &v3.VM{
Name: &vm.VMName,
Resources: &v3.VMResources{
GuestCustomization: guestCustomization,
NumSockets: &vm.CPU,
MemorySizeMib: &vm.MemoryMB,
PowerState: &PowerStateOn,
PowerState: &powerStateOn,
DiskList: DiskList,
NicList: NICList,
GpuList: GPUList,
Expand Down

0 comments on commit ae99550

Please sign in to comment.