def test_get_identity(self):
     identity = common.get_identity('schedules')
     self.assertEqual(identity, 'name', 'Schedules did not get proper identity {}'.format(identity))
     identity = common.get_identity('inventory')
     self.assertEqual(identity, 'name', 'Inventory did not get proper identity {}'.format(identity))
     identity = common.get_identity('user')
     self.assertEqual(identity, 'username', 'User did not get proper identity {}'.format(identity))
 def test_get_identity(self):
     identity = common.get_identity('schedules')
     self.assertEqual(
         identity, 'name',
         'Schedules did not get proper identity {}'.format(identity))
     identity = common.get_identity('inventory')
     self.assertEqual(
         identity, 'name',
         'Inventory did not get proper identity {}'.format(identity))
     identity = common.get_identity('user')
     self.assertEqual(
         identity, 'username',
         'User did not get proper identity {}'.format(identity))
    def go_ham(self, all=False, asset_input=None):
        stdout = click.get_text_stream('stdout')
        stdin = click.get_text_stream('stdin')

        assets_from_input = common.get_assets_from_input(all, asset_input)

        stdout.write(
            "Please confirm that you want to clean the Tower instance by typing 'YES': "
        )
        response = stdin.readline()

        if response.strip() != 'YES':
            stdout.write("\nAborting request to empty the instance\n")
            return

        self.print_intro()

        for asset_type in common.SEND_ORDER[::-1]:
            if asset_type not in assets_from_input:
                continue

            identifier = common.get_identity(asset_type)

            assets_to_remove = []
            if assets_from_input[asset_type]['all']:
                resources = tower_cli.get_resource(asset_type).list(
                    all_pages=True)
                if 'results' not in resources:
                    continue
                assets_to_remove = assets_to_remove + resources['results']
            else:
                for name in assets_from_input[asset_type]['names']:
                    try:
                        resource = tower_cli.get_resource(asset_type).get(
                            **{identifier: name})
                        assets_to_remove.append(resource)
                    except TowerCLIError:
                        self.print_header_row(asset_type, name)
                        self.log_ok("Asset does not exist")

            for asset in assets_to_remove:
                self.print_header_row(asset_type, asset[identifier])

                if 'managed_by_tower' in asset and asset['managed_by_tower']:
                    self.log_warn(
                        "{} is managed by tower and can not be deleted".format(
                            asset[identifier]))
                    continue

                try:
                    tower_cli.get_resource(asset_type).delete(asset['id'])
                    self.log_change("Asset removed")
                except Exception as e:
                    self.log_error("Unable to delete : {}".format(e))

        self.print_recap()
Exemple #4
0
    def export_assets(self, all, asset_input):

        # Extract and consolidate all of the items we got on the command line
        assets_to_export = common.get_assets_from_input(all, asset_input)

        # These will be returned from this method
        exported_objects = []

        for asset_type in assets_to_export:

            # Load the API options for this asset_type of asset
            types_api_options = common.get_api_options(asset_type)

            # Now we are going to extract the objects from Tower and put them into an array for processing
            acquired_assets_to_export = []
            identifier = common.get_identity(asset_type)

            # Now we are either going to get everything or just one item and append that to the assets_to_export
            if assets_to_export[asset_type]['all']:
                resources = tower_cli.get_resource(asset_type).list(
                    all_pages=True)
                if 'results' not in resources:
                    continue
                acquired_assets_to_export = acquired_assets_to_export + resources[
                    'results']
            else:
                for name in assets_to_export[asset_type]['names']:
                    try:
                        resource = tower_cli.get_resource(asset_type).get(
                            **{identifier: name})
                    except TowerCLIError as e:
                        raise TowerCLIError(
                            "Unable to get {} named {} : {}".format(
                                asset_type, name, e))
                    acquired_assets_to_export.append(resource)

            # Next we are going to loop over the objects we got from Tower
            for asset in acquired_assets_to_export:

                # If this object is managed_by_tower then move on
                if 'managed_by_tower' in asset and asset['managed_by_tower']:
                    continue

                # Resolve the dependencies
                common.resolve_asset_dependencies(asset, asset_type)

                # Create a new object with the ASSET_TYPE_KEY and merge the options in from the object we got
                exported_asset = {common.ASSET_TYPE_KEY: asset_type}
                common.map_node_to_post_options(types_api_options, asset,
                                                exported_asset)

                # Clean up any $encrypted$ values
                common.remove_encrypted_values(exported_asset)

                # Special cases for touch up
                if asset_type == 'project':
                    # Exported projects that are not manual don't need a local path
                    common.remove_local_path_from_scm_project(exported_asset)

                # Next we are going to go after any of
                for relation in tower_cli.get_resource(asset_type).related:
                    if common.ASSET_RELATION_KEY not in exported_asset:
                        exported_asset[common.ASSET_RELATION_KEY] = {}

                    if relation == 'workflow_nodes':
                        exported_asset[common.ASSET_RELATION_KEY][
                            relation] = common.extract_workflow_nodes(asset)

                    elif relation == 'survey_spec':
                        survey_spec = tower_cli.get_resource(
                            asset_type).survey(asset['id'])
                        exported_asset[
                            common.ASSET_RELATION_KEY][relation] = survey_spec

                    elif relation == 'host' or relation == 'inventory_source':
                        exported_asset[common.ASSET_RELATION_KEY][relation] = \
                            common.extract_inventory_relations(asset, relation)['items']

                    elif relation == 'group':
                        exported_asset[common.ASSET_RELATION_KEY][relation] = \
                            common.extract_inventory_groups(asset)['items']

                    elif relation == 'notification_templates':
                        for notification_type in common.NOTIFICATION_TYPES:
                            exported_asset[common.ASSET_RELATION_KEY][notification_type] = \
                                common.extract_notifications(asset, notification_type)

                    elif relation == 'extra_credentials':
                        exported_asset[common.ASSET_RELATION_KEY][relation] =\
                            common.extract_extra_credentials(asset)['items']

                    elif relation == 'schedules':
                        exported_asset[common.ASSET_RELATION_KEY][relation] =\
                            common.extract_schedules(asset)['items']

                # If this asset type is in the RESOURCE_FIELDS of the Role object than export its roles
                if asset_type in RESOURCE_FIELDS:
                    if common.ASSET_RELATION_KEY not in exported_asset:
                        exported_asset[common.ASSET_RELATION_KEY] = {}
                    exported_asset[common.ASSET_RELATION_KEY][
                        'roles'] = common.extract_roles(asset)['items']

                # Finally add the object to the list of objects that are being exported
                exported_objects.append(exported_asset)

        return exported_objects
Exemple #5
0
    def export_assets(self, all, asset_input):
        # Extract and consolidate all of the items we got on the command line
        assets_to_export = common.get_assets_from_input(all, asset_input)

        # These will be returned from this method
        exported_objects = []

        for asset_type in assets_to_export:
            # Load the API options for this asset_type of asset
            types_api_options = common.get_api_options(asset_type)

            # Now we are going to extract the objects from Tower and put them into an array for processing
            acquired_assets_to_export = []
            identifier = common.get_identity(asset_type)

            # Now we are either going to get everything or just one item and append that to the assets_to_export
            if assets_to_export[asset_type]['all']:
                resources = tower_cli.get_resource(asset_type).list(all_pages=True)
                if 'results' not in resources:
                    continue
                acquired_assets_to_export = acquired_assets_to_export + resources['results']
            else:
                for name in assets_to_export[asset_type]['names']:
                    try:
                        resource = tower_cli.get_resource(asset_type).get(**{identifier: name})
                    except TowerCLIError as e:
                        raise TowerCLIError("Unable to get {} named {} : {}".format(asset_type, name, e))
                    acquired_assets_to_export.append(resource)

            # Next we are going to loop over the objects we got from Tower
            for asset in acquired_assets_to_export:
                # If this object is managed_by_tower then move on
                if 'managed_by_tower' in asset and asset['managed_by_tower']:
                    continue

                # Resolve the dependencies
                common.resolve_asset_dependencies(asset, asset_type)

                # Create a new object with the ASSET_TYPE_KEY and merge the options in from the object we got
                exported_asset = {common.ASSET_TYPE_KEY: asset_type}
                common.map_node_to_post_options(types_api_options, asset, exported_asset)

                # Clean up any $encrypted$ values
                common.remove_encrypted_values(exported_asset)

                # Special cases for touch up
                if asset_type == 'project':
                    # Exported projects that are not manual don't need a local path
                    common.remove_local_path_from_scm_project(exported_asset)

                # Next we are going to go after any of
                for relation in tower_cli.get_resource(asset_type).related:
                    if common.ASSET_RELATION_KEY not in exported_asset:
                        exported_asset[common.ASSET_RELATION_KEY] = {}

                    if relation == 'workflow_nodes':
                        exported_asset[common.ASSET_RELATION_KEY][relation] = common.extract_workflow_nodes(asset)

                    elif relation == 'survey_spec':
                        survey_spec = tower_cli.get_resource(asset_type).survey(asset['id'])
                        exported_asset[common.ASSET_RELATION_KEY][relation] = survey_spec

                    elif relation == 'host' or relation == 'inventory_source':
                        exported_asset[common.ASSET_RELATION_KEY][relation] = \
                            common.extract_inventory_relations(asset, relation)['items']

                    elif relation == 'group':
                        exported_asset[common.ASSET_RELATION_KEY][relation] = \
                            common.extract_inventory_groups(asset)['items']

                    elif relation == 'notification_templates':
                        for notification_type in common.NOTIFICATION_TYPES:
                            exported_asset[common.ASSET_RELATION_KEY][notification_type] = \
                                common.extract_notifications(asset, notification_type)

                    elif relation == 'extra_credentials':
                        exported_asset[common.ASSET_RELATION_KEY][relation] =\
                            common.extract_extra_credentials(asset)['items']

                    elif relation == 'schedules':
                        exported_asset[common.ASSET_RELATION_KEY][relation] =\
                            common.extract_schedules(asset)['items']

                    elif relation == 'labels':
                        exported_asset[common.ASSET_RELATION_KEY][relation] =\
                            common.extract_labels(asset)['items']

                # If this asset type is in the RESOURCE_FIELDS of the Role object than export its roles
                if asset_type in RESOURCE_FIELDS:
                    if common.ASSET_RELATION_KEY not in exported_asset:
                        exported_asset[common.ASSET_RELATION_KEY] = {}
                    exported_asset[common.ASSET_RELATION_KEY]['roles'] = common.extract_roles(asset)['items']

                # Finally add the object to the list of objects that are being exported
                exported_objects.append(exported_asset)

        return exported_objects