| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
from __future__ import absolute_import, division, print_function |
| 20 |
__metaclass__ = type |
| 21 |
|
| 22 |
|
| 23 |
DOCUMENTATION = ''' |
| 24 |
--- |
| 25 |
module: compute_profile |
| 26 |
version_added: 1.0.0 |
| 27 |
short_description: Manage Compute Profiles |
| 28 |
description: |
| 29 |
- Create, update, and delete Compute Profiles |
| 30 |
author: |
| 31 |
- "Philipp Joos (@philippj)" |
| 32 |
- "Baptiste Agasse (@bagasse)" |
| 33 |
options: |
| 34 |
name: |
| 35 |
description: compute profile name |
| 36 |
required: true |
| 37 |
type: str |
| 38 |
updated_name: |
| 39 |
description: new compute profile name |
| 40 |
required: false |
| 41 |
type: str |
| 42 |
compute_attributes: |
| 43 |
description: Compute attributes related to this compute profile. Some of these attributes are specific to the underlying compute resource type |
| 44 |
required: false |
| 45 |
type: list |
| 46 |
elements: dict |
| 47 |
suboptions: |
| 48 |
compute_resource: |
| 49 |
description: |
| 50 |
- Name of the compute resource the attribute should be for |
| 51 |
type: str |
| 52 |
vm_attrs: |
| 53 |
description: |
| 54 |
- Hash containing the data of vm_attrs |
| 55 |
aliases: |
| 56 |
- vm_attributes |
| 57 |
type: dict |
| 58 |
extends_documentation_fragment: |
| 59 |
- theforeman.foreman.foreman |
| 60 |
- theforeman.foreman.foreman.entity_state |
| 61 |
''' |
| 62 |
|
| 63 |
EXAMPLES = ''' |
| 64 |
- name: compute profile |
| 65 |
theforeman.foreman.compute_profile: |
| 66 |
name: example_compute_profile |
| 67 |
server_url: "https://foreman.example.com" |
| 68 |
username: "admin" |
| 69 |
password: "changeme" |
| 70 |
state: present |
| 71 |
|
| 72 |
- name: another compute profile |
| 73 |
theforeman.foreman.compute_profile: |
| 74 |
name: another_example_compute_profile |
| 75 |
compute_attributes: |
| 76 |
- compute_resource: ovirt_compute_resource1 |
| 77 |
vm_attrs: |
| 78 |
cluster: 'a96d44a4-f14a-1015-82c6-f80354acdf01' |
| 79 |
template: 'c88af4b7-a24a-453b-9ac2-bc647ca2ef99' |
| 80 |
instance_type: 'cb8927e7-a404-40fb-a6c1-06cbfc92e077' |
| 81 |
server_url: "https://foreman.example.com" |
| 82 |
username: "admin" |
| 83 |
password: "changeme" |
| 84 |
state: present |
| 85 |
|
| 86 |
- name: compute profile2 |
| 87 |
theforeman.foreman.compute_profile: |
| 88 |
name: example_compute_profile2 |
| 89 |
compute_attributes: |
| 90 |
- compute_resource: ovirt_compute_resource01 |
| 91 |
vm_attrs: |
| 92 |
cluster: a96d44a4-f14a-1015-82c6-f80354acdf01 |
| 93 |
cores: 1 |
| 94 |
sockets: 1 |
| 95 |
memory: 1073741824 |
| 96 |
ha: 0 |
| 97 |
interfaces_attributes: |
| 98 |
0: |
| 99 |
name: "" |
| 100 |
network: 390666e1-dab3-4c99-9f96-006b2e2fd801 |
| 101 |
interface: virtio |
| 102 |
volumes_attributes: |
| 103 |
0: |
| 104 |
size_gb: 16 |
| 105 |
storage_domain: 19c50090-1ab4-4023-a63f-75ee1018ed5e |
| 106 |
preallocate: '1' |
| 107 |
wipe_after_delete: '0' |
| 108 |
interface: virtio_scsi |
| 109 |
bootable: 'true' |
| 110 |
- compute_resource: libvirt_compute_resource03 |
| 111 |
vm_attrs: |
| 112 |
cpus: 1 |
| 113 |
memory: 2147483648 |
| 114 |
nics_attributes: |
| 115 |
0: |
| 116 |
type: bridge |
| 117 |
bridge: "" |
| 118 |
model: virtio |
| 119 |
volumes_attributes: |
| 120 |
0: |
| 121 |
pool_name: default |
| 122 |
capacity: 16G |
| 123 |
allocation: 16G |
| 124 |
format_type: raw |
| 125 |
server_url: "https://foreman.example.com" |
| 126 |
username: "admin" |
| 127 |
password: "changeme" |
| 128 |
state: present |
| 129 |
|
| 130 |
- name: Remove compute profile |
| 131 |
theforeman.foreman.compute_profile: |
| 132 |
name: example_compute_profile2 |
| 133 |
server_url: "https://foreman.example.com" |
| 134 |
username: "admin" |
| 135 |
password: "changeme" |
| 136 |
state: absent |
| 137 |
''' |
| 138 |
|
| 139 |
RETURN = ''' |
| 140 |
entity: |
| 141 |
description: Final state of the affected entities grouped by their type. |
| 142 |
returned: success |
| 143 |
type: dict |
| 144 |
contains: |
| 145 |
compute_profiles: |
| 146 |
description: List of compute profiles. |
| 147 |
type: list |
| 148 |
elements: dict |
| 149 |
contains: |
| 150 |
id: |
| 151 |
description: Database id of the compute profile. |
| 152 |
type: int |
| 153 |
name: |
| 154 |
description: Name of the compute profile. |
| 155 |
type: str |
| 156 |
compute_attributes: |
| 157 |
description: Attributes for this compute profile. |
| 158 |
type: list |
| 159 |
''' |
| 160 |
|
| 161 |
from ansible_collections.theforeman.foreman.plugins.module_utils.foreman_helper import ForemanEntityAnsibleModule |
| 162 |
|
| 163 |
|
| 164 |
compute_attribute_foreman_spec = { |
| 165 |
'id': {'invisible': True}, |
| 166 |
'compute_resource': {'type': 'entity'}, |
| 167 |
'vm_attrs': {'type': 'dict', 'aliases': ['vm_attributes']}, |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
class ForemanComputeProfileModule(ForemanEntityAnsibleModule): |
| 172 |
pass |
| 173 |
|
| 174 |
|
| 175 |
def main(): |
| 176 |
module = ForemanComputeProfileModule( |
| 177 |
foreman_spec=dict( |
| 178 |
name=dict(required=True), |
| 179 |
compute_attributes=dict(type='nested_list', foreman_spec=compute_attribute_foreman_spec), |
| 180 |
), |
| 181 |
argument_spec=dict( |
| 182 |
updated_name=dict(), |
| 183 |
), |
| 184 |
) |
| 185 |
|
| 186 |
compute_attributes = module.foreman_params.pop('compute_attributes', None) |
| 187 |
|
| 188 |
with module.api_connection(): |
| 189 |
entity = module.run() |
| 190 |
|
| 191 |
|
| 192 |
if entity and module.state == 'present' and compute_attributes is not None: |
| 193 |
|
| 194 |
scope = {'compute_profile_id': entity['id']} |
| 195 |
for ca_module_params in compute_attributes: |
| 196 |
ca_module_params['compute_resource'] = module.find_resource_by_name( |
| 197 |
'compute_resources', name=ca_module_params['compute_resource'], failsafe=False, thin=False) |
| 198 |
compute_resource = ca_module_params['compute_resource'] |
| 199 |
|
| 200 |
if 'vm_attrs' in ca_module_params: |
| 201 |
if 'cluster' in ca_module_params['vm_attrs']: |
| 202 |
cluster = module.find_cluster(ca_module_params['vm_attrs']['cluster'], compute_resource) |
| 203 |
ca_module_params['vm_attrs']['cluster'] = cluster['_api_identifier'] |
| 204 |
else: |
| 205 |
cluster = None |
| 206 |
|
| 207 |
if 'volumes_attributes' in ca_module_params['vm_attrs']: |
| 208 |
for volume in ca_module_params['vm_attrs']['volumes_attributes'].values(): |
| 209 |
if 'storage_pod' in volume: |
| 210 |
storage_pod = module.find_storage_pod(volume['storage_pod'], compute_resource, cluster) |
| 211 |
volume['storage_pod'] = storage_pod['name'] |
| 212 |
if 'storage_domain' in volume: |
| 213 |
storage_domain = module.find_storage_domain(volume['storage_domain'], compute_resource, cluster) |
| 214 |
volume['storage_domain'] = storage_domain['id'] |
| 215 |
|
| 216 |
if 'interfaces_attributes' in ca_module_params['vm_attrs']: |
| 217 |
for interface in ca_module_params['vm_attrs']['interfaces_attributes'].values(): |
| 218 |
if 'network' in interface: |
| 219 |
network = module.find_network(interface['network'], compute_resource, cluster) |
| 220 |
interface['network'] = network['id'] |
| 221 |
|
| 222 |
ca_entities = ca_module_params['compute_resource'].get('compute_attributes', []) |
| 223 |
ca_entity = next((item for item in ca_entities if item.get('compute_profile_id') == entity['id']), None) |
| 224 |
module.ensure_entity('compute_attributes', ca_module_params, ca_entity, foreman_spec=compute_attribute_foreman_spec, params=scope) |
| 225 |
|
| 226 |
|
| 227 |
if __name__ == '__main__': |
| 228 |
main() |