~salvatore-orlando/neutron/quantum-api

« back to all changes in this revision

Viewing changes to test_scripts/miniclient.py

  • Committer: Salvatore Orlando
  • Date: 2011-06-24 13:52:17 UTC
  • mfrom: (6.1.14 quantum-trunk)
  • Revision ID: salvatore.orlando@eu.citrix.com-20110624135217-h6uz1zu3fxxpf3wt
Merge trunk
Resolving conflicts

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
 
 
3
 
# Copyright 2011 Citrix Systems
4
 
# All Rights Reserved.
5
 
#
6
 
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
7
 
#    not use this file except in compliance with the License. You may obtain
8
 
#    a copy of the License at
9
 
#
10
 
#         http://www.apache.org/licenses/LICENSE-2.0
11
 
#
12
 
#    Unless required by applicable law or agreed to in writing, software
13
 
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
 
#    License for the specific language governing permissions and limitations
16
 
#    under the License.
17
 
 
18
 
import httplib
19
 
import socket
20
 
import urllib
21
 
 
22
 
class MiniClient(object):
23
 
 
24
 
    """A base client class - derived from Glance.BaseClient"""
25
 
 
26
 
    action_prefix = '/v0.1/tenants/{tenant_id}'
27
 
 
28
 
    def __init__(self, host, port, use_ssl):
29
 
        """
30
 
        Creates a new client to some service.
31
 
 
32
 
        :param host: The host where service resides
33
 
        :param port: The port where service resides
34
 
        :param use_ssl: Should we use HTTPS?
35
 
        """
36
 
        self.host = host
37
 
        self.port = port
38
 
        self.use_ssl = use_ssl
39
 
        self.connection = None
40
 
 
41
 
    def get_connection_type(self):
42
 
        """
43
 
        Returns the proper connection type
44
 
        """
45
 
        if self.use_ssl:
46
 
            return httplib.HTTPSConnection
47
 
        else:
48
 
            return httplib.HTTPConnection
49
 
 
50
 
    def do_request(self, tenant, method, action, body=None,
51
 
                   headers=None, params=None):
52
 
        """
53
 
        Connects to the server and issues a request.  
54
 
        Returns the result data, or raises an appropriate exception if
55
 
        HTTP status code is not 2xx
56
 
 
57
 
        :param method: HTTP method ("GET", "POST", "PUT", etc...)
58
 
        :param body: string of data to send, or None (default)
59
 
        :param headers: mapping of key/value pairs to add as headers
60
 
        :param params: dictionary of key/value pairs to add to append
61
 
                             to action
62
 
 
63
 
        """
64
 
        action = MiniClient.action_prefix + action
65
 
        action = action.replace('{tenant_id}',tenant)
66
 
        if type(params) is dict:
67
 
            action += '?' + urllib.urlencode(params)
68
 
 
69
 
        try:
70
 
            connection_type = self.get_connection_type()
71
 
            headers = headers or {}
72
 
            
73
 
            # Open connection and send request
74
 
            c = connection_type(self.host, self.port)
75
 
            c.request(method, action, body, headers)
76
 
            res = c.getresponse()
77
 
            status_code = self.get_status_code(res)
78
 
            if status_code in (httplib.OK,
79
 
                               httplib.CREATED,
80
 
                               httplib.ACCEPTED,
81
 
                               httplib.NO_CONTENT):
82
 
                return res
83
 
            else:
84
 
                raise Exception("Server returned error: %s" % res.read())
85
 
 
86
 
        except (socket.error, IOError), e:
87
 
            raise Exception("Unable to connect to "
88
 
                            "server. Got error: %s" % e)
89
 
 
90
 
    def get_status_code(self, response):
91
 
        """
92
 
        Returns the integer status code from the response, which
93
 
        can be either a Webob.Response (used in testing) or httplib.Response
94
 
        """
95
 
        if hasattr(response, 'status_int'):
96
 
            return response.status_int
97
 
        else:
98
 
            return response.status
 
 
b'\\ No newline at end of file'