def save(self): interface_code = self.cleaned_data['file'].read() parser = InterfaceParser() interface = parser.parse(interface_code) with transaction.commit_on_success(): i = Interface(name=interface['interface_name'], interface_file=self.cleaned_data['file']) i.save() for method in interface['precondition_methods']: m = Method(name=method['method_name'], interface=i) m.save() for parameter in method['parameters']: mp = MethodParameter(method=m, name=parameter) mp.save() for trigger in interface['trigger_methods']: m = Method.objects.get(interface__name=interface['interface_name'], name=trigger) t = Trigger(method=m) t.save() # Send file to Mirri event_handler = EventHandler() try: client = MirriClient() client.upload_interface_file(interface['interface_name'], self.cleaned_data['file']) except (MirriConnectionError, MirriTimeoutError, MirriNotFoundError), e: logger.error('Interface file %s could not be posted to Mirri: %s' % (self.cleaned_data['file'].name, e)) event_handler.add_event('Interface file %s could not be posted to Mirri: %s' % (self.cleaned_data['file'].name, e)) else:
def save(self): action_code = self.cleaned_data['file'].read() parser = ActionParser() action = parser.parse(action_code) with transaction.commit_on_success(): a = Action(name=action['action_name'], precondition_expression=action['precondition_expression'], action_file=self.cleaned_data['file']) a.save() for parameter in action['parameters']: ap = ActionParameter(action=a, name=parameter['name'], parameter_position=parameter['position']) ap.save() action_devices = {} for key, device in action['devices'].items(): ad = ActionDevice(action=a, name=key, parameter_position=device['parameter_position']) ad.save() action_devices[key] = ad for interface_name in device['interfaces']: i = Interface.objects.get(name=interface_name) adi = ActionDeviceInterface(action_device=ad, interface=i) adi.save() for position, replacement in enumerate(action['precondition_replacements']): apm = ActionPreconditionMethod(expression_position=position, action=a, action_device=action_devices[replacement['device']]) m = Method.objects.get(name=replacement['method'], interface__name=replacement['interface']) apm.method = m apm.save() # Send file to Mirri event_handler = EventHandler() try: client = MirriClient() client.upload_action_file(self.cleaned_data['file']) except (MirriConnectionError, MirriTimeoutError, MirriNotFoundError), e: logger.error('Action file %s could not be posted to Mirri: %s' % (self.cleaned_data['file'].name, e)) event_handler.add_event('Action file %s could not be posted to Mirri: %s' % (self.cleaned_data['file'].name, e)) else:
logger.error(e) else: argument_values.append(state_value_argument.value) break mirri_payload.extend(argument_values) #logger.debug("mirri_payload %s, action_name %s" % (mirri_payload, action_name)) if action_name != "": init_parameters = body_parameters = mirri_payload #logger.debug('Action %s payload: %r' % (action_name, mirri_payload)) # Send post to Mirri client = MirriClient() try: response = client.start_action(action_name, init_parameters, body_parameters) except (MirriConnectionError, MirriTimeoutError, MirriNotFoundError), e: logger.error('The request to Mirri failed: %s' % e) event_handler.add_event('The request to Mirri failed: %s' % e) else: #set devices as reseved # action_devices = models.Device.objects.filter(mac_address__in=mirri_payload['device_ids'].keys()) # for dev in action_devices: # dev.is_reserved = True # dev.save() event_handler.add_event('Action %s sent to Mirri' % action_name) logger.debug(response) else: event_handler.add_event(u'No action was started' )