Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
automatically run sync samples instead of within a try catch block
  • Loading branch information
iscai-msft committed Sep 12, 2019
commit 3c8072cbf99ba4f03f5ac31ce149a6c433839694
71 changes: 31 additions & 40 deletions sdk/keyvault/azure-keyvault-certificates/samples/contacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,48 +26,39 @@
# 3. Delete contacts (delete_contacts)
# ----------------------------------------------------------------------------------------------------------

def run_sample():
# Instantiate a certificate client that will be used to call the service.
# Notice that the client is using default Azure credentials.
# To make default credentials work, ensure that environment variables 'AZURE_CLIENT_ID',
# 'AZURE_CLIENT_SECRET' and 'AZURE_TENANT_ID' are set with the service principal credentials.
VAULT_URL = os.environ["VAULT_URL"]
credential = DefaultAzureCredential()
client = CertificateClient(vault_url=VAULT_URL, credential=credential)
try:
# First we create a list of Contacts that we would like to make the certificate contacts for this key vault.
contact_list = [
Contact(email='[email protected]',
name='John Doe',
phone='1111111111'),
Contact(email='[email protected]',
name='John Doe2',
phone='2222222222')
]
# Instantiate a certificate client that will be used to call the service.
# Notice that the client is using default Azure credentials.
# To make default credentials work, ensure that environment variables 'AZURE_CLIENT_ID',
# 'AZURE_CLIENT_SECRET' and 'AZURE_TENANT_ID' are set with the service principal credentials.
VAULT_URL = os.environ["VAULT_URL"]
credential = DefaultAzureCredential()
client = CertificateClient(vault_url=VAULT_URL, credential=credential)
try:
# First we create a list of Contacts that we would like to make the certificate contacts for this key vault.
contact_list = [
Contact(email='[email protected]',
name='John Doe',
phone='1111111111'),
Contact(email='[email protected]',
name='John Doe2',
phone='2222222222')
]

# Creates and sets the certificate contacts for this key vault.
client.create_contacts(contacts=contact_list)
# Creates and sets the certificate contacts for this key vault.
client.create_contacts(contacts=contact_list)

# Gets the certificate contacts for this key vault.
contacts = client.get_contacts()
for contact in contacts:
print(contact.name)
print(contact.email)
print(contact.phone)
# Gets the certificate contacts for this key vault.
contacts = client.get_contacts()
for contact in contacts:
print(contact.name)
print(contact.email)
print(contact.phone)

# Deletes all of the certificate contacts for this key vault.
client.delete_contacts()
# Deletes all of the certificate contacts for this key vault.
client.delete_contacts()

except HttpResponseError as e:
print("\nrun_sample has caught an error. {0}".format(e.message))
except HttpResponseError as e:
print("\nrun_sample has caught an error. {0}".format(e.message))

finally:
print("\nrun_sample done")


if __name__ == "__main__":
try:
run_sample()

except Exception as e:
print("Top level Error: {0}".format(str(e)))
finally:
print("\nrun_sample done")
121 changes: 56 additions & 65 deletions sdk/keyvault/azure-keyvault-certificates/samples/hello_world.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,75 +29,66 @@
#
# ----------------------------------------------------------------------------------------------------------

def run_sample():
# Instantiate a certificate client that will be used to call the service.
# Notice that the client is using default Azure credentials.
# To make default credentials work, ensure that environment variables 'AZURE_CLIENT_ID',
# 'AZURE_CLIENT_SECRET' and 'AZURE_TENANT_ID' are set with the service principal credentials.
VAULT_URL = os.environ["VAULT_URL"]
credential = DefaultAzureCredential()
client = CertificateClient(vault_url=VAULT_URL, credential=credential)
try:
# Let's create a certificate for holding bank account credentials valid for 1 year.
# if the certificate already exists in the Key Vault, then a new version of the certificate is created.
print("\n.. Create Certificate")
# Instantiate a certificate client that will be used to call the service.
# Notice that the client is using default Azure credentials.
# To make default credentials work, ensure that environment variables 'AZURE_CLIENT_ID',
# 'AZURE_CLIENT_SECRET' and 'AZURE_TENANT_ID' are set with the service principal credentials.
VAULT_URL = os.environ["VAULT_URL"]
credential = DefaultAzureCredential()
client = CertificateClient(vault_url=VAULT_URL, credential=credential)
try:
# Let's create a certificate for holding bank account credentials valid for 1 year.
# if the certificate already exists in the Key Vault, then a new version of the certificate is created.
print("\n.. Create Certificate")

# Before creating your certificate, let's create the management policy for your certificate.
# Here you specify the properties of the key, secret, and issuer backing your certificate,
# the X509 component of your certificate, and any lifetime actions you would like to be taken
# on your certificate
# Before creating your certificate, let's create the management policy for your certificate.
# Here you specify the properties of the key, secret, and issuer backing your certificate,
# the X509 component of your certificate, and any lifetime actions you would like to be taken
# on your certificate

# Alternatively, if you would like to use our default policy, don't pass a policy parameter to
# our certificate creation method
cert_policy = CertificatePolicy(key_properties=KeyProperties(exportable=True,
key_type='RSA',
key_size=2048,
reuse_key=False),
content_type=SecretContentType.PKCS12,
issuer_name='Self',
subject_name='CN=*.microsoft.com',
validity_in_months=24,
san_dns_names=['sdk.azure-int.net']
)
cert_name = "HelloWorldCertificate"
create_certificate_poller = client.create_certificate(name=cert_name, policy=cert_policy)
create_certificate_poller.wait()
print("Certificate with name '{0}' created".format(cert_name))
# Alternatively, if you would like to use our default policy, don't pass a policy parameter to
# our certificate creation method
cert_policy = CertificatePolicy(key_properties=KeyProperties(exportable=True,
key_type='RSA',
key_size=2048,
reuse_key=False),
content_type=SecretContentType.PKCS12,
issuer_name='Self',
subject_name='CN=*.microsoft.com',
validity_in_months=24,
san_dns_names=['sdk.azure-int.net']
)
cert_name = "HelloWorldCertificate"
create_certificate_poller = client.create_certificate(name=cert_name, policy=cert_policy)
create_certificate_poller.wait()
print("Certificate with name '{0}' created".format(cert_name))

# Let's get the bank certificate using its name
print("\n.. Get a Certificate by name")
bank_certificate = client.get_certificate_with_policy(name=cert_name)
print("Certificate with name '{0}' was found'.".format(bank_certificate.name))
# Let's get the bank certificate using its name
print("\n.. Get a Certificate by name")
bank_certificate = client.get_certificate_with_policy(name=cert_name)
print("Certificate with name '{0}' was found'.".format(bank_certificate.name))

# After one year, the bank account is still active, and we have decided to update the tags.
print("\n.. Update a Certificate by name")
tags = {"a": "b"}
updated_certificate = client.update_certificate(name=bank_certificate.name, tags=tags)
print("Certificate with name '{0}' was updated on date '{1}'".format(
bank_certificate.name,
updated_certificate.updated)
)
print("Certificate with name '{0}' was updated with tags '{1}'".format(
bank_certificate.name,
updated_certificate.tags)
)
# After one year, the bank account is still active, and we have decided to update the tags.
print("\n.. Update a Certificate by name")
tags = {"a": "b"}
updated_certificate = client.update_certificate(name=bank_certificate.name, tags=tags)
print("Certificate with name '{0}' was updated on date '{1}'".format(
bank_certificate.name,
updated_certificate.updated)
)
print("Certificate with name '{0}' was updated with tags '{1}'".format(
bank_certificate.name,
updated_certificate.tags)
)

# The bank account was closed, need to delete its credentials from the Key Vault.
print("\n.. Delete Certificate")
deleted_certificate = client.delete_certificate(name=bank_certificate.name)
print("Deleting Certificate..")
print("Certificate with name '{0}' was deleted.".format(deleted_certificate.name))
# The bank account was closed, need to delete its credentials from the Key Vault.
print("\n.. Delete Certificate")
deleted_certificate = client.delete_certificate(name=bank_certificate.name)
print("Deleting Certificate..")
print("Certificate with name '{0}' was deleted.".format(deleted_certificate.name))

except HttpResponseError as e:
print("\nrun_sample has caught an error. {0}".format(e.message))
except HttpResponseError as e:
print("\nrun_sample has caught an error. {0}".format(e.message))

finally:
print("\nrun_sample done")


if __name__ == "__main__":
try:
run_sample()

except Exception as e:
print("Top level Error: {0}".format(str(e)))
finally:
print("\nrun_sample done")
112 changes: 52 additions & 60 deletions sdk/keyvault/azure-keyvault-certificates/samples/issuers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,74 +30,66 @@
# 5. Delete an issuer (delete_issuer)
# ----------------------------------------------------------------------------------------------------------

def run_sample():
# Instantiate a certificate client that will be used to call the service.
# Notice that the client is using default Azure credentials.
# To make default credentials work, ensure that environment variables 'AZURE_CLIENT_ID',
# 'AZURE_CLIENT_SECRET' and 'AZURE_TENANT_ID' are set with the service principal credentials.
VAULT_URL = os.environ["VAULT_URL"]
credential = DefaultAzureCredential()
client = CertificateClient(vault_url=VAULT_URL, credential=credential)
try:
# First we specify the AdministratorDetails for our issuers.
admin_details = [AdministratorDetails(
first_name="John",
last_name="Doe",
email="[email protected]",
phone="4255555555"
)]
# Instantiate a certificate client that will be used to call the service.
# Notice that the client is using default Azure credentials.
# To make default credentials work, ensure that environment variables 'AZURE_CLIENT_ID',
# 'AZURE_CLIENT_SECRET' and 'AZURE_TENANT_ID' are set with the service principal credentials.
VAULT_URL = os.environ["VAULT_URL"]
credential = DefaultAzureCredential()
client = CertificateClient(vault_url=VAULT_URL, credential=credential)
try:
# First we specify the AdministratorDetails for our issuers.
admin_details = [AdministratorDetails(
first_name="John",
last_name="Doe",
email="[email protected]",
phone="4255555555"
)]

# Next we create an issuer with these administrator details
# The name field refers to the name you would like to get the issuer. There are also pre-set names, such as 'Self' and 'Unknown'
# The provider for your issuer must exist for your vault location and tenant id.
client.create_issuer(
name="issuer1",
provider="Test",
account_id="keyvaultuser",
admin_details=admin_details,
enabled=True
)
# Next we create an issuer with these administrator details
# The name field refers to the name you would like to get the issuer. There are also pre-set names, such as 'Self' and 'Unknown'
# The provider for your issuer must exist for your vault location and tenant id.
client.create_issuer(
name="issuer1",
provider="Test",
account_id="keyvaultuser",
admin_details=admin_details,
enabled=True
)

# Now we get this issuer by name
issuer1 = client.get_issuer(name="issuer1")
# Now we get this issuer by name
issuer1 = client.get_issuer(name="issuer1")

print(issuer1.name)
print(issuer1.provider)
print(issuer1.account_id)
print(issuer1.name)
print(issuer1.provider)
print(issuer1.account_id)

for admin_detail in issuer1.admin_details:
print(admin_detail.first_name)
print(admin_detail.last_name)
print(admin_detail.email)
print(admin_detail.phone)
for admin_detail in issuer1.admin_details:
print(admin_detail.first_name)
print(admin_detail.last_name)
print(admin_detail.email)
print(admin_detail.phone)

# Now we will list all of the certificate issuers for this key vault. To better demonstrate this, we will first create another issuer.
client.create_issuer(
name="issuer2",
provider="Test",
account_id="keyvaultuser",
enabled=True
)
# Now we will list all of the certificate issuers for this key vault. To better demonstrate this, we will first create another issuer.
client.create_issuer(
name="issuer2",
provider="Test",
account_id="keyvaultuser",
enabled=True
)

issuers = client.list_issuers()
issuers = client.list_issuers()

for issuer in issuers:
print(issuer.name)
print(issuer.provider)
for issuer in issuers:
print(issuer.name)
print(issuer.provider)

# Finally, we delete our first issuer by name.
client.delete_issuer(name="issuer1")
# Finally, we delete our first issuer by name.
client.delete_issuer(name="issuer1")

except HttpResponseError as e:
print("\nrun_sample has caught an error. {0}".format(e.message))
except HttpResponseError as e:
print("\nrun_sample has caught an error. {0}".format(e.message))

finally:
print("\nrun_sample done")
finally:
print("\nrun_sample done")


if __name__ == "__main__":
try:
run_sample()

except Exception as e:
print("Top level Error: {0}".format(str(e)))
Loading