-
Notifications
You must be signed in to change notification settings - Fork 42
/
vmclient.py
205 lines (161 loc) · 8.62 KB
/
vmclient.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# -*- coding: utf-8 -*-
# --------------------------------------------------------------------------
# Copyright Commvault Systems, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# --------------------------------------------------------------------------
"""
VMClient class is defined in this file.
VMClient: Class for a single vm client of the commcell
VMClient
=======
_return_parent_subclient() -- Returns the parent subclient where the vm has been backed up
_child_job_subclient_details() -- returns the subclient details of the child job
full_vm_restore_in_place() -- Performs in place full vm restore and return job object
full_vm_restore_out_of_place() -- Performs out of place full vm restore and return job object
"""
import copy
from ..job import Job
from ..exception import SDKException
from ..client import Client
class VMClient(Client):
""" Class for representing client of a vm client."""
def __init__(self, commcell_object, client_name, client_id=None):
"""Initialise the VM Client class instance.
Args:
commcell_object (object) -- instance of the Commcell class
client_name (str) -- name of the client
client_id (str) -- id of the client
default: None
Returns:
object - instance of the VM Client class
"""
super(VMClient, self).__init__(commcell_object, client_name, client_id)
def _return_parent_subclient(self):
"""
Returns the parent subclient if the client is VSA client and is backed up else returns None
Returns:
_parent_subclient (object) : Subclient object
"""
_subclient_entity = copy.deepcopy(self.properties.get('vmStatusInfo', {}).get('vsaSubClientEntity'))
if _subclient_entity:
_parent_client = self._commcell_object.clients.get(_subclient_entity.get('clientName'))
_parent_agent = _parent_client.agents.get(_subclient_entity.get('appName'))
_parent_instance = _parent_agent.instances.get(_subclient_entity.get('instanceName'))
_parent_backupset = _parent_instance.backupsets.get(_subclient_entity.get('backupsetName'))
_parent_subclient = _parent_backupset.subclients.get(_subclient_entity.get('subclientName'))
return _parent_subclient
else:
return None
def _child_job_subclient_details(self, parent_job_id):
"""
Returns the child subclient details
Args:
parent_job_id (string): job id of the parent
Returns:
_child_job_obj (dict): Child subclient details:
eg: {
'clientName': 'vm_client1',
'instanceName': 'VMInstance',
'displayName': 'vm_client1',
'backupsetId': 12,
'instanceId': 2,
'subclientId': 123,
'clientId': 1234,
'appName': 'Virtual Server',
'backupsetName': 'defaultBackupSet',
'applicationId': 106,
'subclientName': 'default'
}
"""
_parent_job_obj = Job(self._commcell_object, parent_job_id)
_child_jobs = _parent_job_obj.get_child_jobs()
if _child_jobs:
_child_job = None
for _job in _child_jobs:
if self.vm_guid == _job['GUID']:
_child_job = _job['jobID']
break
if not _child_job:
return None
_child_job_obj = Job(self._commcell_object, _child_job)
return _child_job_obj.details.get('jobDetail', {}).get('generalInfo', {}).get('subclient')
else:
return None
def full_vm_restore_in_place(self, **kwargs):
"""Restores in place FULL Virtual machine for the client
Args:
**kwargs : Arbitrary keyword arguments Properties as of
full_vm_restore_in_place
eg:
overwrite (bool) -- overwrite the existing VM
power_on (bool) -- power on the restored VM
copy_precedence (int) -- copy precedence value
Returns:
object - instance of the Job class for this restore job
Raises:
SDKException:
if inputs are not of correct type as per definition
if failed to initialize job
if response is empty
if response is not success
"""
if self.vm_guid:
_sub_client_obj = self._return_parent_subclient()
kwargs.pop('vm_to_restore', None)
if self.properties.get('clientProps', {}).get('isIndexingV2VSA'):
_child_details = self._child_job_subclient_details(self.properties['vmStatusInfo']['vmBackupJob'])
vm_restore_job = _sub_client_obj.full_vm_restore_in_place(vm_to_restore=self.name,
v2_details=_child_details,
**kwargs)
else:
vm_restore_job = _sub_client_obj.full_vm_restore_in_place(vm_to_restore=self.name,
**kwargs)
return vm_restore_job
else:
return None
def full_vm_restore_out_of_place(self, **kwargs):
"""Restores out of place FULL Virtual machine for the client
Args:
**kwargs : Arbitrary keyword arguments Properties as of
full_vm_restore_out_of_place
ex:
restored_vm_name (str) -- new name of vm. If nothing is passed,
'del' is appended to the original vm name
vcenter_client (str) -- name of the vcenter client where the VM
should be restored.
esx_host (str) -- destination esx host. Restores to the source
VM esx if this value is not specified
Returns:
object - instance of the Job class for this restore job
Raises:
SDKException:
if inputs are not of correct type as per definition
if failed to initialize job
if response is empty
if response is not success
"""
if self.vm_guid:
_sub_client_obj = self._return_parent_subclient()
kwargs.pop('vm_to_restore', None)
if self.properties.get('clientProps', {}).get('isIndexingV2VSA'):
_child_details = self._child_job_subclient_details(self.properties['vmStatusInfo']['vmBackupJob'])
vm_restore_job = _sub_client_obj.full_vm_restore_out_of_place(vm_to_restore=self.name,
v2_details=_child_details,
**kwargs)
else:
vm_restore_job = _sub_client_obj.full_vm_restore_out_of_place(vm_to_restore=self.name,
**kwargs)
return vm_restore_job
else:
return None