Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # (c) 2018, Sean O'Keeffe <[email protected]>
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: resource_info
25 version_added: 1.0.0
26 short_description: Gather information about resources
27 description:
28 - Gather information about resources
29 author:
30 - "Sean O'Keeffe (@sean797)"
31 options:
32 resource:
33 description:
34 - Resource to search
35 - Set to an invalid choice like I(foo) see all available options.
36 required: true
37 type: str
38 search:
39 description:
40 - Search query to use
41 - If None, all resources are returned
42 type: str
43 params:
44 description:
45 - Add parameters to the API call if necessary
46 - If not specified, no additional parameters are passed
47 type: dict
48 organization:
49 description:
50 - Scope the searched resource by organization
51 type: str
52 full_details:
53 description:
54 - If C(True) all details about the found resources are returned
55 type: bool
56 default: false
57 aliases: [ info ]
58 notes:
59 - Some resources don't support scoping and will return errors when you pass I(organization) or unknown data in I(params).
60 extends_documentation_fragment:
61 - theforeman.foreman.foreman
62 '''
63
64 EXAMPLES = '''
65 - name: "Read a Setting"
66 theforeman.foreman.resource_info:
67 username: "admin"
68 password: "changeme"
69 server_url: "https://foreman.example.com"
70 resource: settings
71 search: name = foreman_url
72 register: result
73 - debug:
74 var: result.resources[0].value
75
76 - name: "Read all Registries"
77 theforeman.foreman.resource_info:
78 username: "admin"
79 password: "changeme"
80 server_url: "https://foreman.example.com"
81 resource: registries
82 register: result
83 - debug:
84 var: item.name
85 with_items: "{{ result.resources }}"
86
87 - name: "Read all Organizations with full details"
88 theforeman.foreman.resource_info:
89 username: "admin"
90 password: "changeme"
91 server_url: "https://foreman.example.com"
92 resource: organizations
93 full_details: true
94 register: result
95 - debug:
96 var: result.resources
97
98 - name: Get all existing subscriptions for organization with id 1
99 theforeman.foreman.resource_info:
100 username: "admin"
101 password: "changeme"
102 server_url: "https://foreman.example.com"
103 resource: subscriptions
104 params:
105 organization_id: 1
106 register: result
107 - debug:
108 var: result
109
110 - name: Get all existing activation keys for organization ACME
111 theforeman.foreman.resource_info:
112 username: "admin"
113 password: "changeme"
114 server_url: "https://foreman.example.com"
115 resource: activation_keys
116 organization: ACME
117 register: result
118 - debug:
119 var: result
120 '''
121
122 RETURN = '''
123 resources:
124 description: Resource information
125 returned: always
126 type: list
127 '''
128
129 from ansible_collections.theforeman.foreman.plugins.module_utils.foreman_helper import ForemanAnsibleModule
130
131
132 def main():
133
134 module = ForemanAnsibleModule(
135 foreman_spec=dict(
136 resource=dict(type='str', required=True),
137 search=dict(),
138 full_details=dict(type='bool', aliases=['info'], default='false'),
139 params=dict(type='dict'),
140 organization=dict(),
141 ),
142 )
143
144 module_params = module.foreman_params
145 resource = module_params['resource']
146 search = module_params.get('search')
147 params = module_params.get('params', {})
148
149 with module.api_connection():
150 if resource not in module.foremanapi.resources:
151 existing_resources = [
152 resource for resource in module.foremanapi.resources
153 if module.foremanapi.resource(resource).has_action('index')
154 and module.foremanapi.resource(resource).has_action('show')
155 ]
156 msg = "Resource '{0}' does not exist in the API. Existing resources: {1}".format(resource, ', '.join(sorted(existing_resources)))
157 module.fail_json(msg=msg)
158 if 'organization' in module_params:
159 params['organization_id'] = module.find_resource_by_name('organizations', module_params['organization'], thin=True)['id']
160
161 if 'id' not in params:
162 response = module.list_resource(resource, search, params)
163
164 if module_params['full_details']:
165 resources = []
166 for found_resource in response:
167 resources.append(module.show_resource(resource, found_resource['id'], params))
168 else:
169 resources = response
170 else:
171 res_id = params.pop('id')
172 resources = [module.show_resource(resource, res_id, params)]
173
174 module.exit_json(resources=resources)
175
176
177 if __name__ == '__main__':
178 main()