Skip to content

Commit 271b448

Browse files
Add cs_instance user_data_detailand fix validates_certs (#134)
1 parent 6589d1e commit 271b448

File tree

1 file changed

+55
-6
lines changed

1 file changed

+55
-6
lines changed

plugins/modules/cs_instance.py

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,16 @@
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.
184184
type: str
185+
user_data_details:
186+
description:
187+
- Map used to specify the parameters values for the variables in userdata.
188+
type: dict
189+
version_added: 2.5.0
190+
user_data_name:
191+
description:
192+
- Name of user data to be used. This have precedence over I(user_data).
193+
type: str
194+
version_added: 2.5.0
185195
force:
186196
description:
187197
- Force stop/start the instance if required to apply changes, otherwise a running instance will not be changed.
@@ -592,6 +602,31 @@ def get_instance(self):
592602
break
593603
return self.instance
594604

605+
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:
609+
args = {
610+
'account': self.get_account(key='name'),
611+
'domainid': self.get_domain(key='id'),
612+
'projectid': self.get_project(key='id'),
613+
'listall': True,
614+
# name or keyword is documented but not work on cloudstack 4.19
615+
# commented util will work it
616+
# 'name': name,
617+
}
618+
619+
user_data_list = self.query_api('listUserData', **args)
620+
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
629+
595630
def _get_instance_user_data(self, instance):
596631
# Query the user data if we need to
597632
if 'userdata' in instance:
@@ -776,6 +811,8 @@ def deploy_instance(self, start_vm=True):
776811
args['networkids'] = networkids
777812
args['iptonetworklist'] = self.get_iptonetwork_mappings()
778813
args['userdata'] = self.get_user_data()
814+
args['userdataid'] = self.get_user_data_id_by_name()
815+
args['userdatadetails'] = self.module.params.get('user_data_details')
779816
args['keyboard'] = self.module.params.get('keyboard')
780817
args['ipaddress'] = self.module.params.get('ip_address')
781818
args['ip6address'] = self.module.params.get('ip6_address')
@@ -815,12 +852,22 @@ def update_instance(self, instance, start_vm=True):
815852
args_service_offering['serviceofferingid'] = self.get_service_offering_id()
816853
service_offering_changed = self.has_changed(args_service_offering, instance)
817854

818-
# Instance data
819-
args_instance_update = {
820-
'id': instance['id'],
821-
'userdata': self.get_user_data(),
822-
}
823-
instance['userdata'] = self._get_instance_user_data(instance)
855+
# Instance UserData
856+
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+
}
861+
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+
868+
if self.module.params.get('user_data_details'):
869+
args_instance_update['userdatadetails'] = self.module.params.get('user_data_details')
870+
824871
args_instance_update['ostypeid'] = self.get_os_type(key='id')
825872
if self.module.params.get('group'):
826873
args_instance_update['group'] = self.module.params.get('group')
@@ -1105,6 +1152,8 @@ def main():
11051152
account=dict(),
11061153
project=dict(),
11071154
user_data=dict(),
1155+
user_data_name=dict(),
1156+
user_data_details=dict(type='dict'),
11081157
zone=dict(required=True),
11091158
ssh_key=dict(no_log=False),
11101159
force=dict(type='bool', default=False),

0 commit comments

Comments
 (0)