Skip to content

Commit 59538ef

Browse files
authored
Follow-up #134 (#138)
1 parent 271b448 commit 59538ef

File tree

2 files changed

+28
-25
lines changed

2 files changed

+28
-25
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
minor_changes:
3+
- cs_instance - Added new arguments ``user_data_name`` and ``user_data_details`` (https://github.com/ngine-io/ansible-collection-cloudstack/pull/134).

plugins/modules/cs_instance.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@
181181
- Optional data (ASCII) that can be sent to the instance upon a successful deployment.
182182
- The data will be automatically base64 encoded.
183183
- Consider switching to HTTP_POST by using I(CLOUDSTACK_METHOD=post) to increase the HTTP_GET size limit of 2KB to 32 KB.
184+
- Mutually exclusive with I(user_data_name) option.
184185
type: str
185186
user_data_details:
186187
description:
@@ -189,7 +190,8 @@
189190
version_added: 2.5.0
190191
user_data_name:
191192
description:
192-
- Name of user data to be used. This have precedence over I(user_data).
193+
- Name of user data to be used.
194+
- Mutually exclusive with I(user_data) option.
193195
type: str
194196
version_added: 2.5.0
195197
force:
@@ -603,9 +605,10 @@ def get_instance(self):
603605
return self.instance
604606

605607
def get_user_data_id_by_name(self):
606-
name = self.module.params.get('user_data_name')
607-
user_data_id = None
608-
if name:
608+
name = self.module.params.get('user_data_name')
609+
if not name:
610+
return None
611+
609612
args = {
610613
'account': self.get_account(key='name'),
611614
'domainid': self.get_domain(key='id'),
@@ -615,17 +618,13 @@ def get_user_data_id_by_name(self):
615618
# commented util will work it
616619
# 'name': name,
617620
}
618-
621+
619622
user_data_list = self.query_api('listUserData', **args)
620623
if user_data_list:
621-
for v in user_data_list.get('userdata', []):
622-
if name.lower() in [v['name'].lower()]:
623-
user_data_id = v['id']
624-
break
625-
if user_data_id is None:
626-
self.module.fail_json(msg="User data '%s' not found" % user_data_list)
627-
628-
return user_data_id
624+
for v in user_data_list.get('userdata') or []:
625+
if name in [v['name'], v['id']]:
626+
return v['id']
627+
self.module.fail_json(msg="User data '%s' not found" % user_data_list)
629628

630629
def _get_instance_user_data(self, instance):
631630
# Query the user data if we need to
@@ -831,7 +830,7 @@ def deploy_instance(self, start_vm=True):
831830
args['podid'] = self.get_pod_id()
832831

833832
template_iso = self.get_template_or_iso()
834-
if 'hypervisor' not in template_iso:
833+
if template_iso and 'hypervisor' not in template_iso:
835834
args['hypervisor'] = self.get_hypervisor()
836835

837836
instance = None
@@ -854,20 +853,20 @@ def update_instance(self, instance, start_vm=True):
854853

855854
# Instance UserData
856855
if self.module.params.get('user_data_name') is not None:
857-
args_instance_update = {
858-
'id': instance['id'],
859-
'userdataid': self.get_user_data_id_by_name()
860-
}
856+
args_instance_update = {
857+
'id': instance['id'],
858+
'userdataid': self.get_user_data_id_by_name()
859+
}
861860
else:
862-
args_instance_update = {
863-
'id': instance['id'],
864-
'userdata': self.get_user_data()
865-
}
866-
instance['userdata'] = self._get_instance_user_data(instance)
867-
861+
args_instance_update = {
862+
'id': instance['id'],
863+
'userdata': self.get_user_data()
864+
}
865+
instance['userdata'] = self._get_instance_user_data(instance)
866+
868867
if self.module.params.get('user_data_details'):
869868
args_instance_update['userdatadetails'] = self.module.params.get('user_data_details')
870-
869+
871870
args_instance_update['ostypeid'] = self.get_os_type(key='id')
872871
if self.module.params.get('group'):
873872
args_instance_update['group'] = self.module.params.get('group')
@@ -1171,6 +1170,7 @@ def main():
11711170
),
11721171
mutually_exclusive=(
11731172
['template', 'iso'],
1173+
["user_data", "user_data_name"],
11741174
),
11751175
supports_check_mode=True
11761176
)

0 commit comments

Comments
 (0)