Beispiel #1
0
    def __present(self, data, resource):

        server_template_name = data.pop('serverProfileTemplateName', '')
        server_hardware_name = data.pop('serverHardwareName', '')
        server_template = None
        changed = False
        created = False

        ServerProfileReplaceNamesByUris().replace(self.oneview_client, data)

        if server_hardware_name:
            selected_server_hardware = self.__get_server_hardware_by_name(server_hardware_name)
            if not selected_server_hardware:
                raise HPOneViewValueError(self.MSG_HARDWARE_NOT_FOUND.format(server_hardware_name))
            data['serverHardwareUri'] = selected_server_hardware['uri']

        if server_template_name:
            server_template = self.oneview_client.server_profile_templates.get_by_name(server_template_name)
            if not server_template:
                raise HPOneViewValueError(self.MSG_TEMPLATE_NOT_FOUND.format(server_template_name))
            data['serverProfileTemplateUri'] = server_template['uri']
        elif data.get('serverProfileTemplateUri'):
            server_template = self.oneview_client.server_profile_templates.get(data['serverProfileTemplateUri'])

        if not resource:
            resource = self.__create_profile(data, server_template)
            changed = True
            created = True
            msg = self.MSG_CREATED
        else:
            # This allows unassigning a profile if a SH key is specifically passed in as None
            if not self.auto_assign_server_hardware:
                server_hardware_uri_exists = False
                if 'serverHardwareUri' in self.module.params['data'].keys() or 'serverHardwareName' in self.module.params['data'].keys():
                    server_hardware_uri_exists = True
                if data.get('serverHardwareUri') is None and server_hardware_uri_exists:
                    data['serverHardwareUri'] = None

            # Auto assigns a Server Hardware to Server Profile if auto_assign_server_hardware is True and no SH uris exist
            if not resource.get('serverHardwareUri') and not data.get('serverHardwareUri') and self.auto_assign_server_hardware:
                data['serverHardwareUri'] = self._auto_assign_server_profile(data, server_template)

            merged_data = ServerProfileMerger().merge_data(resource, data)

            self.__validations_for_os_custom_attributes(data, merged_data, resource)

            if not ResourceComparator.compare(resource, merged_data):

                resource = self.__update_server_profile(merged_data, resource)
                changed = True
                msg = self.MSG_UPDATED
            else:
                msg = self.MSG_ALREADY_PRESENT

        return created, changed, msg, resource
    def __present(self, data, template):

        ServerProfileReplaceNamesByUris().replace(self.oneview_client, data)

        data = self.__spt_from_sp(data) or data

        if not template:
            changed, msg, resource = self.__create(data)
        else:
            changed, msg, resource = self.__update(data, template)

        return dict(changed=changed,
                    msg=msg,
                    ansible_facts=dict(server_profile_template=resource))
    def __present(self):
        server_template_name = self.data.pop('serverProfileTemplateName', '')
        server_hardware_name = self.data.pop('serverHardwareName', '')
        changed = False
        created = False

        ServerProfileReplaceNamesByUris().replace(self.oneview_client,
                                                  self.data)

        if server_hardware_name:
            selected_server_hardware = self.__get_server_hardware_by_name(
                server_hardware_name)
            if not selected_server_hardware:
                raise OneViewModuleValueError(
                    self.MSG_HARDWARE_NOT_FOUND.format(server_hardware_name))
            self.data['serverHardwareUri'] = selected_server_hardware['uri']

        if server_template_name:
            self.server_template = self.server_profile_templates.get_by_name(
                server_template_name)
            if not self.server_template:
                raise OneViewModuleValueError(
                    self.MSG_TEMPLATE_NOT_FOUND.format(server_template_name))
            self.data['serverProfileTemplateUri'] = self.server_template.data[
                'uri']
        elif self.data.get('serverProfileTemplateUri'):
            self.server_template = self.server_profile_templates.get_by_uri(
                self.data['serverProfileTemplateUri'])

        if not self.current_resource:
            self.current_resource = self.__create_profile()
            changed = True
            created = True
            msg = self.MSG_CREATED
        else:
            # This allows unassigning a profile if a SH key is specifically passed in as None
            if not self.auto_assign_server_hardware:
                server_hardware_uri_exists = False
                if 'serverHardwareUri' in self.module.params['data'].keys(
                ) or 'serverHardwareName' in self.module.params['data'].keys():
                    server_hardware_uri_exists = True
                if self.data.get('serverHardwareUri'
                                 ) is None and server_hardware_uri_exists:
                    self.data['serverHardwareUri'] = None

            # Auto assigns a Server Hardware to Server Profile if auto_assign_server_hardware is True and no SH uris/enclosure uri and bay exist
            if not self.current_resource.data.get('serverHardwareUri') and not self.data.get('serverHardwareUri') and self.auto_assign_server_hardware \
                and not self.current_resource.data.get('enclosureUri') and not self.current_resource.data.get('enclosureBay') \
                    and not self.data.get('enclosureUri') and not self.data.get('enclosureBay'):
                self.data[
                    'serverHardwareUri'] = self._auto_assign_server_profile()

            merged_data = ServerProfileMerger().merge_data(
                self.current_resource.data, self.data)

            self.__validations_for_os_custom_attributes(
                merged_data, self.current_resource.data)

            # removed the below fields as part of idempotency checks
            updated_data = deepcopy(merged_data)
            updated_data.pop('initialScopeUris', None)

            if not compare(self.current_resource.data, updated_data):
                self.__update_server_profile(merged_data)
                changed = True
                msg = self.MSG_UPDATED
            else:
                msg = self.MSG_ALREADY_PRESENT

        return created, changed, msg, self.current_resource.data