Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cloudstack: cs_instance: fix KeyError: 'sshkeypair' #26399

Merged
merged 1 commit into from
Jul 4, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 31 additions & 18 deletions lib/ansible/modules/cloud/cloudstack/cs_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,37 +560,50 @@ def get_iptonetwork_mappings(self):
res.append({'networkid': ids[i], 'ip': data['ip']})
return res

def get_ssh_keypair(self, key=None, name=None, fail_on_missing=True):
ssh_key_name = name or self.module.params.get('ssh_key')
if ssh_key_name is None:
return

args = {
'domainid': self.get_domain('id'),
'account': self.get_account('name'),
'projectid': self.get_project('id'),
'name': ssh_key_name,
}
ssh_key_pairs = self.cs.listSSHKeyPairs(**args)
if 'errortext' in ssh_key_pairs:
self.module.fail_json(msg="Failed: '%s'" % ssh_key_pairs['errortext'])

elif 'sshkeypair' in ssh_key_pairs:
return self._get_by_key(key=key, my_dict=ssh_key_pairs['sshkeypair'][0])

elif fail_on_missing:
self.module.fail_json(msg="SSH key not found: %s" % ssh_key_name)

def ssh_key_has_changed(self):
ssh_key_name = self.module.params.get('ssh_key')
if ssh_key_name is None:
return False

# Fails if keypair for param is inexistent
param_ssh_key_fp = self.get_ssh_keypair(key='fingerprint')

# CloudStack 4.5 does return keypair on instance for a non existent key.
instance_ssh_key_name = self.instance.get('keypair')
if instance_ssh_key_name is None:
return True

if ssh_key_name == instance_ssh_key_name:
return False

args = {
'domainid': self.get_domain('id'),
'account': self.get_account('name'),
'projectid': self.get_project('id')
}

args['name'] = instance_ssh_key_name
res = self.cs.listSSHKeyPairs(**args)
instance_ssh_key = res['sshkeypair'][0]
# Get fingerprint for keypair of instance but do not fail if inexistent.
instance_ssh_key_fp = self.get_ssh_keypair(key='fingerprint', name=instance_ssh_key_name, fail_on_missing=False)
if not instance_ssh_key_fp:
return True

args['name'] = ssh_key_name
res = self.cs.listSSHKeyPairs(**args)
param_ssh_key = res['sshkeypair'][0]
if param_ssh_key['fingerprint'] != instance_ssh_key['fingerprint']:
# Compare fingerprints to ensure the keypair changed
if instance_ssh_key_fp != param_ssh_key_fp:
return True
return False


def security_groups_has_changed(self):
security_groups = self.module.params.get('security_groups')
if security_groups is None:
Expand Down Expand Up @@ -709,7 +722,7 @@ def deploy_instance(self, start_vm=True):
args['name'] = self.module.params.get('name')
args['displayname'] = self.get_or_fallback('display_name', 'name')
args['group'] = self.module.params.get('group')
args['keypair'] = self.module.params.get('ssh_key')
args['keypair'] = self.get_ssh_keypair(key='name')
args['size'] = self.module.params.get('disk_size')
args['startvm'] = start_vm
args['rootdisksize'] = self.module.params.get('root_disk_size')
Expand Down