mirror of
https://github.com/ansible-collections/hetzner.hcloud
synced 2024-11-10 06:34:13 +00:00
refactor: use f-strings (#310)
##### SUMMARY Replace string interpolation with f-strings. Improves readability, and should help prevent #309
This commit is contained in:
parent
deee06281e
commit
c56cbab1a0
7 changed files with 43 additions and 36 deletions
3
.flake8
Normal file
3
.flake8
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
[flake8]
|
||||||
|
extend-ignore =
|
||||||
|
E501
|
|
@ -314,7 +314,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
|
||||||
# method specified in `connect_with`. Users might use `compose` to
|
# method specified in `connect_with`. Users might use `compose` to
|
||||||
# override the connection method, or implement custom logic, so we
|
# override the connection method, or implement custom logic, so we
|
||||||
# do not need to abort if nothing matched.
|
# do not need to abort if nothing matched.
|
||||||
self.display.v("[hcloud] %s" % e, server.name)
|
self.display.v(f"[hcloud] {e}", server.name)
|
||||||
|
|
||||||
return server_dict
|
return server_dict
|
||||||
|
|
||||||
|
|
|
@ -250,8 +250,9 @@ class AnsibleHCloudFloatingIP(AnsibleHCloud):
|
||||||
self._mark_as_changed()
|
self._mark_as_changed()
|
||||||
elif server != self.hcloud_floating_ip.server.name:
|
elif server != self.hcloud_floating_ip.server.name:
|
||||||
self.module.warn(
|
self.module.warn(
|
||||||
"Floating IP is already assigned to another server %s. You need to unassign the Floating IP or use force=true."
|
"Floating IP is already assigned to another server "
|
||||||
% self.hcloud_floating_ip.server.name
|
f"{self.hcloud_floating_ip.server.name}. You need to "
|
||||||
|
"unassign the Floating IP or use force=true."
|
||||||
)
|
)
|
||||||
self._mark_as_changed()
|
self._mark_as_changed()
|
||||||
elif server is not None and self.hcloud_floating_ip.server is None:
|
elif server is not None and self.hcloud_floating_ip.server is None:
|
||||||
|
@ -289,8 +290,9 @@ class AnsibleHCloudFloatingIP(AnsibleHCloud):
|
||||||
self.client.floating_ips.delete(self.hcloud_floating_ip)
|
self.client.floating_ips.delete(self.hcloud_floating_ip)
|
||||||
else:
|
else:
|
||||||
self.module.warn(
|
self.module.warn(
|
||||||
"Floating IP is currently assigned to server %s. You need to unassign the Floating IP or use force=true."
|
"Floating IP is currently assigned to server "
|
||||||
% self.hcloud_floating_ip.server.name
|
f"{self.hcloud_floating_ip.server.name}. You need to "
|
||||||
|
"unassign the Floating IP or use force=true."
|
||||||
)
|
)
|
||||||
self._mark_as_changed()
|
self._mark_as_changed()
|
||||||
self.hcloud_floating_ip = None
|
self.hcloud_floating_ip = None
|
||||||
|
@ -314,7 +316,7 @@ class AnsibleHCloudFloatingIP(AnsibleHCloud):
|
||||||
"choices": ["absent", "present"],
|
"choices": ["absent", "present"],
|
||||||
"default": "present",
|
"default": "present",
|
||||||
},
|
},
|
||||||
**super().base_module_arguments()
|
**super().base_module_arguments(),
|
||||||
),
|
),
|
||||||
required_one_of=[["id", "name"]],
|
required_one_of=[["id", "name"]],
|
||||||
mutually_exclusive=[["home_location", "server"]],
|
mutually_exclusive=[["home_location", "server"]],
|
||||||
|
|
|
@ -117,12 +117,12 @@ class AnsibleHCloudLoadBalancerNetwork(AnsibleHCloud):
|
||||||
network = self.module.params.get("network")
|
network = self.module.params.get("network")
|
||||||
self.hcloud_network = self.client.networks.get_by_name(network)
|
self.hcloud_network = self.client.networks.get_by_name(network)
|
||||||
if not self.hcloud_network:
|
if not self.hcloud_network:
|
||||||
self.module.fail_json(msg="Network does not exist: %s" % network)
|
self.module.fail_json(msg=f"Network does not exist: {network}")
|
||||||
|
|
||||||
load_balancer_name = self.module.params.get("load_balancer")
|
load_balancer_name = self.module.params.get("load_balancer")
|
||||||
self.hcloud_load_balancer = self.client.load_balancers.get_by_name(load_balancer_name)
|
self.hcloud_load_balancer = self.client.load_balancers.get_by_name(load_balancer_name)
|
||||||
if not self.hcloud_load_balancer:
|
if not self.hcloud_load_balancer:
|
||||||
self.module.fail_json(msg="Load balancer does not exist: %s" % load_balancer_name)
|
self.module.fail_json(msg=f"Load balancer does not exist: {load_balancer_name}")
|
||||||
|
|
||||||
self.hcloud_load_balancer_network = None
|
self.hcloud_load_balancer_network = None
|
||||||
except HCloudException as e:
|
except HCloudException as e:
|
||||||
|
@ -181,7 +181,7 @@ class AnsibleHCloudLoadBalancerNetwork(AnsibleHCloud):
|
||||||
"choices": ["absent", "present"],
|
"choices": ["absent", "present"],
|
||||||
"default": "present",
|
"default": "present",
|
||||||
},
|
},
|
||||||
**super().base_module_arguments()
|
**super().base_module_arguments(),
|
||||||
),
|
),
|
||||||
supports_check_mode=True,
|
supports_check_mode=True,
|
||||||
)
|
)
|
||||||
|
|
|
@ -343,7 +343,7 @@ class AnsibleHCloudLoadBalancerService(AnsibleHCloud):
|
||||||
load_balancer_name = self.module.params.get("load_balancer")
|
load_balancer_name = self.module.params.get("load_balancer")
|
||||||
self.hcloud_load_balancer = self.client.load_balancers.get_by_name(load_balancer_name)
|
self.hcloud_load_balancer = self.client.load_balancers.get_by_name(load_balancer_name)
|
||||||
if not self.hcloud_load_balancer:
|
if not self.hcloud_load_balancer:
|
||||||
self.module.fail_json(msg="Load balancer does not exist: %s" % load_balancer_name)
|
self.module.fail_json(msg=f"Load balancer does not exist: {load_balancer_name}")
|
||||||
|
|
||||||
self._get_load_balancer_service()
|
self._get_load_balancer_service()
|
||||||
except HCloudException as e:
|
except HCloudException as e:
|
||||||
|
@ -556,7 +556,7 @@ class AnsibleHCloudLoadBalancerService(AnsibleHCloud):
|
||||||
"choices": ["absent", "present"],
|
"choices": ["absent", "present"],
|
||||||
"default": "present",
|
"default": "present",
|
||||||
},
|
},
|
||||||
**super().base_module_arguments()
|
**super().base_module_arguments(),
|
||||||
),
|
),
|
||||||
supports_check_mode=True,
|
supports_check_mode=True,
|
||||||
)
|
)
|
||||||
|
|
|
@ -174,13 +174,13 @@ class AnsibleHCloudLoadBalancerTarget(AnsibleHCloud):
|
||||||
load_balancer_name = self.module.params.get("load_balancer")
|
load_balancer_name = self.module.params.get("load_balancer")
|
||||||
self.hcloud_load_balancer = self.client.load_balancers.get_by_name(load_balancer_name)
|
self.hcloud_load_balancer = self.client.load_balancers.get_by_name(load_balancer_name)
|
||||||
if not self.hcloud_load_balancer:
|
if not self.hcloud_load_balancer:
|
||||||
self.module.fail_json(msg="Load balancer does not exist: %s" % load_balancer_name)
|
self.module.fail_json(msg=f"Load balancer does not exist: {load_balancer_name}")
|
||||||
|
|
||||||
if self.module.params.get("type") == "server":
|
if self.module.params.get("type") == "server":
|
||||||
server_name = self.module.params.get("server")
|
server_name = self.module.params.get("server")
|
||||||
self.hcloud_server = self.client.servers.get_by_name(server_name)
|
self.hcloud_server = self.client.servers.get_by_name(server_name)
|
||||||
if not self.hcloud_server:
|
if not self.hcloud_server:
|
||||||
self.module.fail_json(msg="Server not found: %s" % server_name)
|
self.module.fail_json(msg=f"Server not found: {server_name}")
|
||||||
|
|
||||||
self.hcloud_load_balancer_target = None
|
self.hcloud_load_balancer_target = None
|
||||||
except HCloudException as e:
|
except HCloudException as e:
|
||||||
|
@ -290,7 +290,7 @@ class AnsibleHCloudLoadBalancerTarget(AnsibleHCloud):
|
||||||
"choices": ["absent", "present"],
|
"choices": ["absent", "present"],
|
||||||
"default": "present",
|
"default": "present",
|
||||||
},
|
},
|
||||||
**super().base_module_arguments()
|
**super().base_module_arguments(),
|
||||||
),
|
),
|
||||||
supports_check_mode=True,
|
supports_check_mode=True,
|
||||||
)
|
)
|
||||||
|
|
|
@ -502,21 +502,21 @@ class AnsibleHCloudServer(AnsibleHCloud):
|
||||||
try:
|
try:
|
||||||
image = self.client.images.get_by_id(self.module.params.get("image"))
|
image = self.client.images.get_by_id(self.module.params.get("image"))
|
||||||
except HCloudException as e:
|
except HCloudException as e:
|
||||||
self.fail_json_hcloud(e, msg="Image %s was not found" % self.module.params.get("image"))
|
self.fail_json_hcloud(e, msg=f"Image {self.module.params.get('image')} was not found")
|
||||||
if image.deprecated is not None:
|
if image.deprecated is not None:
|
||||||
available_until = image.deprecated + timedelta(days=90)
|
available_until = image.deprecated + timedelta(days=90)
|
||||||
if self.module.params.get("allow_deprecated_image"):
|
if self.module.params.get("allow_deprecated_image"):
|
||||||
self.module.warn(
|
self.module.warn(
|
||||||
"You try to use a deprecated image. The image %s will continue to be available until %s."
|
f"You try to use a deprecated image. The image {image.name} will "
|
||||||
% (image.name, available_until.strftime("%Y-%m-%d"))
|
f"continue to be available until {available_until.strftime('%Y-%m-%d')}."
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
self.module.fail_json(
|
self.module.fail_json(
|
||||||
msg=(
|
msg=(
|
||||||
"You try to use a deprecated image. The image %s will continue to be available until %s."
|
f"You try to use a deprecated image. The image {image.name} will "
|
||||||
|
f"continue to be available until {available_until.strftime('%Y-%m-%d')}. "
|
||||||
"If you want to use this image use allow_deprecated_image=true."
|
"If you want to use this image use allow_deprecated_image=true."
|
||||||
)
|
)
|
||||||
% (image.name, available_until.strftime("%Y-%m-%d"))
|
|
||||||
)
|
)
|
||||||
return image
|
return image
|
||||||
|
|
||||||
|
@ -528,7 +528,7 @@ class AnsibleHCloudServer(AnsibleHCloud):
|
||||||
except HCloudException as e:
|
except HCloudException as e:
|
||||||
self.fail_json_hcloud(
|
self.fail_json_hcloud(
|
||||||
e,
|
e,
|
||||||
msg="server_type %s was not found" % self.module.params.get("server_type"),
|
msg=f"server_type {self.module.params.get('server_type')} was not found",
|
||||||
)
|
)
|
||||||
|
|
||||||
self._check_and_warn_deprecated_server(server_type)
|
self._check_and_warn_deprecated_server(server_type)
|
||||||
|
@ -541,18 +541,20 @@ class AnsibleHCloudServer(AnsibleHCloud):
|
||||||
|
|
||||||
if server_type.deprecation.unavailable_after < datetime.now(timezone.utc):
|
if server_type.deprecation.unavailable_after < datetime.now(timezone.utc):
|
||||||
self.module.warn(
|
self.module.warn(
|
||||||
"Attention: The server plan %s is deprecated and can no longer be ordered. Existing servers of "
|
f"Attention: The server plan {server_type.name} is deprecated and can "
|
||||||
% server_type.name
|
"no longer be ordered. Existing servers of that plan will continue to "
|
||||||
+ "that plan will continue to work as before and no action is required on your part. It is possible "
|
"work as before and no action is required on your part. "
|
||||||
"to migrate this server to another server plan by setting the server_type parameter on the hetzner.hcloud.hcloud_server module."
|
"It is possible to migrate this server to another server plan by setting "
|
||||||
|
"the server_type parameter on the hetzner.hcloud.hcloud_server module."
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
|
server_type_unavailable_date = server_type.deprecation.unavailable_after.strftime("%Y-%m-%d")
|
||||||
self.module.warn(
|
self.module.warn(
|
||||||
"Attention: The server plan %s is deprecated and will no longer be available for order as of "
|
f"Attention: The server plan {server_type.name} is deprecated and will "
|
||||||
% server_type.name
|
f"no longer be available for order as of {server_type_unavailable_date}. "
|
||||||
+ "%s. Existing servers of that plan will continue to work as before "
|
"Existing servers of that plan will continue to work as before and no "
|
||||||
% server_type.deprecation.unavailable_after.strftime("%Y-%m-%d")
|
"action is required on your part. "
|
||||||
+ "and no action is required on your part. It is possible to migrate this server to another server plan by setting "
|
"It is possible to migrate this server to another server plan by setting "
|
||||||
"the server_type parameter on the hetzner.hcloud.hcloud_server module."
|
"the server_type parameter on the hetzner.hcloud.hcloud_server module."
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -567,7 +569,7 @@ class AnsibleHCloudServer(AnsibleHCloud):
|
||||||
except HCloudException as e:
|
except HCloudException as e:
|
||||||
self.fail_json_hcloud(
|
self.fail_json_hcloud(
|
||||||
e,
|
e,
|
||||||
msg="placement_group %s was not found" % self.module.params.get("placement_group"),
|
msg=f"placement_group {self.module.params.get('placement_group')} was not found",
|
||||||
)
|
)
|
||||||
|
|
||||||
return placement_group
|
return placement_group
|
||||||
|
@ -581,7 +583,7 @@ class AnsibleHCloudServer(AnsibleHCloud):
|
||||||
try:
|
try:
|
||||||
primary_ip = self.client.primary_ips.get_by_id(self.module.params.get(field))
|
primary_ip = self.client.primary_ips.get_by_id(self.module.params.get(field))
|
||||||
except HCloudException as e:
|
except HCloudException as e:
|
||||||
self.fail_json_hcloud(e, msg="primary_ip %s was not found" % self.module.params.get(field))
|
self.fail_json_hcloud(e, msg=f"primary_ip {self.module.params.get(field)} was not found")
|
||||||
|
|
||||||
return primary_ip
|
return primary_ip
|
||||||
|
|
||||||
|
@ -643,7 +645,7 @@ class AnsibleHCloudServer(AnsibleHCloud):
|
||||||
if not self.module.check_mode:
|
if not self.module.check_mode:
|
||||||
fw = self.client.firewalls.get_by_name(fname)
|
fw = self.client.firewalls.get_by_name(fname)
|
||||||
if fw is None:
|
if fw is None:
|
||||||
self.module.fail_json(msg="firewall %s was not found" % fname)
|
self.module.fail_json(msg=f"firewall {fname} was not found")
|
||||||
r = FirewallResource(type="server", server=self.hcloud_server)
|
r = FirewallResource(type="server", server=self.hcloud_server)
|
||||||
actions = self.client.firewalls.apply_to_resources(fw, [r])
|
actions = self.client.firewalls.apply_to_resources(fw, [r])
|
||||||
for a in actions:
|
for a in actions:
|
||||||
|
@ -826,8 +828,8 @@ class AnsibleHCloudServer(AnsibleHCloud):
|
||||||
return previous_server_status
|
return previous_server_status
|
||||||
else:
|
else:
|
||||||
self.module.warn(
|
self.module.warn(
|
||||||
"You can not upgrade a running instance %s. You need to stop the instance or use force=true."
|
f"You can not upgrade a running instance {self.hcloud_server.name}. "
|
||||||
% self.hcloud_server.name
|
"You need to stop the instance or use force=true."
|
||||||
)
|
)
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
@ -896,7 +898,7 @@ class AnsibleHCloudServer(AnsibleHCloud):
|
||||||
"choices": ["absent", "present", "restarted", "started", "stopped", "rebuild"],
|
"choices": ["absent", "present", "restarted", "started", "stopped", "rebuild"],
|
||||||
"default": "present",
|
"default": "present",
|
||||||
},
|
},
|
||||||
**super().base_module_arguments()
|
**super().base_module_arguments(),
|
||||||
),
|
),
|
||||||
required_one_of=[["id", "name"]],
|
required_one_of=[["id", "name"]],
|
||||||
mutually_exclusive=[["location", "datacenter"]],
|
mutually_exclusive=[["location", "datacenter"]],
|
||||||
|
|
Loading…
Reference in a new issue