Skip to content

Commit

Permalink
break up errors
Browse files Browse the repository at this point in the history
  • Loading branch information
bwagner5 committed Feb 26, 2022
1 parent 4c684f2 commit cbcd376
Showing 1 changed file with 43 additions and 18 deletions.
61 changes: 43 additions & 18 deletions pkg/cloudprovider/aws/apis/v1alpha1/provider_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,27 +176,52 @@ func (a *AWS) validateStringEnum(value, field string, validValues []string) *api
}

func (a *AWS) validateBlockDeviceMappings() (errs *apis.FieldError) {
if a.BlockDeviceMappings == nil {
return nil
}
for i, blockDeviceMapping := range a.BlockDeviceMappings {
if blockDeviceMapping.DeviceName == nil {
errs = errs.Also(apis.ErrMissingField(fmt.Sprintf("%s[%d].deviceName", blockDeviceMappingsPath, i)))
}
if blockDeviceMapping.EBS == nil {
errs = errs.Also(apis.ErrMissingField(fmt.Sprintf("%s[%d].ebs", blockDeviceMappingsPath, i)))
continue
if err := a.validateBlockDeviceMapping(blockDeviceMapping); err != nil {
errs = errs.Also(err.ViaFieldIndex(blockDeviceMappingsPath, i))
}
if blockDeviceMapping.EBS.VolumeType != nil {
if fieldErr := a.validateStringEnum(*blockDeviceMapping.EBS.VolumeType, fmt.Sprintf("%s[%d].ebs.volumeType", blockDeviceMappingsPath, i), ec2.VolumeType_Values()); fieldErr != nil {
errs = errs.Also(fieldErr)
}
}
if blockDeviceMapping.EBS.VolumeSize == nil {
errs = errs.Also(apis.ErrMissingField(fmt.Sprintf("%s[%d].ebs.volumeSize", blockDeviceMappingsPath, i)))
} else if blockDeviceMapping.EBS.VolumeSize.Cmp(minVolumeSize) == -1 || blockDeviceMapping.EBS.VolumeSize.Cmp(maxVolumeSize) == 1 {
errs = errs.Also(apis.ErrOutOfBoundsValue(blockDeviceMapping.EBS.VolumeSize.String(), minVolumeSize.String(), maxVolumeSize.String(), fmt.Sprintf("%s[%d].ebs.volumeSize", blockDeviceMappingsPath, i)))
}
return errs
}

func (a *AWS) validateBlockDeviceMapping(blockDeviceMapping *BlockDeviceMapping) (errs *apis.FieldError) {
return errs.Also(a.validateDeviceName(blockDeviceMapping), a.validateEBS(blockDeviceMapping))
}

func (a *AWS) validateDeviceName(blockDeviceMapping *BlockDeviceMapping) (err *apis.FieldError) {
if blockDeviceMapping.DeviceName == nil {
return apis.ErrMissingField("deviceName")
}
return nil
}

func (a *AWS) validateEBS(blockDeviceMapping *BlockDeviceMapping) (errs *apis.FieldError) {
if blockDeviceMapping.EBS == nil {
return apis.ErrMissingField("ebs")
}
for _, err := range []*apis.FieldError{
a.validateVolumeType(blockDeviceMapping),
a.validateVolumeSize(blockDeviceMapping),
} {
if err != nil {
errs = errs.Also(err.ViaField("ebs"))
}
}
return errs
}

func (a *AWS) validateVolumeType(blockDeviceMapping *BlockDeviceMapping) (err *apis.FieldError) {
if blockDeviceMapping.EBS.VolumeType != nil {
return a.validateStringEnum(*blockDeviceMapping.EBS.VolumeType, "volumeType", ec2.VolumeType_Values())
}
return nil
}

func (a *AWS) validateVolumeSize(blockDeviceMapping *BlockDeviceMapping) (err *apis.FieldError) {
if blockDeviceMapping.EBS.VolumeSize == nil {
return apis.ErrMissingField("volumeSize")
} else if blockDeviceMapping.EBS.VolumeSize.Cmp(minVolumeSize) == -1 || blockDeviceMapping.EBS.VolumeSize.Cmp(maxVolumeSize) == 1 {
return apis.ErrOutOfBoundsValue(blockDeviceMapping.EBS.VolumeSize.String(), minVolumeSize.String(), maxVolumeSize.String(), "volumeSize")
}
return nil
}

0 comments on commit cbcd376

Please sign in to comment.