Debian Codegraph @

ansible_collections/theforeman/foreman/plugins/modules/setting.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # (c) 2018 Matthias M Dellweg (ATIX AG)
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 from __future__ import absolute_import, division, print_function
19 __metaclass__ = type
20
21
22 DOCUMENTATION = '''
23 ---
24 module: setting
25 version_added: 1.0.0
26 short_description: Manage Settings
27 description:
28 - Manage Settings
29 author:
30 - "Matthias M Dellweg (@mdellweg) ATIX AG"
31 options:
32 name:
33 description:
34 - Name of the Setting
35 required: true
36 type: str
37 value:
38 description:
39 - value to set the Setting to
40 - if missing, reset to default
41 required: false
42 type: raw
43 notes:
44 - To obtain a list of possible settings for your installation, use the M(theforeman.foreman.setting_info) module
45 or the C(hammer settings list) command and look for the C(name) attribute.
46 - The web interface by default shows the C(full_name) attribute, which can't be used with this module.
47 extends_documentation_fragment:
48 - theforeman.foreman.foreman
49 '''
50
51 EXAMPLES = '''
52 - name: "Set a Setting"
53 theforeman.foreman.setting:
54 username: "admin"
55 password: "changeme"
56 server_url: "https://foreman.example.com"
57 name: "http_proxy"
58 value: "http://localhost:8088"
59
60 - name: "Reset a Setting"
61 theforeman.foreman.setting:
62 username: "admin"
63 password: "changeme"
64 server_url: "https://foreman.example.com"
65 name: "http_proxy"
66 '''
67
68 RETURN = '''
69 foreman_setting:
70 description: Created / Updated state of the setting (deprecated)
71 returned: success
72 type: dict
73 entity:
74 description: Final state of the affected entities grouped by their type.
75 returned: success
76 type: dict
77 contains:
78 settings:
79 description: List of settings.
80 type: list
81 elements: dict
82 '''
83
84
85 from ansible_collections.theforeman.foreman.plugins.module_utils.foreman_helper import ForemanStatelessEntityAnsibleModule, parameter_value_to_str
86
87
88 class ForemanSettingModule(ForemanStatelessEntityAnsibleModule):
89 pass
90
91
92 def main():
93 module = ForemanSettingModule(
94 foreman_spec=dict(
95 name=dict(required=True),
96 value=dict(type='raw'),
97 ),
98 )
99
100 with module.api_connection():
101 entity = module.lookup_entity('entity')
102
103 if 'value' not in module.foreman_params:
104 module.foreman_params['value'] = entity['default'] or ''
105
106 settings_type = entity['settings_type']
107 new_value = module.foreman_params['value']
108 # Allow to pass integers as string
109 if settings_type == 'integer':
110 new_value = int(new_value)
111 module.foreman_params['value'] = parameter_value_to_str(new_value, settings_type)
112 old_value = entity['value']
113 entity['value'] = parameter_value_to_str(old_value, settings_type)
114
115 entity = module.ensure_entity('settings', module.foreman_params, entity, state='present')
116
117 if entity:
118 # Fake the not serialized input value as output
119 entity['value'] = new_value
120
121 module.exit_json(foreman_setting=entity)
122
123
124 if __name__ == '__main__':
125 main()