~ubuntu-branches/ubuntu/raring/nova/raring-proposed

« back to all changes in this revision

Viewing changes to nova/virt/vmwareapi/fake.py

  • Committer: Package Import Robot
  • Author(s): Adam Gandelman
  • Date: 2013-08-09 10:12:27 UTC
  • mto: This revision was merged to the branch mainline in revision 107.
  • Revision ID: package-import@ubuntu.com-20130809101227-flqfubhwpot76pob
Tags: upstream-2013.1.3
ImportĀ upstreamĀ versionĀ 2013.1.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
2
 
 
3
# Copyright (c) 2013 Hewlett-Packard Development Company, L.P.
3
4
# Copyright (c) 2012 VMware, Inc.
4
5
# Copyright (c) 2011 Citrix Systems, Inc.
5
6
# Copyright 2011 OpenStack Foundation
20
21
A fake VMware VI API implementation.
21
22
"""
22
23
 
 
24
import collections
23
25
import pprint
24
26
import uuid
25
27
 
81
83
    return lst_objs
82
84
 
83
85
 
84
 
class Prop(object):
 
86
class Property(object):
85
87
    """Property Object base class."""
86
88
 
87
 
    def __init__(self):
88
 
        self.name = None
89
 
        self.val = None
 
89
    def __init__(self, name=None, val=None):
 
90
        self.name = name
 
91
        self.val = val
 
92
 
 
93
 
 
94
class ManagedObjectReference(object):
 
95
    """A managed object reference is a remote identifier."""
 
96
 
 
97
    def __init__(self, value="object-123", _type="ManagedObject"):
 
98
        super(ManagedObjectReference, self)
 
99
        # Managed Object Reference value attributes
 
100
        # typically have values like vm-123 or
 
101
        # host-232 and not UUID.
 
102
        self.value = value
 
103
        # Managed Object Reference _type
 
104
        # attributes hold the name of the type
 
105
        # of the vCenter object the value
 
106
        # attribute is the identifier for
 
107
        self._type = _type
 
108
 
 
109
 
 
110
class ObjectContent(object):
 
111
    """ObjectContent array holds dynamic properties."""
 
112
 
 
113
    # This class is a *fake* of a class sent back to us by
 
114
    # SOAP. It has its own names. These names are decided
 
115
    # for us by the API we are *faking* here.
 
116
    def __init__(self, obj_ref, prop_list=None, missing_list=None):
 
117
        self.obj = obj_ref
 
118
 
 
119
        if not isinstance(prop_list, collections.Iterable):
 
120
            prop_list = []
 
121
 
 
122
        if not isinstance(missing_list, collections.Iterable):
 
123
            missing_list = []
 
124
 
 
125
        # propSet is the name your Python code will need to
 
126
        # use since this is the name that the API will use
 
127
        self.propSet = prop_list
 
128
 
 
129
        # missingSet is the name your python code will
 
130
        # need to use since this is the name that the
 
131
        # API we are talking to will use.
 
132
        self.missingSet = missing_list
90
133
 
91
134
 
92
135
class ManagedObject(object):
93
 
    """Managed Data Object base class."""
 
136
    """Managed Object base class."""
94
137
 
95
 
    def __init__(self, name="ManagedObject", obj_ref=None):
 
138
    def __init__(self, name="ManagedObject", obj_ref=None, value=None):
96
139
        """Sets the obj property which acts as a reference to the object."""
97
140
        super(ManagedObject, self).__setattr__('objName', name)
 
141
 
 
142
        # A managed object is a local representation of a
 
143
        # remote object that you can reference using the
 
144
        # object reference.
98
145
        if obj_ref is None:
99
 
            obj_ref = str(uuid.uuid4())
 
146
            if value is None:
 
147
                value = 'obj-123'
 
148
            obj_ref = ManagedObjectReference(value, name)
 
149
 
 
150
        # we use __setattr__ here because below the
 
151
        # default setter has been altered for this class.
100
152
        object.__setattr__(self, 'obj', obj_ref)
101
153
        object.__setattr__(self, 'propSet', [])
102
154
 
116
168
        return self.__getattr__(attr)
117
169
 
118
170
    def __setattr__(self, attr, val):
 
171
        # TODO(hartsocks): this is adds unnecessary complexity to the class
119
172
        for prop in self.propSet:
120
173
            if prop.name == attr:
121
174
                prop.val = val
122
175
                return
123
 
        elem = Prop()
 
176
        elem = Property()
124
177
        elem.name = attr
125
178
        elem.val = val
126
179
        self.propSet.append(elem)
127
180
 
128
181
    def __getattr__(self, attr):
 
182
        # TODO(hartsocks): remove this
 
183
        # in a real ManagedObject you have to iterate the propSet
 
184
        # in a real ManagedObject, the propSet is a *set* not a list
129
185
        for elem in self.propSet:
130
186
            if elem.name == attr:
131
187
                return elem.val
136
192
 
137
193
class DataObject(object):
138
194
    """Data object base class."""
139
 
    pass
 
195
    def __init__(self, obj_name=None):
 
196
        self.obj_name = obj_name
140
197
 
141
198
 
142
199
class VirtualDisk(DataObject):
184
241
    """Virtual Machine class."""
185
242
 
186
243
    def __init__(self, **kwargs):
187
 
        super(VirtualMachine, self).__init__("VirtualMachine")
 
244
        super(VirtualMachine, self).__init__("VirtualMachine", value='vm-10')
188
245
        self.set("name", kwargs.get("name"))
189
246
        self.set("runtime.connectionState",
190
247
                 kwargs.get("conn_state", "connected"))
202
259
        self.set("summary.config.memorySizeMB", kwargs.get("mem", 1))
203
260
        self.set("config.hardware.device", kwargs.get("virtual_device", None))
204
261
        self.set("config.extraConfig", kwargs.get("extra_config", None))
 
262
        self.set('runtime.host',
 
263
                 ManagedObjectReference(value='host-123', _type="HostSystem"))
 
264
        self.device = kwargs.get("virtual_device")
205
265
 
206
266
    def reconfig(self, factory, val):
207
267
        """
224
284
            controller = VirtualLsiLogicController()
225
285
            controller.key = controller_key
226
286
 
227
 
            nic = VirtualPCNet32()
228
 
 
229
 
            self.set("config.hardware.device", [disk, controller, nic])
 
287
            self.set("config.hardware.device", [disk, controller,
 
288
                                                  self.device[0]])
230
289
        except AttributeError:
231
290
            # Case of Reconfig of VM to set extra params
232
291
            self.set("config.extraConfig", val.extraConfig)
255
314
        super(Datastore, self).__init__("Datastore")
256
315
        self.set("summary.type", "VMFS")
257
316
        self.set("summary.name", "fake-ds")
258
 
        self.set("summary.capacity", 1024 * 1024 * 1024)
259
 
        self.set("summary.freeSpace", 500 * 1024 * 1024)
 
317
        self.set("summary.capacity", 1024 * 1024 * 1024 * 1024)
 
318
        self.set("summary.freeSpace", 500 * 1024 * 1024 * 1024)
260
319
 
261
320
 
262
321
class HostNetworkSystem(ManagedObject):
278
337
class HostSystem(ManagedObject):
279
338
    """Host System class."""
280
339
 
281
 
    def __init__(self):
282
 
        super(HostSystem, self).__init__("HostSystem")
 
340
    def __init__(self, obj_ref=None, value='host-123'):
 
341
        super(HostSystem, self).__init__("HostSystem", obj_ref, value)
283
342
        self.set("name", "ha-host")
284
343
        if _db_content.get("HostNetworkSystem", None) is None:
285
344
            create_host_network_system()
308
367
        config.product = product
309
368
        summary.config = config
310
369
 
 
370
        pnic_do = DataObject()
 
371
        pnic_do.device = "vmnic0"
 
372
        net_info_pnic = DataObject()
 
373
        net_info_pnic.PhysicalNic = [pnic_do]
 
374
 
311
375
        self.set("summary", summary)
 
376
        self.set("config.network.pnic", net_info_pnic)
312
377
 
313
378
        if _db_content.get("Network", None) is None:
314
379
            create_network()
495
560
 
496
561
    def create(self, obj_name):
497
562
        """Creates a namespace object."""
498
 
        return DataObject()
 
563
        return DataObject(obj_name)
499
564
 
500
565
 
501
566
class FakeVim(object):
577
642
                  "powerstate": "poweredOff",
578
643
                  "vmPathName": config_spec.files.vmPathName,
579
644
                  "numCpu": config_spec.numCPUs,
580
 
                  "mem": config_spec.memoryMB}
 
645
                  "mem": config_spec.memoryMB,
 
646
                  "virtual_device": config_spec.deviceChange,
 
647
                  "extra_config": config_spec.extraConfig}
581
648
        virtual_machine = VirtualMachine(**vm_dict)
582
649
        _create_object("VirtualMachine", virtual_machine)
583
650
        task_mdo = create_task(method, "success")