Ejemplo n.º 1
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 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;
        }