outfile=sys.stdout
rspec = parse_rspec(infile)
+rspec.type = 'request'
nodes = file(command.opts.nodefile).read().split()
try:
if rspec.version['type'].lower() == 'protogeni':
rspec.xml.set('type', 'request')
- rspec.remove_element('available')
slivers = [{'hostname': node} for node in nodes]
rspec.add_slivers(slivers)
except:
print >> sys.stderr, "FAILED: %s" % nodes
sys.exit(1)
-print >>outfile, rspec.toxml()
+print >>outfile, rspec.toxml(cleanup=True)
sys.exit(0)
if slice_xrn:
# If slicename is specified then resulting rspec is a manifest.
# Add sliver details to rspec and remove 'advertisement' elements
- rspec.remove_element('available')
slice_hrn, _ = urn_to_hrn(slice_xrn)
slice_name = hrn_to_pl_slicename(slice_hrn)
slices = self.api.plshell.GetSlices(self.api.plauth, slice_name)
sliver.tags.append(tag)
rspec.add_slivers(slivers, sliver_urn=slice_xrn)
- return rspec.toxml()
+ return rspec.toxml(cleanup=True)
if self.type:
self.xml.set('type', self.type)
- def remove_element(self, element_name, root_node=None):
- if not element_name.startswith('rspecv2:'):
- element_name = 'rspecv2:' + element_name
- RSpec.remove_element(self, element_name, root_node)
-
def get_network(self):
network = None
nodes = self.xml.xpath('//rspecv2:node[@component_manager_uuid][1]', namespaces=self.namespaces)
return set(networks)
def get_node_elements(self):
- nodes = self.xml.xpath('//rspecv2:node', namespaces=self.namespaces)
+ nodes = self.xml.xpath('//rspecv2:node | //node', namespaces=self.namespaces)
return nodes
def get_nodes(self, network=None):
- return self.xml.xpath('//rspecv2:node[@component_name]/@component_name', namespaces=self.namespaces)
+ xpath = '//rspecv2:node[@component_name]/@component_name | //node[@component_name]/@component_name'
+ return self.xml.xpath(xpath, namespaces=self.namespaces)
def get_nodes_with_slivers(self, network=None):
if network:
for child in root.getchildren():
self.xml.append(child)
-
+ def cleanup(self):
+ # remove unncecessary elements, attributes
+ if self.type in ['request', 'manifest']:
+ # remove nodes without slivers
+ nodes = self.get_node_elements()
+ for node in nodes:
+ delete = True
+ hostname = node.get('component_name')
+ parent = node.getparent()
+ children = node.getchildren()
+ for child in children:
+ if child.tag.endswith('sliver_type'):
+ delete = False
+ if delete:
+ parent.remove(node)
+
+ # remove 'available' element from remaining node elements
+ self.remove_element('//rspecv2:available | //available')
if __name__ == '__main__':
rspec = PGRSpec()
if not root_node:
root_node = self.xml
- elements = root_node.xpath('//%s ' % element_name, namespaces=self.namespaces)
+ if not element_name.startswith('//'):
+ element_name = '//' + element_name
+
+ elements = root_node.xpath('%s ' % element_name, namespaces=self.namespaces)
for element in elements:
parent = element.getparent()
parent.remove(element)
return True
+ def cleanup(self):
+ """
+ Optional method which inheriting classes can choose to implent.
+ """
+ pass
+
def __str__(self):
return self.toxml()
- def toxml(self):
+ def toxml(self, cleanup=False):
+ if cleanup:
+ self.cleanup()
return self.header + etree.tostring(self.xml, pretty_print=True)
def save(self, filename):