Commit 4fcc16cb authored by Alain Takoudjou's avatar Alain Takoudjou

slapos_slap_tool: migrate business template to new format

parent 8df0d28d
def compareAndUpdateAddressList(document, address_list, additional_dict=None):
if additional_dict is None:
additional_dict = {}
to_delete_ip_id_list = []
to_add_ip_dict_list = address_list[:]
existing_address_list = document.contentValues(portal_type='Internet Protocol Address')
existing_address_list.sort(key=lambda x: {0: 1, 1: 2}[int(x.id == 'default_network_interface')])
for address in existing_address_list:
current_dict = {
'addr': address.getIpAddress(),
'netmask': address.getNetmask()
}
if current_dict in to_add_ip_dict_list:
to_add_ip_dict_list.remove(current_dict)
else:
# XXX - Only delete if Network interface are supposed to be the same
if additional_dict.has_key('network_interface'):
if address.getNetworkInterface('') and additional_dict['network_interface'] != address.getNetworkInterface():
continue
to_delete_ip_id_list.append(address.getId())
for address in to_add_ip_dict_list:
if to_delete_ip_id_list:
id = to_delete_ip_id_list.pop()
address_document = document.restrictedTraverse(id)
else:
kw = {'portal_type': 'Internet Protocol Address'}
if not len(document.objectIds(portal_type='Internet Protocol Address')):
kw.update(id='default_network_address')
address_document = document.newContent(**kw)
address_document.edit(
ip_address=address['addr'],
netmask=address['netmask'],
**additional_dict
)
if to_delete_ip_id_list:
document.deleteContent(to_delete_ip_id_list)
# Getting existing partitions
existing_partition_dict = {}
for c in context.contentValues(portal_type="Computer Partition"):
existing_partition_dict[c.getReference()] = c
# update computer data
quantity = len(computer_dict['partition_list'])
if context.getQuantity() != quantity:
context.edit(quantity=quantity)
compareAndUpdateAddressList(context, [{'addr': computer_dict['address'], 'netmask': computer_dict['netmask']}])
expected_partition_dict = {}
for send_partition in computer_dict['partition_list']:
partition = existing_partition_dict.get(send_partition['reference'], None)
expected_partition_dict[send_partition['reference']] = True
if partition is None:
partition = context.newContent(portal_type='Computer Partition')
partition.validate()
partition.markFree()
elif partition.getSlapState() == 'inactive':
# Reactivate partition
partition.markFree(comment="Reactivated by slapformat")
if partition.getValidationState() == "invalidated":
partition.validate(comment="Reactivated by slapformat")
if partition.getReference() != send_partition['reference']:
partition.edit(reference=send_partition['reference'])
network_interface = send_partition['tap']['name']
compareAndUpdateAddressList(partition, send_partition['address_list'], {'network_interface': network_interface})
tap_addr_list = []
additional_dict = {'network_interface': 'route_' + network_interface}
if send_partition['tap'].has_key('ipv4_addr') and send_partition['tap']['ipv4_addr']:
tap_addr_list.append({
'addr': send_partition['tap']['ipv4_addr'],
'netmask': send_partition['tap']['ipv4_netmask']
})
additional_dict['gateway_ip_address'] = send_partition['tap']['ipv4_gateway']
additional_dict['network_address'] = send_partition['tap']['ipv4_network']
compareAndUpdateAddressList(partition, tap_addr_list, additional_dict)
# Desactivate all other partitions
for key, value in existing_partition_dict.items():
if key not in expected_partition_dict:
if value.getSlapState() == "free":
value.markInactive(comment="Desactivated by slapformat")
if value.getValidationState() == "validated":
value.invalidate(comment="Desactivated by slapformat")
......@@ -48,96 +48,6 @@
</object>
</value>
</item>
<item>
<key> <string>_body</string> </key>
<value> <string>def compareAndUpdateAddressList(document, address_list, additional_dict=None):\n
if additional_dict is None:\n
additional_dict = {}\n
to_delete_ip_id_list = []\n
to_add_ip_dict_list = address_list[:]\n
existing_address_list = document.contentValues(portal_type=\'Internet Protocol Address\')\n
existing_address_list.sort(key=lambda x: {0: 1, 1: 2}[int(x.id == \'default_network_interface\')])\n
for address in existing_address_list:\n
current_dict = {\n
\'addr\': address.getIpAddress(),\n
\'netmask\': address.getNetmask()\n
}\n
if current_dict in to_add_ip_dict_list:\n
to_add_ip_dict_list.remove(current_dict)\n
else:\n
# XXX - Only delete if Network interface are supposed to be the same\n
if additional_dict.has_key(\'network_interface\'):\n
if address.getNetworkInterface(\'\') and additional_dict[\'network_interface\'] != address.getNetworkInterface():\n
continue\n
to_delete_ip_id_list.append(address.getId())\n
\n
for address in to_add_ip_dict_list:\n
if to_delete_ip_id_list:\n
id = to_delete_ip_id_list.pop()\n
address_document = document.restrictedTraverse(id)\n
else:\n
kw = {\'portal_type\': \'Internet Protocol Address\'}\n
if not len(document.objectIds(portal_type=\'Internet Protocol Address\')):\n
kw.update(id=\'default_network_address\')\n
address_document = document.newContent(**kw)\n
address_document.edit(\n
ip_address=address[\'addr\'],\n
netmask=address[\'netmask\'],\n
**additional_dict\n
)\n
if to_delete_ip_id_list:\n
document.deleteContent(to_delete_ip_id_list)\n
\n
\n
# Getting existing partitions\n
existing_partition_dict = {}\n
for c in context.contentValues(portal_type="Computer Partition"):\n
existing_partition_dict[c.getReference()] = c\n
\n
# update computer data\n
quantity = len(computer_dict[\'partition_list\'])\n
if context.getQuantity() != quantity:\n
context.edit(quantity=quantity)\n
\n
compareAndUpdateAddressList(context, [{\'addr\': computer_dict[\'address\'], \'netmask\': computer_dict[\'netmask\']}])\n
expected_partition_dict = {}\n
for send_partition in computer_dict[\'partition_list\']:\n
partition = existing_partition_dict.get(send_partition[\'reference\'], None)\n
expected_partition_dict[send_partition[\'reference\']] = True\n
if partition is None:\n
partition = context.newContent(portal_type=\'Computer Partition\')\n
partition.validate()\n
partition.markFree()\n
elif partition.getSlapState() == \'inactive\':\n
# Reactivate partition\n
partition.markFree(comment="Reactivated by slapformat")\n
\n
if partition.getValidationState() == "invalidated":\n
partition.validate(comment="Reactivated by slapformat")\n
if partition.getReference() != send_partition[\'reference\']:\n
partition.edit(reference=send_partition[\'reference\'])\n
network_interface = send_partition[\'tap\'][\'name\']\n
compareAndUpdateAddressList(partition, send_partition[\'address_list\'], {\'network_interface\': network_interface})\n
tap_addr_list = []\n
additional_dict = {\'network_interface\': \'route_\' + network_interface}\n
if send_partition[\'tap\'].has_key(\'ipv4_addr\') and send_partition[\'tap\'][\'ipv4_addr\']:\n
tap_addr_list.append({\n
\'addr\': send_partition[\'tap\'][\'ipv4_addr\'],\n
\'netmask\': send_partition[\'tap\'][\'ipv4_netmask\']\n
})\n
additional_dict[\'gateway_ip_address\'] = send_partition[\'tap\'][\'ipv4_gateway\']\n
additional_dict[\'network_address\'] = send_partition[\'tap\'][\'ipv4_network\']\n
compareAndUpdateAddressList(partition, tap_addr_list, additional_dict)\n
\n
# Desactivate all other partitions\n
for key, value in existing_partition_dict.items():\n
if key not in expected_partition_dict:\n
if value.getSlapState() == "free":\n
value.markInactive(comment="Desactivated by slapformat")\n
if value.getValidationState() == "validated":\n
value.invalidate(comment="Desactivated by slapformat")\n
</string> </value>
</item>
<item>
<key> <string>_params</string> </key>
<value> <string>computer_dict</string> </value>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment