Skip to content

Instantly share code, notes, and snippets.

@sivel
Last active August 29, 2015 14:23
Show Gist options
  • Select an option

  • Save sivel/f7c7e1112a287a4f946f to your computer and use it in GitHub Desktop.

Select an option

Save sivel/f7c7e1112a287a4f946f to your computer and use it in GitHub Desktop.
python-openstacksdk Rackspace API Key Authenticator
from openstack.auth.identity.v2 import Password
class ApiKey(Password):
valid_options = {
'access_info',
'auth_url',
'username',
'user_id',
'password',
'project_id',
'project_name',
'tenant_name',
'tenant_id',
'reauthenticate',
'trust_id',
'api_key',
}
def __init__(self, *args, **kwargs):
api_key = kwargs.pop('api_key')
super(ApiKey, self).__init__(*args, **kwargs)
self.api_key = api_key or password
def get_auth_data(self, headers=None):
return {
'RAX-KSKEY:apiKeyCredentials': {
'username': '%s' % self.username,
'apiKey': '%s' % self.api_key
}
}
#!/usr/bin/env python
import os
import raxauth
from openstack.profile import Profile
from openstack.connection import Connection
profile = Profile()
profile.set_version('identity', 'v2')
profile.set_region(profile.ALL, 'IAD')
authenticator = raxauth.ApiKey(os.getenv('OS_AUTH_URL'),
tenant_id=os.getenv('OS_TENANT_ID'),
username=os.getenv('OS_USERNAME'),
api_key=os.getenv('OS_PASSWORD'))
conn = Connection(authenticator=authenticator, profile=profile)
print len(list(conn.compute.servers()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment