Example #1
0
 private void VerifyCreateUpdateRoleDefinition(SqlRoleDefinitionCreateUpdateParameters sqlRoleDefinitionCreateUpdateParameters, SqlRoleDefinitionGetResults sqlRoleDefinitionGetResults)
 {
     Assert.Equal(sqlRoleDefinitionCreateUpdateParameters.RoleName, sqlRoleDefinitionGetResults.RoleName);
     Assert.Equal(sqlRoleDefinitionCreateUpdateParameters.AssignableScopes.Count, sqlRoleDefinitionGetResults.AssignableScopes.Count);
     Assert.Equal(sqlRoleDefinitionCreateUpdateParameters.Permissions.Count, sqlRoleDefinitionGetResults.Permissions.Count);
     for (int i = 0; i < sqlRoleDefinitionCreateUpdateParameters.Permissions.Count; i++)
     {
         Assert.Equal(sqlRoleDefinitionCreateUpdateParameters.Permissions[i].DataActions.Count, sqlRoleDefinitionGetResults.Permissions[i].DataActions.Count);
     }
 }
Example #2
0
        public void SqlRoleTests()
        {
            using (var context = MockContext.Start(this.GetType()))
            {
                fixture.Init(context);
                var databaseAccountName = this.fixture.GetDatabaseAccountName(TestFixture.AccountType.PitrSql);
                var client = this.fixture.CosmosDBManagementClient.SqlResources;

                var databaseName = TestUtilities.GenerateName("database");
                SqlDatabaseCreateUpdateParameters sqlDatabaseCreateUpdateParameters2 = new SqlDatabaseCreateUpdateParameters
                {
                    Location = this.fixture.Location,
                    Resource = new SqlDatabaseResource {
                        Id = databaseName
                    },
                };
                client.CreateUpdateSqlDatabaseWithHttpMessagesAsync(
                    this.fixture.ResourceGroupName,
                    databaseAccountName,
                    databaseName,
                    sqlDatabaseCreateUpdateParameters2
                    ).GetAwaiter().GetResult();

                SqlRoleDefinitionCreateUpdateParameters sqlRoleDefinitionCreateUpdateParameters = new SqlRoleDefinitionCreateUpdateParameters
                {
                    RoleName         = "roleName",
                    Type             = RoleDefinitionType.CustomRole,
                    AssignableScopes = new List <string>
                    {
                        string.Format(
                            "/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.DocumentDB/databaseAccounts/{2}",
                            this.fixture.CosmosDBManagementClient.SubscriptionId,
                            this.fixture.ResourceGroupName,
                            databaseAccountName),
                    },
                    Permissions = new List <Permission>
                    {
                        new Permission
                        {
                            DataActions = new List <string>
                            {
                                "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/items/create",
                                "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/items/read"
                            }
                        }
                    }
                };

                SqlRoleDefinitionGetResults sqlRoleDefinitionGetResults = client.CreateUpdateSqlRoleDefinitionWithHttpMessagesAsync(roleDefinitionId, this.fixture.ResourceGroupName, databaseAccountName, sqlRoleDefinitionCreateUpdateParameters).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlRoleDefinitionGetResults);
                Assert.Equal(roleDefinitionId, sqlRoleDefinitionGetResults.Name);
                VerifyCreateUpdateRoleDefinition(sqlRoleDefinitionCreateUpdateParameters, sqlRoleDefinitionGetResults);

                SqlRoleDefinitionGetResults sqlRoleDefinitionGetResults2 = client.GetSqlRoleDefinitionWithHttpMessagesAsync(roleDefinitionId, this.fixture.ResourceGroupName, databaseAccountName).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlRoleDefinitionGetResults2);
                Assert.Equal(roleDefinitionId, sqlRoleDefinitionGetResults2.Name);

                VerifyEqualSqlRoleDefinitions(sqlRoleDefinitionGetResults, sqlRoleDefinitionGetResults2);

                SqlRoleDefinitionCreateUpdateParameters sqlRoleDefinitionCreateUpdateParameters2 = new SqlRoleDefinitionCreateUpdateParameters
                {
                    RoleName         = "roleName2",
                    Type             = RoleDefinitionType.CustomRole,
                    AssignableScopes = new List <string>
                    {
                        string.Format(
                            "/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.DocumentDB/databaseAccounts/{2}",
                            this.fixture.CosmosDBManagementClient.SubscriptionId,
                            this.fixture.ResourceGroupName,
                            databaseAccountName
                            )
                    },
                    Permissions = new List <Permission>
                    {
                        new Permission
                        {
                            DataActions = new List <string>
                            {
                                "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/items/create",
                                "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/items/read",
                                "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/items/delete",
                                "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/items/replace"
                            }
                        }
                    }
                };

                SqlRoleDefinitionGetResults sqlRoleDefinitionGetResults3 = client.CreateUpdateSqlRoleDefinitionWithHttpMessagesAsync(
                    roleDefinitionId2,
                    this.fixture.ResourceGroupName,
                    databaseAccountName,
                    sqlRoleDefinitionCreateUpdateParameters2
                    ).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlRoleDefinitionGetResults3);
                Assert.Equal(roleDefinitionId2, sqlRoleDefinitionGetResults3.Name);
                VerifyCreateUpdateRoleDefinition(sqlRoleDefinitionCreateUpdateParameters2, sqlRoleDefinitionGetResults3);

                SqlRoleDefinitionCreateUpdateParameters sqlRoleDefinitionCreateUpdateParameters3 = new SqlRoleDefinitionCreateUpdateParameters
                {
                    RoleName         = "roleName3",
                    Type             = RoleDefinitionType.CustomRole,
                    AssignableScopes = new List <string>
                    {
                        string.Format(
                            "/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.DocumentDB/databaseAccounts/{2}",
                            this.fixture.CosmosDBManagementClient.SubscriptionId,
                            this.fixture.ResourceGroupName,
                            databaseAccountName
                            )
                    },
                    Permissions = new List <Permission>
                    {
                        new Permission
                        {
                            DataActions = new List <string>
                            {
                                "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/items/create",
                                "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/items/delete",
                                "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/items/replace"
                            }
                        }
                    }
                };

                SqlRoleDefinitionGetResults sqlRoleDefinitionGetResults4 = client.CreateUpdateSqlRoleDefinitionWithHttpMessagesAsync(roleDefinitionId, this.fixture.ResourceGroupName, databaseAccountName, sqlRoleDefinitionCreateUpdateParameters3).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlRoleDefinitionGetResults4);
                Assert.Equal(roleDefinitionId, sqlRoleDefinitionGetResults4.Name);
                VerifyCreateUpdateRoleDefinition(sqlRoleDefinitionCreateUpdateParameters3, sqlRoleDefinitionGetResults4);

                IEnumerable <SqlRoleDefinitionGetResults> sqlRoleDefinitions = client.ListSqlRoleDefinitionsWithHttpMessagesAsync(this.fixture.ResourceGroupName, databaseAccountName).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlRoleDefinitions);
                foreach (SqlRoleDefinitionGetResults sqlRoleDefinition in sqlRoleDefinitions)
                {
                    if (sqlRoleDefinition.Name == sqlRoleDefinitionGetResults3.Name)
                    {
                        VerifyEqualSqlRoleDefinitions(sqlRoleDefinitionGetResults3, sqlRoleDefinition);
                    }
                    if (sqlRoleDefinition.Name == sqlRoleDefinitionGetResults4.Name)
                    {
                        VerifyEqualSqlRoleDefinitions(sqlRoleDefinitionGetResults4, sqlRoleDefinition);
                    }
                }

                SqlRoleAssignmentCreateUpdateParameters sqlRoleAssignmentCreateUpdateParameters = new SqlRoleAssignmentCreateUpdateParameters
                {
                    RoleDefinitionId = sqlRoleDefinitionGetResults.Id,
                    Scope            = string.Format(
                        "/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.DocumentDB/databaseAccounts/{2}/dbs/{3}",
                        this.fixture.CosmosDBManagementClient.SubscriptionId,
                        this.fixture.ResourceGroupName,
                        databaseAccountName,
                        databaseName
                        ),
                    PrincipalId = principalId
                };

                SqlRoleAssignmentGetResults sqlRoleAssignmentGetResults = client.CreateUpdateSqlRoleAssignmentWithHttpMessagesAsync(
                    roleAssignmentId,
                    this.fixture.ResourceGroupName,
                    databaseAccountName,
                    sqlRoleAssignmentCreateUpdateParameters
                    ).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlRoleAssignmentGetResults);
                Assert.Equal(roleAssignmentId, sqlRoleAssignmentGetResults.Name);
                VerifyCreateUpdateRoleAssignment(sqlRoleAssignmentCreateUpdateParameters, sqlRoleAssignmentGetResults);

                SqlRoleAssignmentGetResults sqlRoleAssignmentGetResults2 = client.GetSqlRoleAssignmentWithHttpMessagesAsync(roleAssignmentId, this.fixture.ResourceGroupName, databaseAccountName).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlRoleAssignmentGetResults2);
                Assert.Equal(roleAssignmentId, sqlRoleAssignmentGetResults2.Name);

                VerifyEqualSqlRoleAssignments(sqlRoleAssignmentGetResults, sqlRoleAssignmentGetResults2);

                SqlRoleAssignmentCreateUpdateParameters sqlRoleAssignmentCreateUpdateParameters2 = new SqlRoleAssignmentCreateUpdateParameters
                {
                    RoleDefinitionId = sqlRoleDefinitionGetResults3.Id,
                    Scope            = string.Format("/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.DocumentDB/databaseAccounts/{2}", this.fixture.CosmosDBManagementClient.SubscriptionId, this.fixture.ResourceGroupName, databaseAccountName),
                    PrincipalId      = principalId2
                };

                SqlRoleAssignmentGetResults sqlRoleAssignmentGetResults3 = client.CreateUpdateSqlRoleAssignmentWithHttpMessagesAsync(roleAssignmentId2, this.fixture.ResourceGroupName, databaseAccountName, sqlRoleAssignmentCreateUpdateParameters2).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlRoleAssignmentGetResults3);
                Assert.Equal(roleAssignmentId2, sqlRoleAssignmentGetResults3.Name);
                VerifyCreateUpdateRoleAssignment(sqlRoleAssignmentCreateUpdateParameters2, sqlRoleAssignmentGetResults3);

                IEnumerable <SqlRoleAssignmentGetResults> sqlRoleAssignments = client.ListSqlRoleAssignmentsWithHttpMessagesAsync(this.fixture.ResourceGroupName, databaseAccountName).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlRoleAssignments);
                foreach (SqlRoleAssignmentGetResults sqlRoleAssignment in sqlRoleAssignments)
                {
                    VerifyEqualSqlRoleAssignments(sqlRoleAssignment.Name == sqlRoleAssignmentGetResults.Name ? sqlRoleAssignmentGetResults : sqlRoleAssignmentGetResults3, sqlRoleAssignment);
                }

                foreach (SqlRoleAssignmentGetResults sqlRoleAssignment in sqlRoleAssignments)
                {
                    client.DeleteSqlRoleAssignmentWithHttpMessagesAsync(sqlRoleAssignment.Name, this.fixture.ResourceGroupName, databaseAccountName).GetAwaiter().GetResult();
                }

                foreach (SqlRoleDefinitionGetResults sqlRoleDefinition in sqlRoleDefinitions)
                {
                    if (sqlRoleDefinition.Name == sqlRoleDefinitionGetResults3.Name || sqlRoleDefinition.Name == sqlRoleDefinitionGetResults4.Name)
                    {
                        client.DeleteSqlRoleDefinitionWithHttpMessagesAsync(sqlRoleDefinition.Name, this.fixture.ResourceGroupName, databaseAccountName).GetAwaiter().GetResult();
                    }
                }

                const string InvalidActionName = "invalid-action-name";

                SqlRoleDefinitionCreateUpdateParameters sqlRoleDefinitionCreateUpdateParameters4 = new SqlRoleDefinitionCreateUpdateParameters
                {
                    RoleName         = "roleName4",
                    Type             = RoleDefinitionType.CustomRole,
                    AssignableScopes = new List <string>
                    {
                        string.Format("/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.DocumentDB/databaseAccounts/{2}", this.fixture.CosmosDBManagementClient.SubscriptionId, this.fixture.ResourceGroupName, databaseAccountName)
                    },
                    Permissions = new List <Permission>
                    {
                        new Permission
                        {
                            DataActions = new List <string>
                            {
                                InvalidActionName,
                                "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/items/delete",
                                "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/items/replace"
                            }
                        }
                    }
                };

                Exception exception =
                    Assert.ThrowsAnyAsync <Exception>(() =>
                                                      client.CreateUpdateSqlRoleDefinitionWithHttpMessagesAsync(roleDefinitionId3, this.fixture.ResourceGroupName, databaseAccountName, sqlRoleDefinitionCreateUpdateParameters4))
                    .GetAwaiter().GetResult();

                Assert.Contains(InvalidActionName, exception.Message);
            }
        }
Example #3
0
        public override void ExecuteCmdlet()
        {
            if (ParameterSetName.Equals(ParentObjectDataActionsParameterSet, StringComparison.Ordinal) ||
                ParameterSetName.Equals(ParentObjectPermissionsParameterSet, StringComparison.Ordinal))
            {
                ResourceIdentifier resourceIdentifier = new ResourceIdentifier(ParentObject.Id);
                ResourceGroupName = resourceIdentifier.ResourceGroupName;
                AccountName       = resourceIdentifier.ResourceName;
            }

            List <Permission> permissions;

            if (ParameterSetName.Equals(FieldsDataActionsParameterSet, StringComparison.Ordinal) ||
                ParameterSetName.Equals(ParentObjectDataActionsParameterSet, StringComparison.Ordinal))
            {
                permissions = new List <Permission>
                {
                    new Permission
                    {
                        DataActions = DataAction
                    }
                };
            }
            else
            {
                permissions = new List <Permission>(Permission.Select(p => new Permission(p.DataActions)));
            }

            Id = string.IsNullOrWhiteSpace(Id) ? Guid.NewGuid().ToString() : RoleHelper.ParseToRoleDefinitionId(Id);

            SqlRoleDefinitionGetResults readSqlRoleDefinitionGetResults = null;

            try
            {
                readSqlRoleDefinitionGetResults = CosmosDBManagementClient.SqlResources.GetSqlRoleDefinition(Id, ResourceGroupName, AccountName);
            }
            catch (CloudException e)
            {
                if (e.Response.StatusCode != HttpStatusCode.NotFound)
                {
                    throw;
                }
            }

            if (readSqlRoleDefinitionGetResults != null)
            {
                throw new ConflictingResourceException(message: string.Format(ExceptionMessage.ConflictSqlRoleResourceId, "Definition", Id));
            }

            AssignableScope = new List <string>(AssignableScope.Select(s => RoleHelper.ParseToFullyQualifiedScope(s, DefaultProfile.DefaultContext.Subscription.Id, ResourceGroupName, AccountName)));

            SqlRoleDefinitionCreateUpdateParameters sqlRoleDefinitionCreateUpdateParameters = new SqlRoleDefinitionCreateUpdateParameters
            {
                RoleName         = RoleName,
                Type             = (RoleDefinitionType)Enum.Parse(typeof(RoleDefinitionType), Type),
                AssignableScopes = AssignableScope,
                Permissions      = permissions
            };

            if (ShouldProcess(Id, "Creating a new CosmosDB Sql Role Definition"))
            {
                SqlRoleDefinitionGetResults sqlRoleDefinitionGetResults = CosmosDBManagementClient.SqlResources.CreateUpdateSqlRoleDefinitionWithHttpMessagesAsync(Id, ResourceGroupName, AccountName, sqlRoleDefinitionCreateUpdateParameters).GetAwaiter().GetResult().Body;
                WriteObject(new PSSqlRoleDefinitionGetResults(sqlRoleDefinitionGetResults));
            }

            return;
        }
        public void SqlRoleTests()
        {
            var handler1 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType()))
            {
                // Create client
                CosmosDBManagementClient cosmosDBManagementClient = CosmosDBTestUtilities.GetCosmosDBClient(context, handler1);

                SqlRoleDefinitionCreateUpdateParameters sqlRoleDefinitionCreateUpdateParameters = new SqlRoleDefinitionCreateUpdateParameters
                {
                    RoleName         = "roleName",
                    Type             = RoleDefinitionType.CustomRole,
                    AssignableScopes = new List <string>
                    {
                        string.Format("/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.DocumentDB/databaseAccounts/{2}", cosmosDBManagementClient.SubscriptionId, resourceGroupName, databaseAccountName2),
                    },
                    Permissions = new List <Permission>
                    {
                        new Permission
                        {
                            DataActions = new List <string>
                            {
                                "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/items/create",
                                "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/items/read"
                            }
                        }
                    }
                };

                SqlRoleDefinitionGetResults sqlRoleDefinitionGetResults = cosmosDBManagementClient.SqlResources.CreateUpdateSqlRoleDefinitionWithHttpMessagesAsync(roleDefinitionId, resourceGroupName, databaseAccountName2, sqlRoleDefinitionCreateUpdateParameters).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlRoleDefinitionGetResults);
                Assert.Equal(roleDefinitionId, sqlRoleDefinitionGetResults.Name);
                VerifyCreateUpdateRoleDefinition(sqlRoleDefinitionCreateUpdateParameters, sqlRoleDefinitionGetResults);

                SqlRoleDefinitionGetResults sqlRoleDefinitionGetResults2 = cosmosDBManagementClient.SqlResources.GetSqlRoleDefinitionWithHttpMessagesAsync(roleDefinitionId, resourceGroupName, databaseAccountName2).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlRoleDefinitionGetResults2);
                Assert.Equal(roleDefinitionId, sqlRoleDefinitionGetResults2.Name);

                VerifyEqualSqlRoleDefinitions(sqlRoleDefinitionGetResults, sqlRoleDefinitionGetResults2);

                SqlRoleDefinitionCreateUpdateParameters sqlRoleDefinitionCreateUpdateParameters2 = new SqlRoleDefinitionCreateUpdateParameters
                {
                    RoleName         = "roleName2",
                    Type             = RoleDefinitionType.CustomRole,
                    AssignableScopes = new List <string>
                    {
                        string.Format("/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.DocumentDB/databaseAccounts/{2}", cosmosDBManagementClient.SubscriptionId, resourceGroupName, databaseAccountName2)
                    },
                    Permissions = new List <Permission>
                    {
                        new Permission
                        {
                            DataActions = new List <string>
                            {
                                "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/items/create",
                                "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/items/read",
                                "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/items/delete",
                                "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/items/replace"
                            }
                        }
                    }
                };

                SqlRoleDefinitionGetResults sqlRoleDefinitionGetResults3 = cosmosDBManagementClient.SqlResources.CreateUpdateSqlRoleDefinitionWithHttpMessagesAsync(roleDefinitionId2, resourceGroupName, databaseAccountName2, sqlRoleDefinitionCreateUpdateParameters2).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlRoleDefinitionGetResults3);
                Assert.Equal(roleDefinitionId2, sqlRoleDefinitionGetResults3.Name);
                VerifyCreateUpdateRoleDefinition(sqlRoleDefinitionCreateUpdateParameters2, sqlRoleDefinitionGetResults3);

                SqlRoleDefinitionCreateUpdateParameters sqlRoleDefinitionCreateUpdateParameters3 = new SqlRoleDefinitionCreateUpdateParameters
                {
                    RoleName         = "roleName3",
                    Type             = RoleDefinitionType.CustomRole,
                    AssignableScopes = new List <string>
                    {
                        string.Format("/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.DocumentDB/databaseAccounts/{2}", cosmosDBManagementClient.SubscriptionId, resourceGroupName, databaseAccountName)
                    },
                    Permissions = new List <Permission>
                    {
                        new Permission
                        {
                            DataActions = new List <string>
                            {
                                "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/items/create",
                                "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/items/delete",
                                "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/items/replace"
                            }
                        }
                    }
                };

                SqlRoleDefinitionGetResults sqlRoleDefinitionGetResults4 = cosmosDBManagementClient.SqlResources.CreateUpdateSqlRoleDefinitionWithHttpMessagesAsync(roleDefinitionId, resourceGroupName, databaseAccountName2, sqlRoleDefinitionCreateUpdateParameters3).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlRoleDefinitionGetResults4);
                Assert.Equal(roleDefinitionId, sqlRoleDefinitionGetResults4.Name);
                VerifyCreateUpdateRoleDefinition(sqlRoleDefinitionCreateUpdateParameters3, sqlRoleDefinitionGetResults4);

                IEnumerable <SqlRoleDefinitionGetResults> sqlRoleDefinitions = cosmosDBManagementClient.SqlResources.ListSqlRoleDefinitionsWithHttpMessagesAsync(resourceGroupName, databaseAccountName2).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlRoleDefinitions);
                foreach (SqlRoleDefinitionGetResults sqlRoleDefinition in sqlRoleDefinitions)
                {
                    VerifyEqualSqlRoleDefinitions(sqlRoleDefinition.Name == sqlRoleDefinitionGetResults3.Name ? sqlRoleDefinitionGetResults3 : sqlRoleDefinitionGetResults4, sqlRoleDefinition);
                }

                SqlRoleAssignmentCreateUpdateParameters sqlRoleAssignmentCreateUpdateParameters = new SqlRoleAssignmentCreateUpdateParameters
                {
                    RoleDefinitionId = sqlRoleDefinitionGetResults.Id,
                    Scope            = string.Format("/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.DocumentDB/databaseAccounts/{2}/dbs/{3}", cosmosDBManagementClient.SubscriptionId, resourceGroupName, databaseAccountName2, databaseName),
                    PrincipalId      = principalId
                };

                SqlRoleAssignmentGetResults sqlRoleAssignmentGetResults = cosmosDBManagementClient.SqlResources.CreateUpdateSqlRoleAssignmentWithHttpMessagesAsync(roleAssignmentId, resourceGroupName, databaseAccountName2, sqlRoleAssignmentCreateUpdateParameters).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlRoleAssignmentGetResults);
                Assert.Equal(roleAssignmentId, sqlRoleAssignmentGetResults.Name);
                VerifyCreateUpdateRoleAssignment(sqlRoleAssignmentCreateUpdateParameters, sqlRoleAssignmentGetResults);

                SqlRoleAssignmentGetResults sqlRoleAssignmentGetResults2 = cosmosDBManagementClient.SqlResources.GetSqlRoleAssignmentWithHttpMessagesAsync(roleAssignmentId, resourceGroupName, databaseAccountName2).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlRoleAssignmentGetResults2);
                Assert.Equal(roleAssignmentId, sqlRoleAssignmentGetResults2.Name);

                VerifyEqualSqlRoleAssignments(sqlRoleAssignmentGetResults, sqlRoleAssignmentGetResults2);

                SqlRoleAssignmentCreateUpdateParameters sqlRoleAssignmentCreateUpdateParameters2 = new SqlRoleAssignmentCreateUpdateParameters
                {
                    RoleDefinitionId = sqlRoleDefinitionGetResults3.Id,
                    Scope            = string.Format("/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.DocumentDB/databaseAccounts/{2}", cosmosDBManagementClient.SubscriptionId, resourceGroupName, databaseAccountName2),
                    PrincipalId      = principalId2
                };

                SqlRoleAssignmentGetResults sqlRoleAssignmentGetResults3 = cosmosDBManagementClient.SqlResources.CreateUpdateSqlRoleAssignmentWithHttpMessagesAsync(roleAssignmentId2, resourceGroupName, databaseAccountName2, sqlRoleAssignmentCreateUpdateParameters2).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlRoleAssignmentGetResults3);
                Assert.Equal(roleAssignmentId2, sqlRoleAssignmentGetResults3.Name);
                VerifyCreateUpdateRoleAssignment(sqlRoleAssignmentCreateUpdateParameters2, sqlRoleAssignmentGetResults3);

                IEnumerable <SqlRoleAssignmentGetResults> sqlRoleAssignments = cosmosDBManagementClient.SqlResources.ListSqlRoleAssignmentsWithHttpMessagesAsync(resourceGroupName, databaseAccountName2).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlRoleAssignments);
                foreach (SqlRoleAssignmentGetResults sqlRoleAssignment in sqlRoleAssignments)
                {
                    VerifyEqualSqlRoleAssignments(sqlRoleAssignment.Name == sqlRoleAssignmentGetResults.Name ? sqlRoleAssignmentGetResults : sqlRoleAssignmentGetResults3, sqlRoleAssignment);
                }

                foreach (SqlRoleAssignmentGetResults sqlRoleAssignment in sqlRoleAssignments)
                {
                    cosmosDBManagementClient.SqlResources.DeleteSqlRoleAssignmentWithHttpMessagesAsync(sqlRoleAssignment.Name, resourceGroupName, databaseAccountName2).GetAwaiter().GetResult();
                }

                foreach (SqlRoleDefinitionGetResults sqlRoleDefinition in sqlRoleDefinitions)
                {
                    cosmosDBManagementClient.SqlResources.DeleteSqlRoleDefinitionWithHttpMessagesAsync(sqlRoleDefinition.Name, resourceGroupName, databaseAccountName2).GetAwaiter().GetResult();
                }
            }
        }
        public override void ExecuteCmdlet()
        {
            List <Permission> permissions = null;

            if (ParameterSetName.Equals(ParentObjectParameterSet, StringComparison.Ordinal))
            {
                ResourceIdentifier resourceIdentifier = new ResourceIdentifier(ParentObject.Id);
                ResourceGroupName = resourceIdentifier.ResourceGroupName;
                AccountName       = resourceIdentifier.ResourceName;
            }
            else if (ParameterSetName.Equals(ObjectParameterSet))
            {
                RoleName        = InputObject.RoleName;
                Type            = InputObject.Type;
                AssignableScope = new List <String>(InputObject.AssignableScopes);
                Id          = InputObject.Id;
                permissions = new List <Permission>(InputObject.Permissions);

                ResourceIdentifier resourceIdentifier = new ResourceIdentifier(InputObject.Id);
                ResourceGroupName = resourceIdentifier.ResourceGroupName;
                AccountName       = resourceIdentifier.GetDatabaseAccountName();
            }

            if (DataAction != null && Permission != null)
            {
                throw new ArgumentException($"Cannot specify both [{nameof(DataAction)}] and [{nameof(Permission)}]");
            }

            if (DataAction != null)
            {
                permissions = new List <Permission>
                {
                    new Permission
                    {
                        DataActions = DataAction
                    }
                };
            }
            else if (Permission != null)
            {
                permissions = new List <Permission>(Permission.Select(p => new Permission(p.DataActions)));
            }

            Id = RoleHelper.ParseToRoleDefinitionId(Id);

            SqlRoleDefinitionGetResults readSqlRoleDefinitionGetResults = null;

            try
            {
                readSqlRoleDefinitionGetResults = CosmosDBManagementClient.SqlResources.GetSqlRoleDefinition(Id, ResourceGroupName, AccountName);
            }
            catch (CloudException e)
            {
                if (e.Response.StatusCode == System.Net.HttpStatusCode.NotFound)
                {
                    throw new ResourceNotFoundException(message: string.Format(ExceptionMessage.NotFoundSqlRoleResourceId, "Definition", Id), innerException: e);
                }
                else
                {
                    throw e;
                }
            }

            AssignableScope = AssignableScope ?? new List <string>(readSqlRoleDefinitionGetResults.AssignableScopes);
            AssignableScope = new List <string>(AssignableScope.Select(s => RoleHelper.ParseToFullyQualifiedScope(s, DefaultProfile.DefaultContext.Subscription.Id, ResourceGroupName, AccountName)));

            SqlRoleDefinitionCreateUpdateParameters sqlRoleDefinitionCreateUpdateParameters = new SqlRoleDefinitionCreateUpdateParameters
            {
                RoleName         = RoleName ?? readSqlRoleDefinitionGetResults.RoleName,
                Type             = (RoleDefinitionType)Enum.Parse(typeof(RoleDefinitionType), Type ?? readSqlRoleDefinitionGetResults.Type),
                AssignableScopes = AssignableScope,
                Permissions      = permissions ?? readSqlRoleDefinitionGetResults.Permissions,
            };

            if (ShouldProcess(Id, "Updating an existing CosmosDB Sql Role Definition"))
            {
                SqlRoleDefinitionGetResults sqlRoleDefinitionGetResults = CosmosDBManagementClient.SqlResources.CreateUpdateSqlRoleDefinitionWithHttpMessagesAsync(Id, ResourceGroupName, AccountName, sqlRoleDefinitionCreateUpdateParameters).GetAwaiter().GetResult().Body;
                WriteObject(new PSSqlRoleDefinitionGetResults(sqlRoleDefinitionGetResults));
            }

            return;
        }