示例#1
0
// ToServerCreateMap constructs a request body using all of the OpenStack extensions that are
// active on Rackspace.
func (opts CreateOpts) ToServerCreateMap() (map[string]interface{}, error) {
	base := os.CreateOpts{
		Name:             opts.Name,
		ImageRef:         opts.ImageRef,
		ImageName:        opts.ImageName,
		FlavorRef:        opts.FlavorRef,
		FlavorName:       opts.FlavorName,
		SecurityGroups:   opts.SecurityGroups,
		UserData:         opts.UserData,
		AvailabilityZone: opts.AvailabilityZone,
		Networks:         opts.Networks,
		Metadata:         opts.Metadata,
		Personality:      opts.Personality,
		ConfigDrive:      opts.ConfigDrive,
		AdminPass:        opts.AdminPass,
	}

	drive := diskconfig.CreateOptsExt{
		CreateOptsBuilder: base,
		DiskConfig:        opts.DiskConfig,
	}

	res, err := drive.ToServerCreateMap()
	if err != nil {
		return nil, err
	}

	if len(opts.BlockDevice) != 0 {
		bfv := bootfromvolume.CreateOptsExt{
			CreateOptsBuilder: drive,
			BlockDevice:       opts.BlockDevice,
		}

		res, err = bfv.ToServerCreateMap()
		if err != nil {
			return nil, err
		}
	}

	// key_name doesn't actually come from the extension (or at least isn't documented there) so
	// we need to add it manually.
	serverMap := res["server"].(map[string]interface{})
	if opts.KeyPair != "" {
		serverMap["key_name"] = opts.KeyPair
	}

	return res, nil
}
示例#2
0
func TestCreateOpts(t *testing.T) {
	base := servers.CreateOpts{
		Name:      "createdserver",
		ImageRef:  "asdfasdfasdf",
		FlavorRef: "performance1-1",
	}

	ext := osBFV.CreateOptsExt{
		CreateOptsBuilder: base,
		BlockDevice: []osBFV.BlockDevice{
			osBFV.BlockDevice{
				UUID:            "123456",
				SourceType:      osBFV.Image,
				DestinationType: "volume",
				VolumeSize:      10,
			},
		},
	}

	expected := `
    {
      "server": {
        "name": "createdserver",
        "imageRef": "asdfasdfasdf",
        "flavorRef": "performance1-1",
	"flavorName": "",
	"imageName": "",
        "block_device_mapping_v2":[
          {
            "uuid":"123456",
            "source_type":"image",
            "destination_type":"volume",
            "boot_index": "0",
            "delete_on_termination": "false",
            "volume_size": "10"
          }
        ]
      }
    }
  `
	actual, err := ext.ToServerCreateMap()
	th.AssertNoErr(t, err)
	th.CheckJSONEquals(t, expected, actual)
}