1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

iam_server_certificate_facts: list all certs

If a server name isn't passed, retrieve all server certificates by
default. Change return value to a dict with the server_cert_name being
the key.
This commit is contained in:
Allen Sanabria 2016-07-14 11:21:32 -07:00 committed by Matt Clay
parent f5fd2a418f
commit 885bdf3a4e

View file

@ -94,8 +94,8 @@ except ImportError:
HAS_BOTO3 = False HAS_BOTO3 = False
def get_server_cert(iam, name=None): def get_server_certs(iam, name=None):
"""Retrieve the attributes of a server certificate if it exists """Retrieve the attributes of a server certificate if it exists or all certs.
Args: Args:
iam (botocore.client.IAM): The boto3 iam instance. iam (botocore.client.IAM): The boto3 iam instance.
@ -107,7 +107,6 @@ def get_server_cert(iam, name=None):
>>> iam = boto3.client('iam') >>> iam = boto3.client('iam')
>>> name = "server-cert-name" >>> name = "server-cert-name"
>>> results = get_server_cert(iam, name) >>> results = get_server_cert(iam, name)
[
{ {
"upload_date": "2015-04-25T00:36:40+00:00", "upload_date": "2015-04-25T00:36:40+00:00",
"server_certificate_id": "ADWAJXWTZAXIPIMQHMJPO", "server_certificate_id": "ADWAJXWTZAXIPIMQHMJPO",
@ -117,13 +116,19 @@ def get_server_cert(iam, name=None):
"path": "/", "path": "/",
"arn": "arn:aws:iam::911277865346:server-certificate/server-cert-name" "arn": "arn:aws:iam::911277865346:server-certificate/server-cert-name"
} }
]
""" """
results = [] results = dict()
try: try:
server_cert = iam.get_server_certificate(ServerCertificateName=name)['ServerCertificate'] 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'] cert_md = server_cert['ServerCertificateMetadata']
cert_data = { results[cert_md['ServerCertificateName']] = {
'certificate_body': server_cert['CertificateBody'], 'certificate_body': server_cert['CertificateBody'],
'server_certificate_id': cert_md['ServerCertificateId'], 'server_certificate_id': cert_md['ServerCertificateId'],
'server_certificate_name': cert_md['ServerCertificateName'], 'server_certificate_name': cert_md['ServerCertificateName'],
@ -132,16 +137,16 @@ def get_server_cert(iam, name=None):
'expiration': cert_md['Expiration'].isoformat(), 'expiration': cert_md['Expiration'].isoformat(),
'upload_date': cert_md['UploadDate'].isoformat(), 'upload_date': cert_md['UploadDate'].isoformat(),
} }
results.append(cert_data)
except botocore.exceptions.ClientError: except botocore.exceptions.ClientError:
pass pass
return results return results
def main(): def main():
argument_spec = ec2_argument_spec() argument_spec = ec2_argument_spec()
argument_spec.update(dict( argument_spec.update(dict(
name=dict(required=True, type='str'), name=dict(type='str'),
)) ))
module = AnsibleModule(argument_spec=argument_spec,) module = AnsibleModule(argument_spec=argument_spec,)