Skip to content

Commit

Permalink
{Keyvault} Fix test failure in az keyvault certificate create (#27016)
Browse files Browse the repository at this point in the history
* Fix certificate create

* clean code

* recover original logic

* refine

* fix linter
  • Loading branch information
evelyn-ys committed Jul 27, 2023
1 parent 399b8ed commit 8ac19ca
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 120 deletions.
36 changes: 7 additions & 29 deletions src/azure-cli/azure/cli/command_modules/keyvault/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
from azure.cli.command_modules.keyvault.security_domain.shared_secret import SharedSecret
from azure.cli.command_modules.keyvault.security_domain.sp800_108 import KDF
from azure.cli.command_modules.keyvault.security_domain.utils import Utils
from azure.cli.core import telemetry
from azure.cli.core.azclierror import InvalidArgumentValueError, RequiredArgumentMissingError,\
MutuallyExclusiveArgumentError
from azure.cli.core.profiles import ResourceType, AZURE_API_PROFILES, SDKProfile
Expand Down Expand Up @@ -1548,38 +1547,17 @@ def create_certificate(client, certificate_name, policy,
disabled=False, tags=None):
logger.info("Starting long-running operation 'keyvault certificate create'")

client.begin_create_certificate(
certificate_name=certificate_name, policy=policy, enabled=not disabled, tags=tags)

poller = client.begin_create_certificate(certificate_name=certificate_name,
policy=policy,
enabled=not disabled,
tags=tags)
if policy.issuer_name.lower() == 'unknown':
# return immediately for a pending certificate
return client.get_certificate_operation(certificate_name)

# otherwise loop until the certificate creation is complete
while True:
check = client.get_certificate_operation(certificate_name)
if check.status != 'inProgress':
logger.info(
"Long-running operation 'keyvault certificate create' finished with result %s.",
check)
return check
try:
time.sleep(10)
except KeyboardInterrupt:
logger.info("Long-running operation wait cancelled.")
raise
except Exception as client_exception:
telemetry.set_exception(exception=client_exception, fault_type='cert-create-error',
summary='Unexpected client exception during cert creation')
message = getattr(client_exception, 'message', client_exception)

try:
ex_message = json.loads(client_exception.response.text) # pylint: disable=no-member
message = str(message) + ' ' + ex_message['error']['details'][0]['message']
except: # pylint: disable=bare-except
pass

raise CLIError('{}'.format(message))
# otherwise polling until the certificate creation is complete
poller.result()
return client.get_certificate_operation(certificate_name)


def _asn1_to_iso8601(asn1_date):
Expand Down
Loading

0 comments on commit 8ac19ca

Please sign in to comment.