Skip to content

Commit 51733d0

Browse files
committed
added example for instance actions with volumes
1 parent 9c9d795 commit 51733d0

1 file changed

Lines changed: 38 additions & 7 deletions

File tree

examples/instances_and_volumes.py

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,46 @@
88
# Create datcrunch client
99
datacrunch = DataCrunchClient(CLIENT_ID, CLIENT_SECRET)
1010

11-
# Create instance with extra volumes
12-
# TODO:
11+
# Get some volume type constants
12+
NVMe = datacrunch.constants.volume_types.NVMe
13+
HDD = datacrunch.constants.volume_types.HDD
14+
15+
EXISTING_OS_VOLUME_ID = '81e45bf0-5da2-412b-97d7-c20a7564fca0'
16+
EXAMPLE_VOLUME_ID = '225dde24-ae44-4787-9224-2b9f56f44394'
17+
EXAMPLE_INSTANCE_ID = '1eeabba4-caf7-4b4a-9143-0107034cc7f5'
18+
19+
# Get all SSH keys
20+
ssh_keys = datacrunch.ssh_keys.get()
21+
22+
# Create instance with extra attached volumes
23+
instance_with_extra_volumes = datacrunch.instances.create(instance_type='1V100.6V',
24+
image='fastai',
25+
ssh_key_ids=ssh_keys[0].id,
26+
hostname='example',
27+
description='example instance',
28+
volumes=[
29+
{"type": HDD, "name": "volume-1", "size": 95},
30+
{"type": NVMe, "name": "volume-2", "size": 95}
31+
])
1332

1433
# Create instance with existing OS volume as an image
15-
# TODO:
34+
instance_with_existing_os_volume = datacrunch.instances.create(instance_type='1V100.6V',
35+
image=EXISTING_OS_VOLUME_ID,
36+
ssh_key_ids=ssh_keys[0].id,
37+
hostname='example',
38+
description='example instance')
39+
40+
# Delete instance AND OS volume (the rest of the volumes would be detached)
41+
datacrunch.instances.action(instance_id=EXAMPLE_INSTANCE_ID,
42+
action=datacrunch.constants.instance_actions.DELETE)
1643

17-
# Delete instance without deleting the OS volume
18-
# TODO:
44+
# Delete instance WITHOUT deleting the OS volume (will detach all volumes of the instance)
45+
datacrunch.instances.action(instance_id=EXAMPLE_INSTANCE_ID,
46+
action=datacrunch.constants.instance_actions.DELETE,
47+
volume_ids=[])
1948

20-
# Delete instance and one of it's volumes
21-
# TODO:
2249

50+
# Delete instance and one of it's volumes (will delete one volume, detach the rest)
51+
datacrunch.instances.action(instance_id=EXAMPLE_INSTANCE_ID,
52+
action=datacrunch.constants.instance_actions.DELETE,
53+
volume_ids=[EXAMPLE_VOLUME_ID])

0 commit comments

Comments
 (0)