Beispiel #1
0
    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 updated_data.get('firmware'):
                updated_data['firmware'].pop('firmwareActivationType', 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
    def __present(self):

        if not self.data.get('hostname'):
            raise OneViewModuleValueError(
                self.MSG_MANDATORY_FIELD_MISSING.format("data.hostname"))

        self.current_resource = self.resource_client.get_by_name(
            self.data['hostname'])

        scope_uris = self.data.pop('scopeUris', None)

        result = dict()

        if not self.current_resource:
            self.current_resource = self.resource_client.add(self.data)
            result = dict(
                changed=True,
                msg=self.MSG_ADDED,
                ansible_facts={'server_hardware': self.current_resource.data})
        else:
            result = dict(
                changed=False,
                msg=self.MSG_ALREADY_PRESENT,
                ansible_facts={'server_hardware': self.current_resource.data})

        if scope_uris is not None:
            result = self.resource_scopes_set(result, 'server_hardware',
                                              scope_uris)

        return result
Beispiel #3
0
 def execute_module(self):
     if self.oneview_client.api_version < 600:
         raise OneViewModuleValueError(self.MSG_API_VERSION_ERROR)
     if self.state == 'present':
         return self.resource_present(self.RESOURCE_FACT_NAME)
     elif self.state == 'absent':
         return self.resource_absent()
    def execute_module(self):

        if self.state == 'present':
            return self.__present()
        elif self.state == 'multiple_servers_added':
            changed, msg, ansible_facts = self.__add_multiple_rack_mount_servers(
            )
        else:
            if not self.data.get('name'):
                raise OneViewModuleValueError(
                    self.MSG_MANDATORY_FIELD_MISSING.format("data.name"))

            if self.state == 'absent':
                return self.resource_absent(method='remove')
            else:
                if not self.current_resource:
                    raise OneViewModuleResourceNotFound(
                        self.MSG_SERVER_HARDWARE_NOT_FOUND)

                if self.state == 'power_state_set':
                    changed, msg, ansible_facts = self.__set_power_state()
                elif self.state == 'refresh_state_set':
                    changed, msg, ansible_facts = self.__set_refresh_state()
                elif self.state == 'ilo_firmware_version_updated':
                    changed, msg, ansible_facts = self.__update_mp_firmware_version(
                    )
                elif self.state == 'environmental_configuration_set':
                    changed, msg, ansible_facts = self.__set_environmental_configuration(
                    )
                else:
                    changed, msg, ansible_facts = self.__patch()

        return dict(changed=changed, msg=msg, ansible_facts=ansible_facts)
    def __update(self):
        if "newName" in self.data:
            new_resource = self.resource_client.get_by_name(
                self.data["newName"])
            if new_resource:
                raise OneViewModuleValueError(self.MSG_NEW_NAME_INVALID)
            self.data["name"] = self.data.pop("newName")

        merged_data = self.current_resource.data.copy()
        input_data = self.data.copy()

        if input_data.get("properties"):
            del input_data["properties"]
        if input_data.get("templateUri"):
            input_data["volumeTemplateUri"] = input_data.pop("templateUri")

        merged_data.update(input_data)

        if compare(self.current_resource.data, merged_data):
            changed = False
            msg = self.MSG_NO_CHANGES_PROVIDED
        else:
            changed = True
            self.current_resource.update(merged_data)
            msg = self.MSG_UPDATED

        return dict(
            changed=changed,
            msg=msg,
            ansible_facts=dict(storage_volume=self.current_resource.data))
    def __present(self):
        changed = False
        message = self.MSG_ALREADY_PRESENT

        configuration_data = self.data.copy()
        name = configuration_data.pop('newName',
                                      configuration_data.pop('name', None))
        rack_name = configuration_data.pop('rackName', None)
        calibrated_max_power = configuration_data.pop('calibratedMaxPower',
                                                      None)
        scope_uris = configuration_data.pop('scopeUris', None)

        if 'hostname' in self.data:
            resource_by_hostname = self.resource_client.get_by_hostname(
                self.data['hostname'])
            if not resource_by_hostname:
                self.current_resource = self.resource_client.add(
                    configuration_data)
                message = self.MSG_CREATED
                changed = True
            else:
                self.current_resource = resource_by_hostname

        if not self.current_resource:
            raise OneViewModuleValueError(self.MSG_ENCLOSURE_REQUIRED_FIELDS)

        if self.__name_has_changes(name):
            self.__replace_enclosure_name(name)
            changed = True
            message = self.MSG_UPDATED

        if self.__rack_name_has_changes(rack_name):
            self.__replace_enclosure_rack_name(rack_name)
            changed = True
            message = self.MSG_UPDATED

        if calibrated_max_power:
            self.__set_calibrated_max_power(calibrated_max_power)
            changed = True
            message = self.MSG_UPDATED

        resource = self.current_resource.data

        if scope_uris is not None:
            state = {
                'ansible_facts': {
                    'enclosure': self.current_resource.data
                },
                'changed': changed,
                'msg': message
            }
            result = self.resource_scopes_set(state, 'enclosure', scope_uris)
            resource = result['ansible_facts']['enclosure']
            changed = result['changed']
            message = result['msg']

        return changed, message, resource
Beispiel #7
0
    def __present(self):
        changed = False
        msg = ''

        if not self.current_resource:
            if 'credentials' not in self.data:
                raise OneViewModuleValueError(self.MSG_CREDENTIALS_MANDATORY)

            if self.oneview_client.api_version < 500:
                self.current_resource = self.resource_client.add(
                    self.data['credentials'])
            else:
                options = self.data['credentials'].copy()
                options['family'] = self.data.get('family', None)
                options['hostname'] = self.data.get('hostname', None)
                self.current_resource = self.resource_client.add(options)

            changed = True
            msg = self.MSG_ADDED

        else:
            resource = deepcopy(self.current_resource.data)
            data = self.data.copy()
            merged_data = dict_merge(resource, data)
            temp_list = []
            merged_data_copy = deepcopy(merged_data)
            if merged_data_copy.get('deviceSpecificAttributes') and merged_data_copy.get('deviceSpecificAttributes').get('discoveredPools') and \
                    merged_data_copy.get('deviceSpecificAttributes').get('managedPools'):
                for discoveredPool in merged_data_copy[
                        'deviceSpecificAttributes']['discoveredPools']:
                    for managedPool in merged_data['deviceSpecificAttributes'][
                            'managedPools']:
                        if discoveredPool['name'] == managedPool['name']:
                            temp_list.append(discoveredPool)
                            merged_data['deviceSpecificAttributes'][
                                'discoveredPools'].remove(discoveredPool)
                merged_data['deviceSpecificAttributes'][
                    'managedPools'] = temp_list

            # remove password, it cannot be used in comparison
            if 'credentials' in merged_data and 'password' in merged_data[
                    'credentials']:
                del merged_data['credentials']['password']

            if not compare(self.current_resource.data, merged_data):
                # update the resource
                self.current_resource.update(merged_data)
                # if not changed:
                changed = True
                msg = self.MSG_UPDATED
            else:
                msg = self.MSG_ALREADY_PRESENT

        return dict(
            changed=changed,
            msg=msg,
            ansible_facts=dict(storage_system=self.current_resource.data))
Beispiel #8
0
    def execute_module(self):

        if not self.data.get('name'):
            raise OneViewModuleValueError(self.MSG_MANDATORY_FIELD_MISSING)

        if self.state == 'present':
            return self._present()
        elif self.state == 'absent':
            return self.resource_absent()
    def __retrieve_differences(self, data, resource):
        if 'content' not in data:
            raise OneViewModuleValueError(self.MSG_CONTENT_ATTRIBUTE_MANDATORY)

        differences = self.i3s_client.plan_scripts.retrieve_differences(
            resource['uri'], data['content'])
        return dict(changed=False,
                    msg=self.MSG_DIFFERENCES_RETRIEVED,
                    ansible_facts=dict(plan_script_differences=differences))
Beispiel #10
0
    def execute_module(self):

        if not self.data.get("poolName") and not self.data.get("name"):
            raise OneViewModuleValueError(self.MSG_MANDATORY_FIELD_MISSING)

        if self.data.get("poolName"):
            self.current_resource = self.resource_client.get_by_name(
                self.data.get("poolName"))

        if self.state == 'present':
            return self.__present()
        elif self.state == 'absent':
            return self.__absent()
Beispiel #11
0
    def __find_interconnect(self):
        interconnect_ip = self.module.params['ip']
        if not interconnect_ip and not self.module.params["name"]:
            raise OneViewModuleValueError(self.MSG_MISSING_KEY)

        if not self.current_resource and interconnect_ip:
            interconnects = self.oneview_client.interconnects.get_by(
                'interconnectIP', interconnect_ip) or []
            if interconnects:
                self.current_resource = self.resource_client.get_by_uri(
                    interconnects[0]["uri"])

        if not self.current_resource:
            raise OneViewModuleResourceNotFound(
                self.MSG_INTERCONNECT_NOT_FOUND)
Beispiel #12
0
    def __get_resource_hostname(self, hostname_key, new_hostname_key):
        hostname = self.data.get(hostname_key, None)
        if 'credentials' in self.data and hostname is None:
            hostname = self.data['credentials'].get(hostname_key, None)

        if hostname:
            get_method = getattr(self.oneview_client.storage_systems,
                                 "get_by_{}".format(hostname_key))
            self.current_resource = get_method(hostname)

            if self.data['credentials'].get(new_hostname_key):
                self.data['credentials'][hostname_key] = self.data[
                    'credentials'].pop(new_hostname_key)
            elif self.data.get(new_hostname_key):
                self.data[hostname_key] = self.data.pop(new_hostname_key)

        if not hostname and not self.data.get("name"):
            raise OneViewModuleValueError(self.MSG_MANDATORY_FIELDS_MISSING)
    def __get_specific_attachment(self, params):

        attachment_uri = params.get('storageVolumeAttachmentUri')

        if attachment_uri:
            return [self.resource_client.get_by_uri(attachment_uri)]
        else:
            volume_uri = params.get('storageVolumeUri')
            profile_name = params.get('serverProfileName')

            if not profile_name or not (volume_uri or params.get('storageVolumeName')):
                raise OneViewModuleValueError(self.ATTACHMENT_KEY_REQUIRED)

            if not volume_uri and params.get('storageVolumeName'):
                volumes = self.oneview_client.volumes.get_by('name', params.get('storageVolumeName'))
                if volumes:
                    volume_uri = volumes[0]['uri']

            uri = self.__search_attachment_uri.format(volume_uri, profile_name)
            attachments = self.resource_client.get_by_uri(uri) or {}

            return [attachments.data]
    def __present(self, data, resource):

        changed = False
        msg = ''

        if "newName" in data:
            data["name"] = data.pop("newName")

        self.__replace_name_by_uris(data)
        self.__check_present_consistency(data)

        file_path = data.pop('localImageFilePath', None)

        if not resource:
            if data.get('osVolumeURI'):
                resource = self.i3s_client.golden_images.create(data)
                msg = self.MSG_CREATED
                changed = True
            elif file_path:
                resource = self.i3s_client.golden_images.upload(file_path, data)
                msg = self.MSG_UPLOADED
                changed = True
            else:
                raise OneViewModuleValueError(self.MSG_MISSING_MANDATORY_ATTRIBUTES)
        else:
            merged_data = resource.copy()
            merged_data.update(data)

            if not compare(resource, merged_data):
                resource = self.i3s_client.golden_images.update(merged_data)
                changed = True
                msg = self.MSG_UPDATED
            else:
                msg = self.MSG_ALREADY_PRESENT

        return changed, msg, dict(golden_image=resource)
Beispiel #15
0
 def __validate_key(self):
     if 'name' not in self.data:
         raise OneViewModuleValueError(self.MSG_KEY_REQUIRED)
     if 'logicalInterconnectUri' not in self.data and 'logicalInterconnectName' not in self.data:
         raise OneViewModuleValueError(self.MSG_KEY_REQUIRED)
 def __check_present_consistency(self, data):
     if data.get('osVolumeURI') and data.get('localImageFilePath'):
         raise OneViewModuleValueError(self.MSG_CANT_CREATE_AND_UPLOAD)
Beispiel #17
0
 def __validate_settings(self, data):
     if 'ethernetSettings' not in data and 'fcoeSettings' not in data:
         raise OneViewModuleValueError(self.MSG_NO_OPTIONS_PROVIDED)
Beispiel #18
0
 def __validate_options(self, subresource_type, data):
     if subresource_type not in data:
         raise OneViewModuleValueError(self.MSG_NO_OPTIONS_PROVIDED)