1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-04-23 04:09:04 +00:00

Reformat everything.

This commit is contained in:
Felix Fontein 2025-11-01 12:08:41 +01:00
parent 3f2213791a
commit 340ff8586d
1008 changed files with 61301 additions and 58309 deletions

View file

@ -14,28 +14,28 @@ import time
class OneAndOneResources:
firewall_policy = 'firewall_policy'
load_balancer = 'load_balancer'
monitoring_policy = 'monitoring_policy'
private_network = 'private_network'
public_ip = 'public_ip'
role = 'role'
server = 'server'
user = 'user'
vpn = 'vpn'
firewall_policy = "firewall_policy"
load_balancer = "load_balancer"
monitoring_policy = "monitoring_policy"
private_network = "private_network"
public_ip = "public_ip"
role = "role"
server = "server"
user = "user"
vpn = "vpn"
def get_resource(oneandone_conn, resource_type, resource_id):
switcher = {
'firewall_policy': oneandone_conn.get_firewall,
'load_balancer': oneandone_conn.get_load_balancer,
'monitoring_policy': oneandone_conn.get_monitoring_policy,
'private_network': oneandone_conn.get_private_network,
'public_ip': oneandone_conn.get_public_ip,
'role': oneandone_conn.get_role,
'server': oneandone_conn.get_server,
'user': oneandone_conn.get_user,
'vpn': oneandone_conn.get_vpn,
"firewall_policy": oneandone_conn.get_firewall,
"load_balancer": oneandone_conn.get_load_balancer,
"monitoring_policy": oneandone_conn.get_monitoring_policy,
"private_network": oneandone_conn.get_private_network,
"public_ip": oneandone_conn.get_public_ip,
"role": oneandone_conn.get_role,
"server": oneandone_conn.get_server,
"user": oneandone_conn.get_user,
"vpn": oneandone_conn.get_vpn,
}
return switcher.get(resource_type, None)(resource_id)
@ -47,10 +47,10 @@ def get_datacenter(oneandone_conn, datacenter, full_object=False):
Returns the datacenter ID.
"""
for _datacenter in oneandone_conn.list_datacenters():
if datacenter in (_datacenter['id'], _datacenter['country_code']):
if datacenter in (_datacenter["id"], _datacenter["country_code"]):
if full_object:
return _datacenter
return _datacenter['id']
return _datacenter["id"]
def get_fixed_instance_size(oneandone_conn, fixed_instance_size, full_object=False):
@ -59,11 +59,10 @@ def get_fixed_instance_size(oneandone_conn, fixed_instance_size, full_object=Fal
Return the instance size ID.
"""
for _fixed_instance_size in oneandone_conn.fixed_server_flavors():
if fixed_instance_size in (_fixed_instance_size['id'],
_fixed_instance_size['name']):
if fixed_instance_size in (_fixed_instance_size["id"], _fixed_instance_size["name"]):
if full_object:
return _fixed_instance_size
return _fixed_instance_size['id']
return _fixed_instance_size["id"]
def get_appliance(oneandone_conn, appliance, full_object=False):
@ -71,11 +70,11 @@ def get_appliance(oneandone_conn, appliance, full_object=False):
Validates the appliance exists by ID or name.
Return the appliance ID.
"""
for _appliance in oneandone_conn.list_appliances(q='IMAGE'):
if appliance in (_appliance['id'], _appliance['name']):
for _appliance in oneandone_conn.list_appliances(q="IMAGE"):
if appliance in (_appliance["id"], _appliance["name"]):
if full_object:
return _appliance
return _appliance['id']
return _appliance["id"]
def get_private_network(oneandone_conn, private_network, full_object=False):
@ -84,11 +83,10 @@ def get_private_network(oneandone_conn, private_network, full_object=False):
Return the private network ID.
"""
for _private_network in oneandone_conn.list_private_networks():
if private_network in (_private_network['name'],
_private_network['id']):
if private_network in (_private_network["name"], _private_network["id"]):
if full_object:
return _private_network
return _private_network['id']
return _private_network["id"]
def get_monitoring_policy(oneandone_conn, monitoring_policy, full_object=False):
@ -97,11 +95,10 @@ def get_monitoring_policy(oneandone_conn, monitoring_policy, full_object=False):
Return the monitoring policy ID.
"""
for _monitoring_policy in oneandone_conn.list_monitoring_policies():
if monitoring_policy in (_monitoring_policy['name'],
_monitoring_policy['id']):
if monitoring_policy in (_monitoring_policy["name"], _monitoring_policy["id"]):
if full_object:
return _monitoring_policy
return _monitoring_policy['id']
return _monitoring_policy["id"]
def get_firewall_policy(oneandone_conn, firewall_policy, full_object=False):
@ -110,11 +107,10 @@ def get_firewall_policy(oneandone_conn, firewall_policy, full_object=False):
Return the firewall policy ID.
"""
for _firewall_policy in oneandone_conn.list_firewall_policies():
if firewall_policy in (_firewall_policy['name'],
_firewall_policy['id']):
if firewall_policy in (_firewall_policy["name"], _firewall_policy["id"]):
if full_object:
return _firewall_policy
return _firewall_policy['id']
return _firewall_policy["id"]
def get_load_balancer(oneandone_conn, load_balancer, full_object=False):
@ -123,11 +119,10 @@ def get_load_balancer(oneandone_conn, load_balancer, full_object=False):
Return the load balancer ID.
"""
for _load_balancer in oneandone_conn.list_load_balancers():
if load_balancer in (_load_balancer['name'],
_load_balancer['id']):
if load_balancer in (_load_balancer["name"], _load_balancer["id"]):
if full_object:
return _load_balancer
return _load_balancer['id']
return _load_balancer["id"]
def get_server(oneandone_conn, instance, full_object=False):
@ -136,10 +131,10 @@ def get_server(oneandone_conn, instance, full_object=False):
Returns the server if one was found.
"""
for server in oneandone_conn.list_servers(per_page=1000):
if instance in (server['id'], server['name']):
if instance in (server["id"], server["name"]):
if full_object:
return server
return server['id']
return server["id"]
def get_user(oneandone_conn, user, full_object=False):
@ -148,10 +143,10 @@ def get_user(oneandone_conn, user, full_object=False):
Returns the user if one was found.
"""
for _user in oneandone_conn.list_users(per_page=1000):
if user in (_user['id'], _user['name']):
if user in (_user["id"], _user["name"]):
if full_object:
return _user
return _user['id']
return _user["id"]
def get_role(oneandone_conn, role, full_object=False):
@ -161,10 +156,10 @@ def get_role(oneandone_conn, role, full_object=False):
Returns the role if one was found, else None.
"""
for _role in oneandone_conn.list_roles(per_page=1000):
if role in (_role['id'], _role['name']):
if role in (_role["id"], _role["name"]):
if full_object:
return _role
return _role['id']
return _role["id"]
def get_vpn(oneandone_conn, vpn, full_object=False):
@ -173,10 +168,10 @@ def get_vpn(oneandone_conn, vpn, full_object=False):
Returns the vpn if one was found.
"""
for _vpn in oneandone_conn.list_vpns(per_page=1000):
if vpn in (_vpn['id'], _vpn['name']):
if vpn in (_vpn["id"], _vpn["name"]):
if full_object:
return _vpn
return _vpn['id']
return _vpn["id"]
def get_public_ip(oneandone_conn, public_ip, full_object=False):
@ -185,17 +180,13 @@ def get_public_ip(oneandone_conn, public_ip, full_object=False):
Returns the public ip if one was found.
"""
for _public_ip in oneandone_conn.list_public_ips(per_page=1000):
if public_ip in (_public_ip['id'], _public_ip['ip']):
if public_ip in (_public_ip["id"], _public_ip["ip"]):
if full_object:
return _public_ip
return _public_ip['id']
return _public_ip["id"]
def wait_for_resource_creation_completion(oneandone_conn,
resource_type,
resource_id,
wait_timeout,
wait_interval):
def wait_for_resource_creation_completion(oneandone_conn, resource_type, resource_id, wait_timeout, wait_interval):
"""
Waits for the resource create operation to complete based on the timeout period.
"""
@ -207,33 +198,25 @@ def wait_for_resource_creation_completion(oneandone_conn,
resource = get_resource(oneandone_conn, resource_type, resource_id)
if resource_type == OneAndOneResources.server:
resource_state = resource['status']['state']
resource_state = resource["status"]["state"]
else:
resource_state = resource['state']
resource_state = resource["state"]
if ((resource_type == OneAndOneResources.server and resource_state.lower() == 'powered_on') or
(resource_type != OneAndOneResources.server and resource_state.lower() == 'active')):
if (resource_type == OneAndOneResources.server and resource_state.lower() == "powered_on") or (
resource_type != OneAndOneResources.server and resource_state.lower() == "active"
):
return
elif resource_state.lower() == 'failed':
raise Exception(f'{resource_type} creation failed for {resource_id}')
elif resource_state.lower() in ('active',
'enabled',
'deploying',
'configuring'):
elif resource_state.lower() == "failed":
raise Exception(f"{resource_type} creation failed for {resource_id}")
elif resource_state.lower() in ("active", "enabled", "deploying", "configuring"):
continue
else:
raise Exception(
f'Unknown {resource_type} state {resource_state}')
raise Exception(f"Unknown {resource_type} state {resource_state}")
raise Exception(
f'Timed out waiting for {resource_type} completion for {resource_id}')
raise Exception(f"Timed out waiting for {resource_type} completion for {resource_id}")
def wait_for_resource_deletion_completion(oneandone_conn,
resource_type,
resource_id,
wait_timeout,
wait_interval):
def wait_for_resource_deletion_completion(oneandone_conn, resource_type, resource_id, wait_timeout, wait_interval):
"""
Waits for the resource delete operation to complete based on the timeout period.
"""
@ -242,23 +225,21 @@ def wait_for_resource_deletion_completion(oneandone_conn,
time.sleep(wait_interval)
# Refresh the operation info
logs = oneandone_conn.list_logs(q='DELETE',
period='LAST_HOUR',
sort='-start_date')
logs = oneandone_conn.list_logs(q="DELETE", period="LAST_HOUR", sort="-start_date")
if resource_type == OneAndOneResources.server:
_type = 'VM'
_type = "VM"
elif resource_type == OneAndOneResources.private_network:
_type = 'PRIVATENETWORK'
_type = "PRIVATENETWORK"
else:
raise Exception(
f'Unsupported wait_for delete operation for {resource_type} resource')
raise Exception(f"Unsupported wait_for delete operation for {resource_type} resource")
for log in logs:
if (log['resource']['id'] == resource_id and
log['action'] == 'DELETE' and
log['type'] == _type and
log['status']['state'] == 'OK'):
if (
log["resource"]["id"] == resource_id
and log["action"] == "DELETE"
and log["type"] == _type
and log["status"]["state"] == "OK"
):
return
raise Exception(
f'Timed out waiting for {resource_type} deletion for {resource_id}')
raise Exception(f"Timed out waiting for {resource_type} deletion for {resource_id}")