From 374176c6ce0b8bfeaba1ee25a1841099f5201072 Mon Sep 17 00:00:00 2001 From: gman Date: Fri, 13 Apr 2018 14:21:15 +0200 Subject: [PATCH] cephfs/volumeoptions: added fields; validate values added Pool and ProvisionVolume fields Mounter is now optional --- pkg/cephfs/volumeoptions.go | 76 +++++++++++++++++++++++++++++++------ 1 file changed, 65 insertions(+), 11 deletions(-) diff --git a/pkg/cephfs/volumeoptions.go b/pkg/cephfs/volumeoptions.go index 249e56afa..96ca68707 100644 --- a/pkg/cephfs/volumeoptions.go +++ b/pkg/cephfs/volumeoptions.go @@ -19,18 +19,59 @@ package cephfs import ( "errors" "fmt" + "strconv" ) type volumeOptions struct { Monitors string `json:"monitors"` + Pool string `json:"pool"` RootPath string `json:"rootPath"` - User string `json:"user"` - Mounter string `json:"mounter"` + + Mounter string `json:"mounter"` + ProvisionVolume bool `json:"provisionVolume"` +} + +func validateNonEmptyField(field, fieldName string) error { + if field == "" { + return fmt.Errorf("Parameter '%s' cannot be empty", fieldName) + } + + return nil +} + +func (o *volumeOptions) validate() error { + if err := validateNonEmptyField(o.Monitors, "monitors"); err != nil { + return err + } + + if err := validateNonEmptyField(o.RootPath, "rootPath"); err != nil { + if !o.ProvisionVolume { + return err + } + } else { + if o.ProvisionVolume { + return fmt.Errorf("Non-empty field rootPath is in conflict with provisionVolume=true") + } + } + + if o.ProvisionVolume { + if err := validateNonEmptyField(o.Pool, "pool"); err != nil { + return err + } + } + + if o.Mounter != "" { + if err := validateMounter(o.Mounter); err != nil { + return err + } + } + + return nil } func extractOption(dest *string, optionLabel string, options map[string]string) error { if opt, ok := options[optionLabel]; !ok { - return errors.New("Missing required parameter " + optionLabel) + return errors.New("Missing required field " + optionLabel) } else { *dest = opt return nil @@ -49,25 +90,38 @@ func validateMounter(m string) error { } func newVolumeOptions(volOptions map[string]string) (*volumeOptions, error) { - var opts volumeOptions + var ( + opts volumeOptions + provisionVolumeBool string + err error + ) - if err := extractOption(&opts.Monitors, "monitors", volOptions); err != nil { + if err = extractOption(&opts.Monitors, "monitors", volOptions); err != nil { return nil, err } - if err := extractOption(&opts.RootPath, "rootPath", volOptions); err != nil { + if err = extractOption(&provisionVolumeBool, "provisionVolume", volOptions); err != nil { return nil, err } - if err := extractOption(&opts.User, "user", volOptions); err != nil { - return nil, err + if opts.ProvisionVolume, err = strconv.ParseBool(provisionVolumeBool); err != nil { + return nil, fmt.Errorf("Failed to parse provisionVolume: %v", err) } - if err := extractOption(&opts.Mounter, "mounter", volOptions); err != nil { - return nil, err + if opts.ProvisionVolume { + if err = extractOption(&opts.Pool, "pool", volOptions); err != nil { + return nil, err + } + } else { + if err = extractOption(&opts.RootPath, "rootPath", volOptions); err != nil { + return nil, err + } } - if err := validateMounter(opts.Mounter); err != nil { + // This field is optional, don't check for its presence + extractOption(&opts.Mounter, "mounter", volOptions) + + if err = opts.validate(); err != nil { return nil, err }