diff --git a/lib/ansible/modules/extras/cloud/amazon/iam_server_certificate_facts.py b/lib/ansible/modules/extras/cloud/amazon/iam_server_certificate_facts.py index 0c334f41944..088e88a3de4 100644 --- a/lib/ansible/modules/extras/cloud/amazon/iam_server_certificate_facts.py +++ b/lib/ansible/modules/extras/cloud/amazon/iam_server_certificate_facts.py @@ -94,8 +94,8 @@ except ImportError: HAS_BOTO3 = False -def get_server_cert(iam, name=None): - """Retrieve the attributes of a server certificate if it exists +def get_server_certs(iam, name=None): + """Retrieve the attributes of a server certificate if it exists or all certs. Args: iam (botocore.client.IAM): The boto3 iam instance. @@ -107,41 +107,46 @@ def get_server_cert(iam, name=None): >>> iam = boto3.client('iam') >>> name = "server-cert-name" >>> results = get_server_cert(iam, name) - [ - { - "upload_date": "2015-04-25T00:36:40+00:00", - "server_certificate_id": "ADWAJXWTZAXIPIMQHMJPO", - "certificate_body": "-----BEGIN CERTIFICATE-----\nbunch of random data\n-----END CERTIFICATE-----", - "server_certificate_name": "server-cert-name", - "expiration": "2017-06-15T12:00:00+00:00", - "path": "/", - "arn": "arn:aws:iam::911277865346:server-certificate/server-cert-name" - } - ] - """ - results = [] - try: - server_cert = iam.get_server_certificate(ServerCertificateName=name)['ServerCertificate'] - cert_md = server_cert['ServerCertificateMetadata'] - cert_data = { - 'certificate_body': server_cert['CertificateBody'], - 'server_certificate_id': cert_md['ServerCertificateId'], - 'server_certificate_name': cert_md['ServerCertificateName'], - 'arn': cert_md['Arn'], - 'path': cert_md['Path'], - 'expiration': cert_md['Expiration'].isoformat(), - 'upload_date': cert_md['UploadDate'].isoformat(), + { + "upload_date": "2015-04-25T00:36:40+00:00", + "server_certificate_id": "ADWAJXWTZAXIPIMQHMJPO", + "certificate_body": "-----BEGIN CERTIFICATE-----\nbunch of random data\n-----END CERTIFICATE-----", + "server_certificate_name": "server-cert-name", + "expiration": "2017-06-15T12:00:00+00:00", + "path": "/", + "arn": "arn:aws:iam::911277865346:server-certificate/server-cert-name" } - results.append(cert_data) + """ + results = dict() + try: + if name: + server_certs = [iam.get_server_certificate(ServerCertificateName=name)['ServerCertificate']] + else: + server_certs = iam.list_server_certificates()['ServerCertificateMetadataList'] + + for server_cert in server_certs: + if not name: + server_cert = iam.get_server_certificate(ServerCertificateName=server_cert['ServerCertificateName'])['ServerCertificate'] + cert_md = server_cert['ServerCertificateMetadata'] + results[cert_md['ServerCertificateName']] = { + 'certificate_body': server_cert['CertificateBody'], + 'server_certificate_id': cert_md['ServerCertificateId'], + 'server_certificate_name': cert_md['ServerCertificateName'], + 'arn': cert_md['Arn'], + 'path': cert_md['Path'], + 'expiration': cert_md['Expiration'].isoformat(), + 'upload_date': cert_md['UploadDate'].isoformat(), + } except botocore.exceptions.ClientError: pass + return results def main(): argument_spec = ec2_argument_spec() argument_spec.update(dict( - name=dict(required=True, type='str'), + name=dict(type='str'), )) module = AnsibleModule(argument_spec=argument_spec,)