DR695H | ccff30b | 2017-02-17 18:44:24 -0500 | [diff] [blame] | 1 | *** Settings *** |
| 2 | Documentation The main interface for interacting with Openstack. It handles low level stuff like managing the authtoken and Openstack required fields |
DR695H | d92b007 | 2019-06-17 17:23:14 -0400 | [diff] [blame] | 3 | Library ONAPLibrary.Openstack |
DR695H | ccff30b | 2017-02-17 18:44:24 -0500 | [diff] [blame] | 4 | Library RequestsLibrary |
DR695H | 7aca1a4 | 2019-06-03 17:08:34 -0400 | [diff] [blame] | 5 | Library ONAPLibrary.JSON |
DR695H | ccff30b | 2017-02-17 18:44:24 -0500 | [diff] [blame] | 6 | Library OperatingSystem |
| 7 | Library Collections |
| 8 | Resource ../global_properties.robot |
| 9 | Resource ../json_templater.robot |
| 10 | Resource openstack_common.robot |
| 11 | |
| 12 | *** Variables *** |
| 13 | ${OPENSTACK_NOVA_API_VERSION} /v2 |
| 14 | ${OPENSTACK_NOVA_KEYPAIR_PATH} /os-keypairs |
| 15 | ${OPENSTACK_NOVA_KEYPAIR_ADD_BODY_FILE} robot/assets/templates/nova_add_keypair.template |
| 16 | ${OPENSTACK_NOVA_KEYPAIR_SSH_KEY} robot/assets/keys/robot_ssh_public_key.txt |
| 17 | ${OPENSTACK_NOVA_FLAVORS_PATH} /flavors |
| 18 | ${OPENSTACK_NOVA_SERVERS_PATH} /servers |
| 19 | ${OPENSTACK_NOVA_IMAGES_PATH} /images |
| 20 | ${OPENSTACK_NOVA_SERVERS_REBOOT_BODY} {"reboot" : { "type" : "SOFT" }} |
| 21 | ${OPENSTACK_NOVA_SERVER_ADD_BODY_FILE} robot/assets/templates/nova_add_server.template |
| 22 | |
| 23 | |
| 24 | *** Keywords *** |
| 25 | Get Openstack Keypair |
| 26 | [Documentation] Runs an Openstack Request and returns the keypair info |
| 27 | [Arguments] ${alias} ${keypair_name} |
jf9860 | 99c6329 | 2017-03-09 15:28:42 -0500 | [diff] [blame] | 28 | ${resp}= Internal Get Openstack ${alias} ${GLOBAL_OPENSTACK_NOVA_SERVICE_TYPE} ${OPENSTACK_NOVA_KEYPAIR_PATH} /${keypair_name} |
DR695H | ccff30b | 2017-02-17 18:44:24 -0500 | [diff] [blame] | 29 | [Return] ${resp.json()} |
| 30 | |
| 31 | Add Openstack Keypair |
| 32 | [Documentation] Runs an Openstack Request to add a keypair and returns the keypair name |
| 33 | [Arguments] ${alias} ${name} |
DR695H | ccff30b | 2017-02-17 18:44:24 -0500 | [diff] [blame] | 34 | ${ssh_key}= OperatingSystem.Get File ${OPENSTACK_NOVA_KEYPAIR_SSH_KEY} |
| 35 | ${arguments}= Create Dictionary name=${name} publickey=${ssh_key} |
DR695H | daec16c | 2019-06-19 17:40:26 -0400 | [diff] [blame^] | 36 | ${data}= Fill JSON Template File ${OPENSTACK_NOVA_KEYPAIR_ADD_BODY_FILE} ${arguments} |
jf9860 | 99c6329 | 2017-03-09 15:28:42 -0500 | [diff] [blame] | 37 | ${resp}= Internal Post Openstack ${alias} ${GLOBAL_OPENSTACK_NOVA_SERVICE_TYPE} ${OPENSTACK_NOVA_KEYPAIR_PATH} data_path= data=${data} |
DR695H | ccff30b | 2017-02-17 18:44:24 -0500 | [diff] [blame] | 38 | Should Be Equal As Strings 200 ${resp.status_code} |
| 39 | [Return] ${resp.json()['keypair']['name']} |
| 40 | |
| 41 | Delete Openstack Keypair |
| 42 | [Documentation] Runs an Openstack Request to delete a keypair |
| 43 | [Arguments] ${alias} ${keypair_name} |
jf9860 | 99c6329 | 2017-03-09 15:28:42 -0500 | [diff] [blame] | 44 | ${resp}= Internal Delete Openstack ${alias} ${GLOBAL_OPENSTACK_NOVA_SERVICE_TYPE} ${OPENSTACK_NOVA_KEYPAIR_PATH} /${keypair_name} |
DR695H | ccff30b | 2017-02-17 18:44:24 -0500 | [diff] [blame] | 45 | ${status_string}= Convert To String ${resp.status_code} |
jf9860 | 99c6329 | 2017-03-09 15:28:42 -0500 | [diff] [blame] | 46 | Should Match Regexp ${status_string} ^(204|202|200)$ |
DR695H | ccff30b | 2017-02-17 18:44:24 -0500 | [diff] [blame] | 47 | [Return] ${resp.text} |
| 48 | |
| 49 | |
| 50 | Get Openstack Servers |
| 51 | [Documentation] Returns the list of servers as a dictionary by name |
| 52 | [Arguments] ${alias} |
jf9860 | 99c6329 | 2017-03-09 15:28:42 -0500 | [diff] [blame] | 53 | ${resp}= Internal Get Openstack ${alias} ${GLOBAL_OPENSTACK_NOVA_SERVICE_TYPE} ${OPENSTACK_NOVA_SERVERS_PATH} /detail |
DR695H | ccff30b | 2017-02-17 18:44:24 -0500 | [diff] [blame] | 54 | Log Returned from Internal Get Openstack |
| 55 | ${by_name}= Make List Into Dict ${resp.json()['servers']} name |
| 56 | Log got it |
| 57 | [Return] ${by_name} |
jf9860 | 99c6329 | 2017-03-09 15:28:42 -0500 | [diff] [blame] | 58 | |
DR695H | ccff30b | 2017-02-17 18:44:24 -0500 | [diff] [blame] | 59 | Get Openstack Server By Id |
| 60 | [Documentation] Returns the openstack stacks info for the given stack name |
jf9860 | 99c6329 | 2017-03-09 15:28:42 -0500 | [diff] [blame] | 61 | [Arguments] ${alias} ${server_id} |
| 62 | ${resp}= Internal Get Openstack ${alias} ${GLOBAL_OPENSTACK_NOVA_SERVICE_TYPE} ${OPENSTACK_NOVA_SERVERS_PATH} /${server_id} |
DR695H | ccff30b | 2017-02-17 18:44:24 -0500 | [diff] [blame] | 63 | [Return] ${resp} |
jf9860 | 99c6329 | 2017-03-09 15:28:42 -0500 | [diff] [blame] | 64 | |
DR695H | ccff30b | 2017-02-17 18:44:24 -0500 | [diff] [blame] | 65 | Get Openstack Flavors |
| 66 | [Documentation] Runs an Openstack Request and returns the flavor list |
| 67 | [Arguments] ${alias} |
jf9860 | 99c6329 | 2017-03-09 15:28:42 -0500 | [diff] [blame] | 68 | ${resp}= Internal Get Openstack ${alias} ${GLOBAL_OPENSTACK_NOVA_SERVICE_TYPE} ${OPENSTACK_NOVA_FLAVORS_PATH} |
DR695H | ccff30b | 2017-02-17 18:44:24 -0500 | [diff] [blame] | 69 | [Return] ${resp.json()} |
| 70 | |
| 71 | Get Openstack Images |
| 72 | [Documentation] Runs an Openstack Request and returns the flavor list |
| 73 | [Arguments] ${alias} |
jf9860 | 99c6329 | 2017-03-09 15:28:42 -0500 | [diff] [blame] | 74 | ${resp}= Internal Get Openstack ${alias} ${GLOBAL_OPENSTACK_NOVA_SERVICE_TYPE} ${OPENSTACK_NOVA_IMAGES_PATH} |
DR695H | ccff30b | 2017-02-17 18:44:24 -0500 | [diff] [blame] | 75 | [Return] ${resp.json()} |
| 76 | |
| 77 | Reboot Server |
| 78 | [Documentation] Requests a reboot of the passed server id |
jf9860 | 99c6329 | 2017-03-09 15:28:42 -0500 | [diff] [blame] | 79 | [Arguments] ${alias} ${server_id} |
| 80 | ${resp}= Internal Post Openstack ${alias} ${GLOBAL_OPENSTACK_NOVA_SERVICE_TYPE} ${OPENSTACK_NOVA_SERVERS_PATH} /${server_id}/action ${OPENSTACK_NOVA_SERVERS_REBOOT_BODY} |
DR695H | ccff30b | 2017-02-17 18:44:24 -0500 | [diff] [blame] | 81 | [Return] ${resp} |
| 82 | |
| 83 | Add Server |
| 84 | [Documentation] Adds a server for the passed if |
| 85 | [Arguments] ${alias} ${name} ${imageRef} ${flavorRef} |
| 86 | ${dict}= Create Dictionary name=${name} imageRef=${imageRef} flavorRef=${flavorRef} |
jf9860 | 99c6329 | 2017-03-09 15:28:42 -0500 | [diff] [blame] | 87 | ${data}= Fill JSON Template File ${OPENSTACK_NOVA_SERVER_ADD_BODY_FILE} ${dict} |
| 88 | ${resp}= Internal Post Openstack ${alias} ${GLOBAL_OPENSTACK_NOVA_SERVICE_TYPE} ${OPENSTACK_NOVA_SERVERS_PATH} data_path= data=${data} |
DR695H | ccff30b | 2017-02-17 18:44:24 -0500 | [diff] [blame] | 89 | [Return] ${resp} |
| 90 | |
| 91 | Add Server For Image Name |
| 92 | [Documentation] Adds a server for the passed if |
Jerry Flood | 339809a | 2017-10-30 20:19:16 -0400 | [diff] [blame] | 93 | [Arguments] ${alias} ${name} ${imageName} ${flavorName} ${public_net_id} |
DR695H | ccff30b | 2017-02-17 18:44:24 -0500 | [diff] [blame] | 94 | ${images}= Get Openstack Images ${alias} |
| 95 | ${flavors}= Get Openstack Flavors ${alias} |
| 96 | ${images}= Get From Dictionary ${images} images |
| 97 | ${flavors}= Get From Dictionary ${flavors} flavors |
| 98 | ${imageRef}= Get Id For Name ${images} ${imageName} |
| 99 | ${flavorRef}= Get Id For Name ${flavors} ${flavorName} |
Jerry Flood | 339809a | 2017-10-30 20:19:16 -0400 | [diff] [blame] | 100 | ${dict}= Create Dictionary name=${name} imageRef=${imageRef} flavorRef=${flavorRef} public_net_id=${public_net_id} |
jf9860 | 99c6329 | 2017-03-09 15:28:42 -0500 | [diff] [blame] | 101 | ${data}= Fill JSON Template File ${OPENSTACK_NOVA_SERVER_ADD_BODY_FILE} ${dict} |
| 102 | ${resp}= Internal Post Openstack ${alias} ${GLOBAL_OPENSTACK_NOVA_SERVICE_TYPE} ${OPENSTACK_NOVA_SERVERS_PATH} data_path= data=${data} |
DR695H | ccff30b | 2017-02-17 18:44:24 -0500 | [diff] [blame] | 103 | ${status_string}= Convert To String ${resp.status_code} |
| 104 | Should Match Regexp ${status_string} ^(202)$ |
| 105 | [Return] ${resp.json()} |
| 106 | |
| 107 | Wait for Server to Be Active |
| 108 | [Arguments] ${alias} ${server_id} ${timeout}=300s |
jf9860 | 99c6329 | 2017-03-09 15:28:42 -0500 | [diff] [blame] | 109 | ${server_info}= Wait Until Keyword Succeeds ${timeout} 10 sec Get Active Server ${alias} ${server_id} |
DR695H | ccff30b | 2017-02-17 18:44:24 -0500 | [diff] [blame] | 110 | ${status}= Get From Dictionary ${server_info} status |
| 111 | Should Be Equal ${status} ACTIVE |
| 112 | [Return] ${server_info} |
| 113 | |
| 114 | Get Active Server |
jf9860 | 99c6329 | 2017-03-09 15:28:42 -0500 | [diff] [blame] | 115 | [Arguments] ${alias} ${server_id} |
DR695H | ccff30b | 2017-02-17 18:44:24 -0500 | [diff] [blame] | 116 | ${resp}= Get Openstack Server By Id ${alias} ${server_id} |
| 117 | Should Be Equal As Strings ${resp.status_code} 200 |
jf9860 | 99c6329 | 2017-03-09 15:28:42 -0500 | [diff] [blame] | 118 | ${server_info}= Set Variable ${resp.json()} |
DR695H | ccff30b | 2017-02-17 18:44:24 -0500 | [diff] [blame] | 119 | ${server_info}= Get From Dictionary ${server_info} server |
| 120 | ${status}= Get From Dictionary ${server_info} status |
| 121 | Should Not Be Equal ${status} BUILD |
jf9860 | 99c6329 | 2017-03-09 15:28:42 -0500 | [diff] [blame] | 122 | [Return] ${server_info} |
DR695H | ccff30b | 2017-02-17 18:44:24 -0500 | [diff] [blame] | 123 | |
| 124 | Wait for Server to Be Deleted |
| 125 | [Arguments] ${alias} ${server_id} |
| 126 | Wait Until Keyword Succeeds 300s 10s Get Deleted Server ${alias} ${server_id} |
| 127 | |
| 128 | Get Deleted Server |
| 129 | [Arguments] ${alias} ${server_id} |
| 130 | ${resp}= Get Openstack Server By Id ${alias} ${server_id} |
| 131 | Should Be Equal As Strings ${resp.status_code} 404 |
| 132 | |
| 133 | Delete Server |
| 134 | [Documentation] Runs an Openstack Request to delete a keypair |
| 135 | [Arguments] ${alias} ${server_id} |
jf9860 | 99c6329 | 2017-03-09 15:28:42 -0500 | [diff] [blame] | 136 | ${resp}= Internal Delete Openstack ${alias} ${GLOBAL_OPENSTACK_NOVA_SERVICE_TYPE} ${OPENSTACK_NOVA_SERVERS_PATH} /${server_id} |
DR695H | ccff30b | 2017-02-17 18:44:24 -0500 | [diff] [blame] | 137 | ${status_string}= Convert To String ${resp.status_code} |
jf9860 | 99c6329 | 2017-03-09 15:28:42 -0500 | [diff] [blame] | 138 | Should Match Regexp ${status_string} ^(204)$ |
DR695H | ccff30b | 2017-02-17 18:44:24 -0500 | [diff] [blame] | 139 | [Return] ${resp.text} |
| 140 | |
| 141 | Get Id For Name |
| 142 | [Arguments] ${list} ${name} |
DR695H | 910097e | 2019-05-08 13:55:32 -0400 | [diff] [blame] | 143 | :FOR ${item} IN @{list} |
jf9860 | 99c6329 | 2017-03-09 15:28:42 -0500 | [diff] [blame] | 144 | \ ${id}= Get From Dictionary ${item} id |
DR695H | ccff30b | 2017-02-17 18:44:24 -0500 | [diff] [blame] | 145 | \ ${n}= Get From Dictionary ${item} name |
jf9860 | 99c6329 | 2017-03-09 15:28:42 -0500 | [diff] [blame] | 146 | \ Return from Keyword If '${n}' == '${name}' ${id} |
| 147 | [Return] None |