def test_policy(self): # check it's on VSAN self.assertTrue( vsan_info.is_on_vsan(self.VMDK_PATH), "is_on_vsan can't find file %s" % self.VMDK_PATH) # set policy policy_string = \ '(("hostFailuresToTolerate" i0) ("forceProvisioning" i1))' # same policy content with different space/tabs: same_policy = \ ' (( "hostFailuresToTolerate" \ti0) ("forceProvisioning" i1))' # different content: notsame_policy = \ '(("hostFailuresToTolerate" i0) ("forceProvisioning" i0))' err = vsan_info.set_policy(self.VMDK_PATH, policy_string) self.assertEqual(err, None, "failed to set") # get policy and check it p = vsan_info.get_policy(self.VMDK_PATH) self.assertTrue( vsan_info.same_policy(self.VMDK_PATH, p), "failed to compare with get_policy") self.assertTrue( vsan_info.same_policy(self.VMDK_PATH, policy_string), "failed to compare with original policy") self.assertTrue( vsan_info.same_policy(self.VMDK_PATH, same_policy), "failed to compare with same policy, different tabs") self.assertFalse( vsan_info.same_policy(self.VMDK_PATH, notsame_policy), "failed to compare with different policy")
def test_policy(self): # check it's on VSAN self.assertTrue(vsan_info.is_on_vsan(self.VMDK_PATH), "is_on_vsan can't find file %s" % self.VMDK_PATH) # set policy policy_string = \ '(("hostFailuresToTolerate" i0) ("forceProvisioning" i1))' # same policy content with different space/tabs: same_policy = \ ' (( "hostFailuresToTolerate" \ti0) ("forceProvisioning" i1))' # different content: notsame_policy = \ '(("hostFailuresToTolerate" i0) ("forceProvisioning" i0))' self.assertTrue(vsan_info.set_policy(self.VMDK_PATH, policy_string), "failed to set") # get policy and check it p = vsan_info.get_policy(self.VMDK_PATH) self.assertTrue(vsan_info.same_policy(self.VMDK_PATH, p), "failed to compare with get_policy") self.assertTrue(vsan_info.same_policy(self.VMDK_PATH, policy_string), "failed to compare with original policy") self.assertTrue(vsan_info.same_policy(self.VMDK_PATH, same_policy), "failed to compare with same policy, different tabs") self.assertFalse(vsan_info.same_policy(self.VMDK_PATH, notsame_policy), "failed to compare with different policy")
def update_vsan_objects_with_policy(name, content): """ Find all VSAN objects using the policy given by `name` and update the policy contents in their objects. Returns an error string containing the list of volumes that failed to update, or a msg if there were no volumes to update. Returns None if all volumes were updated successfully. Note: This function assumes datastore_path exists. """ update_count = 0 failed_updates = [] dockvols_path = vsan_info.get_vsan_dockvols_path() print("This operation may take a while. Please be patient.") for v in list_volumes_and_policies(): if v['policy'] == name: volume_name = v['volume'] vmdk_path = os.path.join(dockvols_path, volume_name) if vsan_info.set_policy(vmdk_path, content): update_count = 1 else: failed_updates.append(volume_name) if len(failed_updates) != 0: if update_count == 0: # All volumes failed to update, so reset the original policy os.rename(policy_path(backup_policy_filename(name)), policy_path(name)) else: log_failed_updates(failed_updates, name) return ('Successfully updated {0} volumes.\n' 'Failed to update volumes: {0}').format(update_count, failed_updates) # Remove old policy file on success os.remove(policy_path(backup_policy_filename(name))) return None
def update_vsan_objects_with_policy(name, content): """ Find all VSAN objects using the policy given by `name` and update the policy contents in their objects. Returns an error string containing the list of volumes that failed to update, or a msg if there were no volumes to update. Returns None if all volumes were updated successfully. Note: This function assumes datastore_path exists. """ update_count = 0 failed_updates = [] dockvols_path = vsan_info.get_vsan_dockvols_path() print("This operation may take a while. Please be patient.") for v in list_volumes_and_policies(): if v['policy'] == name: volume_name = v['volume'] vmdk_path = os.path.join(dockvols_path, volume_name) if vsan_info.set_policy(vmdk_path, content): update_count = 1 else: failed_updates.append(volume_name) if len(failed_updates) != 0: if update_count == 0: # All volumes failed to update, so reset the original policy os.rename(policy_path(backup_policy_filename(name)), policy_path(name)) else: log_failed_updates(failed_updates, name) return ('Successfully updated {0} volumes.\n' 'Failed to update volumes: {0}').format( update_count, failed_updates) # Remove old policy file on success os.remove(policy_path(backup_policy_filename(name))) return None
def set_policy_by_name(vmdk_path, policy_name): """ Set policy for a given volume. """ content = get_policy_content(policy_name) if not content: return 'Error: {0} does not exist'.format(policy_name) return vsan_info.set_policy(vmdk_path, content)