33using System . Threading . Tasks ;
44using net . openstack . Core . Domain ;
55using net . openstack . Providers . Rackspace ;
6- using Rackspace . CloudNetworks ;
7- using Rackspace . CloudNetworks . v2 ;
6+ using Rackspace . RackConnect . v3 ;
87
98public class AssignPublicIPSamples : ISample
109{
@@ -16,27 +15,54 @@ public async Task Run(string username, string apiKey, string region)
1615 Username = username ,
1716 APIKey = apiKey
1817 } ;
19- var rackConnectService = new RackConnectService ( identity ) ;
20- var serverService = new CloudServersProvider ( identity ) ;
18+ var identityService = new CloudIdentityProvider ( identity ) ;
19+ var result = identityService . Authenticate ( ) ;
2120
22- Console . WriteLine ( "Looking up a RackConnect network..." ) ;
21+ var serverService = new CloudServersProvider ( identity , region , null , null ) ;
22+ var rackConnectService = new RackConnectService ( identityService , region ) ;
23+
24+ // Create a cloud server on your hybrid network
25+ Console . WriteLine ( $ "Looking up your RackConnect network in { region } ...") ;
26+ var networks = await rackConnectService . ListNetworksAsync ( ) ;
27+ var network = networks . FirstOrDefault ( ) ;
28+ if ( network == null )
29+ throw new Exception ( $ "You do not have a Hybrid Cloud / RackConnect network configured in the { region } which is required to run this sample.") ;
2330
2431 Console . WriteLine ( "Creating sample cloud server... " ) ;
25-
26- Console . WriteLine ( "Assigning a public ip address..." ) ;
27- rackConnectService . AssignPublicIPAsync ( server . Id ) ;
32+ // Ubuntu 14.04 LTS (Trusty Tahr) (PVHVM)
33+ const string ubuntuImageId = "09de0a66-3156-48b4-90a5-1cf25a905207" ;
34+ // 512MB Standard Instance
35+ const string standardFlavorId = "2" ;
36+ var requestedServer = serverService . CreateServer ( "sample" , ubuntuImageId , standardFlavorId ,
37+ networks : new string [ ] { network . Id } ) ;
38+ serverService . WaitForServerActive ( requestedServer . Id ) ;
39+
40+ Console . WriteLine ( "Allocating a public IP address..." ) ;
41+ var ip = await rackConnectService . CreatePublicIPAsync (
42+ new PublicIPCreateDefinition { ShouldRetain = true } ) ;
43+ await ip . WaitUntilActiveAsync ( ) ;
44+ Console . WriteLine ( $ "Acquired { ip . PublicIPv4Address } !") ;
45+
46+ Console . WriteLine ( "Assigning the public IP to the sample cloud server..." ) ;
47+ await ip . AssignAsync ( requestedServer . Id ) ;
48+ await ip . WaitUntilActiveAsync ( ) ;
49+
2850 Console . WriteLine ( "Deleting sample cloud server..." ) ;
29- await networkService . DeletePortAsync ( samplePort . Id ) ;
30- await networkService . DeleteNetworkAsync ( sampleNetwork . Id ) ;
51+ serverService . DeleteServer ( requestedServer . Id ) ;
52+
53+ Console . WriteLine ( "Deallocating the public IP address..." ) ;
54+ await ip . DeleteAsync ( ) ;
3155 }
3256
3357 public void PrintTasks ( )
3458 {
3559 Console . WriteLine ( "This sample will perform the following tasks:" ) ;
36- Console . WriteLine ( "\t * Create a network" ) ;
37- Console . WriteLine ( "\t * Add a subnet to the network" ) ;
38- Console . WriteLine ( "\t * Attach a port to the network" ) ;
39- Console . WriteLine ( "\t * Delete the network" ) ;
60+ Console . WriteLine ( "\t * Locate a hybrid/RackConnect network" ) ;
61+ Console . WriteLine ( "\t * Create a cloud server on the network" ) ;
62+ Console . WriteLine ( "\t * Allocate a public IP on the network" ) ;
63+ Console . WriteLine ( "\t * Assign the public IP to the cloud server" ) ;
64+ Console . WriteLine ( "\t * Remove the public IP from the cloud server" ) ;
65+ Console . WriteLine ( "\t * Remove the cloud server" ) ;
4066 }
4167
4268}
0 commit comments