replace aai, closeloop, sdc with jinja
[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           ONAPLibrary.Openstack
4 Library               RequestsLibrary
5 Library           ONAPLibrary.Utilities
6 Library           Collections
7 Library           OperatingSystem
8 Library           String
9 Library           ONAPLibrary.Templating
10 Resource    ../global_properties.robot
11 Resource    openstack_common.robot
12
13 *** Variables ***
14 ${OPENSTACK_KEYSTONE_API_v3_VERSION}   /v3
15 ${OPENSTACK_KEYSTONE_API_v2_VERSION}   /v2.0
16 ${OPENSTACK_KEYSTONE_AUTH_v3_PATH}    /auth/tokens
17 ${OPENSTACK_KEYSTONE_AUTH_v2_PATH}    /tokens
18 ${OPENSTACK_KEYSTONE_AUTH_v2_BODY_FILE}    openstack/keystone_get_v2_auth.jinja
19 ${OPENSTACK_KEYSTONE_AUTH_v3_BODY_FILE}    openstack/keystone_get_v3_auth.jinja
20 ${OPENSTACK_KEYSTONE_TENANT_PATH}    /tenants
21
22 *** Keywords ***
23 Run Openstack Auth Request
24     [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
25     [Arguments]    ${alias}    ${username}=    ${password}=
26     ${username}    ${password}=   Set Openstack Credentials   ${username}    ${password}
27     ${keystone_api_version}=    Run Keyword If    '${GLOBAL_INJECTED_OPENSTACK_KEYSTONE_API_VERSION}'==''    Get KeystoneAPIVersion 
28     ...    ELSE    Set Variable   ${GLOBAL_INJECTED_OPENSTACK_KEYSTONE_API_VERSION}
29     ${url}   ${path}=   Get Keystone Url And Path   ${keystone_api_version}
30     ${session}=    Create Session       keystone        ${url}    verify=True
31     ${uuid}=    Generate UUID4
32     ${data_path}   ${data}=   Run Keyword If   '${keystone_api_version}'=='v2.0'   Get KeyStoneAuthv2 Data   ${username}    ${password}    ${path}
33     ...   ELSE   Get KeyStoneAuthv3 Data   ${username}    ${password}   ${path}
34     ${headers}=  Create Dictionary     Accept=application/json    Content-Type=application/json    X-TransactionId=${GLOBAL_APPLICATION_ID}-${uuid}    X-FromAppId=${GLOBAL_APPLICATION_ID}
35     Log    Sending authenticate post request ${data_path} with headers ${headers} and data ${data}
36     ${resp}=    Post Request    keystone        ${data_path}     data=${data}    headers=${headers}
37     Should Be True    200 <= ${resp.status_code} < 300
38     ${auth_token}=    Evaluate    ''
39     ${auth_token}=    Run Keyword If    '${keystone_api_version}'=='v3'    Get From Dictionary    ${resp.headers}    X-Subject-Token
40     Log    Keystone API Version is ${keystone_api_version}
41     Save Openstack Auth    ${alias}    ${resp.text}    ${auth_token}    ${keystone_api_version}
42     Log    Received response from keystone ${resp.text}
43
44 Get KeystoneAPIVersion
45     [Documentation]    Get Keystone API version
46     ${pieces}=    Url Parse    ${GLOBAL_INJECTED_KEYSTONE}
47     ${url}=    Catenate    ${pieces.scheme}://${pieces.netloc}
48     Log   Keystone URL is ${url}
49     ${session}=    Create Session    keystone    ${url}    verify=True
50     ${uuid}=    Generate UUID4
51     ${headers}=    Create Dictionary    Accept=application/json    Content-Type=application/json    
52     ${resp}=    Get Request    keystone  /   headers=${headers}
53     Log    Received response from keystone ${resp.text}
54     Should Be Equal As Strings    ${resp.status_code}    300
55     ${versions}=   Get From Dictionary    ${resp.json()}   versions
56     ${values}=   Get From Dictionary    ${versions}   values
57     :FOR    ${value}    IN    @{values}
58        \  ${status}=    Get Variable Value    ${value["status"]}
59        \  Run Keyword If    '${status}'=='stable'   Exit For Loop
60     ${href}=  Set Variable     ${value["links"][0]["href"]}
61     ${keystone}=  Set Variable   ${GLOBAL_INJECTED_KEYSTONE}  
62     ${version}=    Remove String  ${href}   ${keystone}  /
63     Return From Keyword If   '${version}'=='v2.0' or '${version}'=='v3'    ${version}
64     Fail   Keystone API version not found or not supported    
65         
66 Get KeyStoneAuthv2 Data
67     [Documentation]    Returns all the data for keystone auth v2 api
68     [Arguments]    ${username}    ${password}    ${path}
69     ${arguments}=    Create Dictionary    username=${username}    password=${password}   tenantId=${GLOBAL_INJECTED_OPENSTACK_TENANT_ID}
70     Create Environment    keystone    ${GLOBAL_TEMPLATE_FOLDER}
71     ${data}=    Apply Template    keystone    ${OPENSTACK_KEYSTONE_AUTH_v2_BODY_FILE}    ${arguments}
72     ${data_path}=    Catenate    ${path}${OPENSTACK_KEYSTONE_AUTH_v2_PATH}
73     [Return]    ${data_path}    ${data}
74
75 Get KeyStoneAuthv3 Data
76     [Documentation]    Returns all the data for keystone auth v3 api
77     [Arguments]    ${username}    ${password}    ${path}
78     ${arguments}=    Create Dictionary    username=${username}    password=${password}   domain_id=${GLOBAL_INJECTED_OPENSTACK_DOMAIN_ID}    project_name=${GLOBAL_INJECTED_OPENSTACK_PROJECT_NAME}
79     Create Environment    keystone    ${GLOBAL_TEMPLATE_FOLDER}
80     ${data}=    Apply Template    keystone    ${OPENSTACK_KEYSTONE_AUTH_v3_BODY_FILE}    ${arguments}
81     ${data_path}=    Catenate    ${path}${OPENSTACK_KEYSTONE_AUTH_v3_PATH}
82     [Return]    ${data_path}    ${data}
83
84 Get Openstack Tenants
85     [Documentation]    Returns all the openstack tenant info
86     [Arguments]    ${alias}
87     ${resp}=    Internal Get Openstack With Region    ${alias}    ${GLOBAL_OPENSTACK_KEYSTONE_SERVICE_TYPE}    region=    url_ext=${OPENSTACK_KEYSTONE_TENANT_PATH}    data_path=
88     [Return]    ${resp.json()}
89
90 Get Openstack Tenant
91     [Documentation]    Returns the openstack tenant info for the specified tenantid
92     [Arguments]    ${alias}     ${tenant_id}
93     ${resp}=    Internal Get Openstack With Region    ${alias}    ${GLOBAL_OPENSTACK_KEYSTONE_SERVICE_TYPE}    region=    url_ext=${OPENSTACK_KEYSTONE_TENANT_PATH}    data_path=/${tenant_id}
94     [Return]    ${resp.json()}
95
96 Set Openstack Credentials
97     [Arguments]    ${username}    ${password}
98     Return From Keyword If    '${username}' != ''   ${username}    ${password}
99     ${user}   ${pass}=   Get Openstack Credentials
100     [Return]   ${user}   ${pass}
101
102 Get Openstack Credentials
103     [Documentation]   Returns the Decripted Password and openstack username using same api_key.txt as SO
104     ${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'
105     [Return]   ${GLOBAL_INJECTED_OPENSTACK_USERNAME}    ${DECRYPTED_OPENSTACK_PASSWORD}
106
107
108 Get Keystone Url And Path
109     [Arguments]    ${keystone_api_version}
110     [Documentation]    Handle arbitrary keystone identiit url. Add v2.0 if not present.
111     ${url}    ${path}=    Run Keyword If    '${keystone_api_version}'=='v2.0'    Set API Version    ${OPENSTACK_KEYSTONE_API_v2_VERSION}
112     ...    ELSE    Set API Version    ${OPENSTACK_KEYSTONE_API_v3_VERSION}
113     Log    Path is ${url} ${path}
114     [Return]   ${url}   ${path}
115
116 Set API Version
117     [Documentation]    Decides the API version to be used
118     [Arguments]    ${openstack_version}
119     ${pieces}=   Url Parse   ${GLOBAL_INJECTED_KEYSTONE}
120     ${url}=      Catenate   ${pieces.scheme}://${pieces.netloc}
121     ${version}=  Evaluate   ''
122     ${version}=  Set Variable If   '${openstack_version}' not in '${pieces.path}'   ${openstack_version}   ${version}
123     ${path}=     Catenate   ${pieces.path}${version}
124     [Return]   ${url}   ${path}