github.com/gophercloud/gophercloud@v1.11.0/openstack/blockstorage/extensions/schedulerhints/requests.go (about)

     1  package schedulerhints
     2  
     3  import (
     4  	"regexp"
     5  
     6  	"github.com/gophercloud/gophercloud"
     7  )
     8  
     9  // SchedulerHints represents a set of scheduling hints that are passed to the
    10  // OpenStack scheduler.
    11  type SchedulerHints struct {
    12  	// DifferentHost will place the volume on a different back-end that does not
    13  	// host the given volumes.
    14  	DifferentHost []string
    15  
    16  	// SameHost will place the volume on a back-end that hosts the given volumes.
    17  	SameHost []string
    18  
    19  	// LocalToInstance will place volume on same host on a given instance
    20  	LocalToInstance string
    21  
    22  	// Query is a conditional statement that results in back-ends able to
    23  	// host the volume.
    24  	Query string
    25  
    26  	// AdditionalProperies are arbitrary key/values that are not validated by nova.
    27  	AdditionalProperties map[string]interface{}
    28  }
    29  
    30  // VolumeCreateOptsBuilder allows extensions to add additional parameters to the
    31  // Create request.
    32  type VolumeCreateOptsBuilder interface {
    33  	ToVolumeCreateMap() (map[string]interface{}, error)
    34  }
    35  
    36  // CreateOptsBuilder builds the scheduler hints into a serializable format.
    37  type CreateOptsBuilder interface {
    38  	ToVolumeSchedulerHintsCreateMap() (map[string]interface{}, error)
    39  }
    40  
    41  // ToVolumeSchedulerHintsMap builds the scheduler hints into a serializable format.
    42  func (opts SchedulerHints) ToVolumeSchedulerHintsCreateMap() (map[string]interface{}, error) {
    43  	sh := make(map[string]interface{})
    44  
    45  	uuidRegex, _ := regexp.Compile("^[a-z0-9]{8}-[a-z0-9]{4}-[1-5][a-z0-9]{3}-[a-z0-9]{4}-[a-z0-9]{12}$")
    46  
    47  	if len(opts.DifferentHost) > 0 {
    48  		for _, diffHost := range opts.DifferentHost {
    49  			if !uuidRegex.MatchString(diffHost) {
    50  				err := gophercloud.ErrInvalidInput{}
    51  				err.Argument = "schedulerhints.SchedulerHints.DifferentHost"
    52  				err.Value = opts.DifferentHost
    53  				err.Info = "The hosts must be in UUID format."
    54  				return nil, err
    55  			}
    56  		}
    57  		sh["different_host"] = opts.DifferentHost
    58  	}
    59  
    60  	if len(opts.SameHost) > 0 {
    61  		for _, sameHost := range opts.SameHost {
    62  			if !uuidRegex.MatchString(sameHost) {
    63  				err := gophercloud.ErrInvalidInput{}
    64  				err.Argument = "schedulerhints.SchedulerHints.SameHost"
    65  				err.Value = opts.SameHost
    66  				err.Info = "The hosts must be in UUID format."
    67  				return nil, err
    68  			}
    69  		}
    70  		sh["same_host"] = opts.SameHost
    71  	}
    72  
    73  	if opts.LocalToInstance != "" {
    74  		if !uuidRegex.MatchString(opts.LocalToInstance) {
    75  			err := gophercloud.ErrInvalidInput{}
    76  			err.Argument = "schedulerhints.SchedulerHints.LocalToInstance"
    77  			err.Value = opts.LocalToInstance
    78  			err.Info = "The instance must be in UUID format."
    79  			return nil, err
    80  		}
    81  		sh["local_to_instance"] = opts.LocalToInstance
    82  	}
    83  
    84  	if opts.Query != "" {
    85  		sh["query"] = opts.Query
    86  	}
    87  
    88  	if opts.AdditionalProperties != nil {
    89  		for k, v := range opts.AdditionalProperties {
    90  			sh[k] = v
    91  		}
    92  	}
    93  
    94  	return sh, nil
    95  }
    96  
    97  // CreateOptsExt adds a SchedulerHints option to the base CreateOpts.
    98  type CreateOptsExt struct {
    99  	VolumeCreateOptsBuilder
   100  
   101  	// SchedulerHints provides a set of hints to the scheduler.
   102  	SchedulerHints CreateOptsBuilder
   103  }
   104  
   105  // ToVolumeCreateMap adds the SchedulerHints option to the base volume creation options.
   106  func (opts CreateOptsExt) ToVolumeCreateMap() (map[string]interface{}, error) {
   107  	base, err := opts.VolumeCreateOptsBuilder.ToVolumeCreateMap()
   108  	if err != nil {
   109  		return nil, err
   110  	}
   111  
   112  	schedulerHints, err := opts.SchedulerHints.ToVolumeSchedulerHintsCreateMap()
   113  	if err != nil {
   114  		return nil, err
   115  	}
   116  
   117  	if len(schedulerHints) == 0 {
   118  		return base, nil
   119  	}
   120  
   121  	base["OS-SCH-HNT:scheduler_hints"] = schedulerHints
   122  
   123  	return base, nil
   124  }