-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathMoveCompartments.cs
More file actions
83 lines (74 loc) · 3.43 KB
/
MoveCompartments.cs
File metadata and controls
83 lines (74 loc) · 3.43 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
/*
* 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.IdentityService;
using Oci.IdentityService.Models;
using Oci.IdentityService.Requests;
using Oci.IdentityService.Responses;
namespace Oci.Examples
{
public class MoveCompartmentExample
{
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
public static async Task MainCompartment()
{
logger.Info("Starting example");
// Create Identity Client
var provider = new ConfigFileAuthenticationDetailsProvider("DEFAULT");
var client = new IdentityClient(provider, new ClientConfiguration());
logger.Info("IdentityClient created");
try
{
var compartmentId = Environment.GetEnvironmentVariable("OCI_COMPARTMENT_ID");
string sourceCompartmentId = await CreateCompartment(client, compartmentId, "source");
string targetCompartmentId = await CreateCompartment(client, compartmentId, "target");
await MoveCompartment(client, sourceCompartmentId, targetCompartmentId);
}
catch (Exception e)
{
logger.Error($"Failed to move compartments: {e}");
}
finally
{
client.Dispose();
}
}
private static async Task<string> CreateCompartment(IdentityClient client, string compartmentId, string name)
{
CreateCompartmentDetails createCompartmentDetails = new CreateCompartmentDetails
{
CompartmentId = compartmentId,
Name = name + "_dotnetSDK",
Description = name + " compartment"
};
CreateCompartmentRequest createCompartmentRequest = new CreateCompartmentRequest
{
CreateCompartmentDetails = createCompartmentDetails
};
CreateCompartmentResponse createCompartmentResponse = await client.CreateCompartment(createCompartmentRequest);
// wait for compartment to be accessible
await Task.Delay(10000);
logger.Info($"{name} compartment created, ID: {createCompartmentResponse.Compartment.Id} and state is {createCompartmentResponse.Compartment.LifecycleState}");
return createCompartmentResponse.Compartment.Id;
}
private static async Task MoveCompartment(IdentityClient client, string sourceCompartmentId, string targetCompartmentId)
{
MoveCompartmentDetails moveCompartmentDetails = new MoveCompartmentDetails
{
TargetCompartmentId = targetCompartmentId
};
MoveCompartmentRequest moveCompartmentRequest = new MoveCompartmentRequest
{
CompartmentId = sourceCompartmentId,
MoveCompartmentDetails = moveCompartmentDetails
};
MoveCompartmentResponse moveCompartmentResponse = await client.MoveCompartment(moveCompartmentRequest);
logger.Info("compartment moved successfully");
}
}
}