Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # (c) 2017 Matthias Dellweg & Bernhard Hopfenmüller (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 # pylint: disable=super-with-arguments
19
20 from __future__ import absolute_import, division, print_function
21 __metaclass__ = type
22
23
24 DOCUMENTATION = '''
25 ---
26 module: global_parameter
27 version_added: 1.0.0
28 short_description: Manage Global Parameters
29 description:
30 - "Manage Global Parameter Entities"
31 author:
32 - "Bernhard Hopfenmueller (@Fobhep) ATIX AG"
33 - "Matthias Dellweg (@mdellweg) ATIX AG"
34 - "Manisha Singhal (@manisha15) ATIX AG"
35 options:
36 name:
37 description:
38 - Name of the Global Parameter
39 required: true
40 type: str
41 updated_name:
42 description:
43 - New name of the Global Parameter. When this parameter is set, the module will not be idempotent.
44 type: str
45 value:
46 description:
47 - Value of the Global Parameter
48 required: false
49 type: raw
50 hidden_value:
51 description:
52 - Whether the value should be hidden in the GUI
53 required: false
54 type: bool
55 parameter_type:
56 description:
57 - Type of value
58 default: string
59 choices:
60 - string
61 - boolean
62 - integer
63 - real
64 - array
65 - hash
66 - yaml
67 - json
68 type: str
69 notes:
70 - The I(parameter_type) only has an effect on Foreman >= 1.22
71 extends_documentation_fragment:
72 - theforeman.foreman.foreman
73 - theforeman.foreman.foreman.entity_state_with_defaults
74 '''
75
76 EXAMPLES = '''
77 - name: "Create a Global Parameter"
78 theforeman.foreman.global_parameter:
79 username: "admin"
80 password: "changeme"
81 server_url: "https://foreman.example.com"
82 name: "TheAnswer"
83 value: "42"
84 state: present_with_defaults
85
86 - name: "Update a Global Parameter"
87 theforeman.foreman.global_parameter:
88 username: "admin"
89 password: "changeme"
90 server_url: "https://foreman.example.com"
91 name: "TheAnswer"
92 value: "43"
93 state: present
94
95 - name: "Delete a Global Parameter"
96 theforeman.foreman.global_parameter:
97 username: "admin"
98 password: "changeme"
99 server_url: "https://foreman.example.com"
100 name: "TheAnswer"
101 state: absent
102 '''
103
104 RETURN = '''
105 entity:
106 description: Final state of the affected entities grouped by their type.
107 returned: success
108 type: dict
109 contains:
110 global_parameters:
111 description: List of global parameters.
112 type: list
113 elements: dict
114 '''
115
116
117 from ansible_collections.theforeman.foreman.plugins.module_utils.foreman_helper import ForemanEntityAnsibleModule, parameter_value_to_str
118
119
120 class ForemanCommonParameterModule(ForemanEntityAnsibleModule):
121 def remove_sensitive_fields(self, entity):
122 if entity and 'hidden_value?' in entity:
123 entity['hidden_value'] = entity.pop('hidden_value?')
124 if entity['hidden_value']:
125 entity['value'] = None
126 return super(ForemanCommonParameterModule, self).remove_sensitive_fields(entity)
127
128
129 def main():
130 module = ForemanCommonParameterModule(
131 foreman_spec=dict(
132 name=dict(required=True),
133 value=dict(type='raw'),
134 hidden_value=dict(type='bool'),
135 parameter_type=dict(default='string', choices=['string', 'boolean', 'integer', 'real', 'array', 'hash', 'yaml', 'json']),
136 ),
137 argument_spec=dict(
138 state=dict(default='present', choices=['present_with_defaults', 'present', 'absent']),
139 updated_name=dict(),
140 ),
141 required_if=[
142 ['state', 'present_with_defaults', ['value']],
143 ['state', 'present', ['value']],
144 ],
145 )
146
147 with module.api_connection():
148 entity = module.lookup_entity('entity', params={'show_hidden': True})
149
150 if not module.desired_absent:
151 # Convert values according to their corresponding parameter_type
152 if entity and 'parameter_type' not in entity:
153 entity['parameter_type'] = 'string'
154 module.foreman_params['value'] = parameter_value_to_str(module.foreman_params['value'], module.foreman_params['parameter_type'])
155 if entity and 'value' in entity:
156 entity['value'] = parameter_value_to_str(entity['value'], entity.get('parameter_type', 'string'))
157 if entity and 'hidden_value?' in entity:
158 entity['hidden_value'] = entity.pop('hidden_value?')
159
160 module.run()
161
162
163 if __name__ == '__main__':
164 main()