-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathWaiterForResourceExample.cs
More file actions
114 lines (100 loc) · 5.19 KB
/
WaiterForResourceExample.cs
File metadata and controls
114 lines (100 loc) · 5.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* Copyright (c) 2020, 2026, Oracle and/or its affiliates. All rights reserved.
* This software is dual-licensed to you under the Universal Permissive License (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose either license.
*/
using System;
using System.Threading.Tasks;
using Oci.Common;
using Oci.Common.Auth;
using Oci.Common.Waiters;
using Oci.CoreService;
using Oci.CoreService.Models;
using Oci.CoreService.Requests;
using Oci.CoreService.Responses;
namespace Oci.Examples
{
/**
* This class demonstrates how to use waiter for a resource by Copying a Boot Volume backup from one region to another region.
*/
public class WaiterExample
{
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
const string OciConfigSourceProfileName = "DEFAULT";
const string OciConfigDestinationProfileName = "DEST";
public static async Task MainWaiter()
{
// Accepts profile name and creates a auth provider based on config file
var provider = new ConfigFileAuthenticationDetailsProvider(OciConfigSourceProfileName);
string bootVolumeBackup = Environment.GetEnvironmentVariable("OCI_BOOT_VOLUME_BACKUP_ID");
string destinationRegion = Environment.GetEnvironmentVariable("OCI_DESTINATION_REGION");
// Create clients for the service to enable using its APIs
var blockStorageClient = new BlockstorageClient(provider, new ClientConfiguration());
try
{
// Copy Boot Volume backup
var backupCopyId = await CopyBootVolumeBackup(blockStorageClient, bootVolumeBackup, destinationRegion);
logger.Info($"Create backup copy with OCID:{backupCopyId}");
// Create new client for making a retrying call for new backup copy in destination region
var destinationProvider = new ConfigFileAuthenticationDetailsProvider(OciConfigDestinationProfileName);
blockStorageClient = new BlockstorageClient(destinationProvider, new ClientConfiguration());
var state = GetBootVolumeBackup(blockStorageClient, backupCopyId);
logger.Info($"Successfully create backup copy with Lifecyle State:{state}");
// Delete the backup copy for cleanup
await DeleteBootVolumeBackup(blockStorageClient, backupCopyId);
}
catch (Exception e)
{
logger.Info($"Received exception due to {e.Message}");
}
finally
{
blockStorageClient.Dispose();
}
}
private static async Task<string> CopyBootVolumeBackup(BlockstorageClient client, string bootVolumeBackupId, string destinationRegion)
{
CopyBootVolumeBackupRequest copyBootVolumeBackupRequest = new CopyBootVolumeBackupRequest
{
BootVolumeBackupId = bootVolumeBackupId,
CopyBootVolumeBackupDetails = new CopyBootVolumeBackupDetails
{
DestinationRegion = destinationRegion,
DisplayName = "Backup-copy-example"
}
};
// Pass copyBootVolumeBackupRequest to client to create the boot volume backup
logger.Info($"Creating backup at {destinationRegion}");
logger.Info("=============");
CopyBootVolumeBackupResponse copyBootVolumeBackupResponse = await client.CopyBootVolumeBackup(copyBootVolumeBackupRequest);
return copyBootVolumeBackupResponse.BootVolumeBackup.Id;
}
private static BootVolumeBackup.LifecycleStateEnum GetBootVolumeBackup(BlockstorageClient client, string bootVolumeBackupId)
{
GetBootVolumeBackupRequest getBootVolumeBackupRequest = new GetBootVolumeBackupRequest
{
BootVolumeBackupId = bootVolumeBackupId
};
WaiterConfiguration waiterConfiguration = new WaiterConfiguration
{
GetNextDelayInSeconds = DelayStrategy.GetExponentialDelayInSeconds,
MaxAttempts = 30
};
GetBootVolumeBackupResponse getBootVolumeBackupResponse =
client.Waiters.ForBootVolumeBackup(getBootVolumeBackupRequest,
waiterConfiguration, new BootVolumeBackup.LifecycleStateEnum[] { BootVolumeBackup.LifecycleStateEnum.Available }).Execute();
return getBootVolumeBackupResponse.BootVolumeBackup.LifecycleState.Value;
}
private static async Task DeleteBootVolumeBackup(BlockstorageClient client, string bootVolumeBackupId)
{
DeleteBootVolumeBackupRequest deleteBootVolumeBackupRequest = new DeleteBootVolumeBackupRequest
{
BootVolumeBackupId = bootVolumeBackupId
};
// Delete the boot volume backup
logger.Info($"Deleting backup with id {bootVolumeBackupId}");
logger.Info("=============");
await client.DeleteBootVolumeBackup(deleteBootVolumeBackupRequest);
logger.Info("Backup deleted!");
}
}
}