def patch_snapshot_policy(client, resource_group_name, account_name, snapshot_policy_name, location, hourly_snapshots=0, hourly_minute=0, daily_snapshots=0, daily_minute=0, daily_hour=0, weekly_snapshots=0, weekly_minute=0, weekly_hour=0, weekly_day=None, monthly_snapshots=0, monthly_minute=0, monthly_hour=0, monthly_days=None, enabled=False): body = SnapshotPolicyPatch( location=location, hourly_schedule=HourlySchedule(snapshots_to_keep=hourly_snapshots, minute=hourly_minute), daily_schedule=DailySchedule(snapshots_to_keep=daily_snapshots, minute=daily_minute, hour=daily_hour), weekly_schedule=WeeklySchedule(snapshots_to_keep=weekly_snapshots, minute=weekly_minute, hour=weekly_hour, day=weekly_day), monthly_schedule=MonthlySchedule(snapshots_to_keep=monthly_snapshots, minute=monthly_minute, hour=monthly_hour, days_of_month=monthly_days), enabled=enabled) return client.update(body, resource_group_name, account_name, snapshot_policy_name)
def test_update_snapshot_policies(self): create_snapshot_policy(self.client, TEST_SNAPSHOT_POLICY_1) snapshot_policy_body = SnapshotPolicyPatch( location=LOCATION, hourly_schedule={}, daily_schedule=DailySchedule(snapshots_to_keep=1, minute=50, hour=1), weekly_schedule={}, monthly_schedule={}, enabled=False ) snapshot_policy = self.client.snapshot_policies.begin_update(TEST_RG, TEST_ACC_1, TEST_SNAPSHOT_POLICY_1, snapshot_policy_body).result() assert snapshot_policy.daily_schedule.snapshots_to_keep == 1 assert snapshot_policy.daily_schedule.hour == 1 assert snapshot_policy.daily_schedule.minute == 50 delete_snapshot_policy(self.client, TEST_SNAPSHOT_POLICY_1, live=self.is_live) delete_account(self.client, TEST_RG, TEST_ACC_1)
def run_example(): """Azure NetApp Files Snapshot Policy SDK management example""" print_header( "Azure NetAppFiles Python SDK Samples - Sample project that creates and updates a Snapshot Policy using " "the Azure NetApp Files SDK") # Authenticating using service principal, refer to README.md file for requirement details credentials, subscription_id = get_credentials() console_output( "Instantiating a new Azure NetApp Files management client...") anf_client = NetAppManagementClient(credentials, subscription_id) console_output("Creating ANF Resources...") # Creating ANF Primary Account console_output("Creating Account...") anf_account = None try: anf_account = create_account(anf_client, RESOURCE_GROUP_NAME, ANF_ACCOUNT_NAME, LOCATION) console_output( "\tAccount successfully created. Resource id: {}".format( anf_account.id)) except AzureError as ex: console_output("An error occurred while creating Account: {}".format( ex.message)) raise # Creating Snapshot Policy console_output("Creating Snapshot Policy...") snapshot_policy = None try: snapshot_policy = create_snapshot_policy(anf_client, RESOURCE_GROUP_NAME, anf_account.name, SNAPSHOT_POLICY_NAME, LOCATION) console_output( "\tSnapshot Policy successfully created. Resource id: {}".format( snapshot_policy.id)) except AzureError as ex: console_output( "An error occurred while creating Snapshot Policy: {}".format( ex.message)) raise # Creating Capacity Pool console_output("Creating Capacity Pool...") capacity_pool = None try: capacity_pool = create_capacity_pool(anf_client, RESOURCE_GROUP_NAME, anf_account.name, CAPACITY_POOL_NAME, CAPACITY_POOL_SIZE, LOCATION) console_output( "\tCapacity Pool successfully created. Resource id: {}".format( capacity_pool.id)) except AzureError as ex: console_output( "An error occurred while creating Capacity Pool: {}".format( ex.message)) raise # Creating Volume console_output("Creating Volume...") subnet_id = '/subscriptions/{}/resourceGroups/{}/providers/Microsoft.Network/virtualNetworks/{}/subnets/{}'.format( subscription_id, RESOURCE_GROUP_NAME, VNET_NAME, SUBNET_NAME) volume = None try: pool_name = resource_uri_utils.get_anf_capacity_pool(capacity_pool.id) volume = create_volume(anf_client, RESOURCE_GROUP_NAME, anf_account.name, pool_name, VOLUME_NAME, VOLUME_SIZE, subnet_id, LOCATION) console_output("\tVolume successfully created. Resource id: {}".format( volume.id)) except AzureError as ex: console_output("An error occurred while creating Volume: {}".format( ex.message)) raise # Update Snapshot Policy console_output("Updating Snapshot Policy...") # Updating number of snapshots to keep for hourly schedule hourly_schedule = snapshot_policy.hourly_schedule hourly_schedule.snapshotsToKeep = 5 snapshot_policy_patch = SnapshotPolicyPatch( hourly_schedule=hourly_schedule, location=snapshot_policy.location, enabled=True) try: anf_client.snapshot_policies.begin_update(RESOURCE_GROUP_NAME, ANF_ACCOUNT_NAME, SNAPSHOT_POLICY_NAME, snapshot_policy_patch) console_output("\tSnapshot Policy successfully updated") except AzureError as ex: console_output( "An error occurred while updating Snapshot Policy: {}".format( ex.message)) raise """ Cleanup process. For this process to take effect please change the value of CLEANUP_RESOURCES global variable to 'True' Note: Volume deletion operations at the RP level are executed serially """ if CLEANUP_RESOURCES: # The cleanup process starts from the innermost resources down in the hierarchy chain. # In this case: Volumes -> Capacity Pools -> (Snapshot Policy) -> Accounts console_output("Cleaning up resources") # Cleaning up Volumes console_output("Deleting Volumes...") try: volume_ids = [volume.id] for volume_id in volume_ids: pool_name = resource_uri_utils.get_resource_value( volume_id, "capacityPools") volume_name = resource_uri_utils.get_anf_volume(volume_id) console_output("\tDeleting {}".format(volume_name)) anf_client.volumes.begin_delete(RESOURCE_GROUP_NAME, anf_account.name, pool_name, volume_name).wait() # ARM Workaround to wait the deletion complete/propagate wait_for_no_anf_resource(anf_client, volume_id) console_output( "\t\tSuccessfully deleted Volume: {}".format(volume_id)) except AzureError as ex: console_output( "An error occurred while deleting volumes: {}".format( ex.message)) raise # Cleaning up Capacity Pools console_output("Deleting Capacity Pools...") try: pool_ids = [capacity_pool.id] for pool_id in pool_ids: pool_name = resource_uri_utils.get_anf_capacity_pool(pool_id) console_output("\tDeleting {}".format(pool_name)) anf_client.pools.begin_delete(RESOURCE_GROUP_NAME, anf_account.name, pool_name).wait() wait_for_no_anf_resource(anf_client, pool_id) console_output( "\t\tSuccessfully deleted Capacity Pool: {}".format( pool_id)) except AzureError as ex: console_output( "An error occurred while deleting capacity pools: {}".format( ex.message)) raise # Cleaning up Snapshot Policy console_output("Deleting Snapshot Policy...") try: console_output("\tDeleting {}".format(snapshot_policy.name)) anf_client.snapshot_policies.begin_delete( RESOURCE_GROUP_NAME, anf_account.name, SNAPSHOT_POLICY_NAME).wait() wait_for_no_anf_resource(anf_client, snapshot_policy.id) console_output( "\t\tSuccessfully deleted Snapshot Policy: {}".format( snapshot_policy.id)) except AzureError as ex: console_output( "An error occurred while deleting snapshot policy: {}".format( ex.message)) raise # Cleaning up Account console_output("Deleting Account...") try: console_output("\tDeleting {}".format(anf_account.name)) anf_client.accounts.begin_delete(RESOURCE_GROUP_NAME, anf_account.name).wait() console_output("\t\tSuccessfully deleted Account: {}".format( anf_account.id)) except AzureError as ex: console_output( "An error occurred while deleting accounts: {}".format( ex.message)) raise console_output("ANF Snapshot Policy sample has completed successfully")