-
Notifications
You must be signed in to change notification settings - Fork 0
/
CoreVirtualNetworkExample.cs
301 lines (272 loc) · 12 KB
/
CoreVirtualNetworkExample.cs
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
using OCISDK.Core;
using OCISDK.Core.Common;
using OCISDK.Core.Core.Model.VirtualNetwork;
using OCISDK.Core.Core.Request.VirtualNetwork;
using OCISDK.Core.Identity;
using OCISDK.Core.Identity.Request;
using System;
namespace Example
{
class CoreVirtualNetworkExample
{
public static void VirtualNetworkExample(ClientConfig config)
{
Console.WriteLine();
Console.WriteLine("VertualNetwork Example Menu");
Console.WriteLine("[1]: Display List");
Console.WriteLine("[2]: Create new VCN");
Console.WriteLine("[ESC] or [E(e)] : Back Example Menu");
Console.WriteLine();
var presskey = Console.ReadKey(true);
if (presskey.Key == ConsoleKey.Escape || presskey.KeyChar == 'E' || presskey.KeyChar == 'e')
{
Console.WriteLine("Back Example Menu");
return;
}
var select = presskey.KeyChar;
if (!int.TryParse(select.ToString(), out int mode))
{
Console.WriteLine("Incorrect input...");
return;
}
if (mode == 1)
{
VirtualNetworkConsoleDisplay(config);
}
else if(mode == 2)
{
CreateVirtualNetwork(config);
}
else
{
Console.WriteLine("Incorrect input...");
return;
}
}
private static void VirtualNetworkConsoleDisplay(ClientConfig config)
{
// create client
VirtualNetworkClient client = new VirtualNetworkClient(config)
{
Region = Regions.US_ASHBURN_1
};
Console.WriteLine("* List VCN------------------------");
var listVcnRequest = new ListVcnRequest()
{
// target compartment is root compartment(tenancy)
CompartmentId = config.TenancyId,
Limit = 50,
SortOrder = SortOrder.ASC
};
// get vcns
var listVcn = client.ListVcn(listVcnRequest);
listVcn.Items.ForEach(vcn =>
{
Console.WriteLine(" |-" + vcn.DisplayName);
Console.WriteLine(" | " + vcn.Id);
// get dhcps in vcn
var listDhcpRequest = new ListDhcpOptionsRequest()
{
// target is same compartment and VCN
CompartmentId = vcn.CompartmentId,
VcnId = vcn.Id,
Limit = 50,
SortOrder = SortOrder.ASC
};
var listDhcp = client.ListDhcpOptions(listDhcpRequest);
listDhcp.Items.ForEach(dhcp =>
{
Console.WriteLine(" |\t|- * DHCP");
Console.WriteLine(" |\t|\t" + dhcp.DisplayName);
Console.WriteLine(" |\t|\t" + dhcp.Id);
});
// get RouteTable in vcn
var listRouteTableRequest = new ListRouteTableRequest()
{
// target is same compartment and VCN
CompartmentId = vcn.CompartmentId,
VcnId = vcn.Id,
Limit = 50,
SortOrder = SortOrder.ASC
};
var listRouteTable = client.ListRouteTableOptions(listRouteTableRequest);
listRouteTable.Items.ForEach(rt =>
{
Console.WriteLine(" |\t|- * RouteTable");
Console.WriteLine(" |\t|\t" + rt.DisplayName);
Console.WriteLine(" |\t|\t" + rt.Id);
// RouteRules
rt.RouteRules.ForEach(rr =>
{
if (!string.IsNullOrEmpty(rr.DestinationType))
{
Console.WriteLine(" |\t|\t|- DestinationType:" + rr.DestinationType);
}
if (!string.IsNullOrEmpty(rr.CidrBlock))
{
Console.WriteLine(" |\t|\t|\tCidrBlock:" + rr.CidrBlock);
}
if (!string.IsNullOrEmpty(rr.Destination))
{
Console.WriteLine(" |\t|\t|\tDestination:" + rr.Destination);
}
if (!string.IsNullOrEmpty(rr.NetworkEntityId))
{
Console.WriteLine(" |\t|\t|\tNetworkEntityId:" + rr.NetworkEntityId);
}
});
});
// get RouteTable in vcn
var listSecurityListRequest = new ListSecurityListsRequest()
{
// target is same compartment and VCN
CompartmentId = vcn.CompartmentId,
VcnId = vcn.Id,
Limit = 50,
SortOrder = SortOrder.ASC
};
var listSecurityList = client.ListSecurityLists(listSecurityListRequest);
listSecurityList.Items.ForEach(sl =>
{
Console.WriteLine(" |\t|- * SecurityList");
Console.WriteLine(" |\t|\t" + sl.DisplayName);
Console.WriteLine(" |\t|\t" + sl.Id);
Console.WriteLine(" |\t|\t" + sl.LifecycleState);
});
// get Subnet in vcn
var listSubnetRequest = new ListSubnetsRequest()
{
// target is same compartment and VCN
CompartmentId = vcn.CompartmentId,
VcnId = vcn.Id,
Limit = 50,
SortOrder = SortOrder.ASC
};
var listSubnet = client.ListSubnets(listSubnetRequest);
listSubnet.Items.ForEach(sl =>
{
Console.WriteLine(" |\t|- * Subnet");
Console.WriteLine(" |\t|\t" + sl.DisplayName);
Console.WriteLine(" |\t|\t" + sl.Id);
Console.WriteLine(" |\t|\t" + sl.LifecycleState);
});
// get InternetGateway in vcn
var listIGRequest = new ListInternetGatewaysRequest()
{
// target is same compartment and VCN
CompartmentId = vcn.CompartmentId,
VcnId = vcn.Id,
Limit = 50,
SortOrder = SortOrder.ASC
};
var listIG = client.ListInternetGateways(listIGRequest);
listDhcp.Items.ForEach(ig =>
{
Console.WriteLine(" |\t|- * InternetGateway");
Console.WriteLine(" |\t|\t" + ig.DisplayName);
Console.WriteLine(" |\t|\t" + ig.Id);
});
});
Console.WriteLine("* List DRG------------------------");
var identityClient = new IdentityClient(config)
{
Region = Regions.US_ASHBURN_1
};
var listCompartmentRequest = new ListCompartmentRequest()
{
CompartmentId = config.TenancyId,
CompartmentIdInSubtree = true,
AccessLevel = ListCompartmentRequest.AccessLevels.ACCESSIBLE
};
var compartments = identityClient.ListCompartment(listCompartmentRequest).Items;
foreach (var com in compartments)
{
var listDrgsRequest = new ListDrgsRequest() {
CompartmentId = com.Id
};
var drgs = client.ListDrgs(listDrgsRequest).Items;
foreach (var drg in drgs)
{
Console.WriteLine($" |-name: {drg.DisplayName}");
Console.WriteLine($" | state: {drg.LifecycleState}");
Console.WriteLine($" | timeCreate: {drg.TimeCreated}");
Console.WriteLine($" | compartment: {com.Name}");
}
}
}
private static void CreateVirtualNetwork(ClientConfig config)
{
// warning
Console.WriteLine("Continuing this example, a new VCN will be created. The compartments are root fixed.");
Console.Write("Are you sure you want to continue? (Y/N)");
var presskey = Console.ReadKey();
if (presskey.KeyChar != 'Y' && presskey.KeyChar != 'y')
{
Console.WriteLine("Exit CreateVirtualNetworkExample");
return;
}
Console.WriteLine();
// create client
VirtualNetworkClient client = new VirtualNetworkClient(config)
{
Region = Regions.US_ASHBURN_1
};
// input VCN details
var createVcnDetails = new CreateVcnDetails();
Console.WriteLine("Create new VertualCloudNetwork(VCN)");
Console.Write("VCN name: ");
createVcnDetails.DisplayName = Console.ReadLine();
Console.Write("CidrBlock: ");
createVcnDetails.CidrBlock = Console.ReadLine();
createVcnDetails.CompartmentId = config.TenancyId;
var createVcnRequest = new CreateVcnRequest()
{
CreateVcnDetails = createVcnDetails
};
var newVCNRes = client.CreateVcn(createVcnRequest);
var newVCN = newVCNRes.Vcn;
Console.WriteLine("* Create new VCN--------------------------");
Console.WriteLine(" name: " + newVCN.DisplayName);
Console.WriteLine(" id: " + newVCN.Id);
Console.WriteLine(" DNSlabel: " + newVCN.DnsLabel);
Console.WriteLine(" domainName: " + newVCN.VcnDomainName);
Console.WriteLine(" timeCreate: " + newVCN.TimeCreated);
//get created default DHCP
Console.WriteLine(" DefaultDHCP: ");
Console.WriteLine("\t| id: " + newVCN.DefaultSecurityListId);
var getDhcpRequest = new GetDhcpRequest()
{
DhcpId = newVCN.DefaultDhcpOptionsId
};
var defaultDHCP = client.GetDhcp(getDhcpRequest).DhcpOptions;
Console.WriteLine("\t| name: " + defaultDHCP.DisplayName);
Console.WriteLine("\t| id: " + defaultDHCP.Id);
Console.WriteLine("\t| state: " + defaultDHCP.LifecycleState);
Console.WriteLine("\t| timeCreate: " + defaultDHCP.TimeCreated);
// get created default seculityList
Console.WriteLine(" DefaultSeculityList: ");
Console.WriteLine("\t| id: " + newVCN.DefaultSecurityListId);
var getSecurityListRequest = new GetSecurityListRequest()
{
SecurityListId = newVCN.DefaultSecurityListId
};
var defaultSL = client.GetSecurityList(getSecurityListRequest).SecurityList;
Console.WriteLine("\t| name: " + defaultSL.DisplayName);
Console.WriteLine("\t| id: " + defaultSL.Id);
Console.WriteLine("\t| state: " + defaultSL.LifecycleState);
Console.WriteLine("\t| timeCreate: " + defaultSL.TimeCreated);
// get craeted default
Console.WriteLine(" DefaultRouteTableId: ");
Console.WriteLine("\t| id: " + newVCN.DefaultRouteTableId);
var getRouteTableIdRequest = new GetRouteTableRequest()
{
RtId = newVCN.DefaultRouteTableId
};
var defaultRT = client.GetRouteTable(getRouteTableIdRequest).RouteTable;
Console.WriteLine("\t| name: " + defaultRT.DisplayName);
Console.WriteLine("\t| id: " + defaultRT.Id);
Console.WriteLine("\t| state: " + defaultRT.LifecycleState);
Console.WriteLine("\t| timeCreate: " + defaultRT.TimeCreated);
}
}
}