コード例 #1
0
        public async Task TestCreateUser()
        {
            IDatabaseService provider = UserDatabaseTests.CreateProvider();
            using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TestTimeout(TimeSpan.FromSeconds(600))))
            {
                string host = null;
                UserName userName = UserDatabaseTests.CreateRandomUserName(host);
                string password = UserDatabaseTests.CreateRandomPassword();
                UserConfiguration userConfiguration = new UserConfiguration(userName, password);
                await provider.CreateUserAsync(_instance.Id, userConfiguration, cancellationTokenSource.Token);

                await provider.ListDatabaseUsersAsync(_instance.Id, null, null, cancellationTokenSource.Token);

                await provider.RemoveUserAsync(_instance.Id, userName, cancellationTokenSource.Token);
            }
        }
コード例 #2
0
        public async Task TestUserDatabaseAccess()
        {
            IDatabaseService provider = UserDatabaseTests.CreateProvider();
            using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TestTimeout(TimeSpan.FromSeconds(600))))
            {
                string host = null;
                UserName userName = UserDatabaseTests.CreateRandomUserName(host);
                string password = UserDatabaseTests.CreateRandomPassword();
                UserConfiguration userConfiguration = new UserConfiguration(userName, password);
                await provider.CreateUserAsync(_instance.Id, userConfiguration, cancellationTokenSource.Token);

                ReadOnlyCollection<DatabaseName> accessible;
                accessible = await provider.ListUserAccessAsync(_instance.Id, userName, cancellationTokenSource.Token);

                // grant twice different
                await provider.GrantUserAccessAsync(_instance.Id, _databaseName, userName, cancellationTokenSource.Token);
                await provider.GrantUserAccessAsync(_instance.Id, _databaseName2, userName, cancellationTokenSource.Token);

                accessible = await provider.ListUserAccessAsync(_instance.Id, userName, cancellationTokenSource.Token);

                // revoke twice different
                await provider.RevokeUserAccessAsync(_instance.Id, _databaseName, userName, cancellationTokenSource.Token);
                await provider.RevokeUserAccessAsync(_instance.Id, _databaseName2, userName, cancellationTokenSource.Token);

                accessible = await provider.ListUserAccessAsync(_instance.Id, userName, cancellationTokenSource.Token);

                // grant twice same
                await provider.GrantUserAccessAsync(_instance.Id, _databaseName, userName, cancellationTokenSource.Token);
                await provider.GrantUserAccessAsync(_instance.Id, _databaseName, userName, cancellationTokenSource.Token);

                accessible = await provider.ListUserAccessAsync(_instance.Id, userName, cancellationTokenSource.Token);

                // revoke twice same
                await provider.RevokeUserAccessAsync(_instance.Id, _databaseName, userName, cancellationTokenSource.Token);

                try
                {
                    try
                    {
                        await provider.RevokeUserAccessAsync(_instance.Id, _databaseName, userName, cancellationTokenSource.Token);
                        Assert.Fail("Expected a 404 response.");
                    }
                    catch (WebException webException)
                    {
                        throw new AggregateException(webException);
                    }
                }
                catch (AggregateException ex)
                {
                    ReadOnlyCollection<Exception> innerExceptions = ex.Flatten().InnerExceptions;
                    if (innerExceptions.Count != 1)
                        throw;

                    WebException webException = innerExceptions[0] as WebException;
                    if (webException == null)
                        throw;

                    HttpWebResponse response = webException.Response as HttpWebResponse;
                    if (response == null)
                        throw;

                    Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode);
                }

                accessible = await provider.ListUserAccessAsync(_instance.Id, userName, cancellationTokenSource.Token);

                await provider.RemoveUserAsync(_instance.Id, userName, cancellationTokenSource.Token);
            }
        }
コード例 #3
0
        /// <summary>
        /// Create a new user in a database instance.
        /// </summary>
        /// <param name="service">The database service instance.</param>
        /// <param name="instanceId">The database instance ID. This is obtained from <see cref="DatabaseInstance.Id">DatabaseInstance.Id</see>.</param>
        /// <param name="configuration">A <see cref="UserConfiguration"/> object describing the configuration of the new user.</param>
        /// <exception cref="ArgumentNullException">If <paramref name="service"/> is <see langword="null"/>.</exception>
        /// <exception cref="ArgumentNullException">
        /// If <paramref name="instanceId"/> is <see langword="null"/>.
        /// <para>-or-</para>
        /// <para>If <paramref name="configuration"/> is <see langword="null"/>.</para>
        /// </exception>
        /// <exception cref="WebException">If the REST request does not return successfully.</exception>
        /// <seealso href="http://docs.rackspace.com/cdb/api/v1.0/cdb-devguide/content/POST_createUser__version___accountId__instances__instanceId__users_.html">Create User (Rackspace Cloud Databases Developer Guide - API v1.0)</seealso>
        public static void CreateUser(this IDatabaseService service, DatabaseInstanceId instanceId, UserConfiguration configuration)
        {
            if (service == null)
                throw new ArgumentNullException("service");

            try
            {
                service.CreateUserAsync(instanceId, configuration, CancellationToken.None).Wait();
            }
            catch (AggregateException ex)
            {
                ReadOnlyCollection<Exception> innerExceptions = ex.Flatten().InnerExceptions;
                if (innerExceptions.Count == 1)
                    throw innerExceptions[0];

                throw;
            }
        }