Skip to content

Code Samples

alanquillin edited this page Jan 29, 2013 · 7 revisions

This page is designed to help developers quickly get started using the openstack.net SDK. We will try and organize it so that each provider type is group together, however there may be some over lap. Also, we may forget something, or not have thought of a use case, so if you have any questions or suggestion, please reach out to us and we will try and get the examples included.

Rackspace Cloud Examples

Identity service (keystone)

Since all service endpoints use the identity service for authentication and authorization, we will start here.

User Authentication

Authenticate with username and password
try
{
    IIdentityProvider identityProvider = new Providers.Rackspace.IdentityProvider();
    var userAccess = identityProvider.Authenticate(new RackspaceCloudIdentity
                                                       {
                                                           Username = "MyUserName", 
                                                           Password = "MyPassword"
                                                       });
}
catch(ResponseException ex2)
{
    // do something
}
Authenticate with username and API key
try
{
    IIdentityProvider identityProvider = new Providers.Rackspace.IdentityProvider();
    var userAccess = identityProvider.Authenticate(new RackspaceCloudIdentity
                                                       {
                                                           Username = "MyUserName", 
                                                           APIKey = "MyAPIKey"
                                                       });
}
catch(ResponseException ex2)
{
    // do something
}

By default, the US (default) cloud instance is used (This is the instance for all Cloud Regions EXCEPT LON. All future cloud instances will be in US (default) the cloud region). If you need to target the LON cloud instance, you only need to pass in the CloudInstance.UK

Authenticate at the LON cloud instance.
try
{
    IIdentityProvider identityProvider = new Providers.Rackspace.IdentityProvider();
    var userAccess = identityProvider.Authenticate(new RackspaceCloudIdentity{
                                                        Username = "MyUserName", 
                                                        Password = "MyPassword", CloudInstance =CloudInstance.UK});
}
catch(ResponseException ex2)
{
    // do something
}

User Details and Roles

retrieving details about a user is reserved for either admins, or the requesting user retrieving their own information. The examples below show a user retrieving their own if, but we will have an examples of what administrative accounts can do.

Basic user methods

Retrieve user details by username
try
{
    IIdentityProvider identityProvider = new Providers.Rackspace.IdentityProvider();
    var identity = new RackspaceCloudIdentity{ Username = "MyUserName",   APIKey = "MyPassword" };
    var userDetails = identityProvider.GetUserByName(identity, "MyUserName");
}
catch(ResponseException ex2)
{
    // do something
}
Retrieve user details by user id
try
{
    IIdentityProvider identityProvider = new Providers.Rackspace.IdentityProvider();
    var identity = new RackspaceCloudIdentity{ Username = "MyUserName",   APIKey = "MyPassword" };
    var userDetails = identityProvider.GetUser(identity, "UserId");
}
catch(ResponseException ex2)
{
    // do something
}

Administrative user methods

These methods are only available to user that exist in the identity:user-admin role. If the requesting user does not exist in this role, a exception will be thrown.

Add User
List all global Roles
try
{
    IIdentityProvider identityProvider = new Providers.Rackspace.IdentityProvider();
    var identity = new RackspaceCloudIdentity{ Username = "MyAdminUserName",   APIKey = "MyAdminPassword" };
    var userDetails = identityProvider.ListRoles(identity)
}
catch(ResponseException ex2)
{
    // do something
}
Clone this wiki locally