~0x44/nova/extdoc

« back to all changes in this revision

Viewing changes to nova/api/openstack/create_instance_helper.py

  • Committer: Lvov Maxim
  • Date: 2011-07-26 05:50:05 UTC
  • mfrom: (1320 nova)
  • mto: This revision was merged to the branch mainline in revision 1322.
  • Revision ID: usrleon@gmail.com-20110726055005-7olsp0giqup3pao7
merge with trunk, resolve conflicts

Show diffs side-by-side

added added

removed removed

Lines of Context:
71
71
        if not body:
72
72
            raise exc.HTTPUnprocessableEntity()
73
73
 
 
74
        if not 'server' in body:
 
75
            raise exc.HTTPUnprocessableEntity()
 
76
 
 
77
        server_dict = body['server']
74
78
        context = req.environ['nova.context']
75
 
 
76
 
        password = self.controller._get_server_admin_password(body['server'])
 
79
        password = self.controller._get_server_admin_password(server_dict)
77
80
 
78
81
        key_name = None
79
82
        key_data = None
95
98
                                                                    locals())
96
99
            raise exc.HTTPBadRequest(explanation=msg)
97
100
 
98
 
        personality = body['server'].get('personality')
 
101
        personality = server_dict.get('personality')
99
102
 
100
103
        injected_files = []
101
104
        if personality:
107
110
            msg = _("Invalid flavorRef provided.")
108
111
            raise exc.HTTPBadRequest(explanation=msg)
109
112
 
110
 
        if not 'name' in body['server']:
 
113
        if not 'name' in server_dict:
111
114
            msg = _("Server name is not defined")
112
115
            raise exc.HTTPBadRequest(explanation=msg)
113
116
 
114
 
        zone_blob = body['server'].get('blob')
115
 
        name = body['server']['name']
 
117
        zone_blob = server_dict.get('blob')
 
118
        name = server_dict['name']
116
119
        self._validate_server_name(name)
117
120
        name = name.strip()
118
121
 
119
 
        reservation_id = body['server'].get('reservation_id')
120
 
        min_count = body['server'].get('min_count')
121
 
        max_count = body['server'].get('max_count')
 
122
        reservation_id = server_dict.get('reservation_id')
 
123
        min_count = server_dict.get('min_count')
 
124
        max_count = server_dict.get('max_count')
122
125
        # min_count and max_count are optional.  If they exist, they come
123
126
        # in as strings.  We want to default 'min_count' to 1, and default
124
127
        # 'max_count' to be 'min_count'.
145
148
                                  display_description=name,
146
149
                                  key_name=key_name,
147
150
                                  key_data=key_data,
148
 
                                  metadata=body['server'].get('metadata', {}),
 
151
                                  metadata=server_dict.get('metadata', {}),
149
152
                                  injected_files=injected_files,
150
153
                                  admin_password=password,
151
154
                                  zone_blob=zone_blob,
282
285
        return password
283
286
 
284
287
 
285
 
class ServerXMLDeserializer(wsgi.XMLDeserializer):
 
288
class ServerXMLDeserializer(wsgi.MetadataXMLDeserializer):
286
289
    """
287
290
    Deserializer to handle xml-formatted server create requests.
288
291
 
299
302
    def _extract_server(self, node):
300
303
        """Marshal the server attribute of a parsed request"""
301
304
        server = {}
302
 
        server_node = self._find_first_child_named(node, 'server')
 
305
        server_node = self.find_first_child_named(node, 'server')
303
306
        for attr in ["name", "imageId", "flavorId", "imageRef", "flavorRef"]:
304
307
            if server_node.getAttribute(attr):
305
308
                server[attr] = server_node.getAttribute(attr)
306
 
        metadata = self._extract_metadata(server_node)
 
309
        metadata_node = self.find_first_child_named(server_node, "metadata")
 
310
        metadata = self.extract_metadata(metadata_node)
307
311
        if metadata is not None:
308
312
            server["metadata"] = metadata
309
313
        personality = self._extract_personality(server_node)
311
315
            server["personality"] = personality
312
316
        return server
313
317
 
314
 
    def _extract_metadata(self, server_node):
315
 
        """Marshal the metadata attribute of a parsed request"""
316
 
        metadata_node = self._find_first_child_named(server_node, "metadata")
317
 
        if metadata_node is None:
318
 
            return None
319
 
        metadata = {}
320
 
        for meta_node in self._find_children_named(metadata_node, "meta"):
321
 
            key = meta_node.getAttribute("key")
322
 
            metadata[key] = self._extract_text(meta_node)
323
 
        return metadata
324
 
 
325
318
    def _extract_personality(self, server_node):
326
319
        """Marshal the personality attribute of a parsed request"""
327
320
        personality_node = \
328
 
                self._find_first_child_named(server_node, "personality")
 
321
                self.find_first_child_named(server_node, "personality")
329
322
        if personality_node is None:
330
323
            return None
331
324
        personality = []
332
 
        for file_node in self._find_children_named(personality_node, "file"):
 
325
        for file_node in self.find_children_named(personality_node, "file"):
333
326
            item = {}
334
327
            if file_node.hasAttribute("path"):
335
328
                item["path"] = file_node.getAttribute("path")
336
 
            item["contents"] = self._extract_text(file_node)
 
329
            item["contents"] = self.extract_text(file_node)
337
330
            personality.append(item)
338
331
        return personality
339
 
 
340
 
    def _find_first_child_named(self, parent, name):
341
 
        """Search a nodes children for the first child with a given name"""
342
 
        for node in parent.childNodes:
343
 
            if node.nodeName == name:
344
 
                return node
345
 
        return None
346
 
 
347
 
    def _find_children_named(self, parent, name):
348
 
        """Return all of a nodes children who have the given name"""
349
 
        for node in parent.childNodes:
350
 
            if node.nodeName == name:
351
 
                yield node
352
 
 
353
 
    def _extract_text(self, node):
354
 
        """Get the text field contained by the given node"""
355
 
        if len(node.childNodes) == 1:
356
 
            child = node.childNodes[0]
357
 
            if child.nodeType == child.TEXT_NODE:
358
 
                return child.nodeValue
359
 
        return ""