aristanetworks / cvprac

Other
46 stars 47 forks source link

Add function for provision_device and tests #110

Closed mharista closed 4 years ago

mharista commented 4 years ago

Similar to what is in Ansible module utils

mharista commented 4 years ago
def provision_device(self, app_name, device, container, configlets, imageBundle, create_task=True):
        '''Move a device from the undefined container to a target container.
            Optionally apply device-specific configlets and an imageBundle.
            Args:
                device (dict): Device Info
                container (dict): Container Info
                configlets (list): list of dicts with configlet key/name pairs
                imageBundle (dict): imageBundle name and key to apply to device
                create_task (boolean): Create task for this device provisioning sequence.
            Returns:
                response (dict): A dict that contains a status and a list of
                    task ids created (if any).
                    Ex: {u'data': {u'status': u'success', u'taskIds': [u'32']}}
        '''
        info = 'Provision device %s to container %s' % (device['fqdn'], container['name'])
        self.log.debug(info)
        # Allow for deviceId key name variations
        if 'systemMacAddress' not in device.keys() and 'key' in device.keys():
            device['systemMacAddress'] = device['key']
        if 'systemMacAddress' not in device.keys():
            raise KeyError("update_imageBundle: key or systemMacAddress not found in device object %s" % device['name'])
        # Allow for parentContainerId key name variations
        if 'parentContainerId' not in device.keys() and 'parentContainerKey' in device.keys():
            device['parentContainerId'] = device['parentContainerKey']
        if 'parentContainerId' not in device.keys():
            raise KeyError("parentContainerId not found in device object %s" % device['name'])
        # Add action for moving device to specified container
        try:
            self.move_device_to_container('%s:provision_device' % app_name, device, container,
                                          create_task=False)
        except Exception as e:
            self.log.debug('Provision Device - move %s : %s' % (device['fqdn'], e))
            raise Exception("provsion_device-move_to_container:%s" % e)
        # Don't save configlet action if there is an image bundle to add
        if 'key' in imageBundle.keys():
            configlet_task = False
        elif create_task:
            configlet_task = True
        else:
            configlet_task = False
        try:
            created_tasks = self.update_configlets_on_device(app_name, device, configlets, [],
                                                             configlet_task)
        except Exception as e:
            self.log.debug('Provision Device - configlets %s : %s' % (device['fqdn'], e))
            raise Exception("provsion_device-update_configlets:%s" % e)
        # If configlet action created tasks then don't action imageBundles
        # Skip if no imageBundles specified
        if not configlet_task and 'key' in imageBundle.keys():
            try:
                created_tasks = self.update_imageBundle_on_device(app_name, device, imageBundle, {},
                                                                  create_task)
            except Exception as e:
                self.log.debug('Provision Device - imageBundle %s : %s' % (device['fqdn'], e))
                raise Exception("provsion_device-update_imageBundle:%s" % e)
        return created_tasks
mharista commented 4 years ago

Maps to current deploy_device function. Will update deploy_device accordingly.

mharista commented 4 years ago

Updated deploy_device to be more in sync with provision_device from Ansible cvprac.

Fixed 6f263d2b45c60f6d0037df4f45fab9da5c774f7d