本文整理汇总了Python中profitbricks.client.ProfitBricksService.attach_volume方法的典型用法代码示例。如果您正苦于以下问题:Python ProfitBricksService.attach_volume方法的具体用法?Python ProfitBricksService.attach_volume怎么用?Python ProfitBricksService.attach_volume使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类profitbricks.client.ProfitBricksService
的用法示例。
在下文中一共展示了ProfitBricksService.attach_volume方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestServer
# 需要导入模块: from profitbricks.client import ProfitBricksService [as 别名]
# 或者: from profitbricks.client.ProfitBricksService import attach_volume [as 别名]
class TestServer(unittest.TestCase):
@classmethod
def setUpClass(self):
self.resource = resource()
self.client = ProfitBricksService(
username=configuration.USERNAME,
password=configuration.PASSWORD,
headers=configuration.HEADERS)
# Create test datacenter.
self.datacenter = self.client.create_datacenter(
datacenter=Datacenter(**self.resource['datacenter']))
wait_for_completion(self.client, self.datacenter, 'create_datacenter')
# Create test volume1.
self.volume1 = self.client.create_volume(
datacenter_id=self.datacenter['id'],
volume=Volume(**self.resource['volume']))
wait_for_completion(self.client, self.volume1, 'create_volume')
# Create test volume2 (attach volume test).
self.volume2 = self.client.create_volume(
datacenter_id=self.datacenter['id'],
volume=Volume(**self.resource['volume']))
wait_for_completion(self.client, self.volume2, 'create_volume')
# Create test server.
server = Server(**self.resource['server'])
server.attach_volumes = [self.volume1['id']]
self.server = self.client.create_server(
datacenter_id=self.datacenter['id'],
server=server)
wait_for_completion(self.client, self.server, 'create_server')
# Create test NIC.
self.nic = self.client.create_nic(
datacenter_id=self.datacenter['id'],
server_id=self.server['id'],
nic=NIC(**self.resource['nic']))
wait_for_completion(self.client, self.nic, 'create_nic')
# Find an Ubuntu image for testing.
for item in self.client.list_images()['items']:
if 'Ubuntu-15' in item['properties']['name'] and item['properties']['location'] == configuration.LOCATION:
self.image = item
@classmethod
def tearDownClass(self):
self.client.delete_datacenter(datacenter_id=self.datacenter['id'])
def test_list(self):
servers = self.client.list_servers(datacenter_id=self.datacenter['id'])
self.assertGreater(len(servers), 0)
self.assertEqual(servers['items'][0]['type'], 'server')
assertRegex(self, servers['items'][0]['id'], self.resource['uuid_match'])
def test_get(self):
server = self.client.get_server(
datacenter_id=self.datacenter['id'],
server_id=self.server['id']
)
self.assertEqual(server['type'], 'server')
self.assertEqual(server['id'], self.server['id'])
self.assertEqual(server['properties']['name'], self.resource['server']['name'])
self.assertEqual(server['properties']['cores'], self.resource['server']['cores'])
self.assertEqual(server['properties']['ram'], self.resource['server']['ram'])
def test_delete(self):
server = self.client.create_server(
datacenter_id=self.datacenter['id'],
server=Server(**self.resource['server'])
)
wait_for_completion(self.client, server, 'create_server')
response = self.client.delete_server(
datacenter_id=self.datacenter['id'],
server_id=server['id']
)
self.assertTrue(response)
def test_update(self):
server = self.client.update_server(
datacenter_id=self.datacenter['id'],
server_id=self.server['id'],
name=self.resource['server']['name'] + ' RENAME')
wait_for_completion(self.client, server, 'update_server')
server = self.client.get_server(
datacenter_id=self.datacenter['id'],
server_id=self.server['id']
)
self.assertEqual(server['id'], self.server['id'])
self.assertEqual(server['properties']['name'], self.resource['server']['name'] + ' RENAME')
self.assertEqual(server['properties']['cores'], self.resource['server']['cores'])
self.assertEqual(server['properties']['ram'], self.resource['server']['ram'])
def test_create_simple(self):
#.........这里部分代码省略.........
示例2: main
# 需要导入模块: from profitbricks.client import ProfitBricksService [as 别名]
# 或者: from profitbricks.client.ProfitBricksService import attach_volume [as 别名]
#.........这里部分代码省略.........
raise ValueError("user or password resolved to None")
pbclient = ProfitBricksService(user, password)
if args.metatype == 'OVF':
metadata = OFVData(args.metafile)
metadata.parse()
else:
sys.stderr.write("Metadata type '{}' is not supported"
.format(args.metatype))
return 1
# we need the DC first to have the location defined
dc_id = None
if args.datacenterid is None:
if args.dcname is None or args.location is None:
sys.stderr.write("Either '-d <id>' or '-D <name> -l <loc>' must be specified")
return 1
# else: we will create the DC later after parsing the meta data
else:
dc_id = args.datacenterid
if dc_id is None:
location = args.location
dc = Datacenter(name=args.dcname, location=location,
description="created by pb_importVM")
print("create new DC {}".format(str(dc)))
response = pbclient.create_datacenter(dc)
dc_id = response['id']
result = wait_for_request(pbclient, response['requestId'])
print("wait loop returned {}".format(result))
else:
dc = pbclient.get_datacenter(dc_id)
location = dc['properties']['location']
print("use existing DC {} in location {}"
.format(dc['properties']['name'], location))
# check if images exist
for disk in metadata.disks:
disk_name = disk['file']
images = get_disk_image_by_name(pbclient, location, disk_name)
if len(images) == 0:
raise ValueError("No HDD image with name '{}' found in location {}"
.format(disk_name, location))
if len(images) > 1:
raise ValueError("Ambigous image name '{}' in location {}"
.format(disk_name, location))
disk['image'] = images[0]['id']
# now we're ready to create the VM
# Server
server = Server(name=metadata.name,
cores=metadata.cpus, ram=metadata.ram)
print("create server {}".format(str(Server)))
response = pbclient.create_server(dc_id, server)
srv_id = response['id']
result = wait_for_request(pbclient, response['requestId'])
print("wait loop returned {}".format(str(result)))
# NICs (note that createing LANs may be implicit)
for nic in metadata.nics:
dcnic = NIC(name=nic['nic'], lan=nic['lanid'])
print("create NIC {}".format(str(dcnic)))
response = pbclient.create_nic(dc_id, srv_id, dcnic)
nic_id = response['id']
result = wait_for_request(pbclient, response['requestId'])
print("wait loop returned {}".format(str(result)))
response = pbclient.get_nic(dc_id, srv_id, nic_id, 2)
mac = response['properties']['mac']
print("dcnic has MAC {} for {}".format(mac, nic_id))
# end for(nics)
# Volumes (we use the image name as volume name too
requests = []
for disk in metadata.disks:
dcvol = Volume(name=disk['file'], size=disk['capacity'],
image=disk['image'],
licence_type=metadata.licenseType)
print("create Volume {}".format(str(dcvol)))
response = pbclient.create_volume(dc_id, dcvol)
requests.append(response['requestId'])
disk['volume_id'] = response['id']
# end for(disks)
if len(requests) != 0:
result = wait_for_requests(pbclient, requests, initial_wait=10, scaleup=15)
print("wait loop returned {}".format(str(result)))
for disk in metadata.disks:
print("attach volume {}".format(disk))
response = pbclient.attach_volume(dc_id, srv_id, disk['volume_id'])
result = wait_for_request(pbclient, response['requestId'])
print("wait loop returned {}".format(str(result)))
# end for(disks)
print("import of VM succesfully finished")
return 0
except KeyboardInterrupt:
### handle keyboard interrupt ###
return 0
except Exception:
traceback.print_exc()
sys.stderr.write("\n" + program_name + ": for help use --help\n")
return 2
示例3: TestServer
# 需要导入模块: from profitbricks.client import ProfitBricksService [as 别名]
# 或者: from profitbricks.client.ProfitBricksService import attach_volume [as 别名]
#.........这里部分代码省略.........
volume2 = Volume(
name='volume2',
size=56,
image='<IMAGE/SNAPSHOT-ID>',
bus='VIRTIO'
)
nics = [nic1, nic2]
create_volumes = [volume1, volume2]
i = Server(
name='server1',
ram=4096,
cores=4,
nics=nics,
create_volumes=create_volumes
)
response = self.server.create_server(
datacenter_id=datacenter_id, server=i)
self.assertEqual(response['id'], server_id)
self.assertEqual(response['properties']['name'], 'New Server')
self.assertEqual(response['properties']['cores'], '4')
self.assertEqual(response['properties']['ram'], '4096')
self.assertEqual(
response['properties']['availabilityZone'], 'ZONE_1')
self.assertEqual(response['properties']['vmState'], 'SHUTOFF')
def test_create_with_existing_volume(self):
volume_id = '<NEW-STORAGE-ID>'
attach_volumes = [volume_id]
i = Server(
name='server1',
ram=4096,
cores=4,
attach_volumes=attach_volumes
)
response = self.server.create_server(
datacenter_id=datacenter_id, server=i)
self.assertEqual(response['id'], server_id)
self.assertEqual(response['properties']['name'], 'New Server')
self.assertEqual(response['properties']['cores'], '4')
self.assertEqual(response['properties']['ram'], '4096')
self.assertEqual(
response['properties']['availabilityZone'], 'ZONE_1')
self.assertEqual(response['properties']['vmState'], 'SHUTOFF')
def test_create_with_volumes_only(self):
volume1 = Volume(
name='volume1',
size=56,
image='<IMAGE/SNAPSHOT-ID>',
bus='VIRTIO'
)
volume2 = Volume(
name='volume2',
size=56,
image='<IMAGE/SNAPSHOT-ID>',
bus='VIRTIO'
示例4: main
# 需要导入模块: from profitbricks.client import ProfitBricksService [as 别名]
# 或者: from profitbricks.client.ProfitBricksService import attach_volume [as 别名]
#.........这里部分代码省略.........
requests = []
print("create Servers {}".format(str(dc)))
# we do NOT consider dangling volumes, only server-attached ones
for server in dcdef["entities"]["servers"]["items"]:
print("- server {}".format(server["properties"]["name"]))
if "custom" in server and "id" in server["custom"]:
srv_id = server["custom"]["id"]
print("using existing server w/ id {}".format(str(srv_id)))
else:
dcsrv = getServerObject(server)
print("OBJ: {}".format(str(dcsrv)))
response = pbclient.create_server(dc_id, dcsrv)
server.update({"custom": {"id": response["id"]}})
requests.append(response["requestId"])
# end for(server)
if len(requests) != 0:
result = wait_for_requests(pbclient, requests, initial_wait=10, scaleup=15)
print("wait loop returned {}".format(str(result)))
tmpfile = usefile + ".tmp_postsrv"
write_dc_definition(pbclient, dcdef, tmpfile)
else:
print("all servers existed already")
# TODO: only do this if we have lan entities
requests = []
# Huuh, looks like we get unpredictable order for LANs!
# Nope, order of creation determines the LAN id,
# thus we better wait for each request
print("create LANs {}".format(str(dc)))
for lan in dcdef["entities"]["lans"]["items"]:
print("- lan {}".format(lan["properties"]["name"]))
dclan = getLANObject(lan)
print("OBJ: {}".format(str(dclan)))
response = pbclient.create_lan(dc_id, dclan)
lan.update({"custom": {"id": response["id"]}})
result = wait_for_request(pbclient, response["requestId"])
print("wait loop returned {}".format(str(result)))
# end for(lan)
tmpfile = usefile + ".tmp_postlan"
write_dc_definition(pbclient, dcdef, tmpfile)
requests = []
# Attention:
# NICs appear in OS in the order, they are created.
# But DCD rearranges the display by ascending MAC addresses.
# This does not change the OS order.
# MAC may not be available from create response,
# thus we wait for each request :-(
print("create NICs {}".format(str(dc)))
for server in dcdef["entities"]["servers"]["items"]:
print("- server {}".format(server["properties"]["name"]))
srv_id = server["custom"]["id"]
if "nics" not in server["entities"]:
print(" server {} has no NICs".format(server["properties"]["name"]))
continue
macmap = dict()
for nic in server["entities"]["nics"]["items"]:
dcnic = getNICObject(nic)
response = pbclient.create_nic(dc_id, srv_id, dcnic)
# print("dcnic response {}".format(str(response)))
# mac = response['properties']['mac'] # we don't get it here !?
nic_id = response["id"]
result = wait_for_request(pbclient, response["requestId"])
print("wait loop returned {}".format(str(result)))
response = pbclient.get_nic(dc_id, srv_id, nic_id, 2)
mac = response["properties"]["mac"]
print("dcnic has MAC {} for {}".format(mac, nic_id))
macmap[mac] = nic_id
# end for(nic)
macs = sorted(macmap)
print("macs will be displayed by DCD in th following order:")
for mac in macs:
print("mac {} -> id{}".format(mac, macmap[mac]))
# end for(server)
tmpfile = usefile + ".tmp_postnic"
write_dc_definition(pbclient, dcdef, tmpfile)
requests = []
# don't know if we get a race here too, so better wait for each request :-/
print("attach volumes {}".format(str(dc)))
for server in dcdef["entities"]["servers"]["items"]:
print("- server {}".format(server["properties"]["name"]))
if "volumes" not in server["entities"]:
print(" server {} has no volumes".format(server["properties"]["name"]))
continue
srv_id = server["custom"]["id"]
for volume in server["entities"]["volumes"]["items"]:
print("OBJ: {}".format(volume["properties"]["name"]))
response = pbclient.attach_volume(dc_id, srv_id, volume["custom"]["id"])
result = wait_for_request(pbclient, response["requestId"])
print("wait loop returned {}".format(str(result)))
# end for(volume)
# end for(server)
tmpfile = usefile + ".tmp_postatt"
write_dc_definition(pbclient, dcdef, tmpfile)
# TODO: do we need to set boot volume for each server?
# looks like it's working without
return 0