Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # (c) 2019 Manisha Singhal (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: scc_product
25 version_added: 1.0.0
26 short_description: Subscribe SUSE Customer Center Account Products
27 description:
28 - Manage SUSE Customer Center Products
29 - This module requires the foreman_scc_manager plugin set up in the server
30 - See U(https://github.com/ATIX-AG/foreman_scc_manager)
31 author:
32 - "Manisha Singhal (@manisha15) ATIX AG"
33 options:
34 scc_product:
35 description:
36 - Full name of the product of suse customer center account.
37 - The I(friendly_name) alias is deprecated as it refers to an attribute that does not
38 uniquely identify a product and not used for product lookups since SCC Manager 1.8.6.
39 required: true
40 type: str
41 aliases:
42 - friendly_name
43 scc_account:
44 description: Name of the suse customer center account associated with product
45 required: true
46 type: str
47 extends_documentation_fragment:
48 - theforeman.foreman.foreman
49 - theforeman.foreman.foreman.organization
50 '''
51
52 EXAMPLES = '''
53 - name: "Subscribe to suse customer center product"
54 theforeman.foreman.scc_product:
55 scc_product: "Product1"
56 scc_account: "Test"
57 organization: "Test Organization"
58 '''
59
60 RETURN = ''' # '''
61
62
63 from ansible_collections.theforeman.foreman.plugins.module_utils.foreman_helper import KatelloAnsibleModule
64 from ansible_collections.theforeman.foreman.plugins.module_utils._version import LooseVersion
65
66
67 def main():
68 module = KatelloAnsibleModule(
69 foreman_spec=dict(
70 scc_product=dict(required=True, type='entity', aliases=['friendly_name'], scope=['scc_account'], thin=False),
71 scc_account=dict(required=True, type='entity', scope=['organization']),
72 ),
73 required_plugins=[('scc_manager', ['*'])],
74 )
75
76 module.task_timeout = 4 * 60
77
78 with module.api_connection():
79 scc_version = '1.0.0' # fallback
80 try:
81 statuses = module.foremanapi.resource('ping').call('statuses')
82 plugins = statuses['results']['foreman']['plugins']
83 for plugin in plugins:
84 if isinstance(plugin, dict):
85 if plugin['name'] == 'foreman_scc_manager':
86 scc_version = plugin['version']
87 else:
88 if 'foreman_scc_manager' in plugin:
89 scc_version = plugin.split(',')[1]
90 except Exception:
91 pass
92
93 if LooseVersion(scc_version.strip()) < LooseVersion('1.8.6'):
94 scc_search_by = 'friendly_name'
95 else:
96 scc_search_by = 'name'
97
98 module.foreman_spec['scc_product']['search_by'] = scc_search_by
99 scc_product = module.lookup_entity('scc_product')
100
101 if not scc_product.get('product_id'):
102 payload = {'id': scc_product['id']}
103 payload.update(module.scope_for('scc_account'))
104 module.resource_action('scc_products', 'subscribe', payload)
105
106
107 if __name__ == '__main__':
108 main()