// ToLBMonitorUpdateMap allows UpdateOpts to satisfy the UpdateOptsBuilder // interface func (opts UpdateOpts) ToLBMonitorUpdateMap() (map[string]interface{}, error) { if opts.Delay > 0 && opts.Timeout > 0 && opts.Delay < opts.Timeout { err := gophercloud.ErrInvalidInput{} err.Argument = "monitors.CreateOpts.Delay/monitors.CreateOpts.Timeout" err.Value = fmt.Sprintf("%d/%d", opts.Delay, opts.Timeout) err.Info = "Delay must be greater than or equal to timeout" return nil, err } return gophercloud.BuildRequestBody(opts, "health_monitor") }
// ToMap is a convenience function for creating sub-maps for individual users. func (opts CreateOpts) ToMap() (map[string]interface{}, error) { if opts.Name == "root" { err := gophercloud.ErrInvalidInput{} err.Argument = "users.CreateOpts.Name" err.Value = "root" err.Info = "root is a reserved user name and cannot be used" return nil, err } return gophercloud.BuildRequestBody(opts, "") }
// ToMap is a helper function to convert individual DB create opt structures // into sub-maps. func (opts CreateOpts) ToMap() (map[string]interface{}, error) { if len(opts.Name) > 64 { err := gophercloud.ErrInvalidInput{} err.Argument = "databases.CreateOpts.Name" err.Value = opts.Name err.Info = "Must be less than 64 chars long" return nil, err } return gophercloud.BuildRequestBody(opts, "") }
// ToInstanceCreateMap will render a JSON map. func (opts CreateOpts) ToInstanceCreateMap() (map[string]interface{}, error) { if opts.Size > 300 || opts.Size < 1 { err := gophercloud.ErrInvalidInput{} err.Argument = "instances.CreateOpts.Size" err.Value = opts.Size err.Info = "Size (GB) must be between 1-300" return nil, err } if opts.FlavorRef == "" { return nil, gophercloud.ErrMissingInput{Argument: "instances.CreateOpts.FlavorRef"} } instance := map[string]interface{}{ "volume": map[string]int{"size": opts.Size}, "flavorRef": opts.FlavorRef, } if opts.Name != "" { instance["name"] = opts.Name } if opts.Databases != nil { dbs, err := opts.Databases.ToDBCreateMap() if err != nil { return nil, err } instance["databases"] = dbs["databases"] } if opts.Users != nil { users, err := opts.Users.ToUserCreateMap() if err != nil { return nil, err } instance["users"] = users["users"] } if opts.Datastore != nil { datastore, err := opts.Datastore.ToMap() if err != nil { return nil, err } instance["datastore"] = datastore } return map[string]interface{}{"instance": instance}, nil }
// ToServerSchedulerHintsMap builds the scheduler hints into a serializable format. func (opts SchedulerHints) ToServerSchedulerHintsCreateMap() (map[string]interface{}, error) { sh := make(map[string]interface{}) 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}$") if opts.Group != "" { if !uuidRegex.MatchString(opts.Group) { err := gophercloud.ErrInvalidInput{} err.Argument = "schedulerhints.SchedulerHints.Group" err.Value = opts.Group err.Info = "Group must be a UUID" return nil, err } sh["group"] = opts.Group } if len(opts.DifferentHost) > 0 { for _, diffHost := range opts.DifferentHost { if !uuidRegex.MatchString(diffHost) { err := gophercloud.ErrInvalidInput{} err.Argument = "schedulerhints.SchedulerHints.DifferentHost" err.Value = opts.DifferentHost err.Info = "The hosts must be in UUID format." return nil, err } } sh["different_host"] = opts.DifferentHost } if len(opts.SameHost) > 0 { for _, sameHost := range opts.SameHost { if !uuidRegex.MatchString(sameHost) { err := gophercloud.ErrInvalidInput{} err.Argument = "schedulerhints.SchedulerHints.SameHost" err.Value = opts.SameHost err.Info = "The hosts must be in UUID format." return nil, err } } sh["same_host"] = opts.SameHost } /* Query can be something simple like: [">=", "$free_ram_mb", 1024] Or more complex like: ['and', ['>=', '$free_ram_mb', 1024], ['>=', '$free_disk_mb', 200 * 1024] ] Because of the possible complexity, just make sure the length is a minimum of 3. */ if len(opts.Query) > 0 { if len(opts.Query) < 3 { err := gophercloud.ErrInvalidInput{} err.Argument = "schedulerhints.SchedulerHints.Query" err.Value = opts.Query err.Info = "Must be a conditional statement in the format of [op,variable,value]" return nil, err } sh["query"] = opts.Query } if opts.TargetCell != "" { sh["target_cell"] = opts.TargetCell } if opts.BuildNearHostIP != "" { if _, _, err := net.ParseCIDR(opts.BuildNearHostIP); err != nil { err := gophercloud.ErrInvalidInput{} err.Argument = "schedulerhints.SchedulerHints.BuildNearHostIP" err.Value = opts.BuildNearHostIP err.Info = "Must be a valid subnet in the form 192.168.1.1/24" return nil, err } ipParts := strings.Split(opts.BuildNearHostIP, "/") sh["build_near_host_ip"] = ipParts[0] sh["cidr"] = "/" + ipParts[1] } return sh, nil }