Пример #1
0
class ElementSWAccount(object):
    """
    Element SW Account
    """
    def __init__(self):
        self.argument_spec = netapp_utils.ontap_sf_host_argument_spec()
        self.argument_spec.update(
            dict(
                state=dict(required=True, choices=['present', 'absent']),
                element_username=dict(required=True, type='str'),
                account_id=dict(required=False, type='int', default=None),
                new_element_username=dict(required=False,
                                          type='str',
                                          default=None),
                initiator_secret=dict(required=False, type='str'),
                target_secret=dict(required=False, type='str'),
                attributes=dict(required=False, type='dict'),
                status=dict(required=False, type='str'),
            ))

        self.module = AnsibleModule(argument_spec=self.argument_spec,
                                    supports_check_mode=True)

        params = self.module.params

        # set up state variables
        self.state = params['state']
        self.element_username = params['element_username']
        self.account_id = params['account_id']

        self.new_element_username = params['new_element_username']
        self.initiator_secret = params['initiator_secret']
        self.target_secret = params['target_secret']
        self.attributes = params['attributes']
        self.status = params['status']

        if HAS_SF_SDK is False:
            self.module.fail_json(
                msg="Unable to import the Element SW Python SDK")
        else:
            self.sfe = netapp_utils.create_sf_connection(module=self.module)

        self.elementsw_helper = NaElementSWModule(self.sfe)

        # add telemetry attributes
        if self.attributes is not None:
            self.attributes.update(
                self.elementsw_helper.set_element_attributes(
                    source='na_elementsw_account'))
        else:
            self.attributes = self.elementsw_helper.set_element_attributes(
                source='na_elementsw_account')

    def get_account(self):
        """
        Get Account
            :description: Get Account object from account id

            :return: Details about the account. None if not found.
            :rtype: object (Account object)
        """

        account_list = self.sfe.list_accounts()
        account_obj = None

        for account in account_list.accounts:
            if account.username == self.element_username:
                # Update self.account_id:
                if self.account_id is not None:
                    if account.account_id == self.account_id:
                        account_obj = account
                else:
                    self.account_id = account.account_id
                    account_obj = account

        return account_obj

    def create_account(self):
        """
        Create the Account
        """
        try:
            self.sfe.add_account(username=self.element_username,
                                 initiator_secret=self.initiator_secret,
                                 target_secret=self.target_secret,
                                 attributes=self.attributes)
        except Exception as e:
            self.module.fail_json(msg='Error creating account %s: %s)' %
                                  (self.element_username, to_native(e)),
                                  exception=traceback.format_exc())

    def delete_account(self):
        """
        Delete the Account
        """
        try:
            self.sfe.remove_account(account_id=self.account_id)

        except Exception as e:
            self.module.fail_json(msg='Error deleting account %s: %s' %
                                  (self.account_id, to_native(e)),
                                  exception=traceback.format_exc())

    def update_account(self):
        """
        Update the Account
        """
        try:
            self.sfe.modify_account(account_id=self.account_id,
                                    username=self.new_element_username,
                                    status=self.status,
                                    initiator_secret=self.initiator_secret,
                                    target_secret=self.target_secret,
                                    attributes=self.attributes)

        except Exception as e:
            self.module.fail_json(msg='Error updating account %s: %s' %
                                  (self.account_id, to_native(e)),
                                  exception=traceback.format_exc())

    def apply(self):
        """
        Process the account operation on the Element OS Cluster
        """
        changed = False
        account_exists = False
        update_account = False
        account_detail = self.get_account()

        if account_detail:
            account_exists = True

            if self.state == 'absent':
                changed = True

            elif self.state == 'present':
                # Check if we need to update the account

                if account_detail.username is not None and self.new_element_username is not None and \
                        account_detail.username != self.new_element_username:
                    update_account = True
                    changed = True

                elif account_detail.status is not None and self.status is not None \
                        and account_detail.status != self.status:
                    update_account = True
                    changed = True

                elif account_detail.initiator_secret is not None and self.initiator_secret is not None \
                        and account_detail.initiator_secret != self.initiator_secret:
                    update_account = True
                    changed = True

                elif account_detail.target_secret is not None and self.target_secret is not None \
                        and account_detail.target_secret != self.target_secret:
                    update_account = True
                    changed = True

                elif account_detail.attributes is not None and self.attributes is not None \
                        and account_detail.attributes != self.attributes:
                    update_account = True
                    changed = True
        else:
            if self.state == 'present' and self.status is None:
                changed = True

        if changed:
            if self.module.check_mode:
                pass
            else:
                if self.state == 'present':
                    if not account_exists:
                        self.create_account()
                    elif update_account:
                        self.update_account()

                elif self.state == 'absent':
                    self.delete_account()

        self.module.exit_json(changed=changed)
Пример #2
0
class ElementOSVolume(object):
    """
    Contains methods to parse arguments,
    derive details of  ElementSW objects
    and send requests to ElementOS via
    the ElementSW SDK
    """

    def __init__(self):
        """
        Parse arguments, setup state variables,
        check paramenters and ensure SDK is installed
        """
        self._size_unit_map = netapp_utils.SF_BYTE_MAP

        self.argument_spec = netapp_utils.ontap_sf_host_argument_spec()
        self.argument_spec.update(dict(
            state=dict(required=True, choices=['present', 'absent']),
            name=dict(required=True, type='str'),
            account_id=dict(required=True),
            enable512e=dict(type='bool', aliases=['512emulation']),
            qos=dict(required=False, type='dict', default=None),
            attributes=dict(required=False, type='dict', default=None),
            size=dict(type='int'),
            size_unit=dict(default='gb',
                           choices=['bytes', 'b', 'kb', 'mb', 'gb', 'tb',
                                    'pb', 'eb', 'zb', 'yb'], type='str'),

            access=dict(required=False, type='str', default=None, choices=['readOnly', 'readWrite',
                                                                           'locked', 'replicationTarget']),

        ))

        self.module = AnsibleModule(
            argument_spec=self.argument_spec,
            required_if=[
                ('state', 'present', ['size', 'enable512e'])
            ],
            supports_check_mode=True
        )

        param = self.module.params

        # set up state variables
        self.state = param['state']
        self.name = param['name']
        self.account_id = param['account_id']
        self.enable512e = param['enable512e']
        self.qos = param['qos']
        self.attributes = param['attributes']
        self.access = param['access']
        self.size_unit = param['size_unit']
        if param['size'] is not None:
            self.size = param['size'] * self._size_unit_map[self.size_unit]
        else:
            self.size = None

        if HAS_SF_SDK is False:
            self.module.fail_json(msg="Unable to import the ElementSW Python SDK")
        else:
            try:
                self.sfe = netapp_utils.create_sf_connection(module=self.module)
            except solidfire.common.ApiServerError:
                self.module.fail_json(msg="Unable to create the connection")

        self.elementsw_helper = NaElementSWModule(self.sfe)

        # add telemetry attributes
        if self.attributes is not None:
            self.attributes.update(self.elementsw_helper.set_element_attributes(source='na_elementsw_volume'))
        else:
            self.attributes = self.elementsw_helper.set_element_attributes(source='na_elementsw_volume')

    def get_account_id(self):
        """
            Return account id if found
        """
        try:
            # Update and return self.account_id
            self.account_id = self.elementsw_helper.account_exists(self.account_id)
            return self.account_id
        except Exception as err:
            self.module.fail_json(msg="Error: account_id %s does not exist" % self.account_id, exception=to_native(err))

    def get_volume(self):
        """
            Return volume details if found
        """
        # Get volume details
        volume_id = self.elementsw_helper.volume_exists(self.name, self.account_id)

        if volume_id is not None:
            # Return volume_details
            volume_details = self.elementsw_helper.get_volume(volume_id)
            if volume_details is not None:
                return volume_details
        return None

    def create_volume(self):
        """
        Create Volume

        :return: True if created, False if fails
        """
        try:
            self.sfe.create_volume(name=self.name,
                                   account_id=self.account_id,
                                   total_size=self.size,
                                   enable512e=self.enable512e,
                                   qos=self.qos,
                                   attributes=self.attributes)

        except Exception as err:
            self.module.fail_json(msg="Error provisioning volume %s of size %s" % (self.name, self.size),
                                  exception=to_native(err))

    def delete_volume(self, volume_id):
        """
         Delete and purge the volume using volume id

         :return: Success : True , Failed : False
        """
        try:
            self.sfe.delete_volume(volume_id=volume_id)
            self.sfe.purge_deleted_volume(volume_id=volume_id)
            # Delete method will delete and also purge the volume instead of moving the volume state to inactive.

        except Exception as err:
            # Throwing the exact error message instead of generic error message
            self.module.fail_json(msg=err.message,
                                  exception=to_native(err))

    def update_volume(self, volume_id):
        """

        Update the volume with the specified param

        :return: Success : True, Failed : False
        """
        try:
            self.sfe.modify_volume(volume_id,
                                   account_id=self.account_id,
                                   access=self.access,
                                   qos=self.qos,
                                   total_size=self.size,
                                   attributes=self.attributes)

        except Exception as err:
            # Throwing the exact error message instead of generic error message
            self.module.fail_json(msg=err.message,
                                  exception=to_native(err))

    def apply(self):
        # Perform pre-checks, call functions and exit
        changed = False
        volume_exists = False
        update_volume = False

        self.get_account_id()
        volume_detail = self.get_volume()

        if volume_detail:
            volume_exists = True
            volume_id = volume_detail.volume_id
            if self.state == 'absent':
                # Checking for state change(s) here, and applying it later in the code allows us to support
                # check_mode

                changed = True

            elif self.state == 'present':
                # Checking all the params for update operation
                if volume_detail.access is not None and self.access is not None and volume_detail.access != self.access:
                    update_volume = True
                    changed = True

                elif volume_detail.account_id is not None and self.account_id is not None \
                        and volume_detail.account_id != self.account_id:
                    update_volume = True
                    changed = True

                elif volume_detail.qos is not None and self.qos is not None:
                    """
                    Actual volume_detail.qos has ['burst_iops', 'burst_time', 'curve', 'max_iops', 'min_iops'] keys.
                    As only minOPS, maxOPS, burstOPS is important to consider, checking only these values.
                    """
                    volume_qos = volume_detail.qos.__dict__
                    if volume_qos['min_iops'] != self.qos['minIOPS'] or volume_qos['max_iops'] != self.qos['maxIOPS'] \
                       or volume_qos['burst_iops'] != self.qos['burstIOPS']:
                        update_volume = True
                        changed = True
                else:
                    # If check fails, do nothing
                    pass

                if volume_detail.total_size is not None and volume_detail.total_size != self.size:
                    size_difference = abs(float(volume_detail.total_size - self.size))
                    # Change size only if difference is bigger than 0.001
                    if size_difference / self.size > 0.001:
                        update_volume = True
                        changed = True

                else:
                    # If check fails, do nothing
                    pass

                if volume_detail.attributes is not None and self.attributes is not None and \
                        volume_detail.attributes != self.attributes:
                    update_volume = True
                    changed = True
        else:
            if self.state == 'present':
                changed = True

        result_message = ""

        if changed:
            if self.module.check_mode:
                result_message = "Check mode, skipping changes"
            else:
                if self.state == 'present':
                    if not volume_exists:
                        self.create_volume()
                        result_message = "Volume created"
                    elif update_volume:
                        self.update_volume(volume_id)
                        result_message = "Volume updated"

                elif self.state == 'absent':
                    self.delete_volume(volume_id)
                    result_message = "Volume deleted"

        self.module.exit_json(changed=changed, msg=result_message)
Пример #3
0
class ElementOSVolumeClone(object):
    """
    Contains methods to parse arguments,
    derive details of Element Software objects
    and send requests to Element OS via
    the Solidfire SDK
    """
    def __init__(self):
        """
        Parse arguments, setup state variables,
        check paramenters and ensure SDK is installed
        """
        self._size_unit_map = netapp_utils.SF_BYTE_MAP

        self.argument_spec = netapp_utils.ontap_sf_host_argument_spec()
        self.argument_spec.update(
            dict(
                name=dict(required=True),
                src_volume_id=dict(required=True),
                src_snapshot_id=dict(),
                account_id=dict(required=True),
                attributes=dict(type='dict', default=None),
                size=dict(type='int'),
                size_unit=dict(default='gb',
                               choices=[
                                   'bytes', 'b', 'kb', 'mb', 'gb', 'tb', 'pb',
                                   'eb', 'zb', 'yb'
                               ],
                               type='str'),
                access=dict(type='str',
                            default=None,
                            choices=[
                                'readOnly', 'readWrite', 'locked',
                                'replicationTarget'
                            ]),
            ))

        self.module = AnsibleModule(argument_spec=self.argument_spec,
                                    supports_check_mode=True)

        parameters = self.module.params

        # set up state variables
        self.name = parameters['name']
        self.src_volume_id = parameters['src_volume_id']
        self.src_snapshot_id = parameters['src_snapshot_id']
        self.account_id = parameters['account_id']
        self.attributes = parameters['attributes']

        self.size_unit = parameters['size_unit']
        if parameters['size'] is not None:
            self.size = parameters['size'] * \
                self._size_unit_map[self.size_unit]
        else:
            self.size = None
        self.access = parameters['access']

        if HAS_SF_SDK is False:
            self.module.fail_json(
                msg="Unable to import the SolidFire Python SDK")
        else:
            self.sfe = netapp_utils.create_sf_connection(module=self.module)

        self.elementsw_helper = NaElementSWModule(self.sfe)

        # add telemetry attributes
        if self.attributes is not None:
            self.attributes.update(
                self.elementsw_helper.set_element_attributes(
                    source='na_elementsw_volume_clone'))
        else:
            self.attributes = self.elementsw_helper.set_element_attributes(
                source='na_elementsw_volume_clone')

    def get_account_id(self):
        """
            Return account id if found
        """
        try:
            # Update and return self.account_id
            self.account_id = self.elementsw_helper.account_exists(
                self.account_id)
            return self.account_id
        except Exception as err:
            self.module.fail_json(msg="Error: account_id %s does not exist" %
                                  self.account_id,
                                  exception=to_native(err))

    def get_snapshot_id(self):
        """
            Return snapshot details if found
        """
        src_snapshot = self.elementsw_helper.get_snapshot(
            self.src_snapshot_id, self.src_volume_id)
        # Update and return self.src_snapshot_id
        if src_snapshot is not None:
            self.src_snapshot_id = src_snapshot.snapshot_id
            # Return src_snapshot
            return self.src_snapshot_id
        return None

    def get_src_volume_id(self):
        """
            Return volume id if found
        """
        src_vol_id = self.elementsw_helper.volume_exists(
            self.src_volume_id, self.account_id)
        if src_vol_id is not None:
            # Update and return self.volume_id
            self.src_volume_id = src_vol_id
            # Return src_volume_id
            return self.src_volume_id
        return None

    def clone_volume(self):
        """Clone Volume from source"""
        try:
            self.sfe.clone_volume(volume_id=self.src_volume_id,
                                  name=self.name,
                                  new_account_id=self.account_id,
                                  new_size=self.size,
                                  access=self.access,
                                  snapshot_id=self.src_snapshot_id,
                                  attributes=self.attributes)

        except Exception as err:
            self.module.fail_json(msg="Error creating clone %s of size %s" %
                                  (self.name, self.size),
                                  exception=to_native(err))

    def apply(self):
        """Perform pre-checks, call functions and exit"""
        changed = False
        result_message = ""

        if self.get_account_id() is None:
            self.module.fail_json(msg="Account id not found: %s" %
                                  (self.account_id))

        # there is only one state. other operations
        # are part of the volume module

        # ensure that a volume with the clone name
        # isn't already present
        if self.elementsw_helper.volume_exists(self.name,
                                               self.account_id) is None:
            # check for the source volume
            if self.get_src_volume_id() is not None:
                # check for a valid snapshot
                if self.src_snapshot_id and not self.get_snapshot_id():
                    self.module.fail_json(msg="Snapshot id not found: %s" %
                                          (self.src_snapshot_id))
                # change required
                changed = True
            else:
                self.module.fail_json(msg="Volume id not found %s" %
                                      (self.src_volume_id))

        if changed:
            if self.module.check_mode:
                result_message = "Check mode, skipping changes"
            else:
                self.clone_volume()
                result_message = "Volume cloned"

        self.module.exit_json(changed=changed, msg=result_message)
Пример #4
0
class ElementOSSnapshot(object):
    """
    Element OS Snapshot Manager
    """

    def __init__(self):
        self.argument_spec = netapp_utils.ontap_sf_host_argument_spec()
        self.argument_spec.update(dict(
            state=dict(required=False, choices=['present', 'absent'], default='present'),
            account_id=dict(required=True, type='str'),
            name=dict(required=False, type='str'),
            src_volume_id=dict(required=True, type='str'),
            retention=dict(required=False, type='str'),
            src_snapshot_id=dict(required=False, type='str'),
            enable_remote_replication=dict(required=False, type='bool'),
            expiration_time=dict(required=False, type='str'),
            snap_mirror_label=dict(required=False, type='str')
        ))

        self.module = AnsibleModule(
            argument_spec=self.argument_spec,
            supports_check_mode=True
        )

        input_params = self.module.params

        self.state = input_params['state']
        self.name = input_params['name']
        self.account_id = input_params['account_id']
        self.src_volume_id = input_params['src_volume_id']
        self.src_snapshot_id = input_params['src_snapshot_id']
        self.retention = input_params['retention']
        self.properties_provided = False

        self.expiration_time = input_params['expiration_time']
        if input_params['expiration_time'] is not None:
            self.properties_provided = True

        self.enable_remote_replication = input_params['enable_remote_replication']
        if input_params['enable_remote_replication'] is not None:
            self.properties_provided = True

        self.snap_mirror_label = input_params['snap_mirror_label']
        if input_params['snap_mirror_label'] is not None:
            self.properties_provided = True

        if self.state == 'absent' and self.src_snapshot_id is None:
            self.module.fail_json(
                msg="Please provide required parameter : snapshot_id")

        if HAS_SF_SDK is False:
            self.module.fail_json(
                msg="Unable to import the SolidFire Python SDK")
        else:
            self.sfe = netapp_utils.create_sf_connection(module=self.module)

        self.elementsw_helper = NaElementSWModule(self.sfe)

        # add telemetry attributes
        self.attributes = self.elementsw_helper.set_element_attributes(source='na_elementsw_snapshot')

    def get_account_id(self):
        """
            Return account id if found
        """
        try:
            # Update and return self.account_id
            self.account_id = self.elementsw_helper.account_exists(self.account_id)
            return self.account_id
        except Exception as err:
            self.module.fail_json(msg="Error: account_id %s does not exist" % self.account_id, exception=to_native(err))

    def get_src_volume_id(self):
        """
            Return volume id if found
        """
        src_vol_id = self.elementsw_helper.volume_exists(self.src_volume_id, self.account_id)
        if src_vol_id is not None:
            # Update and return self.volume_id
            self.src_volume_id = src_vol_id
            # Return src_volume_id
            return self.src_volume_id
        return None

    def get_snapshot(self, name=None):
        """
            Return snapshot details if found
        """
        src_snapshot = None
        if name is not None:
            src_snapshot = self.elementsw_helper.get_snapshot(name, self.src_volume_id)
        elif self.src_snapshot_id is not None:
            src_snapshot = self.elementsw_helper.get_snapshot(self.src_snapshot_id, self.src_volume_id)
        if src_snapshot is not None:
            # Update self.src_snapshot_id
            self.src_snapshot_id = src_snapshot.snapshot_id
        # Return src_snapshot
        return src_snapshot

    def create_snapshot(self):
        """
        Create Snapshot
        """
        try:
            self.sfe.create_snapshot(volume_id=self.src_volume_id,
                                     snapshot_id=self.src_snapshot_id,
                                     name=self.name,
                                     enable_remote_replication=self.enable_remote_replication,
                                     retention=self.retention,
                                     snap_mirror_label=self.snap_mirror_label,
                                     attributes=self.attributes)
        except Exception as exception_object:
            self.module.fail_json(
                msg='Error creating snapshot %s' % (
                    to_native(exception_object)),
                exception=traceback.format_exc())

    def modify_snapshot(self):
        """
        Modify Snapshot Properties
        """
        try:
            self.sfe.modify_snapshot(snapshot_id=self.src_snapshot_id,
                                     expiration_time=self.expiration_time,
                                     enable_remote_replication=self.enable_remote_replication,
                                     snap_mirror_label=self.snap_mirror_label)
        except Exception as exception_object:
            self.module.fail_json(
                msg='Error modify snapshot %s' % (
                    to_native(exception_object)),
                exception=traceback.format_exc())

    def delete_snapshot(self):
        """
        Delete Snapshot
        """
        try:
            self.sfe.delete_snapshot(snapshot_id=self.src_snapshot_id)
        except Exception as exception_object:
            self.module.fail_json(
                msg='Error delete snapshot %s' % (
                    to_native(exception_object)),
                exception=traceback.format_exc())

    def apply(self):
        """
        Check, process and initiate snapshot operation
        """
        changed = False
        snapshot_delete = False
        snapshot_create = False
        snapshot_modify = False
        result_message = None
        self.get_account_id()

        # Dont proceed if source volume is not found
        if self.get_src_volume_id() is None:
            self.module.fail_json(msg="Volume id not found %s" % self.src_volume_id)

        # Get snapshot details using source volume
        snapshot_detail = self.get_snapshot()

        if snapshot_detail:
            if self.properties_provided:
                if self.expiration_time != snapshot_detail.expiration_time:
                    changed = True
                else:   # To preserve value in case  parameter expiration_time is not defined/provided.
                    self.expiration_time = snapshot_detail.expiration_time

                if self.enable_remote_replication != snapshot_detail.enable_remote_replication:
                    changed = True
                else:   # To preserve value in case  parameter enable_remote_Replication is not defined/provided.
                    self.enable_remote_replication = snapshot_detail.enable_remote_replication

                if self.snap_mirror_label != snapshot_detail.snap_mirror_label:
                    changed = True
                else:   # To preserve value in case  parameter snap_mirror_label is not defined/provided.
                    self.snap_mirror_label = snapshot_detail.snap_mirror_label

        if self.account_id is None or self.src_volume_id is None or self.module.check_mode:
            changed = False
            result_message = "Check mode, skipping changes"
        elif self.state == 'absent' and snapshot_detail is not None:
            self.delete_snapshot()
            changed = True
        elif self.state == 'present' and snapshot_detail is not None:
            if changed:
                self.modify_snapshot()   # Modify Snapshot properties
            elif not self.properties_provided:
                if self.name is not None:
                    snapshot = self.get_snapshot(self.name)
                    # If snapshot with name already exists return without performing any action
                    if snapshot is None:
                        self.create_snapshot()  # Create Snapshot using parent src_snapshot_id
                        changed = True
                else:
                    self.create_snapshot()
                    changed = True
        elif self.state == 'present':
            if self.name is not None:
                snapshot = self.get_snapshot(self.name)
                # If snapshot with name already exists return without performing any action
                if snapshot is None:
                    self.create_snapshot()  # Create Snapshot using parent src_snapshot_id
                    changed = True
            else:
                self.create_snapshot()
                changed = True
        else:
            changed = False
            result_message = "No changes requested, skipping changes"

        self.module.exit_json(changed=changed, msg=result_message)
Пример #5
0
class ElementSWInitiators(object):
    """
    Element Software Manage Element SW initiators
    """
    def __init__(self):
        self.argument_spec = netapp_utils.ontap_sf_host_argument_spec()

        self.argument_spec.update(dict(
            initiators=dict(
                type='list',
                options=dict(
                    name=dict(type='str', required=True),
                    alias=dict(type='str', default=None),
                    initiator_id=dict(type='int', default=None),
                    volume_access_groups=dict(type='list', default=None),
                    volume_access_group_id=dict(type='int', default=None),
                    attributes=dict(type='dict', default=None),
                )
            ),
            state=dict(choices=['present', 'absent'], default='present'),
        ))

        self.module = AnsibleModule(
            argument_spec=self.argument_spec,
            supports_check_mode=True
        )

        self.na_helper = NetAppModule()
        self.parameters = self.na_helper.set_parameters(self.module.params)

        if HAS_SF_SDK is False:
            self.module.fail_json(msg="Unable to import the SolidFire Python SDK")
        else:
            self.sfe = netapp_utils.create_sf_connection(module=self.module)

        self.elementsw_helper = NaElementSWModule(self.sfe)

        # iterate over each user-provided initiator
        for initiator in self.parameters.get('initiators'):
            # add telemetry attributes
            if 'attributes' in initiator:
                initiator['attributes'].update(self.elementsw_helper.set_element_attributes(source='na_elementsw_initiators'))
            else:
                initiator['attributes'] = self.elementsw_helper.set_element_attributes(source='na_elementsw_initiators')

    def compare_initiators(self, user_initiator, existing_initiator):
        """
        compare user input initiator with existing dict
        :return: True if matched, False otherwise
        """
        if user_initiator is None or existing_initiator is None:
            return False
        for param in user_initiator:
            # lookup initiator_name instead of name
            if param == 'name':
                if user_initiator['name'] == existing_initiator['initiator_name']:
                    pass
            elif user_initiator[param] == existing_initiator[param]:
                pass
            else:
                return True
        return False

    def initiator_to_dict(self, initiator_obj):
        """
        converts initiator class object to dict
        :return: reconstructed initiator dict
        """
        known_params = ['initiator_name',
                        'alias',
                        'initiator_id',
                        'volume_access_groups',
                        'volume_access_group_id',
                        'attributes']
        initiator_dict = {}

        # missing parameter cause error
        # so assign defaults
        for param in known_params:
            initiator_dict[param] = getattr(initiator_obj, param, None)
        return initiator_dict

    def find_initiator(self, id=None, name=None):
        """
        find a specific initiator
        :return: initiator dict
        """
        initiator_details = None
        if self.all_existing_initiators is None:
            return initiator_details
        for initiator in self.all_existing_initiators:
            # if name is provided or
            # if id is provided
            if name is not None:
                if initiator.initiator_name == name:
                    initiator_details = self.initiator_to_dict(initiator)
            elif id is not None:
                if initiator.initiator_id == id:
                    initiator_details = self.initiator_to_dict(initiator)
            else:
                # if neither id nor name provided
                # return everything
                initiator_details = self.all_existing_initiators
        return initiator_details

    def create_initiators(self, initiator):
        """
        create initiators
        """
        # create_initiators needs an array
        # so enclose this initiator in an array
        initiator_list = [initiator]
        try:
            self.sfe.create_initiators(initiator_list)
        except Exception as exception_object:
            self.module.fail_json(msg='Error creating initiator %s' % (to_native(exception_object)),
                                  exception=traceback.format_exc())

    def delete_initiators(self, initiator):
        """
        delete initiators
        """
        # delete_initiators needs an array
        # so enclose this initiator in an array
        initiator_id_array = [initiator]
        try:
            self.sfe.delete_initiators(initiator_id_array)
        except Exception as exception_object:
            self.module.fail_json(msg='Error deleting initiator %s' % (to_native(exception_object)),
                                  exception=traceback.format_exc())

    def modify_initiators(self, initiator, existing_initiator):
        """
        modify initiators
        """
        # create the new initiator dict
        # by merging old and new values
        merged_initiator = existing_initiator.copy()
        merged_initiator.update(initiator)

        # we MUST create an object before sending
        # the new initiator to modify_initiator
        initiator_object = ModifyInitiator(initiator_id=merged_initiator['initiator_id'],
                                           alias=merged_initiator['alias'],
                                           volume_access_group_id=merged_initiator['volume_access_group_id'],
                                           attributes=merged_initiator['attributes'])
        initiator_list = [initiator_object]
        try:
            self.sfe.modify_initiators(initiators=initiator_list)
        except Exception as exception_object:
            self.module.fail_json(msg='Error modifying initiator %s' % (to_native(exception_object)),
                                  exception=traceback.format_exc())

    def apply(self):
        """
        configure initiators
        """
        changed = False
        modify = None
        result_message = None

        # get all user provided initiators
        input_initiators = self.parameters.get('initiators')

        # get all initiators
        # store in a cache variable
        self.all_existing_initiators = self.sfe.list_initiators().initiators

        # iterate over each user-provided initiator
        for in_initiator in input_initiators:
            if self.parameters.get('state') == 'present':
                # check if initiator_id is provided and exists
                if 'initiator_id' in in_initiator and in_initiator['initiator_id'] is not None and \
                        self.find_initiator(id=in_initiator['initiator_id']) is not None:
                    if self.compare_initiators(in_initiator, self.find_initiator(id=in_initiator['initiator_id'])):
                        changed = True
                        result_message = 'modifying initiator(s)'
                        self.modify_initiators(in_initiator, self.find_initiator(id=in_initiator['initiator_id']))
                # otherwise check if name is provided and exists
                elif 'name' in in_initiator and in_initiator['name'] is not None and self.find_initiator(name=in_initiator['name']) is not None:
                    if self.compare_initiators(in_initiator, self.find_initiator(name=in_initiator['name'])):
                        changed = True
                        result_message = 'modifying initiator(s)'
                        self.modify_initiators(in_initiator, self.find_initiator(name=in_initiator['name']))
                # this is a create op if initiator doesn't exist
                else:
                    changed = True
                    result_message = 'creating initiator(s)'
                    self.create_initiators(in_initiator)
            elif self.parameters.get('state') == 'absent':
                # delete_initiators only processes ids
                # so pass ids of initiators to method
                if 'name' in in_initiator and in_initiator['name'] is not None and \
                        self.find_initiator(name=in_initiator['name']) is not None:
                    changed = True
                    result_message = 'deleting initiator(s)'
                    self.delete_initiators(self.find_initiator(name=in_initiator['name'])['initiator_id'])
                elif 'initiator_id' in in_initiator and in_initiator['initiator_id'] is not None and \
                        self.find_initiator(id=in_initiator['initiator_id']) is not None:
                    changed = True
                    result_message = 'deleting initiator(s)'
                    self.delete_initiators(in_initiator['initiator_id'])
        if self.module.check_mode is True:
            result_message = "Check mode, skipping changes"
        self.module.exit_json(changed=changed, msg=result_message)
class ElementSWCluster(object):
    """
    Element Software Initialize node with ownership for cluster formation
    """
    def __init__(self):
        self.argument_spec = netapp_utils.ontap_sf_host_argument_spec()
        self.argument_spec.update(
            dict(management_virtual_ip=dict(required=True, type='str'),
                 storage_virtual_ip=dict(required=True, type='str'),
                 replica_count=dict(required=False, type='str', default='2'),
                 cluster_admin_username=dict(required=False, type='str'),
                 cluster_admin_password=dict(required=False,
                                             type='str',
                                             no_log=True),
                 accept_eula=dict(required=True, type='bool'),
                 nodes=dict(required=False, type=list, default=None),
                 attributes=dict(required=False, type=list, default=None)))

        self.module = AnsibleModule(argument_spec=self.argument_spec,
                                    supports_check_mode=True)

        input_params = self.module.params

        self.management_virtual_ip = input_params['management_virtual_ip']
        self.storage_virtual_ip = input_params['storage_virtual_ip']
        self.replica_count = input_params['replica_count']
        self.accept_eula = input_params['accept_eula']
        self.attributes = input_params['attributes']
        self.nodes = input_params['nodes']

        if input_params['cluster_admin_username'] is None:
            self.cluster_admin_username = self.username
        else:
            self.cluster_admin_username = input_params[
                'cluster_admin_username']

        if input_params['cluster_admin_password']:
            self.cluster_admin_password = self.password
        else:
            self.cluster_admin_password = input_params[
                'cluster_admin_password']

        if HAS_SF_SDK is False:
            self.module.fail_json(
                msg="Unable to import the SolidFire Python SDK")
        else:
            self.sfe = netapp_utils.create_sf_connection(module=self.module,
                                                         port=442)

        self.elementsw_helper = NaElementSWModule(self.sfe)

        # add telemetry attributes
        if self.attributes is not None:
            self.attributes.update(
                self.elementsw_helper.set_element_attributes(
                    source='na_elementsw_cluster'))
        else:
            self.attributes = self.elementsw_helper.set_element_attributes(
                source='na_elementsw_cluster')

    def create_cluster(self):
        """
        Create Cluster
        """
        try:
            self.sfe.create_cluster(mvip=self.management_virtual_ip,
                                    svip=self.storage_virtual_ip,
                                    rep_count=self.replica_count,
                                    username=self.cluster_admin_username,
                                    password=self.cluster_admin_password,
                                    accept_eula=self.accept_eula,
                                    nodes=self.nodes,
                                    attributes=self.attributes)
        except Exception as exception_object:
            self.module.fail_json(msg='Error create cluster %s' %
                                  (to_native(exception_object)),
                                  exception=traceback.format_exc())

    def check_connection(self):
        """
        Check connections to mvip, svip  address.
        :description: To test connection to given IP addressed for mvip and svip

        :rtype: bool
        """
        try:
            mvip_test = self.sfe.test_connect_mvip(
                mvip=self.management_virtual_ip)
            svip_test = self.sfe.test_connect_svip(
                svip=self.storage_virtual_ip)

            if mvip_test.details.connected and svip_test.details.connected:
                return True
            else:
                return False
        except Exception as e:
            return False

    def apply(self):
        """
        Check connection and initialize node with cluster ownership
        """
        changed = False
        result_message = None
        if self.module.supports_check_mode and self.accept_eula:
            if self.check_connection():
                self.create_cluster()
                changed = True
            else:
                self.module.fail_json(
                    msg='Error connecting mvip and svip address')
        else:
            result_message = "Skipping changes, No change requested"
        self.module.exit_json(changed=changed, msg=result_message)
Пример #7
0
class ElementSWBackup(object):
    ''' class to handle backup operations '''

    def __init__(self):
        """
            Setup Ansible parameters and SolidFire connection
        """
        self.argument_spec = {}
        self.argument_spec.update(dict(

            src_hostname=dict(aliases=['hostname'], required=True, type='str'),
            src_username=dict(aliases=['username', 'user'], required=True, type='str'),
            src_password=dict(aliases=['password', 'pass'], required=True, type='str', no_log=True),
            src_volume_id=dict(aliases=['volume_id'], required=True, type='str'),
            dest_hostname=dict(required=False, type='str'),
            dest_username=dict(required=False, type='str'),
            dest_password=dict(required=False, type='str', no_log=True),
            dest_volume_id=dict(required=True, type='str'),
            format=dict(required=False, choices=['native', 'uncompressed'], default='native'),
            script=dict(required=False, type='str'),
            script_parameters=dict(required=False, type='dict')


        ))

        self.module = AnsibleModule(
            argument_spec=self.argument_spec,
            required_together=[['script', 'script_parameters']],
            supports_check_mode=True
        )
        if HAS_SF_SDK is False:
            self.module.fail_json(msg="Unable to import the SolidFire Python SDK")

        # If destination cluster details are not specified , set the destination to be the same as the source
        if self.module.params["dest_hostname"] is None:
            self.module.params["dest_hostname"] = self.module.params["src_hostname"]
        if self.module.params["dest_username"] is None:
            self.module.params["dest_username"] = self.module.params["src_username"]
        if self.module.params["dest_password"] is None:
            self.module.params["dest_password"] = self.module.params["src_password"]

        params = self.module.params

        # establish a connection to both source and destination sf clusters

        self.module.params["username"] = params["src_username"]
        self.module.params["password"] = params["src_password"]
        self.module.params["hostname"] = params["src_hostname"]
        self.src_connection = netapp_utils.create_sf_connection(self.module)
        self.module.params["username"] = params["dest_username"]
        self.module.params["password"] = params["dest_password"]
        self.module.params["hostname"] = params["dest_hostname"]
        self.dest_connection = netapp_utils.create_sf_connection(self.module)

        self.elementsw_helper = NaElementSWModule(self.sfe)

        # add telemetry attributes
        self.attributes = self.elementsw_helper.set_element_attributes(source='na_elementsw_backup')

    def apply(self):
        """
            Apply backup creation logic
        """
        self.create_backup()
        self.module.exit_json(changed=True)

    def create_backup(self):
        """
            Create backup
        """

        # Start volume write on destination cluster

        try:
            write_obj = self.dest_connection.start_bulk_volume_write(volume_id=self.module.params["dest_volume_id"],
                                                                     format=self.module.params["format"],
                                                                     attributes=self.attributes)
            write_key = write_obj.key
        except solidfire.common.ApiServerError as err:
            self.module.fail_json(msg="Error starting bulk write on destination cluster", exception=to_native(err))

        # Set script parameters if not passed by user
        # These parameters are equivalent to the options used when a backup is executed via the GUI

        if self.module.params["script"] is None and self.module.params["script_parameters"] is None:

            self.module.params["script"] = 'bv_internal.py'
            self.module.params["script_parameters"] = {"write": {
                "mvip": self.module.params["dest_hostname"],
                "username": self.module.params["dest_username"],
                "password": self.module.params["dest_password"],
                "key": write_key,
                "endpoint": "solidfire",
                "format": self.module.params["format"]},
                "range": {"lba": 0, "blocks": 244224}}

        # Start volume read on source cluster

        try:
            read_obj = self.src_connection.start_bulk_volume_read(self.module.params["src_volume_id"],
                                                                  self.module.params["format"],
                                                                  script=self.module.params["script"],
                                                                  script_parameters=self.module.params["script_parameters"],
                                                                  attributes=self.attributes)
        except solidfire.common.ApiServerError as err:
            self.module.fail_json(msg="Error starting bulk read on source cluster", exception=to_native(err))

        # Poll job status until it has completed
        # SF will automatically timeout if the job is not successful after certain amount of time

        completed = False
        while completed is not True:
            # Sleep between polling iterations to reduce api load
            time.sleep(2)
            try:
                result = self.src_connection.get_async_result(read_obj.async_handle, True)
            except solidfire.common.ApiServerError as err:
                self.module.fail_json(msg="Unable to check backup job status", exception=to_native(err))

            if result["status"] != 'running':
                completed = True
        if 'error' in result:
            self.module.fail_json(msg=result['error']['message'])
class ElementSWVlan(object):
    """ class to handle VLAN operations """
    def __init__(self):
        """
            Setup Ansible parameters and ElementSW connection
        """
        self.argument_spec = netapp_utils.ontap_sf_host_argument_spec()
        self.argument_spec.update(
            dict(state=dict(required=False,
                            choices=['present', 'absent'],
                            default='present'),
                 name=dict(required=False, type='str'),
                 vlan_tag=dict(required=True, type='str'),
                 svip=dict(required=False, type='str'),
                 netmask=dict(required=False, type='str'),
                 gateway=dict(required=False, type='str'),
                 namespace=dict(required=False, type='bool'),
                 attributes=dict(required=False, type='dict'),
                 address_blocks=dict(required=False, type='list')))

        self.module = AnsibleModule(argument_spec=self.argument_spec,
                                    supports_check_mode=True)

        if HAS_SF_SDK is False:
            self.module.fail_json(
                msg="Unable to import the SolidFire Python SDK")
        else:
            self.elem = netapp_utils.create_sf_connection(module=self.module)

        self.na_helper = NetAppModule()
        self.parameters = self.na_helper.set_parameters(self.module.params)

        self.elementsw_helper = NaElementSWModule(self.sfe)

        # add telemetry attributes
        if self.parameters['attributes'] is not None:
            self.parameters['attributes'].update(
                self.elementsw_helper.set_element_attributes(
                    source='na_elementsw_vlan'))
        else:
            self.parameters[
                'attributes'] = self.elementsw_helper.set_element_attributes(
                    source='na_elementsw_vlan')

    def validate_keys(self):
        """
            Validate if all required keys are present before creating
        """
        required_keys = ['address_blocks', 'svip', 'netmask', 'name']
        if all(item in self.parameters.keys()
               for item in required_keys) is False:
            self.module.fail_json(
                msg=
                "One or more required fields %s for creating VLAN is missing" %
                required_keys)
        addr_blk_fields = ['start', 'size']
        for address in self.parameters['address_blocks']:
            if 'start' not in address or 'size' not in address:
                self.module.fail_json(
                    msg=
                    "One or more required fields %s for address blocks is missing"
                    % addr_blk_fields)

    def create_network(self):
        """
            Add VLAN
        """
        try:
            self.validate_keys()
            create_params = self.parameters.copy()
            for key in [
                    'username', 'hostname', 'password', 'state', 'vlan_tag'
            ]:
                del create_params[key]
            self.elem.add_virtual_network(
                virtual_network_tag=self.parameters['vlan_tag'],
                **create_params)
        except solidfire.common.ApiServerError as err:
            self.module.fail_json(msg="Error creating VLAN %s" %
                                  self.parameters['vlan_name'],
                                  exception=to_native(err))

    def delete_network(self):
        """
            Remove VLAN
        """
        try:
            self.elem.remove_virtual_network(
                virtual_network_tag=self.parameters['vlan_tag'])
        except solidfire.common.ApiServerError as err:
            self.module.fail_json(msg="Error deleting VLAN %s" %
                                  self.parameters['vlan_tag'],
                                  exception=to_native(err))

    def modify_network(self, modify):
        """
            Modify the VLAN
        """
        try:
            self.elem.modify_virtual_network(
                virtual_network_tag=self.parameters['vlan_tag'], **modify)
        except solidfire.common.ApiServerError as err:
            self.module.fail_json(msg="Error modifying VLAN %s" %
                                  self.parameters['vlan_tag'],
                                  exception=to_native(err))

    def get_network_details(self):
        """
            Check existing VLANs
            :return: vlan details if found, None otherwise
            :type: dict
        """
        vlans = self.elem.list_virtual_networks(
            virtual_network_tag=self.parameters['vlan_tag'])
        vlan_details = dict()
        for vlan in vlans.virtual_networks:
            if vlan is not None:
                vlan_details['name'] = vlan.name
                vlan_details['address_blocks'] = list()
                for address in vlan.address_blocks:
                    vlan_details['address_blocks'].append({
                        'start': address.start,
                        'size': address.size
                    })
                vlan_details['svip'] = vlan.svip
                vlan_details['gateway'] = vlan.gateway
                vlan_details['netmask'] = vlan.netmask
                vlan_details['namespace'] = vlan.namespace
                vlan_details['attributes'] = vlan.attributes
                return vlan_details
        return None

    def apply(self):
        """
            Call create / delete / modify vlan methods
        """
        network = self.get_network_details()
        # calling helper to determine action
        cd_action = self.na_helper.get_cd_action(network, self.parameters)
        modify = self.na_helper.get_modified_attributes(
            network, self.parameters)
        if cd_action == "create":
            self.create_network()
        elif cd_action == "delete":
            self.delete_network()
        elif modify:
            self.modify_network(modify)
        self.module.exit_json(changed=self.na_helper.changed)
Пример #9
0
class ElementOSSnapshotRestore(object):
    """
    Element OS Restore from snapshot
    """
    def __init__(self):
        self.argument_spec = netapp_utils.ontap_sf_host_argument_spec()
        self.argument_spec.update(
            dict(account_id=dict(required=True, type='str'),
                 src_volume_id=dict(required=True, type='str'),
                 dest_volume_name=dict(required=True, type='str'),
                 src_snapshot_id=dict(required=True, type='str')))

        self.module = AnsibleModule(argument_spec=self.argument_spec,
                                    supports_check_mode=True)

        input_params = self.module.params

        self.account_id = input_params['account_id']
        self.src_volume_id = input_params['src_volume_id']
        self.dest_volume_name = input_params['dest_volume_name']
        self.src_snapshot_id = input_params['src_snapshot_id']

        if HAS_SF_SDK is False:
            self.module.fail_json(
                msg="Unable to import the SolidFire Python SDK")
        else:
            self.sfe = netapp_utils.create_sf_connection(module=self.module)

        self.elementsw_helper = NaElementSWModule(self.sfe)

        # add telemetry attributes
        self.attributes = self.elementsw_helper.set_element_attributes(
            source='na_elementsw_snapshot_restore')

    def get_account_id(self):
        """
            Get account id if found
        """
        try:
            # Update and return self.account_id
            self.account_id = self.elementsw_helper.account_exists(
                self.account_id)
            return self.account_id
        except Exception as err:
            self.module.fail_json(msg="Error: account_id %s does not exist" %
                                  self.account_id,
                                  exception=to_native(err))

    def get_snapshot_id(self):
        """
            Return snapshot details if found
        """
        src_snapshot = self.elementsw_helper.get_snapshot(
            self.src_snapshot_id, self.src_volume_id)
        # Update and return self.src_snapshot_id
        if src_snapshot:
            self.src_snapshot_id = src_snapshot.snapshot_id
            # Return self.src_snapshot_id
            return self.src_snapshot_id
        return None

    def restore_snapshot(self):
        """
        Restore Snapshot to Volume
        """
        try:
            self.sfe.clone_volume(volume_id=self.src_volume_id,
                                  name=self.dest_volume_name,
                                  snapshot_id=self.src_snapshot_id,
                                  attributes=self.attributes)
        except Exception as exception_object:
            self.module.fail_json(msg='Error restore snapshot %s' %
                                  (to_native(exception_object)),
                                  exception=traceback.format_exc())

    def apply(self):
        """
        Check, process and initiate restore snapshot to volume operation
        """
        changed = False
        result_message = None
        snapshot_detail = None
        self.get_account_id()
        src_vol_id = self.elementsw_helper.volume_exists(
            self.src_volume_id, self.account_id)

        if src_vol_id is not None:
            # Update self.src_volume_id
            self.src_volume_id = src_vol_id
            if self.get_snapshot_id() is not None:
                # Addressing idempotency by comparing volume does not exist with same volume name
                if self.elementsw_helper.volume_exists(
                        self.dest_volume_name, self.account_id) is None:
                    self.restore_snapshot()
                    changed = True
                else:
                    result_message = "No changes requested, Skipping changes"
            else:
                self.module.fail_json(msg="Snapshot id not found %s" %
                                      self.src_snapshot_id)
        else:
            self.module.fail_json(msg="Volume id not found %s" %
                                  self.src_volume_id)

        self.module.exit_json(changed=changed, msg=result_message)
Пример #10
0
class NetAppElementSWAdminUser(object):
    """
    Class to set, modify and delete admin users on ElementSW box
    """
    def __init__(self):
        """
        Initialize the NetAppElementSWAdminUser class.
        """
        self.argument_spec = netapp_utils.ontap_sf_host_argument_spec()
        self.argument_spec.update(
            dict(state=dict(required=True, choices=['present', 'absent']),
                 element_username=dict(required=True, type='str'),
                 element_password=dict(required=False, type='str',
                                       no_log=True),
                 acceptEula=dict(required=False, type='bool'),
                 access=dict(required=False, type='list')))

        self.module = AnsibleModule(argument_spec=self.argument_spec,
                                    supports_check_mode=True)

        param = self.module.params
        # set up state variables
        self.state = param['state']
        self.element_username = param['element_username']
        self.element_password = param['element_password']
        self.acceptEula = param['acceptEula']
        self.access = param['access']

        if HAS_SF_SDK is False:
            self.module.fail_json(
                msg="Unable to import the SolidFire Python SDK")
        else:
            self.sfe = netapp_utils.create_sf_connection(module=self.module)

        self.elementsw_helper = NaElementSWModule(self.sfe)

        # add telemetry attributes
        self.attributes = self.elementsw_helper.set_element_attributes(
            source='na_elementsw_admin_users')

    def does_admin_user_exist(self):
        """
        Checks to see if an admin user exists or not
        :return: True if the user exist, False if it dose not exist
        """
        admins_list = self.sfe.list_cluster_admins()
        for admin in admins_list.cluster_admins:
            if admin.username == self.element_username:
                return True
        return False

    def get_admin_user(self):
        """
        Get the admin user object
        :return: the admin user object
        """
        admins_list = self.sfe.list_cluster_admins()
        for admin in admins_list.cluster_admins:
            if admin.username == self.element_username:
                return admin
        return None

    def modify_admin_user(self):
        """
        Modify a admin user. If a password is set the user will be modified as there is no way to
        compare a new password with an existing one
        :return: if a user was modified or not
        """
        changed = False
        admin_user = self.get_admin_user()
        if self.access is not None and len(self.access) > 0:
            for access in self.access:
                if access not in admin_user.access:
                    changed = True
        if changed:
            self.sfe.modify_cluster_admin(
                cluster_admin_id=admin_user.cluster_admin_id,
                access=self.access,
                password=self.element_password,
                attributes=self.attributes)

        return changed

    def add_admin_user(self):
        """
        Add's a new admin user to the element cluster
        :return: nothing
        """
        self.sfe.add_cluster_admin(username=self.element_username,
                                   password=self.element_password,
                                   access=self.access,
                                   accept_eula=self.acceptEula,
                                   attributes=self.attributes)

    def delete_admin_user(self):
        """
        Deletes an existing admin user from the element cluster
        :return: nothing
        """
        admin_user = self.get_admin_user()
        self.sfe.remove_cluster_admin(
            cluster_admin_id=admin_user.cluster_admin_id)

    def apply(self):
        """
        determines which method to call to set, delete or modify admin users
        :return:
        """
        changed = False
        if self.state == "present":
            if self.does_admin_user_exist():
                changed = self.modify_admin_user()
            else:
                self.add_admin_user()
                changed = True
        else:
            if self.does_admin_user_exist():
                self.delete_admin_user()
                changed = True

        self.module.exit_json(changed=changed)
Пример #11
0
class ElementSWAccount(object):
    """
    Element SW Account
    """

    def __init__(self):
        self.argument_spec = netapp_utils.ontap_sf_host_argument_spec()
        self.argument_spec.update(dict(
            state=dict(required=True, choices=['present', 'absent']),
            element_username=dict(required=True, aliases=["account_id"], type='str'),
            from_name=dict(required=False, default=None),
            initiator_secret=dict(required=False, type='str'),
            target_secret=dict(required=False, type='str'),
            attributes=dict(required=False, type='dict'),
            status=dict(required=False, type='str'),
        ))

        self.module = AnsibleModule(
            argument_spec=self.argument_spec,
            supports_check_mode=True
        )

        params = self.module.params

        # set up state variables
        self.state = params.get('state')
        self.element_username = params.get('element_username')
        self.from_name = params.get('from_name')
        self.initiator_secret = params.get('initiator_secret')
        self.target_secret = params.get('target_secret')
        self.attributes = params.get('attributes')
        self.status = params.get('status')

        if HAS_SF_SDK is False:
            self.module.fail_json(msg="Unable to import the Element SW Python SDK")
        else:
            self.sfe = netapp_utils.create_sf_connection(module=self.module)

        self.elementsw_helper = NaElementSWModule(self.sfe)

        # add telemetry attributes
        if self.attributes is not None:
            self.attributes.update(self.elementsw_helper.set_element_attributes(source='na_elementsw_account'))
        else:
            self.attributes = self.elementsw_helper.set_element_attributes(source='na_elementsw_account')

    def get_account(self, username):
        """
        Get Account
            :description: Get Account object from account id or name

            :return: Details about the account. None if not found.
            :rtype: object (Account object)
        """

        account_list = self.sfe.list_accounts()

        for account in account_list.accounts:
            # Check and get account object for a given name
            if str(account.account_id) == username:
                return account
            elif account.username == username:
                return account
        return None

    def create_account(self):
        """
        Create the Account
        """
        try:
            self.sfe.add_account(username=self.element_username,
                                 initiator_secret=self.initiator_secret,
                                 target_secret=self.target_secret,
                                 attributes=self.attributes)
        except Exception as e:
            self.module.fail_json(msg='Error creating account %s: %s' % (self.element_username, to_native(e)),
                                  exception=traceback.format_exc())

    def delete_account(self):
        """
        Delete the Account
        """
        try:
            self.sfe.remove_account(account_id=self.account_id)

        except Exception as e:
            self.module.fail_json(msg='Error deleting account %s: %s' % (self.account_id, to_native(e)),
                                  exception=traceback.format_exc())

    def rename_account(self):
        """
        Rename the Account
        """
        try:
            self.sfe.modify_account(account_id=self.account_id,
                                    username=self.element_username,
                                    status=self.status,
                                    initiator_secret=self.initiator_secret,
                                    target_secret=self.target_secret,
                                    attributes=self.attributes)

        except Exception as e:
            self.module.fail_json(msg='Error renaming account %s: %s' % (self.account_id, to_native(e)),
                                  exception=traceback.format_exc())

    def update_account(self):
        """
        Update the Account if account already exists
        """
        try:
            self.sfe.modify_account(account_id=self.account_id,
                                    status=self.status,
                                    initiator_secret=self.initiator_secret,
                                    target_secret=self.target_secret,
                                    attributes=self.attributes)

        except Exception as e:
            self.module.fail_json(msg='Error updating account %s: %s' % (self.account_id, to_native(e)),
                                  exception=traceback.format_exc())

    def apply(self):
        """
        Process the account operation on the Element OS Cluster
        """
        changed = False
        update_account = False
        account_detail = self.get_account(self.element_username)

        if account_detail is None and self.state == 'present':
            changed = True

        elif account_detail is not None:
            # If account found
            self.account_id = account_detail.account_id

            if self.state == 'absent':
                changed = True
            else:
                # If state - present, check for any parameter of exising account needs modification.
                if account_detail.username is not None and self.element_username is not None and \
                        account_detail.username != self.element_username:
                    update_account = True
                    changed = True
                elif account_detail.status is not None and self.status is not None \
                        and account_detail.status != self.status:
                    update_account = True
                    changed = True

                elif account_detail.initiator_secret is not None and self.initiator_secret is not None \
                        and account_detail.initiator_secret != self.initiator_secret:
                    update_account = True
                    changed = True

                elif account_detail.target_secret is not None and self.target_secret is not None \
                        and account_detail.target_secret != self.target_secret:
                    update_account = True
                    changed = True

                elif account_detail.attributes is not None and self.attributes is not None \
                        and account_detail.attributes != self.attributes:
                    update_account = True
                    changed = True
        if changed:
            if self.module.check_mode:
                # Skipping the changes
                pass
            else:
                if self.state == 'present':
                    if update_account:
                        self.update_account()
                    else:
                        if self.from_name is not None:
                            # If from_name is defined
                            account_exists = self.get_account(self.from_name)
                            if account_exists is not None:
                                # If resource pointed by from_name exists, rename the account to name
                                self.account_id = account_exists.account_id
                                self.rename_account()
                            else:
                                # If resource pointed by from_name does not exists, error out
                                self.module.fail_json(msg="Resource does not exist : %s" % self.from_name)
                        else:
                            # If from_name is not defined, create from scratch.
                            self.create_account()
                elif self.state == 'absent':
                    self.delete_account()

        self.module.exit_json(changed=changed)
Пример #12
0
class ElementSWAccessGroup(object):
    """
    Element Software Volume Access Group
    """
    def __init__(self):

        self.argument_spec = netapp_utils.ontap_sf_host_argument_spec()
        self.argument_spec.update(
            dict(
                state=dict(required=True, choices=['present', 'absent']),
                from_name=dict(required=False, type='str'),
                name=dict(required=True,
                          aliases=["src_access_group_id"],
                          type='str'),
                initiators=dict(required=False, type='list'),
                volumes=dict(required=False, type='list'),
                account_id=dict(required=False, type='str'),
                virtual_network_id=dict(required=False, type='list'),
                virtual_network_tags=dict(required=False, type='list'),
                attributes=dict(required=False, type='dict'),
            ))

        self.module = AnsibleModule(argument_spec=self.argument_spec,
                                    required_if=[('state', 'present',
                                                  ['account_id'])],
                                    supports_check_mode=True)

        input_params = self.module.params

        # Set up state variables
        self.state = input_params['state']
        self.from_name = input_params['from_name']
        self.access_group_name = input_params['name']
        self.initiators = input_params['initiators']
        self.volumes = input_params['volumes']
        self.account_id = input_params['account_id']
        self.virtual_network_id = input_params['virtual_network_id']
        self.virtual_network_tags = input_params['virtual_network_tags']
        self.attributes = input_params['attributes']

        if HAS_SF_SDK is False:
            self.module.fail_json(
                msg="Unable to import the SolidFire Python SDK")
        else:
            self.sfe = netapp_utils.create_sf_connection(module=self.module)

        self.elementsw_helper = NaElementSWModule(self.sfe)

        # add telemetry attributes
        if self.attributes is not None:
            self.attributes.update(
                self.elementsw_helper.set_element_attributes(
                    source='na_elementsw_access_group'))
        else:
            self.attributes = self.elementsw_helper.set_element_attributes(
                source='na_elementsw_access_group')

    def get_access_group(self, name):
        """
        Get Access Group
            :description: Get Access Group object for a given name

            :return: object (Group object)
            :rtype: object (Group object)
        """
        access_groups_list = self.sfe.list_volume_access_groups()
        group_obj = None

        for group in access_groups_list.volume_access_groups:
            # Check  and get access_group object for a given name
            if str(group.volume_access_group_id) == name:
                group_obj = group
            elif group.name == name:
                group_obj = group

        return group_obj

    def get_account_id(self):
        # Validate account id
        # Return account_id if found, None otherwise
        try:
            account_id = self.elementsw_helper.account_exists(self.account_id)
            return account_id
        except solidfire.common.ApiServerError:
            return None

    def get_volume_id(self):
        # Validate volume_ids
        # Return volume ids if found, fail if not found
        volume_ids = []
        for volume in self.volumes:
            volume_id = self.elementsw_helper.volume_exists(
                volume, self.account_id)
            if volume_id:
                volume_ids.append(volume_id)
            else:
                self.module.fail_json(
                    msg='Specified volume %s does not exist' % volume)
        return volume_ids

    def create_access_group(self):
        """
        Create the Access Group
        """
        try:
            self.sfe.create_volume_access_group(
                name=self.access_group_name,
                initiators=self.initiators,
                volumes=self.volumes,
                virtual_network_id=self.virtual_network_id,
                virtual_network_tags=self.virtual_network_tags,
                attributes=self.attributes)
        except Exception as e:
            self.module.fail_json(
                msg="Error creating volume access group %s: %s" %
                (self.access_group_name, to_native(e)),
                exception=traceback.format_exc())

    def delete_access_group(self):
        """
        Delete the Access Group
        """
        try:
            self.sfe.delete_volume_access_group(
                volume_access_group_id=self.group_id)

        except Exception as e:
            self.module.fail_json(
                msg="Error deleting volume access group %s: %s" %
                (self.access_group_name, to_native(e)),
                exception=traceback.format_exc())

    def update_access_group(self):
        """
        Update the Access Group if the access_group already exists
        """
        try:
            self.sfe.modify_volume_access_group(
                volume_access_group_id=self.group_id,
                virtual_network_id=self.virtual_network_id,
                virtual_network_tags=self.virtual_network_tags,
                initiators=self.initiators,
                volumes=self.volumes,
                attributes=self.attributes)
        except Exception as e:
            self.module.fail_json(
                msg="Error updating volume access group %s: %s" %
                (self.access_group_name, to_native(e)),
                exception=traceback.format_exc())

    def rename_access_group(self):
        """
        Rename the Access Group to the new name
        """
        try:
            self.sfe.modify_volume_access_group(
                volume_access_group_id=self.from_group_id,
                virtual_network_id=self.virtual_network_id,
                virtual_network_tags=self.virtual_network_tags,
                name=self.access_group_name,
                initiators=self.initiators,
                volumes=self.volumes,
                attributes=self.attributes)
        except Exception as e:
            self.module.fail_json(
                msg="Error updating volume access group %s: %s" %
                (self.from_name, to_native(e)),
                exception=traceback.format_exc())

    def apply(self):
        """
        Process the access group operation on the Element Software Cluster
        """
        changed = False
        update_group = False

        input_account_id = self.account_id
        if self.account_id is not None:
            self.account_id = self.get_account_id()
        if self.state == 'present' and self.volumes is not None:
            if self.account_id:
                self.volumes = self.get_volume_id()
            else:
                self.module.fail_json(
                    msg='Error: Specified account id "%s" does not exist.' %
                    str(input_account_id))

        group_detail = self.get_access_group(self.access_group_name)

        if group_detail is not None:
            # If access group found
            self.group_id = group_detail.volume_access_group_id

            if self.state == "absent":
                self.delete_access_group()
                changed = True
            else:
                # If state - present, check for any parameter of exising group needs modification.
                if self.volumes is not None and len(self.volumes) > 0:
                    # Compare the volume list
                    if not group_detail.volumes:
                        # If access group does not have any volume attached
                        update_group = True
                        changed = True
                    else:
                        for volumeID in group_detail.volumes:
                            if volumeID not in self.volumes:
                                update_group = True
                                changed = True
                                break

                elif self.initiators is not None and group_detail.initiators != self.initiators:
                    update_group = True
                    changed = True

                elif self.virtual_network_id is not None or self.virtual_network_tags is not None:
                    update_group = True
                    changed = True

                if update_group:
                    self.update_access_group()

        else:
            # access_group does not exist
            if self.state == "present" and self.from_name is not None:
                group_detail = self.get_access_group(self.from_name)
                if group_detail is not None:
                    # If resource pointed by from_name exists, rename the access_group to name
                    self.from_group_id = group_detail.volume_access_group_id
                    self.rename_access_group()
                    changed = True
                else:
                    # If resource pointed by from_name does not exists, error out
                    self.module.fail_json(msg="Resource does not exist : %s" %
                                          self.from_name)
            elif self.state == "present":
                # If from_name is not defined, Create from scratch.
                self.create_access_group()
                changed = True

        self.module.exit_json(changed=changed)
Пример #13
0
class ElementSWAccessGroup(object):
    """
    Element Software Volume Access Group
    """

    def __init__(self):

        self.argument_spec = netapp_utils.ontap_sf_host_argument_spec()
        self.argument_spec.update(dict(
            state=dict(required=True, choices=['present', 'absent']),
            src_access_group_id=dict(required=False, type='str'),
            new_name=dict(required=False, type='str'),
            initiators=dict(required=False, type='list'),
            volumes=dict(required=False, type='list'),
            virtual_network_id=dict(required=False, type='list'),
            virtual_network_tags=dict(required=False, type='list'),
            attributes=dict(required=False, type='dict'),
        ))

        self.module = AnsibleModule(
            argument_spec=self.argument_spec,
            supports_check_mode=True
        )

        input_params = self.module.params

        # Set up state variables
        self.state = input_params['state']
        self.src_access_group_id = input_params['src_access_group_id']
        self.new_name = input_params['new_name']
        self.initiators = input_params['initiators']
        self.volumes = input_params['volumes']
        self.virtual_network_id = input_params['virtual_network_id']
        self.virtual_network_tags = input_params['virtual_network_tags']
        self.attributes = input_params['attributes']

        if self.state == "absent" and self.src_access_group_id is None:
            self.module.fail_json(msg="For delete operation, src_access_group_id parameter is required")

        if self.state == 'present' and self.new_name is None and self.src_access_group_id is None:
            self.module.fail_json(msg="new_name parameter or src_access_group is required parameter")

        if HAS_SF_SDK is False:
            self.module.fail_json(msg="Unable to import the SolidFire Python SDK")
        else:
            self.sfe = netapp_utils.create_sf_connection(module=self.module)

        self.elementsw_helper = NaElementSWModule(self.sfe)

        # add telemetry attributes
        if self.attributes is not None:
            self.attributes.update(self.elementsw_helper.set_element_attributes(source='na_elementsw_access_group'))
        else:
            self.attributes = self.elementsw_helper.set_element_attributes(source='na_elementsw_access_group')

    def get_access_group(self):
        """
        Get Access Group
            :description: Get Access Group object for a given src_access_group_id

            :return: object (Group object)
            :rtype: object (Group object)
        """
        access_groups_list = self.sfe.list_volume_access_groups()
        self.new_name_exists = False
        group_obj = None

        for group in access_groups_list.volume_access_groups:
            # Check  and get access_group object for a given src_access_group_id
            if self.src_access_group_id is not None and group_obj is None:
                if str(group.volume_access_group_id) == self.src_access_group_id:
                    group_obj = group
                elif group.name == self.src_access_group_id:
                    self.src_access_group_id = group.volume_access_group_id
                    group_obj = group
            # Check if new_name exists on the list
            if group_obj is not None and self.new_name is None:
                return group_obj
            elif group.name == self.new_name:
                self.new_name_exists = True
                return group_obj
        return group_obj

    def create_access_group(self):
        """
        Create the Access Group
        """
        try:
            self.sfe.create_volume_access_group(name=self.new_name,
                                                initiators=self.initiators,
                                                volumes=self.volumes,
                                                virtual_network_id=self.virtual_network_id,
                                                virtual_network_tags=self.virtual_network_tags,
                                                attributes=self.attributes)
        except Exception as e:
            self.module.fail_json(msg="Error creating volume access group %s: %s" %
                                  (self.name, to_native(e)), exception=traceback.format_exc())

    def delete_access_group(self):
        """
        Delete the Access Group
        """
        try:
            self.sfe.delete_volume_access_group(volume_access_group_id=self.src_access_group_id)

        except Exception as e:
            self.module.fail_json(msg="Error deleting volume access group %s: %s" %
                                  (self.src_access_group_id, to_native(e)),
                                  exception=traceback.format_exc())

    def update_access_group(self):
        """
        Update the Access Group
        """
        try:
            self.sfe.modify_volume_access_group(volume_access_group_id=self.src_access_group_id,
                                                virtual_network_id=self.virtual_network_id,
                                                virtual_network_tags=self.virtual_network_tags,
                                                name=self.new_name,
                                                initiators=self.initiators,
                                                volumes=self.volumes,
                                                attributes=self.attributes)
        except Exception as e:
            self.module.fail_json(msg="Error updating volume access group %s: %s" %
                                  (self.src_access_group_id, to_native(e)), exception=traceback.format_exc())

    def apply(self):
        """
        Process the access group operation on the Element Software Cluster
        """
        changed = False
        group_exists = False
        update_group = False
        group_detail = self.get_access_group()

        if self.module.check_mode is False and group_detail is not None:
            group_exists = True

            if self.state == "absent":
                self.delete_access_group()
                changed = True
            else:
                # Check if we need to update the group
                if self.new_name is not None and self.new_name_exists is False:
                    update_group = True
                    changed = True

                elif self.volumes is not None and group_detail.volumes != self.volumes:
                    update_group = True
                    changed = True

                elif self.initiators is not None and group_detail.initiators != self.initiators:
                    update_group = True
                    changed = True

                elif self.virtual_network_id is not None or self.virtual_network_tags is not None or \
                        self.attributes is not None:
                    update_group = True
                    changed = True

                if update_group:
                    self.update_access_group()

        elif self.module.check_mode is False and self.new_name_exists is False and self.new_name is not None and self.state == 'present':
            self.create_access_group()
            changed = True

        self.module.exit_json(changed=changed)