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
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
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 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)
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")
def __init__(self, address, token, name): self.client = FlashArray(address, api_token=token) self.name = name
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)