~ubuntu-branches/ubuntu/vivid/neutron/vivid-updates

« back to all changes in this revision

Viewing changes to neutron/tests/tempest/services/identity/v2/json/token_client.py

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2015-03-30 11:17:19 UTC
  • mfrom: (1.1.21)
  • Revision ID: package-import@ubuntu.com-20150330111719-h0gx7233p4jkkgfh
Tags: 1:2015.1~b3-0ubuntu1
* New upstream milestone release:
  - d/control: Align version requirements with upstream.
  - d/control: Add new dependency on oslo-log.
  - d/p/*: Rebase.
  - d/control,d/neutron-plugin-hyperv*: Dropped, decomposed into
    separate project upstream.
  - d/control,d/neutron-plugin-openflow*: Dropped, decomposed into
    separate project upstream.
  - d/neutron-common.install: Add neutron-rootwrap-daemon and 
    neutron-keepalived-state-change binaries.
  - d/rules: Ignore neutron-hyperv-agent when installing; only for Windows.
  - d/neutron-plugin-cisco.install: Drop neutron-cisco-cfg-agent as
    decomposed into separate project upstream.
  - d/neutron-plugin-vmware.install: Drop neutron-check-nsx-config and
    neutron-nsx-manage as decomposed into separate project upstream.
  - d/control: Add dependency on python-neutron-fwaas to neutron-l3-agent.
* d/pydist-overrides: Add overrides for oslo packages.
* d/control: Fixup type in package description (LP: #1263539).
* d/p/fixup-driver-test-execution.patch: Cherry pick fix from upstream VCS
  to support unit test exection in out-of-tree vendor drivers.
* d/neutron-common.postinst: Allow general access to /etc/neutron but limit
  access to root/neutron to /etc/neutron/neutron.conf to support execution
  of unit tests in decomposed vendor drivers.
* d/control: Add dependency on python-neutron-fwaas to neutron-l3-agent
  package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2015 NEC Corporation.  All rights reserved.
 
2
#
 
3
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
 
4
#    not use this file except in compliance with the License. You may obtain
 
5
#    a copy of the License at
 
6
#
 
7
#         http://www.apache.org/licenses/LICENSE-2.0
 
8
#
 
9
#    Unless required by applicable law or agreed to in writing, software
 
10
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
11
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
12
#    License for the specific language governing permissions and limitations
 
13
#    under the License.
 
14
 
 
15
import json
 
16
from tempest_lib.common import rest_client
 
17
from tempest_lib import exceptions as lib_exc
 
18
 
 
19
from neutron.tests.tempest.common import service_client
 
20
from neutron.tests.tempest import exceptions
 
21
 
 
22
 
 
23
class TokenClientJSON(rest_client.RestClient):
 
24
 
 
25
    def __init__(self, auth_url, disable_ssl_certificate_validation=None,
 
26
                 ca_certs=None, trace_requests=None):
 
27
        dscv = disable_ssl_certificate_validation
 
28
        super(TokenClientJSON, self).__init__(
 
29
            None, None, None, disable_ssl_certificate_validation=dscv,
 
30
            ca_certs=ca_certs, trace_requests=trace_requests)
 
31
 
 
32
        # Normalize URI to ensure /tokens is in it.
 
33
        if 'tokens' not in auth_url:
 
34
            auth_url = auth_url.rstrip('/') + '/tokens'
 
35
 
 
36
        self.auth_url = auth_url
 
37
 
 
38
    def auth(self, user, password, tenant=None):
 
39
        creds = {
 
40
            'auth': {
 
41
                'passwordCredentials': {
 
42
                    'username': user,
 
43
                    'password': password,
 
44
                },
 
45
            }
 
46
        }
 
47
 
 
48
        if tenant:
 
49
            creds['auth']['tenantName'] = tenant
 
50
 
 
51
        body = json.dumps(creds)
 
52
        resp, body = self.post(self.auth_url, body=body)
 
53
        self.expected_success(200, resp.status)
 
54
 
 
55
        return service_client.ResponseBody(resp, body['access'])
 
56
 
 
57
    def auth_token(self, token_id, tenant=None):
 
58
        creds = {
 
59
            'auth': {
 
60
                'token': {
 
61
                    'id': token_id,
 
62
                },
 
63
            }
 
64
        }
 
65
 
 
66
        if tenant:
 
67
            creds['auth']['tenantName'] = tenant
 
68
 
 
69
        body = json.dumps(creds)
 
70
        resp, body = self.post(self.auth_url, body=body)
 
71
        self.expected_success(200, resp.status)
 
72
 
 
73
        return service_client.ResponseBody(resp, body['access'])
 
74
 
 
75
    def request(self, method, url, extra_headers=False, headers=None,
 
76
                body=None):
 
77
        """A simple HTTP request interface."""
 
78
        if headers is None:
 
79
            headers = self.get_headers(accept_type="json")
 
80
        elif extra_headers:
 
81
            try:
 
82
                headers.update(self.get_headers(accept_type="json"))
 
83
            except (ValueError, TypeError):
 
84
                headers = self.get_headers(accept_type="json")
 
85
 
 
86
        resp, resp_body = self.raw_request(url, method,
 
87
                                           headers=headers, body=body)
 
88
        self._log_request(method, url, resp)
 
89
 
 
90
        if resp.status in [401, 403]:
 
91
            resp_body = json.loads(resp_body)
 
92
            raise lib_exc.Unauthorized(resp_body['error']['message'])
 
93
        elif resp.status not in [200, 201]:
 
94
            raise exceptions.IdentityError(
 
95
                'Unexpected status code {0}'.format(resp.status))
 
96
 
 
97
        if isinstance(resp_body, str):
 
98
            resp_body = json.loads(resp_body)
 
99
        return resp, resp_body
 
100
 
 
101
    def get_token(self, user, password, tenant, auth_data=False):
 
102
        """
 
103
        Returns (token id, token data) for supplied credentials
 
104
        """
 
105
        body = self.auth(user, password, tenant)
 
106
 
 
107
        if auth_data:
 
108
            return body['token']['id'], body
 
109
        else:
 
110
            return body['token']['id']