fa37733cd3e52ebe5ac637db892d264fa25bd6e6
[testsuite.git] / robot / resources / openstack / keystone_interface.robot
1 *** Settings ***
2 Documentation     The main interface for interacting with Openstack Keystone API. It handles low level stuff like managing the authtoken and Openstack required fields
3 Library           OpenstackLibrary
4 Library               RequestsLibrary
5 Library               HTTPUtils
6 Library           UUID
7 Library           Collections
8 Library    OperatingSystem
9 Library    String
10 Library    HttpLibrary.HTTP
11 Resource    ../global_properties.robot
12 Resource    ../json_templater.robot
13 Resource    openstack_common.robot
14
15 *** Variables ***
16 ${OPENSTACK_KEYSTONE_API_v3_VERSION}   /v3
17 ${OPENSTACK_KEYSTONE_API_v2_VERSION}   /v2.0
18 ${OPENSTACK_KEYSTONE_AUTH_v3_PATH}    /auth/tokens
19 ${OPENSTACK_KEYSTONE_AUTH_v2_PATH}    /tokens
20 ${OPENSTACK_KEYSTONE_AUTH_v2_BODY_FILE}    robot/assets/templates/keystone_get_v2_auth.template
21 ${OPENSTACK_KEYSTONE_AUTH_v3_BODY_FILE}    robot/assets/templates/keystone_get_v3_auth.template
22 ${OPENSTACK_KEYSTONE_TENANT_PATH}    /tenants
23
24 *** Keywords ***
25 Run Openstack Auth Request
26     [Documentation]    Runs an Openstack Auth Request and returns the token and service catalog. you need to include the token in future request's x-auth-token headers. Service catalog describes what can be called
27     [Arguments]    ${alias}    ${username}=    ${password}=
28     ${username}    ${password}=   Set Openstack Credentials   ${username}    ${password}
29     ${keystone_api_version}=    Run Keyword If    '${GLOBAL_INJECTED_OPENSTACK_KEYSTONE_API_VERSION}'==''    Get KeystoneAPIVersion 
30     ...    ELSE    Set Variable   ${GLOBAL_INJECTED_OPENSTACK_KEYSTONE_API_VERSION}
31     ${url}   ${path}=   Get Keystone Url And Path   ${keystone_api_version}
32     ${session}=    Create Session       keystone        ${url}    verify=True
33     ${uuid}=    Generate UUID
34     ${data_path}   ${data}=   Run Keyword If   '${keystone_api_version}'=='v2.0'   Get KeyStoneAuthv2 Data   ${username}    ${password}    ${path}
35     ...   ELSE   Get KeyStoneAuthv3 Data   ${username}    ${password}   ${path}
36     ${headers}=  Create Dictionary     Accept=application/json    Content-Type=application/json    X-TransactionId=${GLOBAL_APPLICATION_ID}-${uuid}    X-FromAppId=${GLOBAL_APPLICATION_ID}
37     Log    Sending authenticate post request ${data_path} with headers ${headers} and data ${data}
38     ${resp}=    Post Request    keystone        ${data_path}     data=${data}    headers=${headers}
39     Should Be True    200 <= ${resp.status_code} < 300
40     ${auth_token}=    Evaluate    ''
41     ${auth_token}=    Run Keyword If    '${keystone_api_version}'=='v3'    Get From Dictionary    ${resp.headers}    X-Subject-Token
42     Log    Keystone API Version is ${keystone_api_version}
43     Save Openstack Auth    ${alias}    ${resp.text}    ${auth_token}    ${keystone_api_version}
44     Log    Received response from keystone ${resp.text}
45
46 Get KeystoneAPIVersion
47     [Documentation]    Get Keystone API version
48     ${pieces}=    Url Parse    ${GLOBAL_INJECTED_KEYSTONE}
49     ${url}=    Catenate    ${pieces.scheme}://${pieces.netloc}
50     Log   Keystone URL is ${url}
51     ${session}=    Create Session    keystone    ${url}    verify=True
52     ${uuid}=    Generate UUID
53     ${headers}=    Create Dictionary    Accept=application/json    Content-Type=application/json    
54     ${resp}=    Get Request    keystone  /   headers=${headers}
55     Log    Received response from keystone ${resp.text}
56     Should Be Equal As Strings    ${resp.status_code}    300
57     ${json}=    Parse Json    ${resp.content}
58     ${versions}=   Get From Dictionary    ${json}   versions
59     ${values}=   Get From Dictionary    ${versions}   values
60     : FOR    ${value}    IN    @{values}
61        \  ${status}=    Get Variable Value    ${value["status"]}
62        \  Run Keyword If    '${status}'=='stable'   Exit For Loop
63     ${href}=  Set Variable     ${value["links"][0]["href"]}
64     ${keystone}=  Set Variable   ${GLOBAL_INJECTED_KEYSTONE}  
65     ${version}=    Remove String  ${href}   ${keystone}  /
66     Return From Keyword If   '${version}'=='v2.0' or '${version}'=='v3'    ${version}
67     Fail   Keystone API version not found or not supported    
68         
69 Get KeyStoneAuthv2 Data
70     [Documentation]    Returns all the data for keystone auth v2 api
71     [Arguments]    ${username}    ${password}    ${path}
72     ${data_template}=    OperatingSystem.Get File    ${OPENSTACK_KEYSTONE_AUTH_v2_BODY_FILE}
73     ${arguments}=    Create Dictionary    username=${username}    password=${password}   tenantId=${GLOBAL_INJECTED_OPENSTACK_TENANT_ID}
74     ${data}=    Fill JSON Template    ${data_template}    ${arguments}
75     ${data_path}=    Catenate    ${path}${OPENSTACK_KEYSTONE_AUTH_v2_PATH}
76     [Return]    ${data_path}    ${data}
77
78 Get KeyStoneAuthv3 Data
79     [Documentation]    Returns all the data for keystone auth v3 api
80     [Arguments]    ${username}    ${password}    ${path}
81     ${data_template}=    OperatingSystem.Get File    ${OPENSTACK_KEYSTONE_AUTH_v3_BODY_FILE}
82     ${arguments}=    Create Dictionary    username=${username}    password=${password}   domain_id=${GLOBAL_INJECTED_OPENSTACK_DOMAIN_ID}    project_name=${GLOBAL_INJECTED_OPENSTACK_PROJECT_NAME}
83     ${data}=    Fill JSON Template    ${data_template}    ${arguments}
84     ${data_path}=    Catenate    ${path}${OPENSTACK_KEYSTONE_AUTH_v3_PATH}
85     [Return]    ${data_path}    ${data}
86
87 Get Openstack Tenants
88     [Documentation]    Returns all the openstack tenant info
89     [Arguments]    ${alias}
90     ${resp}=    Internal Get Openstack With Region    ${alias}    ${GLOBAL_OPENSTACK_KEYSTONE_SERVICE_TYPE}    region=    url_ext=${OPENSTACK_KEYSTONE_TENANT_PATH}    data_path=
91     [Return]    ${resp.json()}
92
93 Get Openstack Tenant
94     [Documentation]    Returns the openstack tenant info for the specified tenantid
95     [Arguments]    ${alias}     ${tenant_id}
96     ${resp}=    Internal Get Openstack With Region    ${alias}    ${GLOBAL_OPENSTACK_KEYSTONE_SERVICE_TYPE}    region=    url_ext=${OPENSTACK_KEYSTONE_TENANT_PATH}    data_path=/${tenant_id}
97     [Return]    ${resp.json()}
98
99 Set Openstack Credentials
100     [Arguments]    ${username}    ${password}
101     Return From Keyword If    '${username}' != ''   ${username}    ${password}
102     ${user}   ${pass}=   Get Openstack Credentials
103     [Return]   ${user}   ${pass}
104
105 Get Openstack Credentials
106     [Documentation]   Returns the Decripted Password and openstack username using same api_key.txt as SO
107     ${DECRYPTED_OPENSTACK_PASSWORD}=   Run    echo -n ${GLOBAL_INJECTED_OPENSTACK_API_KEY} | xxd -r -p | openssl enc -aes-128-ecb -d -nosalt -K aa3871669d893c7fb8abbcda31b88b4f | tr -d '\x08'
108     [Return]   ${GLOBAL_INJECTED_OPENSTACK_USERNAME}    ${DECRYPTED_OPENSTACK_PASSWORD}
109
110
111 Get Keystone Url And Path
112     [Arguments]    ${keystone_api_version}
113     [Documentation]    Handle arbitrary keystone identiit url. Add v2.0 if not present.
114     ${url}    ${path}=    Run Keyword If    '${keystone_api_version}'=='v2.0'    Set API Version    ${OPENSTACK_KEYSTONE_API_v2_VERSION}
115     ...    ELSE    Set API Version    ${OPENSTACK_KEYSTONE_API_v3_VERSION}
116     Log    Path is ${url} ${path}
117     [Return]   ${url}   ${path}
118
119 Set API Version
120     [Documentation]    Decides the API version to be used
121     [Arguments]    ${openstack_version}
122     ${pieces}=   Url Parse   ${GLOBAL_INJECTED_KEYSTONE}
123     ${url}=      Catenate   ${pieces.scheme}://${pieces.netloc}
124     ${version}=  Evaluate   ''
125     ${version}=  Set Variable If   '${openstack_version}' not in '${pieces.path}'   ${openstack_version}   ${version}
126     ${path}=     Catenate   ${pieces.path}${version}
127     [Return]   ${url}   ${path}