Debian Codegraph @

ansible_collections/theforeman/foreman/plugins/modules/scc_account.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_account
25 version_added: 1.0.0
26 short_description: Manage SUSE Customer Center Accounts
27 description:
28 - Manage SUSE Customer Center Accounts
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 name:
35 description: Name of the suse customer center account
36 required: true
37 type: str
38 login:
39 description: Login id of suse customer center account
40 required: false
41 type: str
42 scc_account_password:
43 description: Password of suse customer center account
44 required: false
45 type: str
46 base_url:
47 description: URL of SUSE for suse customer center account
48 required: false
49 type: str
50 interval:
51 description: Interval for syncing suse customer center account
52 required: false
53 type: str
54 choices: ["never", "daily", "weekly", "monthly"]
55 sync_date:
56 description: Last Sync time of suse customer center account
57 required: false
58 type: str
59 organization:
60 description: Name of related organization
61 type: str
62 required: true
63 test_connection:
64 description: Test suse customer center account credentials that connects to the server
65 required: false
66 default: false
67 type: bool
68 updated_name:
69 description: Name to be updated of suse customer center account
70 type: str
71 state:
72 description: State of the suse customer center account
73 default: present
74 choices: ["present", "absent", "synced"]
75 type: str
76 extends_documentation_fragment:
77 - theforeman.foreman.foreman
78 - theforeman.foreman.foreman.organization
79 '''
80
81 EXAMPLES = '''
82 - name: "Create a suse customer center account"
83 theforeman.foreman.scc_account:
84 username: "admin"
85 password: "changeme"
86 server_url: "https://foreman.example.com"
87 name: "Test"
88 login: "abcde"
89 scc_account_password: "12345"
90 base_url: "https://scc.suse.com"
91 state: present
92
93 - name: "Update a suse customer center account"
94 theforeman.foreman.scc_account:
95 username: "admin"
96 password: "changeme"
97 server_url: "https://foreman.example.com"
98 name: "Test1"
99 state: present
100
101 - name: "Delete a suse customer center account"
102 theforeman.foreman.scc_account:
103 username: "admin"
104 password: "changeme"
105 server_url: "https://foreman.example.com"
106 name: "Test"
107 state: absent
108 '''
109
110 RETURN = '''
111 entity:
112 description: Final state of the affected entities grouped by their type.
113 returned: success
114 type: dict
115 contains:
116 scc_accounts:
117 description: List of scc accounts.
118 type: list
119 elements: dict
120 '''
121
122
123 from ansible_collections.theforeman.foreman.plugins.module_utils.foreman_helper import KatelloEntityAnsibleModule
124
125
126 class KatelloSccAccountModule(KatelloEntityAnsibleModule):
127 pass
128
129
130 def main():
131 module = KatelloSccAccountModule(
132 foreman_spec=dict(
133 name=dict(required=True),
134 updated_name=dict(),
135 login=dict(),
136 scc_account_password=dict(no_log=True, flat_name='password'),
137 base_url=dict(),
138 sync_date=dict(),
139 interval=dict(choices=['never', 'daily', 'weekly', 'monthly']),
140 ),
141 argument_spec=dict(
142 test_connection=dict(type='bool', default=False),
143 state=dict(default='present', choices=['present', 'absent', 'synced']),
144 ),
145 required_plugins=[('scc_manager', ['*'])],
146 )
147
148 module.task_timeout = 4 * 60
149
150 with module.api_connection():
151 module.foreman_spec['entity']['failsafe'] = (module.state != 'synced')
152 entity = module.lookup_entity('entity')
153
154 if not module.desired_absent:
155 if not entity:
156 if 'login' not in module.foreman_params:
157 module.fail_json(msg="scc account login not provided")
158 if 'scc_account_password' not in module.foreman_params:
159 module.fail_json(msg="Scc account password not provided")
160
161 if module.foreman_params['test_connection']:
162 scc_account_credentials = {}
163 if entity:
164 scc_account_credentials['id'] = entity['id']
165 if 'login' in module.foreman_params:
166 scc_account_credentials['login'] = module.foreman_params['login']
167 if 'scc_account_password' in module.foreman_params:
168 scc_account_credentials['password'] = module.foreman_params['scc_account_password']
169 if 'base_url' in module.foreman_params:
170 scc_account_credentials['base_url'] = module.foreman_params['base_url']
171 module.resource_action('scc_accounts', 'test_connection', scc_account_credentials, ignore_check_mode=True)
172
173 if module.state == 'synced':
174 module.resource_action('scc_accounts', 'sync', {'id': entity['id']})
175 else:
176 module.run()
177
178
179 if __name__ == '__main__':
180 main()