@@ -33,10 +33,144 @@ def __init__(self, client, ordering_manager=None):
33
33
self .client = client
34
34
self .account = client ['Account' ]
35
35
self .host = client ['Virtual_DedicatedHost' ]
36
+ self .guest = client ['Virtual_Guest' ]
36
37
37
38
if ordering_manager is None :
38
39
self .ordering_manager = ordering .OrderingManager (client )
39
40
41
+ def cancel_host (self , host_id ):
42
+ """Cancel a dedicated host immediately, it fails if there are still guests in the host.
43
+
44
+ :param host_id: The ID of the dedicated host to be cancelled.
45
+ :return: True on success or an exception
46
+
47
+ Example::
48
+ # Cancels dedicated host id 12345
49
+ result = mgr.cancel_host(12345)
50
+
51
+ """
52
+ return self .host .deleteObject (id = host_id )
53
+
54
+ def cancel_guests (self , host_id ):
55
+ """Cancel all guests into the dedicated host immediately.
56
+
57
+ To cancel an specified guest use the method VSManager.cancel_instance()
58
+
59
+ :param host_id: The ID of the dedicated host.
60
+ :return: The id, fqdn and status of all guests into a dictionary. The status
61
+ could be 'Cancelled' or an exception message, The dictionary is empty
62
+ if there isn't any guest in the dedicated host.
63
+
64
+ Example::
65
+ # Cancel guests of dedicated host id 12345
66
+ result = mgr.cancel_guests(12345)
67
+ """
68
+ result = []
69
+
70
+ guests = self .host .getGuests (id = host_id , mask = 'id,fullyQualifiedDomainName' )
71
+
72
+ if guests :
73
+ for vs in guests :
74
+ status_info = {
75
+ 'id' : vs ['id' ],
76
+ 'fqdn' : vs ['fullyQualifiedDomainName' ],
77
+ 'status' : self ._delete_guest (vs ['id' ])
78
+ }
79
+ result .append (status_info )
80
+
81
+ return result
82
+
83
+ def list_guests (self , host_id , tags = None , cpus = None , memory = None , hostname = None ,
84
+ domain = None , local_disk = None , nic_speed = None , public_ip = None ,
85
+ private_ip = None , ** kwargs ):
86
+ """Retrieve a list of all virtual servers on the dedicated host.
87
+
88
+ Example::
89
+
90
+ # Print out a list of instances with 4 cpu cores in the host id 12345.
91
+
92
+ for vsi in mgr.list_guests(host_id=12345, cpus=4):
93
+ print vsi['fullyQualifiedDomainName'], vsi['primaryIpAddress']
94
+
95
+ # Using a custom object-mask. Will get ONLY what is specified
96
+ object_mask = "mask[hostname,monitoringRobot[robotStatus]]"
97
+ for vsi in mgr.list_guests(mask=object_mask,cpus=4):
98
+ print vsi
99
+
100
+ :param integer host_id: the identifier of dedicated host
101
+ :param list tags: filter based on list of tags
102
+ :param integer cpus: filter based on number of CPUS
103
+ :param integer memory: filter based on amount of memory
104
+ :param string hostname: filter based on hostname
105
+ :param string domain: filter based on domain
106
+ :param string local_disk: filter based on local_disk
107
+ :param integer nic_speed: filter based on network speed (in MBPS)
108
+ :param string public_ip: filter based on public ip address
109
+ :param string private_ip: filter based on private ip address
110
+ :param dict \\ *\\ *kwargs: response-level options (mask, limit, etc.)
111
+ :returns: Returns a list of dictionaries representing the matching
112
+ virtual servers
113
+ """
114
+ if 'mask' not in kwargs :
115
+ items = [
116
+ 'id' ,
117
+ 'globalIdentifier' ,
118
+ 'hostname' ,
119
+ 'domain' ,
120
+ 'fullyQualifiedDomainName' ,
121
+ 'primaryBackendIpAddress' ,
122
+ 'primaryIpAddress' ,
123
+ 'lastKnownPowerState.name' ,
124
+ 'hourlyBillingFlag' ,
125
+ 'powerState' ,
126
+ 'maxCpu' ,
127
+ 'maxMemory' ,
128
+ 'datacenter' ,
129
+ 'activeTransaction.transactionStatus[friendlyName,name]' ,
130
+ 'status' ,
131
+ ]
132
+ kwargs ['mask' ] = "mask[%s]" % ',' .join (items )
133
+
134
+ _filter = utils .NestedDict (kwargs .get ('filter' ) or {})
135
+
136
+ if tags :
137
+ _filter ['guests' ]['tagReferences' ]['tag' ]['name' ] = {
138
+ 'operation' : 'in' ,
139
+ 'options' : [{'name' : 'data' , 'value' : tags }],
140
+ }
141
+
142
+ if cpus :
143
+ _filter ['guests' ]['maxCpu' ] = utils .query_filter (cpus )
144
+
145
+ if memory :
146
+ _filter ['guests' ]['maxMemory' ] = utils .query_filter (memory )
147
+
148
+ if hostname :
149
+ _filter ['guests' ]['hostname' ] = utils .query_filter (hostname )
150
+
151
+ if domain :
152
+ _filter ['guests' ]['domain' ] = utils .query_filter (domain )
153
+
154
+ if local_disk is not None :
155
+ _filter ['guests' ]['localDiskFlag' ] = (
156
+ utils .query_filter (bool (local_disk )))
157
+
158
+ if nic_speed :
159
+ _filter ['guests' ]['networkComponents' ]['maxSpeed' ] = (
160
+ utils .query_filter (nic_speed ))
161
+
162
+ if public_ip :
163
+ _filter ['guests' ]['primaryIpAddress' ] = (
164
+ utils .query_filter (public_ip ))
165
+
166
+ if private_ip :
167
+ _filter ['guests' ]['primaryBackendIpAddress' ] = (
168
+ utils .query_filter (private_ip ))
169
+
170
+ kwargs ['filter' ] = _filter .to_dict ()
171
+ kwargs ['iter' ] = True
172
+ return self .host .getGuests (id = host_id , ** kwargs )
173
+
40
174
def list_instances (self , tags = None , cpus = None , memory = None , hostname = None ,
41
175
disk = None , datacenter = None , ** kwargs ):
42
176
"""Retrieve a list of all dedicated hosts on the account
@@ -384,3 +518,13 @@ def get_router_options(self, datacenter=None, flavor=None):
384
518
item = self ._get_item (package , flavor )
385
519
386
520
return self ._get_backend_router (location ['location' ]['locationPackageDetails' ], item )
521
+
522
+ def _delete_guest (self , guest_id ):
523
+ """Deletes a guest and returns 'Cancelled' or and Exception message"""
524
+ msg = 'Cancelled'
525
+ try :
526
+ self .guest .deleteObject (id = guest_id )
527
+ except SoftLayer .SoftLayerAPIError as e :
528
+ msg = 'Exception: ' + e .faultString
529
+
530
+ return msg
0 commit comments