Ejemplo n.º 1
0
def retrieve_rack_resources(self):
    __method__ = 'tabs.retrieve_rack_resources'
    resources = []

    logging.debug("%s: before retrieving resources for rack: %s", __method__,
                  self.rack_id)

    # retrieve resources for the rack id passed in (rack_id may be -1 on
    # the initial pass)
    (rc, result_dict) = resource_mgr.list_resources(None, False, None, None,
                                                    False, False,
                                                    [self.rack_id])
    if rc != 0:
        messages.error(
            self.request,
            _('Unable to retrieve Operational'
              ' Management inventory information'
              ' for resources.'))
        logging.error(
            '%s: Unable to retrieve Operational Management'
            'inventory information. A Non-0 return code returned'
            ' from resource_mgr.list_resources.  The return code is:'
            ' %s', __method__, rc)
    else:
        all_resources = result_dict['resources']
        for raw_resource in all_resources:
            resources.append(resource.Resource(raw_resource))

    logging.debug("%s: Found %s resources", __method__, len(resources))
    return resources
Ejemplo n.º 2
0
    def allowed(self, request, datum):
        return False  # hide Remove Rack function for now
        __method__ = 'tables.RemoveRackLink.allowed'

        # The Remove Rack button should always be displayed, but we want
        # it to be disabled when there are any resources present.  For now
        # assume button is NOT disabled.
        disable_delete = False
        if self.rack_id != "":
            # list_resources(labels=None, isbriefly=False, device_types=None,
            # resourceids=None, list_device_id=False, is_detail=False,
            # racks=None)
            # Retrieve the resources for the selected rack
            logging.debug("%s: before retrieving resources for rack: %s",
                          __method__, self.rack_id)

            (rc, result_dict) = resource_mgr.list_resources(
                None, False, None, None, False, False, [self.rack_id])

            if rc != 0:
                # Unexpected.  Unable to retrieve rack information for selected
                # rack.  Log that fact, and allow the remove rack button to be
                # active
                msg = str('Unable to retrieve Operational Management inventory'
                          ' information for resources.')
                messages.error(request, msg)
                logging.error(
                    '%s: Unable to retrieve Operational Management'
                    ' inventory information. A Non-0 return code'
                    ' returned from resource_mgr.list_resources.'
                    ' The return code is: %s', __method__, rc)
            else:
                resources = result_dict['resources']
                # if the rack has any resources associated with it in the
                # inventory don't allow the user to delete it
                logging.debug(
                    "%s: got resource info for rack %s.  Number of "
                    "resources for this rack is: %s", __method__, self.rack_id,
                    len(resources))
                if len(resources) > 0:
                    disable_delete = True

        if disable_delete:
            # Add the disabled class to the button (if it's not already
            # there)
            if 'disabled' not in self.classes:
                self.classes = list(self.classes) + ['disabled']
        else:
            # Remove the disabled class from the button (if it's still there)
            if 'disabled' in self.classes:
                self.classes.remove('disabled')
        return True
Ejemplo n.º 3
0
    def get_data(self):
        try:
            __method__ = 'views.DetailView.get_data'
            failure_message = str(
                "Unable to retrieve detailed data for the resource")
            if "resource_id" in self.kwargs:
                try:
                    (rc, result_dict) = resource_mgr.list_resources(
                        None, False, None, [self.kwargs['resource_id']])
                except Exception as e:
                    logging.error(
                        "%s: Exception received trying to retrieve"
                        " resource information.  Exception is: %s", __method__,
                        e)
                    exceptions.handle(self.request, failure_message)
                    return
            else:
                # This is unexpected.  Resource details called with no context
                # of what to display.  Need to display an error message because the
                # view will not be primed with required data
                logging.error(
                    "%s: DetailView called with no resource id"
                    " context.", __method__)
                messages.error(self.request, failure_message)
                return

            if rc != 0:
                messages.error(self.request, failure_message)
                return
            else:
                # We should have at least one resource in the results...just
                # return the first value
                if len(result_dict['resources']) > 0:
                    return resource.Resource(result_dict['resources'][0])
                else:
                    logging.error(
                        "%s: list_resources returned no information for"
                        " resource with resource id %s", __method__,
                        self.kwargs["resource_id"])
                    messages.error(self.request, failure_message)
                    return
        except Exception:
            redirect = self.get_redirect_url()
            exceptions.handle(self.request,
                              _('Unable to retrieve resource details.'),
                              redirect=redirect)
Ejemplo n.º 4
0
    def get_object(self):
        __method__ = 'views.ChangePasswordView.get_object'
        failure_message = str("Unable to retrieve resource information for" +
                              " the resource having password changed.")
        if "resource_id" in self.kwargs:
            try:
                (rc, result_dict) = resource_mgr.list_resources(
                    None, False, None, [self.kwargs['resource_id']])
            except Exception as e:
                logging.error(
                    "%s: Exception received trying to retrieve"
                    " resource information.  Exception is: %s", __method__, e)
                exceptions.handle(self.request, failure_message)
                return
        else:
            # This is unexpected.  ChangePasswordView called with no context
            # of what to edit.  Need to display an error message because the
            # dialog will not be primed with required data
            logging.error("%s: ChangePasswordView called with no context.",
                          __method__)
            messages.error(self.request, failure_message)
            return

        if rc != 0:
            messages.error(self.request, failure_message)
            return
        else:
            # We should have at least one resource in the results...just
            # return the first value
            if len(result_dict['resources']) > 0:
                return result_dict['resources'][0]
            else:
                logging.error(
                    "%s: list_resources returned no information for"
                    " resource with resource id %s", __method__,
                    self.kwargs["resource_id"])
                messages.error(self.request, failure_message)
                return
Ejemplo n.º 5
0
def list_devices(args):
    labels = None
    types = None
    rack_ids = None
    if args.label:
        labels = get_strip_strings_array(str(args.label))
    if args.type:
        types = get_strip_strings_array(str(args.type))
    if args.rack:
        rack_ids = []
        racks = get_strip_strings_array(str(args.rack))
        for rack in racks:
            rack_id = rack_mgr.get_rack_id_by_label(rack)
            if rack_id is not None:
                rack_ids.append(rack_id)

    (rc, result_dict) = resource_mgr.list_resources(labels,
                                                    args.briefly,
                                                    types,
                                                    racks=rack_ids)

    result = ""
    try:
        SEPARATOR = ","
        labels_array = result_dict['column_titles']
        tags_array = result_dict['column_tags']

        if not args.briefly:
            # find the device id and remove from both arrays to generate list
            # result without the device id info.
            deviceid_index = tags_array.index('resourceid')
            del tags_array[deviceid_index]
            del labels_array[deviceid_index]

        # create the CSV header line
        header = ""
        for label in labels_array:
            if header:
                header += SEPARATOR
            header += label
        header += "\n"
        result += header

        rack_id_to_label_map = {}
        rc, rack_dict = rack_mgr.list_racks()
        racks = rack_dict['racks']
        for rack in racks:
            racklabel = rack['label']
            rackid = rack['rackid']
            rack_id_to_label_map[rackid] = racklabel

        devices = result_dict['resources']
        for device in devices:
            # per device create the CSV line for the device
            line = ""
            for tag in tags_array:
                if line:
                    line += SEPARATOR
                if tag == 'rackid':
                    # resolve the rackid to a label
                    rackid = device['rackid']
                    output_rack_label = rack_id_to_label_map[rackid]
                    line += output_rack_label
                elif tag == 'status':
                    line += str(device[tag])
                elif tag == 'statusTime':
                    line += str(device[tag])
                elif tag == 'auth_method':
                    line += str(device[tag])
                elif tag == 'capabilities' or tag == 'roles':
                    for item in device[tag]:
                        line += item + " "
                    line = line.rstrip()
                else:
                    # normal handling
                    line += device[tag] if device[tag] else ''
            line += "\n"
            result += line
    except KeyError:
        # if a key is missing the error will be in the message
        pass

    # if a message, now output it
    if result_dict['message']:
        result += "\n"
        result += result_dict['message']
    return (rc, result)