mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-04-25 13:12:46 +00:00
Reformat everything.
This commit is contained in:
parent
3f2213791a
commit
340ff8586d
1008 changed files with 61301 additions and 58309 deletions
|
|
@ -227,17 +227,13 @@ from ansible.module_utils.basic import AnsibleModule
|
|||
from ansible_collections.community.general.plugins.module_utils.lxd import LXDClient, LXDClientException
|
||||
|
||||
# ANSIBLE_LXD_DEFAULT_URL is a default value of the lxd endpoint
|
||||
ANSIBLE_LXD_DEFAULT_URL = 'unix:/var/lib/lxd/unix.socket'
|
||||
ANSIBLE_LXD_DEFAULT_URL = "unix:/var/lib/lxd/unix.socket"
|
||||
|
||||
# PROFILE_STATES is a list for states supported
|
||||
PROFILES_STATES = [
|
||||
'present', 'absent'
|
||||
]
|
||||
PROFILES_STATES = ["present", "absent"]
|
||||
|
||||
# CONFIG_PARAMS is a list of config attribute names.
|
||||
CONFIG_PARAMS = [
|
||||
'config', 'description', 'devices'
|
||||
]
|
||||
CONFIG_PARAMS = ["config", "description", "devices"]
|
||||
|
||||
|
||||
class LXDProfileManagement:
|
||||
|
|
@ -248,38 +244,35 @@ class LXDProfileManagement:
|
|||
:type module: ``object``
|
||||
"""
|
||||
self.module = module
|
||||
self.name = self.module.params['name']
|
||||
self.project = self.module.params['project']
|
||||
self.name = self.module.params["name"]
|
||||
self.project = self.module.params["project"]
|
||||
self._build_config()
|
||||
self.state = self.module.params['state']
|
||||
self.new_name = self.module.params.get('new_name', None)
|
||||
self.state = self.module.params["state"]
|
||||
self.new_name = self.module.params.get("new_name", None)
|
||||
|
||||
self.key_file = self.module.params.get('client_key')
|
||||
self.key_file = self.module.params.get("client_key")
|
||||
if self.key_file is None:
|
||||
self.key_file = f"{os.environ['HOME']}/.config/lxc/client.key"
|
||||
self.cert_file = self.module.params.get('client_cert')
|
||||
self.cert_file = self.module.params.get("client_cert")
|
||||
if self.cert_file is None:
|
||||
self.cert_file = f"{os.environ['HOME']}/.config/lxc/client.crt"
|
||||
self.debug = self.module._verbosity >= 4
|
||||
|
||||
try:
|
||||
if self.module.params['url'] != ANSIBLE_LXD_DEFAULT_URL:
|
||||
self.url = self.module.params['url']
|
||||
elif os.path.exists(self.module.params['snap_url'].replace('unix:', '')):
|
||||
self.url = self.module.params['snap_url']
|
||||
if self.module.params["url"] != ANSIBLE_LXD_DEFAULT_URL:
|
||||
self.url = self.module.params["url"]
|
||||
elif os.path.exists(self.module.params["snap_url"].replace("unix:", "")):
|
||||
self.url = self.module.params["snap_url"]
|
||||
else:
|
||||
self.url = self.module.params['url']
|
||||
self.url = self.module.params["url"]
|
||||
except Exception as e:
|
||||
self.module.fail_json(msg=e.msg)
|
||||
|
||||
try:
|
||||
self.client = LXDClient(
|
||||
self.url, key_file=self.key_file, cert_file=self.cert_file,
|
||||
debug=self.debug
|
||||
)
|
||||
self.client = LXDClient(self.url, key_file=self.key_file, cert_file=self.cert_file, debug=self.debug)
|
||||
except LXDClientException as e:
|
||||
self.module.fail_json(msg=e.msg)
|
||||
self.trust_password = self.module.params.get('trust_password', None)
|
||||
self.trust_password = self.module.params.get("trust_password", None)
|
||||
self.actions = []
|
||||
|
||||
def _build_config(self):
|
||||
|
|
@ -290,69 +283,71 @@ class LXDProfileManagement:
|
|||
self.config[attr] = param_val
|
||||
|
||||
def _get_profile_json(self):
|
||||
url = f'/1.0/profiles/{self.name}'
|
||||
url = f"/1.0/profiles/{self.name}"
|
||||
if self.project:
|
||||
url = f'{url}?{urlencode(dict(project=self.project))}'
|
||||
return self.client.do('GET', url, ok_error_codes=[404])
|
||||
url = f"{url}?{urlencode(dict(project=self.project))}"
|
||||
return self.client.do("GET", url, ok_error_codes=[404])
|
||||
|
||||
@staticmethod
|
||||
def _profile_json_to_module_state(resp_json):
|
||||
if resp_json['type'] == 'error':
|
||||
return 'absent'
|
||||
return 'present'
|
||||
if resp_json["type"] == "error":
|
||||
return "absent"
|
||||
return "present"
|
||||
|
||||
def _update_profile(self):
|
||||
if self.state == 'present':
|
||||
if self.old_state == 'absent':
|
||||
if self.state == "present":
|
||||
if self.old_state == "absent":
|
||||
if self.new_name is None:
|
||||
self._create_profile()
|
||||
else:
|
||||
self.module.fail_json(
|
||||
msg='new_name must not be set when the profile does not exist and the state is present',
|
||||
changed=False)
|
||||
msg="new_name must not be set when the profile does not exist and the state is present",
|
||||
changed=False,
|
||||
)
|
||||
else:
|
||||
if self.new_name is not None and self.new_name != self.name:
|
||||
self._rename_profile()
|
||||
if self._needs_to_apply_profile_configs():
|
||||
self._apply_profile_configs()
|
||||
elif self.state == 'absent':
|
||||
if self.old_state == 'present':
|
||||
elif self.state == "absent":
|
||||
if self.old_state == "present":
|
||||
if self.new_name is None:
|
||||
self._delete_profile()
|
||||
else:
|
||||
self.module.fail_json(
|
||||
msg='new_name must not be set when the profile exists and the specified state is absent',
|
||||
changed=False)
|
||||
msg="new_name must not be set when the profile exists and the specified state is absent",
|
||||
changed=False,
|
||||
)
|
||||
|
||||
def _create_profile(self):
|
||||
url = '/1.0/profiles'
|
||||
url = "/1.0/profiles"
|
||||
if self.project:
|
||||
url = f'{url}?{urlencode(dict(project=self.project))}'
|
||||
url = f"{url}?{urlencode(dict(project=self.project))}"
|
||||
config = self.config.copy()
|
||||
config['name'] = self.name
|
||||
self.client.do('POST', url, config)
|
||||
self.actions.append('create')
|
||||
config["name"] = self.name
|
||||
self.client.do("POST", url, config)
|
||||
self.actions.append("create")
|
||||
|
||||
def _rename_profile(self):
|
||||
url = f'/1.0/profiles/{self.name}'
|
||||
url = f"/1.0/profiles/{self.name}"
|
||||
if self.project:
|
||||
url = f'{url}?{urlencode(dict(project=self.project))}'
|
||||
config = {'name': self.new_name}
|
||||
self.client.do('POST', url, config)
|
||||
self.actions.append('rename')
|
||||
url = f"{url}?{urlencode(dict(project=self.project))}"
|
||||
config = {"name": self.new_name}
|
||||
self.client.do("POST", url, config)
|
||||
self.actions.append("rename")
|
||||
self.name = self.new_name
|
||||
|
||||
def _needs_to_change_profile_config(self, key):
|
||||
if key not in self.config:
|
||||
return False
|
||||
old_configs = self.old_profile_json['metadata'].get(key, None)
|
||||
old_configs = self.old_profile_json["metadata"].get(key, None)
|
||||
return self.config[key] != old_configs
|
||||
|
||||
def _needs_to_apply_profile_configs(self):
|
||||
return (
|
||||
self._needs_to_change_profile_config('config') or
|
||||
self._needs_to_change_profile_config('description') or
|
||||
self._needs_to_change_profile_config('devices')
|
||||
self._needs_to_change_profile_config("config")
|
||||
or self._needs_to_change_profile_config("description")
|
||||
or self._needs_to_change_profile_config("devices")
|
||||
)
|
||||
|
||||
def _merge_dicts(self, source, destination):
|
||||
|
|
@ -380,7 +375,7 @@ class LXDProfileManagement:
|
|||
return destination
|
||||
|
||||
def _merge_config(self, config):
|
||||
""" merge profile
|
||||
"""merge profile
|
||||
|
||||
Merge Configuration of the present profile and the new desired configitems
|
||||
|
||||
|
|
@ -393,16 +388,16 @@ class LXDProfileManagement:
|
|||
Returns:
|
||||
dict(config): new config"""
|
||||
# merge or copy the sections from the existing profile to 'config'
|
||||
for item in ['config', 'description', 'devices', 'name', 'used_by']:
|
||||
for item in ["config", "description", "devices", "name", "used_by"]:
|
||||
if item in config:
|
||||
config[item] = self._merge_dicts(config['metadata'][item], config[item])
|
||||
config[item] = self._merge_dicts(config["metadata"][item], config[item])
|
||||
else:
|
||||
config[item] = config['metadata'][item]
|
||||
config[item] = config["metadata"][item]
|
||||
# merge or copy the sections from the ansible-task to 'config'
|
||||
return self._merge_dicts(self.config, config)
|
||||
|
||||
def _generate_new_config(self, config):
|
||||
""" rebuild profile
|
||||
"""rebuild profile
|
||||
|
||||
Rebuild the Profile by the configuration provided in the play.
|
||||
Existing configurations are discarded.
|
||||
|
|
@ -422,7 +417,7 @@ class LXDProfileManagement:
|
|||
return config
|
||||
|
||||
def _apply_profile_configs(self):
|
||||
""" Selection of the procedure: rebuild or merge
|
||||
"""Selection of the procedure: rebuild or merge
|
||||
|
||||
The standard behavior is that all information not contained
|
||||
in the play is discarded.
|
||||
|
|
@ -439,24 +434,24 @@ class LXDProfileManagement:
|
|||
Returns:
|
||||
None"""
|
||||
config = self.old_profile_json.copy()
|
||||
if self.module.params['merge_profile']:
|
||||
if self.module.params["merge_profile"]:
|
||||
config = self._merge_config(config)
|
||||
else:
|
||||
config = self._generate_new_config(config)
|
||||
|
||||
# upload config to lxd
|
||||
url = f'/1.0/profiles/{self.name}'
|
||||
url = f"/1.0/profiles/{self.name}"
|
||||
if self.project:
|
||||
url = f'{url}?{urlencode(dict(project=self.project))}'
|
||||
self.client.do('PUT', url, config)
|
||||
self.actions.append('apply_profile_configs')
|
||||
url = f"{url}?{urlencode(dict(project=self.project))}"
|
||||
self.client.do("PUT", url, config)
|
||||
self.actions.append("apply_profile_configs")
|
||||
|
||||
def _delete_profile(self):
|
||||
url = f'/1.0/profiles/{self.name}'
|
||||
url = f"/1.0/profiles/{self.name}"
|
||||
if self.project:
|
||||
url = f'{url}?{urlencode(dict(project=self.project))}'
|
||||
self.client.do('DELETE', url)
|
||||
self.actions.append('delete')
|
||||
url = f"{url}?{urlencode(dict(project=self.project))}"
|
||||
self.client.do("DELETE", url)
|
||||
self.actions.append("delete")
|
||||
|
||||
def run(self):
|
||||
"""Run the main method."""
|
||||
|
|
@ -470,23 +465,15 @@ class LXDProfileManagement:
|
|||
self._update_profile()
|
||||
|
||||
state_changed = len(self.actions) > 0
|
||||
result_json = {
|
||||
'changed': state_changed,
|
||||
'old_state': self.old_state,
|
||||
'actions': self.actions
|
||||
}
|
||||
result_json = {"changed": state_changed, "old_state": self.old_state, "actions": self.actions}
|
||||
if self.client.debug:
|
||||
result_json['logs'] = self.client.logs
|
||||
result_json["logs"] = self.client.logs
|
||||
self.module.exit_json(**result_json)
|
||||
except LXDClientException as e:
|
||||
state_changed = len(self.actions) > 0
|
||||
fail_params = {
|
||||
'msg': e.msg,
|
||||
'changed': state_changed,
|
||||
'actions': self.actions
|
||||
}
|
||||
fail_params = {"msg": e.msg, "changed": state_changed, "actions": self.actions}
|
||||
if self.client.debug:
|
||||
fail_params['logs'] = e.kwargs['logs']
|
||||
fail_params["logs"] = e.kwargs["logs"]
|
||||
self.module.fail_json(**fail_params)
|
||||
|
||||
|
||||
|
|
@ -495,50 +482,29 @@ def main():
|
|||
|
||||
module = AnsibleModule(
|
||||
argument_spec=dict(
|
||||
name=dict(
|
||||
type='str',
|
||||
required=True
|
||||
),
|
||||
name=dict(type="str", required=True),
|
||||
project=dict(
|
||||
type='str',
|
||||
type="str",
|
||||
),
|
||||
new_name=dict(
|
||||
type='str',
|
||||
type="str",
|
||||
),
|
||||
config=dict(
|
||||
type='dict',
|
||||
type="dict",
|
||||
),
|
||||
description=dict(
|
||||
type='str',
|
||||
type="str",
|
||||
),
|
||||
devices=dict(
|
||||
type='dict',
|
||||
type="dict",
|
||||
),
|
||||
merge_profile=dict(
|
||||
type='bool',
|
||||
default=False
|
||||
),
|
||||
state=dict(
|
||||
choices=PROFILES_STATES,
|
||||
default='present'
|
||||
),
|
||||
url=dict(
|
||||
type='str',
|
||||
default=ANSIBLE_LXD_DEFAULT_URL
|
||||
),
|
||||
snap_url=dict(
|
||||
type='str',
|
||||
default='unix:/var/snap/lxd/common/lxd/unix.socket'
|
||||
),
|
||||
client_key=dict(
|
||||
type='path',
|
||||
aliases=['key_file']
|
||||
),
|
||||
client_cert=dict(
|
||||
type='path',
|
||||
aliases=['cert_file']
|
||||
),
|
||||
trust_password=dict(type='str', no_log=True)
|
||||
merge_profile=dict(type="bool", default=False),
|
||||
state=dict(choices=PROFILES_STATES, default="present"),
|
||||
url=dict(type="str", default=ANSIBLE_LXD_DEFAULT_URL),
|
||||
snap_url=dict(type="str", default="unix:/var/snap/lxd/common/lxd/unix.socket"),
|
||||
client_key=dict(type="path", aliases=["key_file"]),
|
||||
client_cert=dict(type="path", aliases=["cert_file"]),
|
||||
trust_password=dict(type="str", no_log=True),
|
||||
),
|
||||
supports_check_mode=False,
|
||||
)
|
||||
|
|
@ -547,5 +513,5 @@ def main():
|
|||
lxd_manage.run()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue