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

fix(driver): Allow duplicate subnet names #198

Merged
merged 1 commit into from
Sep 26, 2024
Merged
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
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
Loading