Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # (c) 2020 Mark Hlawatschek (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: image
25 version_added: 1.0.0
26 short_description: Manage Images
27 description:
28 - Create, update, and delete Images
29 author:
30 - "Mark Hlawatschek (@hlawatschek) ATIX AG"
31 options:
32 name:
33 description: Image name
34 required: true
35 type: str
36 compute_resource:
37 description: Compute resource the image is assigned to
38 required: true
39 type: str
40 uuid:
41 aliases:
42 - image_uuid
43 description: UUID or Marketplace URN of the operatingsystem image
44 required: true
45 type: str
46 image_username:
47 description: Username that is used to login into the operating system
48 required: true
49 type: str
50 image_password:
51 description: Password that is used to login into the operating system
52 required: false
53 type: str
54 operatingsystem:
55 required: true
56 architecture:
57 description: architecture of the image
58 required: true
59 type: str
60 user_data:
61 description: Image supports user_data
62 required: false
63 type: bool
64 extends_documentation_fragment:
65 - theforeman.foreman.foreman
66 - theforeman.foreman.foreman.entity_state
67 - theforeman.foreman.foreman.operatingsystem
68 '''
69
70 EXAMPLES = '''
71 - name: create Image for EC2
72 theforeman.foreman.image:
73 name: CentOS
74 image_uuid: "ami-0ff760d16d9497662"
75 image_username: "centos"
76 operatingsystem: "CentOS 7"
77 compute_resource: "AWS"
78 architecture: "x86_64"
79 '''
80
81 RETURN = '''
82 entity:
83 description: Final state of the affected entities grouped by their type.
84 returned: success
85 type: dict
86 contains:
87 images:
88 description: List of images.
89 type: list
90 elements: dict
91 '''
92
93 from ansible_collections.theforeman.foreman.plugins.module_utils.foreman_helper import ForemanEntityAnsibleModule
94
95
96 class ForemanImageModule(ForemanEntityAnsibleModule):
97 pass
98
99
100 def main():
101 module = ForemanImageModule(
102 argument_spec=dict(
103 image_username=dict(required=True),
104 image_password=dict(no_log=True),
105 ),
106 foreman_spec=dict(
107 name=dict(required=True),
108 username=dict(invisible=True),
109 uuid=dict(required=True, aliases=['image_uuid']),
110 password=dict(invisible=True, no_log=True),
111 compute_resource=dict(type='entity', required=True),
112 architecture=dict(type='entity', required=True),
113 operatingsystem=dict(type='entity', required=True),
114 user_data=dict(type='bool')
115 ),
116 entity_opts={'scope': ['compute_resource']},
117 )
118
119 module.foreman_params['username'] = module.foreman_params.pop('image_username')
120 if 'image_password' in module.foreman_params:
121 module.foreman_params['password'] = module.foreman_params.pop('image_password')
122 with module.api_connection():
123 scope = module.scope_for('compute_resource')
124 operatingsystem_id = module.lookup_entity('operatingsystem')['id']
125 module.set_entity('entity', module.find_resource(
126 'images',
127 search='name="{0}",operatingsystem="{1}"'.format(module.foreman_params['name'], operatingsystem_id),
128 params=scope,
129 failsafe=True,
130 ))
131 module.run()
132
133
134 if __name__ == '__main__':
135 main()