public async Task <Response <KeyData> > CreateIfNotExistAsync(string subscriptionId, string resourceGroupName, string vaultName, string keyName, KeyCreateParameters parameters, CancellationToken cancellationToken = default)
        {
            Argument.AssertNotNullOrEmpty(subscriptionId, nameof(subscriptionId));
            Argument.AssertNotNullOrEmpty(resourceGroupName, nameof(resourceGroupName));
            Argument.AssertNotNullOrEmpty(vaultName, nameof(vaultName));
            Argument.AssertNotNullOrEmpty(keyName, nameof(keyName));
            Argument.AssertNotNull(parameters, nameof(parameters));

            using var message = CreateCreateIfNotExistRequest(subscriptionId, resourceGroupName, vaultName, keyName, parameters);
            await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false);

            switch (message.Response.Status)
            {
            case 200:
            {
                KeyData value = default;
                using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, cancellationToken).ConfigureAwait(false);

                value = KeyData.DeserializeKeyData(document.RootElement);
                return(Response.FromValue(value, message.Response));
            }
Esempio n. 2
0
        public virtual async Task <ArmOperation <VaultKey> > CreateOrUpdateAsync(WaitUntil waitUntil, string keyName, KeyCreateParameters parameters, CancellationToken cancellationToken = default)
        {
            Argument.AssertNotNullOrEmpty(keyName, nameof(keyName));
            Argument.AssertNotNull(parameters, nameof(parameters));

            using var scope = _vaultKeyKeysClientDiagnostics.CreateScope("VaultKeyCollection.CreateOrUpdate");
            scope.Start();
            try
            {
                var response = await _vaultKeyKeysRestClient.CreateIfNotExistAsync(Id.SubscriptionId, Id.ResourceGroupName, Id.Name, keyName, parameters, cancellationToken).ConfigureAwait(false);

                var operation = new KeyVaultArmOperation <VaultKey>(Response.FromValue(new VaultKey(Client, response), response.GetRawResponse()));
                if (waitUntil == WaitUntil.Completed)
                {
                    await operation.WaitForCompletionAsync(cancellationToken).ConfigureAwait(false);
                }
                return(operation);
            }
            catch (Exception e)
            {
                scope.Failed(e);
                throw;
            }
        }
        internal HttpMessage CreateCreateIfNotExistRequest(string subscriptionId, string resourceGroupName, string vaultName, string keyName, KeyCreateParameters parameters)
        {
            var message = _pipeline.CreateMessage();
            var request = message.Request;

            request.Method = RequestMethod.Put;
            var uri = new RawRequestUriBuilder();

            uri.Reset(_endpoint);
            uri.AppendPath("/subscriptions/", false);
            uri.AppendPath(subscriptionId, true);
            uri.AppendPath("/resourceGroups/", false);
            uri.AppendPath(resourceGroupName, true);
            uri.AppendPath("/providers/Microsoft.KeyVault/vaults/", false);
            uri.AppendPath(vaultName, true);
            uri.AppendPath("/keys/", false);
            uri.AppendPath(keyName, true);
            uri.AppendQuery("api-version", _apiVersion, true);
            request.Uri = uri;
            request.Headers.Add("Accept", "application/json");
            request.Headers.Add("Content-Type", "application/json");
            var content = new Utf8JsonRequestContent();

            content.JsonWriter.WriteObjectValue(parameters);
            request.Content = content;
            _userAgent.Apply(message);
            return(message);
        }
 /// <summary>
 /// Creates the first version of a new key if it does not exist. If it already
 /// exists, then the existing key is returned without any write operations
 /// being performed. This API does not create subsequent versions, and does not
 /// update existing keys.
 /// </summary>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='resourceGroupName'>
 /// The name of the resource group which contains the specified key vault.
 /// </param>
 /// <param name='vaultName'>
 /// The name of the key vault which contains the key to be created.
 /// </param>
 /// <param name='keyName'>
 /// The name of the key to be created.
 /// </param>
 /// <param name='parameters'>
 /// The parameters used to create the specified key.
 /// </param>
 /// <param name='cancellationToken'>
 /// The cancellation token.
 /// </param>
 public static async Task <Key> CreateIfNotExistAsync(this IKeysOperations operations, string resourceGroupName, string vaultName, string keyName, KeyCreateParameters parameters, CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var _result = await operations.CreateIfNotExistWithHttpMessagesAsync(resourceGroupName, vaultName, keyName, parameters, null, cancellationToken).ConfigureAwait(false))
     {
         return(_result.Body);
     }
 }
 /// <summary>
 /// Creates the first version of a new key if it does not exist. If it already
 /// exists, then the existing key is returned without any write operations
 /// being performed. This API does not create subsequent versions, and does not
 /// update existing keys.
 /// </summary>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='resourceGroupName'>
 /// The name of the resource group which contains the specified key vault.
 /// </param>
 /// <param name='vaultName'>
 /// The name of the key vault which contains the key to be created.
 /// </param>
 /// <param name='keyName'>
 /// The name of the key to be created.
 /// </param>
 /// <param name='parameters'>
 /// The parameters used to create the specified key.
 /// </param>
 public static Key CreateIfNotExist(this IKeysOperations operations, string resourceGroupName, string vaultName, string keyName, KeyCreateParameters parameters)
 {
     return(operations.CreateIfNotExistAsync(resourceGroupName, vaultName, keyName, parameters).GetAwaiter().GetResult());
 }
Esempio n. 6
0
        public virtual VaultKeyCreateOrUpdateOperation CreateOrUpdate(bool waitForCompletion, string keyName, KeyCreateParameters parameters, CancellationToken cancellationToken = default)
        {
            Argument.AssertNotNullOrEmpty(keyName, nameof(keyName));
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            using var scope = _vaultKeyKeysClientDiagnostics.CreateScope("VaultKeyCollection.CreateOrUpdate");
            scope.Start();
            try
            {
                var response  = _vaultKeyKeysRestClient.CreateIfNotExist(Id.SubscriptionId, Id.ResourceGroupName, Id.Name, keyName, parameters, cancellationToken);
                var operation = new VaultKeyCreateOrUpdateOperation(ArmClient, response);
                if (waitForCompletion)
                {
                    operation.WaitForCompletion(cancellationToken);
                }
                return(operation);
            }
            catch (Exception e)
            {
                scope.Failed(e);
                throw;
            }
        }
Esempio n. 7
0
 public async Task <IActionResult> CreateKey([FromRoute] string keyName, [FromBody] KeyCreateParameters createParameters)
 {
     return(Ok(default(KeyBundle)));
 }