summaryrefslogtreecommitdiff
path: root/novaclient/v2/client.py
diff options
context:
space:
mode:
Diffstat (limited to 'novaclient/v2/client.py')
-rw-r--r--novaclient/v2/client.py239
1 files changed, 239 insertions, 0 deletions
diff --git a/novaclient/v2/client.py b/novaclient/v2/client.py
new file mode 100644
index 0000000..386c649
--- /dev/null
+++ b/novaclient/v2/client.py
@@ -0,0 +1,239 @@
1# Copyright 2012 OpenStack Foundation
2# Copyright 2013 IBM Corp.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
16from novaclient import client
17from novaclient.v2 import agents
18from novaclient.v2 import aggregates
19from novaclient.v2 import availability_zones
20from novaclient.v2 import certs
21from novaclient.v2 import cloudpipe
22from novaclient.v2 import fixed_ips
23from novaclient.v2 import flavor_access
24from novaclient.v2 import flavors
25from novaclient.v2 import floating_ip_dns
26from novaclient.v2 import floating_ip_pools
27from novaclient.v2 import floating_ips
28from novaclient.v2 import floating_ips_bulk
29from novaclient.v2 import fping
30from novaclient.v2 import hosts
31from novaclient.v2 import hypervisors
32from novaclient.v2 import images
33from novaclient.v2 import keypairs
34from novaclient.v2 import limits
35from novaclient.v2 import networks
36from novaclient.v2 import quota_classes
37from novaclient.v2 import quotas
38from novaclient.v2 import security_group_default_rules
39from novaclient.v2 import security_group_rules
40from novaclient.v2 import security_groups
41from novaclient.v2 import server_groups
42from novaclient.v2 import servers
43from novaclient.v2 import services
44from novaclient.v2 import usage
45from novaclient.v2 import versions
46from novaclient.v2 import virtual_interfaces
47from novaclient.v2 import volume_snapshots
48from novaclient.v2 import volume_types
49from novaclient.v2 import volumes
50
51
52class Client(object):
53 """
54 Top-level object to access the OpenStack Compute API.
55
56 Create an instance with your creds::
57
58 >>> client = Client(USERNAME, PASSWORD, PROJECT_ID, AUTH_URL)
59
60 Or, alternatively, you can create a client instance using the
61 keystoneclient.session API::
62
63 >>> from keystoneclient.auth.identity import v2
64 >>> from keystoneclient import session
65 >>> from novaclient.client import Client
66 >>> auth = v2.Password(auth_url=AUTH_URL,
67 username=USERNAME,
68 password=PASSWORD,
69 tenant_name=PROJECT_ID)
70 >>> sess = session.Session(auth=auth)
71 >>> nova = client.Client(VERSION, session=sess)
72
73 Then call methods on its managers::
74
75 >>> client.servers.list()
76 ...
77 >>> client.flavors.list()
78 ...
79
80 It is also possible to use an instance as a context manager in which
81 case there will be a session kept alive for the duration of the with
82 statement::
83
84 >>> with Client(USERNAME, PASSWORD, PROJECT_ID, AUTH_URL) as client:
85 ... client.servers.list()
86 ... client.flavors.list()
87 ...
88
89 It is also possible to have a permanent (process-long) connection pool,
90 by passing a connection_pool=True::
91
92 >>> client = Client(USERNAME, PASSWORD, PROJECT_ID,
93 ... AUTH_URL, connection_pool=True)
94 """
95
96 def __init__(self, username=None, api_key=None, project_id=None,
97 auth_url=None, insecure=False, timeout=None,
98 proxy_tenant_id=None, proxy_token=None, region_name=None,
99 endpoint_type='publicURL', extensions=None,
100 service_type='compute', service_name=None,
101 volume_service_name=None, timings=False, bypass_url=None,
102 os_cache=False, no_cache=True, http_log_debug=False,
103 auth_system='keystone', auth_plugin=None, auth_token=None,
104 cacert=None, tenant_id=None, user_id=None,
105 connection_pool=False, session=None, auth=None,
106 completion_cache=None, **kwargs):
107 # FIXME(comstud): Rename the api_key argument above when we
108 # know it's not being used as keyword argument
109
110 # NOTE(cyeoh): In the novaclient context (unlike Nova) the
111 # project_id is not the same as the tenant_id. Here project_id
112 # is a name (what the Nova API often refers to as a project or
113 # tenant name) and tenant_id is a UUID (what the Nova API
114 # often refers to as a project_id or tenant_id).
115
116 password = api_key
117 self.projectid = project_id
118 self.tenant_id = tenant_id
119 self.user_id = user_id
120 self.flavors = flavors.FlavorManager(self)
121 self.flavor_access = flavor_access.FlavorAccessManager(self)
122 self.images = images.ImageManager(self)
123 self.limits = limits.LimitsManager(self)
124 self.servers = servers.ServerManager(self)
125 self.versions = versions.VersionManager(self)
126
127 # extensions
128 self.agents = agents.AgentsManager(self)
129 self.dns_domains = floating_ip_dns.FloatingIPDNSDomainManager(self)
130 self.dns_entries = floating_ip_dns.FloatingIPDNSEntryManager(self)
131 self.cloudpipe = cloudpipe.CloudpipeManager(self)
132 self.certs = certs.CertificateManager(self)
133 self.floating_ips = floating_ips.FloatingIPManager(self)
134 self.floating_ip_pools = floating_ip_pools.FloatingIPPoolManager(self)
135 self.fping = fping.FpingManager(self)
136 self.volumes = volumes.VolumeManager(self)
137 self.volume_snapshots = volume_snapshots.SnapshotManager(self)
138 self.volume_types = volume_types.VolumeTypeManager(self)
139 self.keypairs = keypairs.KeypairManager(self)
140 self.networks = networks.NetworkManager(self)
141 self.quota_classes = quota_classes.QuotaClassSetManager(self)
142 self.quotas = quotas.QuotaSetManager(self)
143 self.security_groups = security_groups.SecurityGroupManager(self)
144 self.security_group_rules = \
145 security_group_rules.SecurityGroupRuleManager(self)
146 self.security_group_default_rules = \
147 security_group_default_rules.SecurityGroupDefaultRuleManager(self)
148 self.usage = usage.UsageManager(self)
149 self.virtual_interfaces = \
150 virtual_interfaces.VirtualInterfaceManager(self)
151 self.aggregates = aggregates.AggregateManager(self)
152 self.hosts = hosts.HostManager(self)
153 self.hypervisors = hypervisors.HypervisorManager(self)
154 self.hypervisor_stats = hypervisors.HypervisorStatsManager(self)
155 self.services = services.ServiceManager(self)
156 self.fixed_ips = fixed_ips.FixedIPsManager(self)
157 self.floating_ips_bulk = floating_ips_bulk.FloatingIPBulkManager(self)
158 self.os_cache = os_cache or not no_cache
159 self.availability_zones = \
160 availability_zones.AvailabilityZoneManager(self)
161 self.server_groups = server_groups.ServerGroupsManager(self)
162
163 # Add in any extensions...
164 if extensions:
165 for extension in extensions:
166 if extension.manager_class:
167 setattr(self, extension.name,
168 extension.manager_class(self))
169
170 self.client = client._construct_http_client(
171 username=username,
172 password=password,
173 user_id=user_id,
174 project_id=project_id,
175 tenant_id=tenant_id,
176 auth_url=auth_url,
177 auth_token=auth_token,
178 insecure=insecure,
179 timeout=timeout,
180 auth_system=auth_system,
181 auth_plugin=auth_plugin,
182 proxy_token=proxy_token,
183 proxy_tenant_id=proxy_tenant_id,
184 region_name=region_name,
185 endpoint_type=endpoint_type,
186 service_type=service_type,
187 service_name=service_name,
188 volume_service_name=volume_service_name,
189 timings=timings,
190 bypass_url=bypass_url,
191 os_cache=self.os_cache,
192 http_log_debug=http_log_debug,
193 cacert=cacert,
194 connection_pool=connection_pool,
195 session=session,
196 auth=auth,
197 **kwargs)
198
199 self.completion_cache = completion_cache
200
201 def write_object_to_completion_cache(self, obj):
202 if self.completion_cache:
203 self.completion_cache.write_object(obj)
204
205 def clear_completion_cache_for_class(self, obj_class):
206 if self.completion_cache:
207 self.completion_cache.clear_class(obj_class)
208
209 @client._original_only
210 def __enter__(self):
211 self.client.open_session()
212 return self
213
214 @client._original_only
215 def __exit__(self, t, v, tb):
216 self.client.close_session()
217
218 @client._original_only
219 def set_management_url(self, url):
220 self.client.set_management_url(url)
221
222 def get_timings(self):
223 return self.client.get_timings()
224
225 def reset_timings(self):
226 self.client.reset_timings()
227
228 @client._original_only
229 def authenticate(self):
230 """
231 Authenticate against the server.
232
233 Normally this is called automatically when you first access the API,
234 but you can call this method to force authentication right now.
235
236 Returns on success; raises :exc:`exceptions.Unauthorized` if the
237 credentials are wrong.
238 """
239 self.client.authenticate()