1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""Compute v2 API Library
A collection of wrappers for deprecated Compute v2 APIs that are not
intentionally supported by SDK. Most of these are proxy APIs.
"""
import http
from openstack import exceptions as sdk_exceptions
from osc_lib import exceptions
# security groups
def create_security_group(compute_client, name=None, description=None):
"""Create a new security group
https://docs.openstack.org/api-ref/compute/#create-security-group
:param compute_client: A compute client
:param str name: Security group name
:param str description: Security group description
:returns: A security group object
"""
data = {
'name': name,
'description': description,
}
response = compute_client.post(
'/os-security-groups', data=data, microversion='2.1'
)
sdk_exceptions.raise_from_response(response)
return response.json()['security_group']
def list_security_groups(compute_client, all_projects=None):
"""Get all security groups
https://docs.openstack.org/api-ref/compute/#list-security-groups
:param compute_client: A compute client
:param bool all_projects: If true, list from all projects
:returns: A list of security group objects
"""
url = '/os-security-groups'
if all_projects is not None:
url += f'?all_tenants={all_projects}'
response = compute_client.get(url, microversion='2.1')
sdk_exceptions.raise_from_response(response)
return response.json()['security_groups']
def find_security_group(compute_client, name_or_id):
"""Find the security group for a given name or ID
https://docs.openstack.org/api-ref/compute/#show-security-group-details
:param compute_client: A compute client
:param name_or_id: The name or ID of the security group to look up
:returns: A security group object
:raises exception.NotFound: If a matching security group could not be
found or more than one match was found
"""
response = compute_client.get(
f'/os-security-groups/{name_or_id}', microversion='2.1'
)
if response.status_code != http.HTTPStatus.NOT_FOUND:
# there might be other, non-404 errors
sdk_exceptions.raise_from_response(response)
return response.json()['security_group']
response = compute_client.get('/os-security-groups', microversion='2.1')
sdk_exceptions.raise_from_response(response)
found = None
security_groups = response.json()['security_groups']
for security_group in security_groups:
if security_group['name'] == name_or_id:
if found:
raise exceptions.NotFound(
f'multiple matches found for {name_or_id}'
)
found = security_group
if not found:
raise exceptions.NotFound(f'{name_or_id} not found')
return found
def update_security_group(
compute_client, security_group_id, name=None, description=None
):
"""Update an existing security group
https://docs.openstack.org/api-ref/compute/#update-security-group
:param compute_client: A compute client
:param str security_group_id: The ID of the security group to update
:param str name: Security group name
:param str description: Security group description
:returns: A security group object
"""
data = {}
if name:
data['name'] = name
if description:
data['description'] = description
response = compute_client.put(
f'/os-security-groups/{security_group_id}',
data=data,
microversion='2.1',
)
sdk_exceptions.raise_from_response(response)
return response.json()['security_group']
def delete_security_group(compute_client, security_group_id=None):
"""Delete a security group
https://docs.openstack.org/api-ref/compute/#delete-security-group
:param compute_client: A compute client
:param str security_group_id: Security group ID
:returns: None
"""
response = compute_client.delete(
f'/os-security-groups/{security_group_id}', microversion='2.1'
)
sdk_exceptions.raise_from_response(response)
# security group rules
def create_security_group_rule(
compute_client,
security_group_id=None,
ip_protocol=None,
from_port=None,
to_port=None,
remote_ip=None,
remote_group=None,
):
"""Create a new security group rule
https://docs.openstack.org/api-ref/compute/#create-security-group-rule
:param compute_client: A compute client
:param str security_group_id: Security group ID
:param str ip_protocol: IP protocol, 'tcp', 'udp' or 'icmp'
:param int from_port: Source port
:param int to_port: Destination port
:param str remote_ip: Source IP address in CIDR notation
:param str remote_group: Remote security group
:returns: A security group object
"""
data = {
'parent_group_id': security_group_id,
'ip_protocol': ip_protocol,
'from_port': from_port,
'to_port': to_port,
'cidr': remote_ip,
'group_id': remote_group,
}
response = compute_client.post(
'/os-security-group-rules', data=data, microversion='2.1'
)
sdk_exceptions.raise_from_response(response)
return response.json()['security_group_rule']
def delete_security_group_rule(compute_client, security_group_rule_id=None):
"""Delete a security group rule
https://docs.openstack.org/api-ref/compute/#delete-security-group-rule
:param compute_client: A compute client
:param str security_group_rule_id: Security group rule ID
:returns: None
"""
response = compute_client.delete(
f'/os-security-group-rules/{security_group_rule_id}',
microversion='2.1',
)
sdk_exceptions.raise_from_response(response)
# networks
def create_network(compute_client, name, subnet, share_subnet=None):
"""Create a new network
https://docs.openstack.org/api-ref/compute/#create-network
:param compute_client: A compute client
:param str name: Network label
:param int subnet: Subnet for IPv4 fixed addresses in CIDR notation
:param bool share_subnet: Shared subnet between projects
:returns: A network object
"""
data = {
'label': name,
'cidr': subnet,
}
if share_subnet is not None:
data['share_address'] = share_subnet
response = compute_client.post(
'/os-networks', data=data, microversion='2.1'
)
sdk_exceptions.raise_from_response(response)
return response.json()['network']
def list_networks(compute_client):
"""Get all networks
https://docs.openstack.org/api-ref/compute/#list-networks
:param compute_client: A compute client
:returns: A list of network objects
"""
response = compute_client.get('/os-networks', microversion='2.1')
sdk_exceptions.raise_from_response(response)
return response.json()['networks']
def find_network(compute_client, name_or_id):
"""Find the network for a given name or ID
https://docs.openstack.org/api-ref/compute/#show-network-details
:param compute_client: A compute client
:param name_or_id: The name or ID of the network to look up
:returns: A network object
:raises exception.NotFound: If a matching network could not be found or
more than one match was found
"""
response = compute_client.get(
f'/os-networks/{name_or_id}', microversion='2.1'
)
if response.status_code != http.HTTPStatus.NOT_FOUND:
# there might be other, non-404 errors
sdk_exceptions.raise_from_response(response)
return response.json()['network']
response = compute_client.get('/os-networks', microversion='2.1')
sdk_exceptions.raise_from_response(response)
found = None
networks = response.json()['networks']
for network in networks:
if network['label'] == name_or_id:
if found:
raise exceptions.NotFound(
f'multiple matches found for {name_or_id}'
)
found = network
if not found:
raise exceptions.NotFound(f'{name_or_id} not found')
return found
def delete_network(compute_client, network_id):
"""Delete a network
https://docs.openstack.org/api-ref/compute/#delete-network
:param compute_client: A compute client
:param string network_id: The network ID
:returns: None
"""
response = compute_client.delete(
f'/os-networks/{network_id}', microversion='2.1'
)
sdk_exceptions.raise_from_response(response)
# floating ips
def create_floating_ip(compute_client, network):
"""Create a new floating ip
https://docs.openstack.org/api-ref/compute/#create-allocate-floating-ip-address
:param network: Name of floating IP pool
:returns: A floating IP object
"""
response = compute_client.post(
'/os-floating-ips', data={'pool': network}, microversion='2.1'
)
sdk_exceptions.raise_from_response(response)
return response.json()['floating_ip']
def list_floating_ips(compute_client):
"""Get all floating IPs
https://docs.openstack.org/api-ref/compute/#list-floating-ip-addresses
:returns: A list of floating IP objects
"""
response = compute_client.get('/os-floating-ips', microversion='2.1')
sdk_exceptions.raise_from_response(response)
return response.json()['floating_ips']
def get_floating_ip(compute_client, floating_ip_id):
"""Get a floating IP
https://docs.openstack.org/api-ref/compute/#show-floating-ip-address-details
:param string floating_ip_id: The floating IP address
:returns: A floating IP object
"""
response = compute_client.get(
f'/os-floating-ips/{floating_ip_id}', microversion='2.1'
)
sdk_exceptions.raise_from_response(response)
return response.json()['floating_ip']
def delete_floating_ip(compute_client, floating_ip_id):
"""Delete a floating IP
https://docs.openstack.org/api-ref/compute/#delete-deallocate-floating-ip-address
:param string floating_ip_id: The floating IP address
:returns: None
"""
response = compute_client.delete(
f'/os-floating-ips/{floating_ip_id}', microversion='2.1'
)
sdk_exceptions.raise_from_response(response)
# floating ip pools
def list_floating_ip_pools(compute_client):
"""Get all floating IP pools
https://docs.openstack.org/api-ref/compute/#list-floating-ip-pools
:param compute_client: A compute client
:returns: A list of floating IP pool objects
"""
response = compute_client.get('/os-floating-ip-pools', microversion='2.1')
sdk_exceptions.raise_from_response(response)
return response.json()['floating_ip_pools']
|