Skip to content

Commit 262153b

Browse files
committed
Add samples
1 parent 12f26a5 commit 262153b

13 files changed

+542
-3
lines changed

.gitignore

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ docs/Api/
7575
docs/html/
7676
/src/Documentation/Help/
7777

78-
#NuGet
78+
#NuGet/Paket
7979
*.nupkg
80-
/src/packages/
81-
src/testing/migration/packages
80+
packages
81+
paket.exe
82+
paket.lock
27.5 KB
Binary file not shown.

samples/CloudServerSample.cs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using System;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using net.openstack.Core.Domain;
5+
using net.openstack.Providers.Rackspace;
6+
using Rackspace.CloudNetworks;
7+
using Rackspace.CloudNetworks.v2;
8+
9+
public class CloudServerSamples : ISample
10+
{
11+
public async Task Run(string username, string apiKey, string region)
12+
{
13+
const string region = "RegionOne";
14+
// Configure authentication
15+
var user = new CloudIdentityWithProject
16+
{
17+
Username = "user-name",
18+
Password = "password",
19+
ProjectName = "project-name"
20+
};
21+
var identity = new CloudIdentityProvider(user);
22+
var compute = new ComputeService(identity, region);
23+
24+
// Create a new server
25+
26+
var results = await compute.ListServersAsync();
27+
foreach
28+
29+
Console.WriteLine("Creating Sample Network... ");
30+
var networkDefinition = new NetworkDefinition {Name = "Sample"};
31+
var sampleNetwork = await networks.CreateNetworkAsync(networkDefinition);
32+
33+
Console.WriteLine("Adding a subnet to Sample Network...");
34+
var subnetDefinition = new SubnetCreateDefinition(sampleNetwork.Id, IPVersion.IPv4, "192.0.2.0/24")
35+
{
36+
Name = "Sample"
37+
};
38+
var sampleSubnet = await networks.CreateSubnetAsync(subnetDefinition);
39+
40+
Console.WriteLine("Attaching a port to Sample Network...");
41+
var portDefinition = new PortCreateDefinition(sampleNetwork.Id)
42+
{
43+
Name = "Sample"
44+
};
45+
var samplePort = await networks.CreatePortAsync(portDefinition);
46+
47+
Console.WriteLine("Listing Networks...");
48+
var networks = await networks.ListNetworksAsync();
49+
foreach (Network network in networks)
50+
{
51+
Console.WriteLine($"{network.Id}\t\t\t{network.Name}");
52+
}
53+
54+
Console.WriteLine();
55+
Console.WriteLine("Sample Network Information:");
56+
Console.WriteLine();
57+
Console.WriteLine($"Network Id: {sampleNetwork.Id}");
58+
Console.WriteLine($"Network Name: {sampleNetwork.Name}");
59+
Console.WriteLine($"Network Status: {sampleNetwork.Status}");
60+
Console.WriteLine();
61+
Console.WriteLine($"Subnet Id: {sampleSubnet.Id}");
62+
Console.WriteLine($"Subnet Name: {sampleSubnet.Name}");
63+
Console.WriteLine($"Subnet IPs: {sampleSubnet.AllocationPools.First().Start} - {sampleSubnet.AllocationPools.First().End}");
64+
Console.WriteLine();
65+
Console.WriteLine($"Port Id: {samplePort.Id}");
66+
Console.WriteLine($"Port Name: {samplePort.Name}");
67+
Console.WriteLine($"Port Address: {samplePort.MACAddress}");
68+
Console.WriteLine($"Port Status: {samplePort.Status}");
69+
Console.WriteLine();
70+
71+
Console.WriteLine("Deleting Sample Network...");
72+
await networks.DeletePortAsync(samplePort.Id);
73+
await networks.DeleteNetworkAsync(sampleNetwork.Id);
74+
}
75+
76+
public void PrintTasks()
77+
{
78+
Console.WriteLine("This sample will perform the following tasks:");
79+
Console.WriteLine("\t* Create a network");
80+
Console.WriteLine("\t* Add a subnet to the network");
81+
Console.WriteLine("\t* Attach a port to the network");
82+
Console.WriteLine("\t* Delete the network");
83+
}
84+
85+
}

samples/ComputeSample.cs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using System;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using net.openstack.Core.Domain;
5+
using net.openstack.Core.Providers;
6+
using OpenStack.Compute.v2_1;
7+
8+
public class ComputeSample : ISample
9+
{
10+
public async Task Run(string identityEndpoint, string username, string password, string project, string region)
11+
{
12+
// Configure authentication
13+
var user = new CloudIdentityWithProject
14+
{
15+
Username = username,
16+
Password = password,
17+
ProjectName = project
18+
};
19+
var identity = new OpenStackIdentityProvider(new Uri(identityEndpoint), user);
20+
var compute = new ComputeService(identity, region);
21+
22+
Console.WriteLine("Looking up the tiny flavor...");
23+
var flavors = await compute.ListFlavorsAsync();
24+
var tinyFlavor = flavors.FirstOrDefault(x => x.Name.Contains("tiny"));
25+
if(tinyFlavor == null) throw new Exception("Unable to find a flavor with the 'tiny' in the name!");
26+
27+
Console.WriteLine("Looking up the cirros image...");
28+
var images = await compute.ListImagesAsync(new ImageListOptions {Name = "cirros"});
29+
var cirrosImage = images.FirstOrDefault();
30+
if(cirrosImage == null) throw new Exception("Unable to find an image named 'cirros'");
31+
32+
Console.WriteLine("Creating Sample server... ");
33+
var serverDefinition = new ServerCreateDefinition("sample", cirrosImage.Id, tinyFlavor.Id);
34+
var server = await compute.CreateServerAsync(serverDefinition);
35+
36+
Console.WriteLine("Waiting for the sample server to come online...");
37+
await server.WaitUntilActiveAsync();
38+
39+
Console.WriteLine("Taking a snaphot of the sample server...");
40+
var snapshot = await server.SnapshotAsync(new SnapshotServerRequest("sample-snapshot"));
41+
await snapshot.WaitUntilActiveAsync();
42+
43+
Console.WriteLine();
44+
Console.WriteLine("Sample Server Information:");
45+
Console.WriteLine();
46+
Console.WriteLine($"Server Id: {server.Id}");
47+
Console.WriteLine($"Server Name: {server.Name}");
48+
Console.WriteLine($"Server Status: {server.Status}");
49+
Console.WriteLine($"Server Address: {server.IPv4Address}");
50+
Console.WriteLine();
51+
Console.WriteLine("Sample Snapshot Information:");
52+
Console.WriteLine();
53+
Console.WriteLine($"Image Id: {snapshot.Id}");
54+
Console.WriteLine($"Image Name: {snapshot.Name}");
55+
Console.WriteLine($"Image Status: {snapshot.Status}");
56+
Console.WriteLine($"Image Type: {snapshot.Type}");
57+
Console.WriteLine();
58+
59+
Console.WriteLine("Deleting Sample Server...");
60+
await snapshot.DeleteAsync();
61+
await server.DeleteAsync();
62+
}
63+
64+
public void PrintTasks()
65+
{
66+
Console.WriteLine("This sample will perform the following tasks:");
67+
Console.WriteLine("\t* Lookup a flavor with tiny in the name");
68+
Console.WriteLine("\t* Lookup an image named cirros");
69+
Console.WriteLine("\t* Create a server using cirros and the tiny flavor");
70+
Console.WriteLine("\t* Snapshot the server");
71+
Console.WriteLine("\t* Delete the snapshot and server");
72+
}
73+
74+
}

samples/ISample.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using System.Threading.Tasks;
2+
3+
public interface ISample
4+
{
5+
void PrintTasks();
6+
Task Run(string identityEndpoint, string username, string password, string project, string region);
7+
}

samples/NetworkingSample.cs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using System;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using net.openstack.Core.Domain;
5+
using net.openstack.Core.Providers;
6+
using OpenStack.Networking;
7+
using OpenStack.Networking.v2;
8+
9+
public class NetworkingSample : ISample
10+
{
11+
public async Task Run(string identityEndpoint, string username, string password, string project, string region)
12+
{
13+
// Configure authentication
14+
var user = new CloudIdentityWithProject
15+
{
16+
Username = username,
17+
Password = password,
18+
ProjectName = project
19+
};
20+
var identity = new OpenStackIdentityProvider(new Uri(identityEndpoint), user);
21+
var networking = new NetworkingService(identity, region);
22+
23+
Console.WriteLine("Creating Sample Network... ");
24+
var networkDefinition = new NetworkDefinition {Name = "Sample"};
25+
var sampleNetwork = await networking.CreateNetworkAsync(networkDefinition);
26+
27+
Console.WriteLine("Adding a subnet to Sample Network...");
28+
var subnetDefinition = new SubnetCreateDefinition(sampleNetwork.Id, IPVersion.IPv4, "192.0.2.0/24")
29+
{
30+
Name = "Sample"
31+
};
32+
var sampleSubnet = await networking.CreateSubnetAsync(subnetDefinition);
33+
34+
Console.WriteLine("Attaching a port to Sample Network...");
35+
var portDefinition = new PortCreateDefinition(sampleNetwork.Id)
36+
{
37+
Name = "Sample"
38+
};
39+
var samplePort = await networking.CreatePortAsync(portDefinition);
40+
41+
Console.WriteLine("Listing Networks...");
42+
var networks = await networking.ListNetworksAsync();
43+
foreach (Network network in networks)
44+
{
45+
Console.WriteLine($"{network.Id}\t\t\t{network.Name}");
46+
}
47+
48+
Console.WriteLine();
49+
Console.WriteLine("Sample Network Information:");
50+
Console.WriteLine();
51+
Console.WriteLine($"Network Id: {sampleNetwork.Id}");
52+
Console.WriteLine($"Network Name: {sampleNetwork.Name}");
53+
Console.WriteLine($"Network Status: {sampleNetwork.Status}");
54+
Console.WriteLine();
55+
Console.WriteLine($"Subnet Id: {sampleSubnet.Id}");
56+
Console.WriteLine($"Subnet Name: {sampleSubnet.Name}");
57+
Console.WriteLine($"Subnet IPs: {sampleSubnet.AllocationPools.First().Start} - {sampleSubnet.AllocationPools.First().End}");
58+
Console.WriteLine();
59+
Console.WriteLine($"Port Id: {samplePort.Id}");
60+
Console.WriteLine($"Port Name: {samplePort.Name}");
61+
Console.WriteLine($"Port Address: {samplePort.MACAddress}");
62+
Console.WriteLine($"Port Status: {samplePort.Status}");
63+
Console.WriteLine();
64+
65+
Console.WriteLine("Deleting Sample Network...");
66+
await networking.DeletePortAsync(samplePort.Id);
67+
await networking.DeleteNetworkAsync(sampleNetwork.Id);
68+
}
69+
70+
public void PrintTasks()
71+
{
72+
Console.WriteLine("This sample will perform the following tasks:");
73+
Console.WriteLine("\t* Create a network");
74+
Console.WriteLine("\t* Add a subnet to the network");
75+
Console.WriteLine("\t* Attach a port to the network");
76+
Console.WriteLine("\t* Delete the network");
77+
}
78+
79+
}

samples/NuGet.config

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<config>
4+
<add key="repositorypath" value="packages" />
5+
</config>
6+
<packageSources>
7+
<add key="OpenStackNetSDK" value="https://www.myget.org/F/openstacknetsdk/api/v2" />
8+
<add key="nuget.org" value="https://www.nuget.org/api/v2" />
9+
</packageSources>
10+
<activePackageSource>
11+
<add key="All" value="(Aggregate source)" />
12+
</activePackageSource>
13+
<packageRestore>
14+
<add key="enabled" value="True" />
15+
<add key="automatic" value="True" />
16+
</packageRestore>
17+
</configuration>

0 commit comments

Comments
 (0)