Example #1
0
    def connect_purestorage_fa(self,
                               target,
                               username,
                               password=None,
                               api_token=None,
                               verify_https=True):
        """Create a session (verified or unverified) with the requested FlashArray.

        Args:
            target (str): FlashArray IP address or FQDN.
            username (str): Authenticated user for FlashArray.
            password (str): Password for FlashArray authenticated user.
            api_token (str): Authenticated API token for FlashArray.
            verify_https (bool): Whether or not the session should be verified.

        Returns:
            fa_instance (purestorage.FlashArray): Verified or unverified session to FlashArray.
        """
        # If end-user is using CLI and doesn't want to type their password in clear text on screen they can use this
        # option to input their password.
        if not (api_token or password):
            password = getpass.getpass(prompt='FlashArray Password: ',
                                       stream=None)

        if not verify_https:
            urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
            fa_instance = FlashArray(target,
                                     username=username,
                                     password=password,
                                     api_token=api_token,
                                     verify_https=verify_https)
        else:
            fa_instance = FlashArray(target, username, password, api_token)

        return fa_instance
Example #2
0
def create_connection(module, array):
    """Create connection between arrays"""
    changed = True
    if not module.check_mode:
        remote_array = module.params["target_url"]
        user_agent = "%(base)s %(class)s/%(version)s (%(platform)s)" % {
            "base": "Ansible",
            "class": __name__,
            "version": 1.2,
            "platform": platform.platform(),
        }
        try:
            remote_system = FlashArray(
                module.params["target_url"],
                api_token=module.params["target_api"],
                user_agent=user_agent,
            )
            connection_key = remote_system.get(
                connection_key=True)["connection_key"]
            remote_array = remote_system.get()["array_name"]
            api_version = array._list_available_rest_versions()
            # TODO: Refactor when FC async is supported
            if (FC_REPL_VERSION in api_version
                    and module.params["transport"].lower() == "fc"):
                if module.params["connection"].lower() == "async":
                    module.fail_json(
                        msg=
                        "Asynchronous replication not supported using FC transport"
                    )
                array_connection = flasharray.ArrayConnectionPost(
                    type="sync-replication",
                    management_address=module.params["target_url"],
                    replication_transport="fc",
                    connection_key=connection_key,
                )
                array = get_array(module)
                res = array.post_array_connections(
                    array_connection=array_connection)
                if res.status_code != 200:
                    module.fail_json(msg="Array Connection failed. Error: {0}".
                                     format(res.errors[0].message))
            else:
                array.connect_array(
                    module.params["target_url"],
                    connection_key,
                    [module.params["connection"]],
                )
        except Exception:
            module.fail_json(msg="Failed to connect to remote array {0}.".
                             format(remote_array))
    module.exit_json(changed=changed)
 def test_init_with_username_password(self, mock_request):
     mock_request.side_effect = iter([
         self.rest_version_data,
         self.api_token_data,
         self.username_data,
     ])
     array = FlashArray(self.target, self.username, self.password)
     expected = [
         mock.call(array,
                   "GET",
                   "https://{0}/api/api_version".format(self.target),
                   reestablish_session=False),
         mock.call(array,
                   "POST",
                   "auth/apitoken", {
                       "username": self.username,
                       "password": self.password
                   },
                   reestablish_session=False),
         mock.call(array,
                   "POST",
                   "auth/session",
                   self.api_token_data,
                   reestablish_session=False),
     ]
     assert array._target == self.target
     assert array._rest_version == "1.1"
     assert array._renegotiate_rest_version == True
     assert array._api_token == self.api_token
     assert mock_request.call_args_list == expected
    def test_init_verify_https(self, mock_request):
        mock_request.side_effect = iter([
            self.rest_version_data,
            self.username_data,
        ])
        cert_path = '/etc/ssl/certs/ca-cert.crt'
        array = FlashArray(self.target,
                           api_token=self.api_token,
                           verify_https=True,
                           ssl_cert=cert_path)
        expected = [
            mock.call(array,
                      "GET",
                      "https://{0}/api/api_version".format(self.target),
                      reestablish_session=False),
            mock.call(array,
                      "POST",
                      "auth/session",
                      self.api_token_data,
                      reestablish_session=False),
        ]

        mock_request.assert_has_calls(expected)
        assert cert_path == array._ssl_cert
        assert array._verify_https
 def test_init_with_version(self, mock_request):
     mock_request.side_effect = iter([
         {
             "version": ["0.1", "1.1", "1.0", "1.2", "1.3"]
         },
         self.username_data,
     ])
     array = FlashArray(self.target,
                        api_token=self.api_token,
                        rest_version="1.0")
     expected = [
         mock.call(array,
                   "GET",
                   "https://{0}/api/api_version".format(self.target),
                   reestablish_session=False),
         mock.call(array,
                   "POST",
                   "auth/session",
                   self.api_token_data,
                   reestablish_session=False),
     ]
     assert array._target == self.target
     assert array._rest_version == "1.0"
     assert array._renegotiate_rest_version == False
     assert array._api_token == self.api_token
     assert mock_request.call_args_list == expected
    def stream_events(self, inputs, ew):
        """
        Description: Splunk Enterprise calls the modular input,
                     streams XML describing the inputs to stdin,
                     and waits for XML on stdout describing events.
        """

        for self.input_name, self.input_item in inputs.inputs.iteritems():
            array = self.input_item["Array"]
            username = self.input_item["Username"]
            password = self.input_item["Password"]
            session_key = self._input_definition.metadata["session_key"]
            self.USERNAME = username
            try:
                # If the password is not masked, mask it.
                if password != self.MASK:
                    self.encrypt_password(username, password, session_key)
                    self.mask_password(session_key, username)

                self.CLEAR_PASSWORD = self.get_password(session_key, username)
            except Exception as e:
                ew.log("ERROR", "Error: %s" % str(e))

            ew.log(
                "INFO",
                "Starting PureStorage Array REST input processing:  ARRAY=%s USERNAME=%s"
                % (array, username))
            logger.info(
                "Starting PureStorage Array REST input processing:  ARRAY=%s USERNAME=%s"
                % (array, username))
            try:
                connection = FlashArray(array, username, self.CLEAR_PASSWORD)

                logger.info("Flash Array connection successful")

                # Alert and messages details
                self.get_message_details(ew, connection, array,
                                         self.input_name)

                # Array details
                self.get_array_details(ew, connection, array, self.input_name)

                # Volume details
                self.get_volume_details(ew, connection, array, self.input_name)

                # Volume snapshot and protection group details
                self.get_volume_snap_details(ew, connection, array,
                                             self.input_name)

                # Host details
                self.get_host_details(ew, connection, array, self.input_name)

                logger.info("Modular input data dump successful")
            except Exception as e:
                ew.log("ERROR",
                       "Error in modular input script Exception: %s" % e)
                logger.error('Error in modular input script Exception: %s' % e)
                logger.error(traceback.format_exc())
                raise e
Example #7
0
def create_connection(module, array):
    """Create connection between arrays"""
    changed = True
    if not module.check_mode:
        remote_array = module.params['target_url']
        user_agent = '%(base)s %(class)s/%(version)s (%(platform)s)' % {
            'base': 'Ansible',
            'class': __name__,
            'version': 1.2,
            'platform': platform.platform()
        }
        try:
            remote_system = FlashArray(module.params['target_url'],
                                       api_token=module.params['target_api'],
                                       user_agent=user_agent)
            connection_key = remote_system.get(connection_key=True)['connection_key']
            remote_array = remote_system.get()['array_name']
            array.connect_array(module.params['target_url'], connection_key, [module.params['connection']])
        except Exception:
            module.fail_json(msg="Failed to connect to remote array {0}.".format(remote_array))
    module.exit_json(changed=changed)
Example #8
0
import purestorage
from purestorage import FlashArray

# Never do this in prod. SSL warning are there for a reason.
import urllib3
urllib3.disable_warnings()

#Variables
fa_url = "myarray.fqdn"
fa_username = "******"
fa_password = "******"

#Start Session
array = FlashArray(fa_url, fa_username, fa_password)

#Create Volume
array.create_volume("david-python-vol01", "10G")

#Create Hosts
array.create_host("david-python-host1",
                  iqnlist=["iqn.1998-01.com.vmware:david-python-host1"])
array.create_host("david-python-host2",
                  iqnlist=["iqn.1998-01.com.vmware:david-python-host2"])

#Create Host Group and Add Host1 and Host2
array.create_hgroup("david-python-hostgroup",
                    hostlist=["david-python-host1", "david-python-host2"])

#Connect Volume to Host Group
array.connect_hgroup("david-python-hostgroup", "david-python-vol01")
Example #9
0
 def __init__(self, address, token, name):
     self.client = FlashArray(address, api_token=token)
     self.name = name
Example #10
0
class ArrayReport:
    def __init__(self, address, token, name):
        self.client = FlashArray(address, api_token=token)
        self.name = name

    def return_array_space(self, time):
        return self.client.get(space=True, historical=time)

    def return_host_groups(self):
        return self.client.list_hgroups(connect=True)

    def set_volumes(self):
        logger.info("Grouping Volumes by Host Group")
        data = self.return_host_groups()

        self.groups = {}
        for each in data:

            try:
                self.groups[each['name']].append(each['vol'])
            except:
                self.groups[each['name']] = []
                self.groups[each['name']].append(each['vol'])
        return self.groups

    def ret_volumes(self):
        return self.set_volumes()

    def get_historical(self):
        self.set_volumes()
        self.vol_history = {}
        self.hostg_vol = {}
        for group in self.groups:

            self.hostg_vol[group] = {}
            for vol_name in self.groups[group]:
                try:
                    vol_hist = self.client.get_volume(vol_name,
                                                      space=True,
                                                      historical='1y')
                    logger.info("Getting Volume History for volume: " +
                                vol_name)

                    tmphist = {}
                    for vol_entry in vol_hist:
                        if vol_entry['total'] == None:
                            vol_entry['total'] = 0
                        if vol_entry['snapshots'] == None:
                            vol_entry['snapshots'] = 0
                        if vol_entry['size'] == None:
                            vol_entry['size'] = 0
                        tmphist[vol_entry['time']] = vol_entry
                    self.vol_history[vol_name] = tmphist
                    self.hostg_vol[group][vol_name] = vol_hist
                    # self.hostg_vol[group][vol['name']] = tmphist

                except Exception as e:
                    pass

        return self.vol_history

    def calc_volumes(self):

        for group in self.groups:

            for volume in self.groups[group]:
                pass

    def calc_hgroups(self):
        logger.info("Calculating Host Group Data from Volume History")
        self.get_historical()

        self.all_gps = {}

        for group in self.groups:
            logger.info("Calculating for Group: " + group)

            # Find all the sample times
            # First get a set of all times that exist for volumes
            # in this host group
            times = set()
            for volume in self.groups[group]:
                try:
                    for htime in self.vol_history[volume]:
                        # print(htime)
                        times.add(htime)
                except:
                    pass

            self.all_gps[group] = {}

            for time in times:

                total = 0
                snapshots = 0
                size = 0

                try:
                    for volume in self.groups[group]:
                        total += self.vol_history[volume][time]['total']
                        snapshots += self.vol_history[volume][time][
                            'snapshots']
                        size += self.vol_history[volume][time]['size']

                except:
                    pass
                try:
                    self.all_gps[group][time]['total'] = total
                    self.all_gps[group][time]['snapshots'] = snapshots
                    self.all_gps[group][time]['size'] = size
                    self.all_gps[group][time]['date'] = time
                except:
                    self.all_gps[group][time] = {}
                    self.all_gps[group][time]['total'] = total
                    self.all_gps[group][time]['snapshots'] = snapshots
                    self.all_gps[group][time]['size'] = size
                    self.all_gps[group][time]['date'] = time

            self.oput = {}
            for each in self.all_gps:
                self.oput[each] = {}
                for gp in self.all_gps[each]:
                    dto = datetime.strptime(gp, '%Y-%m-%dT%H:%M:%SZ')
                    out = dto.timestamp()
                    self.oput[each][out] = self.all_gps[each][gp]

            ret_out = {}
            for group in self.oput:
                ret_out[group] = []
                for tstamp in sorted(self.oput[group].keys()):
                    ret_out[group].append(self.oput[group][tstamp])

        self.calculated_hgroups = ret_out

    def build_series_data(self, group):

        volumes = self.groups[group]
        ret_data = {}

        for volume in volumes:
            try:
                ret_data[volume] = self.hostg_vol[group][volume]
            except:
                pass

        return ret_data
Example #11
0
password = ''
try:
    password = options.passwd
except:
    pass

while not password:
    try:
        password = getpass.getpass()
    except EOFError:
        PrintWarning('Password may not be empty... try again...')

if not options.array:
    parser.error("Array not provided")
else:
    array = FlashArray(options.array, username, password)

### Verify Array ###
try:
    array_info = array.get()
    print "FlashArray {} (version {}) REST session established!".format(
           array_info['array_name'], array_info['version'])
except:
    print "Array not connected"
    sys.exit(1)

with open(properties) as json_data_file:
    vols = json.load(json_data_file)

### Create the Volumes ###
vols = createAsmVols(array,oracleServer,vols)
Example #12
0
#!python 

from purestorage import FlashArray

storage = FlashArray(target='172.16.128.20',username='******',password='******')

volume_detail = storage.get_volume("HRC-PURE-DS-ADMC-DR-01")

print('Name: {} Size: {}'.format(volume_detail['name'], volume_detail['size']))

for volume in storage.list_volumes():
    print(volume)