Skip to content

Commit c03642f

Browse files
authored
Validate Add Zone Paramters (#207)
1 parent d5b689e commit c03642f

File tree

2 files changed

+136
-3
lines changed

2 files changed

+136
-3
lines changed

restapi/admin_tenants.go

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package restapi
1919
import (
2020
"context"
2121
"encoding/json"
22+
"errors"
2223
"fmt"
2324
"log"
2425
"net"
@@ -192,7 +193,7 @@ func getTenantInfo(tenant *operator.Tenant, tenantInfo *usageInfo) *models.Tenan
192193
zoneModel := &models.Zone{
193194
Name: z.Name,
194195
Servers: swag.Int64(int64(z.Servers)),
195-
VolumesPerServer: &z.VolumesPerServer,
196+
VolumesPerServer: swag.Int32(z.VolumesPerServer),
196197
VolumeConfiguration: &models.ZoneVolumeConfiguration{},
197198
}
198199

@@ -600,9 +601,59 @@ func addTenantZone(ctx context.Context, operatorClient OperatorClient, params ad
600601
return err
601602
}
602603

604+
zoneParams := params.Body
605+
if zoneParams.VolumeConfiguration == nil {
606+
return errors.New("a volume configuration must be specified")
607+
}
608+
609+
if zoneParams.VolumeConfiguration.Size == nil || *zoneParams.VolumeConfiguration.Size <= int64(0) {
610+
return errors.New("volume size must be greater than 0")
611+
}
612+
613+
if zoneParams.Servers == nil || *zoneParams.Servers <= 0 {
614+
return errors.New("number of servers must be greater than 0")
615+
}
616+
617+
if zoneParams.VolumesPerServer == nil || *zoneParams.VolumesPerServer <= 0 {
618+
return errors.New("number of volumes per server must be greater than 0")
619+
}
620+
621+
volumeSize := resource.NewQuantity(*zoneParams.VolumeConfiguration.Size, resource.DecimalExponent)
622+
volTemp := corev1.PersistentVolumeClaimSpec{
623+
AccessModes: []corev1.PersistentVolumeAccessMode{
624+
corev1.ReadWriteOnce,
625+
},
626+
Resources: corev1.ResourceRequirements{
627+
Requests: corev1.ResourceList{
628+
corev1.ResourceStorage: *volumeSize,
629+
},
630+
},
631+
}
632+
if zoneParams.VolumeConfiguration.StorageClassName != "" {
633+
volTemp.StorageClassName = &zoneParams.VolumeConfiguration.StorageClassName
634+
}
635+
636+
// TODO: Calculate this ourselves?
637+
memorySize, err := resource.ParseQuantity(getTenantMemorySize())
638+
if err != nil {
639+
return err
640+
}
641+
603642
minInst.Spec.Zones = append(minInst.Spec.Zones, operator.Zone{
604-
Name: params.Body.Name,
605-
Servers: int32(*params.Body.Servers),
643+
Name: zoneParams.Name,
644+
Servers: int32(*zoneParams.Servers),
645+
VolumesPerServer: *zoneParams.VolumesPerServer,
646+
VolumeClaimTemplate: &corev1.PersistentVolumeClaim{
647+
ObjectMeta: metav1.ObjectMeta{
648+
Name: "data",
649+
},
650+
Spec: volTemp,
651+
},
652+
Resources: corev1.ResourceRequirements{
653+
Requests: corev1.ResourceList{
654+
corev1.ResourceMemory: memorySize,
655+
},
656+
},
606657
})
607658

608659
payloadBytes, err := json.Marshal(minInst)

restapi/admin_tenants_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,10 +415,92 @@ func Test_TenantAddZone(t *testing.T) {
415415
Body: &models.Zone{
416416
Name: "zone-1",
417417
Servers: swag.Int64(int64(4)),
418+
VolumeConfiguration: &models.ZoneVolumeConfiguration{
419+
Size: swag.Int64(2147483648),
420+
StorageClassName: "standard",
421+
},
422+
VolumesPerServer: swag.Int32(4),
418423
},
419424
},
420425
},
421426
wantErr: false,
427+
}, {
428+
name: "Add zone, error size",
429+
args: args{
430+
ctx: context.Background(),
431+
operatorClient: opClient,
432+
nameSpace: "default",
433+
mockTenantPatch: func(ctx context.Context, namespace string, tenantName string, pt types.PatchType, data []byte, options metav1.PatchOptions) (*v1.Tenant, error) {
434+
return &v1.Tenant{}, nil
435+
},
436+
mockTenantGet: func(ctx context.Context, namespace string, tenantName string, options metav1.GetOptions) (*v1.Tenant, error) {
437+
return &v1.Tenant{}, nil
438+
},
439+
params: admin_api.TenantAddZoneParams{
440+
Body: &models.Zone{
441+
Name: "zone-1",
442+
Servers: swag.Int64(int64(4)),
443+
VolumeConfiguration: &models.ZoneVolumeConfiguration{
444+
Size: swag.Int64(0),
445+
StorageClassName: "standard",
446+
},
447+
VolumesPerServer: swag.Int32(4),
448+
},
449+
},
450+
},
451+
wantErr: true,
452+
},
453+
{
454+
name: "Add zone, error servers negative",
455+
args: args{
456+
ctx: context.Background(),
457+
operatorClient: opClient,
458+
nameSpace: "default",
459+
mockTenantPatch: func(ctx context.Context, namespace string, tenantName string, pt types.PatchType, data []byte, options metav1.PatchOptions) (*v1.Tenant, error) {
460+
return &v1.Tenant{}, nil
461+
},
462+
mockTenantGet: func(ctx context.Context, namespace string, tenantName string, options metav1.GetOptions) (*v1.Tenant, error) {
463+
return &v1.Tenant{}, nil
464+
},
465+
params: admin_api.TenantAddZoneParams{
466+
Body: &models.Zone{
467+
Name: "zone-1",
468+
Servers: swag.Int64(int64(-1)),
469+
VolumeConfiguration: &models.ZoneVolumeConfiguration{
470+
Size: swag.Int64(2147483648),
471+
StorageClassName: "standard",
472+
},
473+
VolumesPerServer: swag.Int32(4),
474+
},
475+
},
476+
},
477+
wantErr: true,
478+
},
479+
{
480+
name: "Add zone, error volumes per server negative",
481+
args: args{
482+
ctx: context.Background(),
483+
operatorClient: opClient,
484+
nameSpace: "default",
485+
mockTenantPatch: func(ctx context.Context, namespace string, tenantName string, pt types.PatchType, data []byte, options metav1.PatchOptions) (*v1.Tenant, error) {
486+
return &v1.Tenant{}, nil
487+
},
488+
mockTenantGet: func(ctx context.Context, namespace string, tenantName string, options metav1.GetOptions) (*v1.Tenant, error) {
489+
return &v1.Tenant{}, nil
490+
},
491+
params: admin_api.TenantAddZoneParams{
492+
Body: &models.Zone{
493+
Name: "zone-1",
494+
Servers: swag.Int64(int64(4)),
495+
VolumeConfiguration: &models.ZoneVolumeConfiguration{
496+
Size: swag.Int64(2147483648),
497+
StorageClassName: "standard",
498+
},
499+
VolumesPerServer: swag.Int32(-1),
500+
},
501+
},
502+
},
503+
wantErr: true,
422504
},
423505
{
424506
name: "Error on patch, handle error",

0 commit comments

Comments
 (0)