public async Task Run(string identityEndpoint, string username, string password, string project, string region)
    {
        // Configure authentication
        var user = new CloudIdentityWithProject
        {
            Username = username,
            Password = password,
            ProjectName = project
        };
        var identity = new OpenStackIdentityProvider(new Uri(identityEndpoint), user);
        var networking = new NetworkingService(identity, region);

        Console.WriteLine("Creating Sample Network... ");
        var networkDefinition = new NetworkDefinition {Name = "Sample"};
        var sampleNetwork = await networking.CreateNetworkAsync(networkDefinition);

        Console.WriteLine("Adding a subnet to Sample Network...");
        var subnetDefinition = new SubnetCreateDefinition(sampleNetwork.Id, IPVersion.IPv4, "192.0.2.0/24")
        {
            Name = "Sample"
        };
        var sampleSubnet = await networking.CreateSubnetAsync(subnetDefinition);

        Console.WriteLine("Attaching a port to Sample Network...");
        var portDefinition = new PortCreateDefinition(sampleNetwork.Id)
        {
            Name = "Sample"
        };
        var samplePort = await networking.CreatePortAsync(portDefinition);

        Console.WriteLine("Listing Networks...");
        var networks = await networking.ListNetworksAsync();
        foreach (Network network in networks)
        {
            Console.WriteLine($"{network.Id}\t\t\t{network.Name}");
        }

        Console.WriteLine();
        Console.WriteLine("Sample Network Information:");
        Console.WriteLine();
        Console.WriteLine($"Network Id: {sampleNetwork.Id}");
        Console.WriteLine($"Network Name: {sampleNetwork.Name}");
        Console.WriteLine($"Network Status: {sampleNetwork.Status}");
        Console.WriteLine();
        Console.WriteLine($"Subnet Id: {sampleSubnet.Id}");
        Console.WriteLine($"Subnet Name: {sampleSubnet.Name}");
        Console.WriteLine($"Subnet IPs: {sampleSubnet.AllocationPools.First().Start} - {sampleSubnet.AllocationPools.First().End}");
        Console.WriteLine();
        Console.WriteLine($"Port Id: {samplePort.Id}");
        Console.WriteLine($"Port Name: {samplePort.Name}");
        Console.WriteLine($"Port Address: {samplePort.MACAddress}");
        Console.WriteLine($"Port Status: {samplePort.Status}");
        Console.WriteLine();

        Console.WriteLine("Deleting Sample Network...");
        await networking.DeletePortAsync(samplePort.Id);
        await networking.DeleteNetworkAsync(sampleNetwork.Id);
    }
    public async Task Run(string identityEndpoint, string username, string password, string project, string region)
    {
        // Configure authentication
        var user = new CloudIdentityWithProject
        {
            Username = username,
            Password = password,
            ProjectName = project
        };
        var identity = new OpenStackIdentityProvider(new Uri(identityEndpoint), user);
        var compute = new ComputeService(identity, region);

        Console.WriteLine("Looking up the tiny flavor...");
        var flavors = await compute.ListFlavorsAsync();
        var tinyFlavor = flavors.FirstOrDefault(x => x.Name.Contains("tiny"));
        if(tinyFlavor == null) throw new Exception("Unable to find a flavor with the 'tiny' in the name!");

        Console.WriteLine("Looking up the cirros image...");
        var images = await compute.ListImagesAsync(new ImageListOptions {Name = "cirros"});
        var cirrosImage = images.FirstOrDefault();
        if(cirrosImage == null) throw new Exception("Unable to find an image named 'cirros'");

        Console.WriteLine("Creating Sample server... ");
        var serverDefinition = new ServerCreateDefinition("sample", cirrosImage.Id, tinyFlavor.Id);
        var server = await compute.CreateServerAsync(serverDefinition);

        Console.WriteLine("Waiting for the sample server to come online...");
        await server.WaitUntilActiveAsync();

        Console.WriteLine("Taking a snaphot of the sample server...");
        var snapshot = await server.SnapshotAsync(new SnapshotServerRequest("sample-snapshot"));
        await snapshot.WaitUntilActiveAsync();

        Console.WriteLine();
        Console.WriteLine("Sample Server Information:");
        Console.WriteLine();
        Console.WriteLine($"Server Id: {server.Id}");
        Console.WriteLine($"Server Name: {server.Name}");
        Console.WriteLine($"Server Status: {server.Status}");
        Console.WriteLine($"Server Address: {server.IPv4Address}");
        Console.WriteLine();
        Console.WriteLine("Sample Snapshot Information:");
        Console.WriteLine();
        Console.WriteLine($"Image Id: {snapshot.Id}");
        Console.WriteLine($"Image Name: {snapshot.Name}");
        Console.WriteLine($"Image Status: {snapshot.Status}");
        Console.WriteLine($"Image Type: {snapshot.Type}");
        Console.WriteLine();

        Console.WriteLine("Deleting Sample Server...");
        await snapshot.DeleteAsync();
        await server.DeleteAsync();
    }
Exemple #3
0
        static void Main(string[] args)
        {
            const string region = "RegionOne";

            // Configure OpenStack.NET
            OpenStackNet.Configure(options => options.DefaultTimeout=TimeSpan.FromSeconds(5));

            // Authenticate
            var identityUrl = new Uri("http://example.com");
            var user = new CloudIdentityWithProject();
            var identity = new OpenStackIdentityProvider(identityUrl, user);
            identity.Authenticate();

            // Use legacy and new providers
            var legacyNetworking = new CloudNetworksProvider(null, identity);
            legacyNetworking.ListNetworks();
            var networks = new NetworkingService(identity, region);
            networks.ListNetworks();

            var legacyCompute = new CloudServersProvider(null, identity);
            legacyCompute.ListServers();
            var compute = new ComputeService(identity, region);
            compute.ListServers();
        }
        public static void Main(string[] args)
        {
            // step-1
            var username = "******";
            var password = "******";
            var project_name = "your_project_name";
            var project_id = "your_project_id";
            var auth_url = "http://controller:5000/v2.0";
            var region = "your_region_name";
            var networkid = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";

            var identity = new CloudIdentityWithProject () {
                Username = username,
                Password = password,
                ProjectId = new ProjectId(project_id),
                ProjectName = project_name
            };

            var identityProvider = new OpenStackIdentityProvider (
                new Uri (auth_url));

            var conn = new CloudServersProvider (identity, identityProvider);

            // step-2
            var images = conn.ListImages (region: region);
            foreach (var image in images) {
                Console.WriteLine (string.Format(
                    "Image Id: {0} - Image Name: {1}",
                    image.Id,
                    image.Name));
            }

            // step-3
            var flavors = conn.ListFlavors (region: region);
            foreach (var flavor in flavors) {
                Console.WriteLine (string.Format(
                    "Flavor Id: {0} - Flavor Name: {1}",
                    flavor.Id,
                    flavor.Name));
            }

            // step-4
            var image_id = "97f55846-6ea5-4e9d-b437-bda97586bd0c";
            var _image = conn.GetImage(image_id, region:region);
            Console.WriteLine (string.Format(
                "Image Id: {0} - Image Name: {1}",
                _image.Id,
                _image.Name));

            // step-5
            var flavor_id = "42";
            var _flavor = conn.GetFlavor (flavor_id, region: region);
            Console.WriteLine (string.Format(
                "Flavor Id: {0} - Flavor Name: {1}",
                _flavor.Id,
                _flavor.Name));

            // step-6
            var instance_name = "testing";
            var testing_instance = conn.CreateServer (instance_name,
                _image.Id,
                _flavor.Id,
                region: region,
                networks: new List<String> () { networkid });
            Console.WriteLine (string.Format(
                "Instance Id: {0} at {1}",
                testing_instance.Id,
                testing_instance.Links
            ));

            // step-7
            var instances = conn.ListServers(region:region);
            foreach (var instance in instances) {
                Console.WriteLine (string.Format(
                    "Instance Id: {0} at {1}",
                    testing_instance.Id,
                    testing_instance.Links));
            }

            // step-8
            conn.DeleteServer(testing_instance.Id, region:region);

            // step-9

            // step-10

            // step-11

            // step-12

            // step-13

            // step-14

            // step-15

            Console.Read ();
        }