public async Task <IActionResult> Get(string providerId) { var teamCloudInstance = await teamCloudRepository .GetAsync() .ConfigureAwait(false); if (teamCloudInstance is null) { return(ErrorResult .NotFound($"No TeamCloud Instance was found.") .ActionResult()); } var provider = teamCloudInstance.Providers?.FirstOrDefault(p => p.Id == providerId); if (provider is null) { return(ErrorResult .NotFound($"A Provider with the ID '{providerId}' could not be found in this TeamCloud Instance") .ActionResult()); } return(DataResult <Provider> .Ok(provider) .ActionResult()); }
private IActionResult GetStatusResult(ICommandResult result) { if (result is null) { return(ErrorResult .NotFound($"A status for the provided Tracking Id was not found.") .ActionResult()); } result.Links.TryGetValue("status", out var status); switch (result.RuntimeStatus) { case CommandRuntimeStatus.Completed: if (result.Links.TryGetValue("location", out var location)) { // return 302 (found) with location to resource Response.Headers.Add("Location", location); return(StatusResult .Success(result.CommandId.ToString(), location, result.RuntimeStatus.ToString(), result.CustomStatus) .ActionResult()); } // no resource location (i.e. DELETE command) return 200 (ok) return(StatusResult .Success(result.CommandId.ToString(), result.RuntimeStatus.ToString(), result.CustomStatus) .ActionResult()); case CommandRuntimeStatus.Running: case CommandRuntimeStatus.ContinuedAsNew: case CommandRuntimeStatus.Pending: // command is in an active state, return 202 (accepted) so client can poll return(StatusResult .Accepted(result.CommandId.ToString(), status, result.RuntimeStatus.ToString(), result.CustomStatus) .ActionResult()); case CommandRuntimeStatus.Canceled: case CommandRuntimeStatus.Terminated: case CommandRuntimeStatus.Failed: return(StatusResult .Failed(result.Errors, result.CommandId.ToString(), result.RuntimeStatus.ToString(), result.CustomStatus) .ActionResult()); default: // TODO: this probably isn't right as a default if (result.Errors?.Any() ?? false) { return(StatusResult .Failed(result.Errors, result.CommandId.ToString(), result.RuntimeStatus.ToString(), result.CustomStatus) .ActionResult()); } return(StatusResult .Ok(result.CommandId.ToString(), result.RuntimeStatus.ToString(), result.CustomStatus) .ActionResult()); } }
public async Task <IActionResult> Get() { if (string.IsNullOrEmpty(ProjectId)) { return(ErrorResult .BadRequest($"Project Id provided in the url path is invalid. Must be a valid GUID.", ResultErrorCode.ValidationError) .ActionResult()); } var project = await projectsRepository .GetAsync(ProjectId) .ConfigureAwait(false); if (project is null) { return(ErrorResult .NotFound($"A Project with the identifier '{ProjectId}' was not found in this TeamCloud instance.") .ActionResult()); } if (project?.Identity is null) { return(ErrorResult .NotFound($"A ProjectIdentity was not found for the Project '{ProjectId}'.") .ActionResult()); } return(DataResult <ProjectIdentity> .Ok(project.Identity) .ActionResult()); }
public async Task <IActionResult> Get() { var teamCloudInstance = await teamCloudRepository .GetAsync() .ConfigureAwait(false); if (teamCloudInstance is null) { return(ErrorResult .NotFound($"No TeamCloud Instance was found.") .ActionResult()); } var projectTypes = await projectTypesRepository .ListAsync() .ToListAsync() .ConfigureAwait(false); var config = new TeamCloudConfiguration { ProjectTypes = projectTypes, Providers = teamCloudInstance.Providers, Users = teamCloudInstance.Users, Tags = teamCloudInstance.Tags, Properties = teamCloudInstance.Properties, }; return(DataResult <TeamCloudConfiguration> .Ok(config) .ActionResult()); }
public async Task <IActionResult> GetMe() { if (string.IsNullOrEmpty(ProjectId)) { return(ErrorResult .BadRequest($"Project Id provided in the url path is invalid. Must be a valid GUID.", ResultErrorCode.ValidationError) .ActionResult()); } var me = await userService .CurrentUserAsync() .ConfigureAwait(false); if (me is null) { return(ErrorResult .NotFound($"A User matching the current user was not found in this TeamCloud instance.") .ActionResult()); } if (!me.IsMember(ProjectId)) { return(ErrorResult .NotFound($"A User matching the current user was not found in this Project.") .ActionResult()); } var returnUser = me.PopulateExternalModel(ProjectId); return(DataResult <User> .Ok(returnUser) .ActionResult()); }
public async Task <IActionResult> Delete(string projectNameOrId) { if (string.IsNullOrWhiteSpace(projectNameOrId)) { return(ErrorResult .BadRequest($"The identifier '{projectNameOrId}' provided in the url path is invalid. Must be a valid project name or GUID.", ResultErrorCode.ValidationError) .ActionResult()); } var project = await projectsRepository .GetAsync(projectNameOrId) .ConfigureAwait(false); if (project is null) { return(ErrorResult .NotFound($"A Project with the identifier '{projectNameOrId}' could not be found in this TeamCloud Instance") .ActionResult()); } var currentUserForCommand = await userService .CurrentUserAsync() .ConfigureAwait(false); var command = new OrchestratorProjectDeleteCommand(currentUserForCommand, project); return(await orchestrator .InvokeAndReturnAccepted(command) .ConfigureAwait(false)); }
public IActionResult GetClientError(ActionContext actionContext, IClientErrorActionResult clientError) { if (clientError is null) { throw new System.ArgumentNullException(nameof(clientError)); } if (clientError.StatusCode.HasValue) { switch (clientError.StatusCode.Value) { case StatusCodes.Status400BadRequest: return(ErrorResult.BadRequest().ToActionResult()); case StatusCodes.Status401Unauthorized: return(ErrorResult.Unauthorized().ToActionResult()); case StatusCodes.Status403Forbidden: return(ErrorResult.Forbidden().ToActionResult()); case StatusCodes.Status404NotFound: return(ErrorResult.NotFound("Not Found").ToActionResult()); case StatusCodes.Status409Conflict: return(ErrorResult.Conflict("Conflict").ToActionResult()); case StatusCodes.Status500InternalServerError: return(ErrorResult.ServerError().ToActionResult()); } } return(ErrorResult.Unknown(clientError.StatusCode).ToActionResult()); }
public async Task <IActionResult> GetMe() { var me = await userService .CurrentUserAsync() .ConfigureAwait(false); if (me is null) { return(ErrorResult .NotFound($"A User matching the current authenticated user was not found in this TeamCloud instance.") .ActionResult()); } var projectIds = me.ProjectMemberships.Select(pm => pm.ProjectId); if (!projectIds.Any()) { return(DataResult <List <Project> > .Ok(new List <Project>()) .ActionResult()); } var projectDocuments = await projectsRepository .ListAsync(projectIds) .ToListAsync() .ConfigureAwait(false); var projects = projectDocuments.Select(p => p.PopulateExternalModel()).ToList(); return(DataResult <List <Project> > .Ok(projects) .ActionResult()); }
public async Task <IActionResult> Delete([FromRoute] string userNameOrId) { if (string.IsNullOrWhiteSpace(userNameOrId)) { return(ErrorResult .BadRequest($"The identifier '{userNameOrId}' provided in the url path is invalid. Must be a valid email address or GUID.", ResultErrorCode.ValidationError) .ActionResult()); } var teamCloudInstance = await teamCloudRepository .GetAsync() .ConfigureAwait(false); if (teamCloudInstance is null) { return(ErrorResult .NotFound($"No TeamCloud Instance was found.") .ActionResult()); } if (!Guid.TryParse(userNameOrId, out var userId)) { var idLookup = await userService .GetUserIdAsync(userNameOrId) .ConfigureAwait(false); if (!idLookup.HasValue || idLookup.Value == Guid.Empty) { return(ErrorResult .NotFound($"A User with the email '{userNameOrId}' could not be found.") .ActionResult()); } userId = idLookup.Value; } var user = teamCloudInstance.Users?.FirstOrDefault(u => u.Id == userId); if (user is null) { return(ErrorResult .NotFound($"The specified User could not be found in this TeamCloud Instance.") .ActionResult()); } var command = new OrchestratorTeamCloudUserDeleteCommand(CurrentUser, user); var commandResult = await orchestrator .InvokeAsync(command) .ConfigureAwait(false); if (commandResult.Links.TryGetValue("status", out var statusUrl)) { return(StatusResult .Accepted(commandResult.CommandId.ToString(), statusUrl, commandResult.RuntimeStatus.ToString(), commandResult.CustomStatus) .ActionResult()); } throw new Exception("This shoudn't happen, but we need to decide to do when it does."); }
public Task <IActionResult> Get([FromRoute] string id) => WithContextAsync <Project>(async(contextUser, project) => { if (string.IsNullOrWhiteSpace(id)) { return(ErrorResult .BadRequest($"The id provided in the url path is invalid. Must be a non-empty string.", ResultErrorCode.ValidationError) .ToActionResult()); } var projectTemplate = await projectTemplateRepository .GetAsync(project.Organization, project.Template) .ConfigureAwait(false); var componentTemplate = await componentTemplateRepository .GetAsync(project.Organization, project.Id, id) .ConfigureAwait(false); if (!(componentTemplate?.ParentId?.Equals(projectTemplate.Id, StringComparison.Ordinal) ?? false)) { return(ErrorResult .NotFound($"A Component Template with the id '{id}' could not be found for Project {project.Id}.") .ToActionResult()); } return(DataResult <ComponentTemplate> .Ok(componentTemplate) .ToActionResult()); });
public async Task <IActionResult> Get([FromRoute] string tagKey) { if (string.IsNullOrWhiteSpace(tagKey)) { return(ErrorResult .BadRequest($"The key provided in the url path is invalid. Must be a non-empty string.", ResultErrorCode.ValidationError) .ActionResult()); } var teamCloudInstance = await teamCloudRepository .GetAsync() .ConfigureAwait(false); if (teamCloudInstance is null) { return(ErrorResult .NotFound($"No TeamCloud Instance was found.") .ActionResult()); } if (!teamCloudInstance.Tags.TryGetValue(tagKey, out var tagValue)) { return(ErrorResult .NotFound($"The specified Tag could not be found in this TeamCloud Instance.") .ActionResult()); } return(DataResult <Dictionary <string, string> > .Ok(new Dictionary <string, string> { { tagKey, tagValue } }) .ActionResult()); }
public async Task <IActionResult> Get(string providerDataId) { var provider = await providersRepository .GetAsync(ProviderId) .ConfigureAwait(false); if (provider is null) { return(ErrorResult .NotFound($"A Provider with the ID '{ProviderId}' could not be found in this TeamCloud Instance") .ActionResult()); } var providerData = await providerDataRepository .GetAsync(providerDataId) .ConfigureAwait(false); if (providerData is null) { return(ErrorResult .NotFound($"A Provider Data item with the ID '{providerDataId}' could not be found") .ActionResult()); } var returnData = providerData.PopulateExternalModel(); return(DataResult <ProviderData> .Ok(returnData) .ActionResult()); }
public async Task <IActionResult> Delete([FromRoute] string tagKey) { if (string.IsNullOrWhiteSpace(tagKey)) { return(ErrorResult .BadRequest($"The key provided in the url path is invalid. Must be a non-empty string.", ResultErrorCode.ValidationError) .ActionResult()); } var teamCloudInstance = await teamCloudRepository .GetAsync() .ConfigureAwait(false); if (teamCloudInstance is null) { return(ErrorResult .NotFound($"No TeamCloud Instance was found.") .ActionResult()); } if (!teamCloudInstance.Tags.TryGetValue(tagKey, out _)) { return(ErrorResult .NotFound($"The specified Tag could not be found in this TeamCloud Instance.") .ActionResult()); } // TODO: return(new NoContentResult()); // var command = new ProjectUserDeleteCommand(CurrentUser, user, ProjectId.Value); // return await orchestrator // .InvokeAndReturnAccepted(command) // .ConfigureAwait(false); }
public async Task <IActionResult> Get() { if (string.IsNullOrEmpty(ProjectId)) { return(ErrorResult .BadRequest($"Project Id provided in the url path is invalid. Must be a valid GUID.", ResultErrorCode.ValidationError) .ActionResult()); } var project = await projectsRepository .GetAsync(ProjectId) .ConfigureAwait(false); if (project is null) { return(ErrorResult .NotFound($"A Project with the ID '{ProjectId}' could not be found in this TeamCloud Instance.") .ActionResult()); } var tags = project?.Tags is null ? new Dictionary <string, string>() : new Dictionary <string, string>(project.Tags); return(DataResult <Dictionary <string, string> > .Ok(tags) .ActionResult()); }
public async Task <IActionResult> Delete([FromRoute] string providerDataId) { var provider = await providersRepository .GetAsync(ProviderId) .ConfigureAwait(false); if (provider is null) { return(ErrorResult .NotFound($"A Provider with the ID '{ProviderId}' could not be found in this TeamCloud Instance") .ActionResult()); } var existingProviderData = await providerDataRepository .GetAsync(providerDataId) .ConfigureAwait(false); if (existingProviderData is null) { return(ErrorResult .NotFound($"A Provider Data item with the ID '{providerDataId}' could not be found") .ActionResult()); } _ = await orchestrator .DeleteAsync(existingProviderData) .ConfigureAwait(false); return(DataResult <ProviderData> .NoContent() .ActionResult()); }
public async Task <IActionResult> Post([FromBody] UserDefinition userDefinition) { if (userDefinition is null) { throw new ArgumentNullException(nameof(userDefinition)); } var validation = new UserDefinitionValidator().Validate(userDefinition); if (!validation.IsValid) { return(ErrorResult .BadRequest(validation) .ActionResult()); } var teamCloudInstance = await teamCloudRepository .GetAsync() .ConfigureAwait(false); if (teamCloudInstance is null) { return(ErrorResult .NotFound($"No TeamCloud Instance was found.") .ActionResult()); } var newUser = await userService .GetUserAsync(userDefinition) .ConfigureAwait(false); if (newUser is null) { return(ErrorResult .NotFound($"A User with the Email '{userDefinition.Email}' could not be found.") .ActionResult()); } if (teamCloudInstance.Users.Contains(newUser)) { return(ErrorResult .Conflict($"A User with the Email '{userDefinition.Email}' already exists on this TeamCloud Instance. Please try your request again with a unique email or call PUT to update the existing User.") .ActionResult()); } var command = new OrchestratorTeamCloudUserCreateCommand(CurrentUser, newUser); var commandResult = await orchestrator .InvokeAsync(command) .ConfigureAwait(false); if (commandResult.Links.TryGetValue("status", out var statusUrl)) { return(StatusResult .Accepted(commandResult.CommandId.ToString(), statusUrl, commandResult.RuntimeStatus.ToString(), commandResult.CustomStatus) .ActionResult()); } throw new Exception("This shouldn't happen, but we need to decide to do when it does."); }
public async Task <IActionResult> Get(string projectNameOrId) { if (string.IsNullOrWhiteSpace(projectNameOrId)) { return(ErrorResult .BadRequest($"The identifier '{projectNameOrId}' provided in the url path is invalid. Must be a valid project name or GUID.", ResultErrorCode.ValidationError) .ActionResult()); } var project = await projectsRepository .GetAsync(projectNameOrId) .ConfigureAwait(false); if (project is null) { return(ErrorResult .NotFound($"A Project with the identifier '{projectNameOrId}' could not be found in this TeamCloud Instance") .ActionResult()); } var returnProject = project.PopulateExternalModel(); return(DataResult <Project> .Ok(returnProject) .ActionResult()); }
public async Task <IActionResult> Get() { if (!ProjectId.HasValue) { return(ErrorResult .BadRequest($"Project Id provided in the url path is invalid. Must be a valid GUID.", ResultErrorCode.ValidationError) .ActionResult()); } var project = await projectsRepository .GetAsync(ProjectId.Value) .ConfigureAwait(false); if (project is null) { return(ErrorResult .NotFound($"A Project with the ID '{ProjectId.Value}' could not be found in this TeamCloud Instance") .ActionResult()); } var users = project?.Users ?? new List <User>(); return(DataResult <List <User> > .Ok(users.ToList()) .ActionResult()); }
public async Task <IActionResult> Put([FromBody] User user) { if (user is null) { throw new ArgumentNullException(nameof(user)); } var validation = new UserValidator().Validate(user); if (!validation.IsValid) { return(ErrorResult .BadRequest(validation) .ActionResult()); } var teamCloudInstance = await teamCloudRepository .GetAsync() .ConfigureAwait(false); if (teamCloudInstance is null) { return(ErrorResult .NotFound($"No TeamCloud Instance was found.") .ActionResult()); } var oldUser = teamCloudInstance.Users?.FirstOrDefault(u => u.Id == user.Id); if (oldUser is null) { return(ErrorResult .NotFound($"A User with the ID '{oldUser.Id}' could not be found on this TeamCloud Instance.") .ActionResult()); } if (oldUser.IsAdmin() && !user.IsAdmin() && teamCloudInstance.Users.Count(u => u.IsAdmin()) == 1) { return(ErrorResult .BadRequest($"The TeamCloud instance must have at least one Admin user. To change this user's role you must first add another Admin user.", ResultErrorCode.ValidationError) .ActionResult()); } var command = new OrchestratorTeamCloudUserUpdateCommand(CurrentUser, user); var commandResult = await orchestrator .InvokeAsync(command) .ConfigureAwait(false); if (commandResult.Links.TryGetValue("status", out var statusUrl)) { return(StatusResult .Accepted(commandResult.CommandId.ToString(), statusUrl, commandResult.RuntimeStatus.ToString(), commandResult.CustomStatus) .ActionResult()); } throw new Exception("This shouldn't happen, but we need to decide to do when it does."); }
public async Task <IActionResult> Put([FromBody] ProjectType projectType) { if (projectType is null) { throw new ArgumentNullException(nameof(projectType)); } var validation = new ProjectTypeValidator().Validate(projectType); if (!validation.IsValid) { return(ErrorResult .BadRequest(validation) .ActionResult()); } var existingProjectType = await projectTypesRepository .GetAsync(projectType.Id) .ConfigureAwait(false); if (existingProjectType is null) { return(ErrorResult .NotFound($"A ProjectType with the ID '{projectType.Id}' could not be found in this TeamCloud Instance") .ActionResult()); } var providers = await providersRepository .ListAsync() .ToListAsync() .ConfigureAwait(false); var validProviders = projectType.Providers .All(p => providers.Any(provider => provider.Id == p.Id)); if (!validProviders) { var validProviderIds = string.Join(", ", providers.Select(p => p.Id)); return(ErrorResult .BadRequest(new ValidationError { Field = "projectType", Message = $"All provider ids on a ProjectType must match the id of a registered Provider on the TeamCloud instance. Valid provider ids are: {validProviderIds}" }) .ActionResult()); } existingProjectType.PopulateFromExternalModel(projectType); var updateResult = await orchestrator .UpdateAsync(existingProjectType) .ConfigureAwait(false); var returnUpdateResult = updateResult.PopulateExternalModel(); return(DataResult <ProjectType> .Ok(returnUpdateResult) .ActionResult()); }
public async Task <IActionResult> Delete([FromRoute] string providerDataId) { var provider = await providersRepository .GetAsync(ProviderId) .ConfigureAwait(false); if (provider is null) { return(ErrorResult .NotFound($"A Provider with the ID '{ProviderId}' could not be found in this TeamCloud Instance") .ToActionResult()); } var existingProviderData = await providerDataRepository .GetAsync(providerDataId) .ConfigureAwait(false); if (existingProviderData is null) { return(ErrorResult .NotFound($"A Provider Data item with the ID '{providerDataId}' could not be found") .ToActionResult()); } if (existingProviderData.Scope == ProviderDataScope.System) { return(ErrorResult .BadRequest("The specified Provider Data item is not scoped to a project use the system api to delete.", ResultErrorCode.ValidationError) .ToActionResult()); } var project = await projectsRepository .GetAsync(ProjectId) .ConfigureAwait(false); if (project is null) { return(ErrorResult .NotFound($"A Project with the identifier '{ProjectId}' could not be found in this TeamCloud Instance") .ToActionResult()); } if (!existingProviderData.ProjectId.Equals(project.Id, StringComparison.OrdinalIgnoreCase)) { return(ErrorResult .NotFound($"A Provider Data item with the ID '{providerDataId}' could not be found for project '{ProjectId}'") .ToActionResult()); } _ = await orchestrator .DeleteAsync(existingProviderData) .ConfigureAwait(false); return(DataResult <ProviderData> .NoContent() .ToActionResult()); }
public async Task <IActionResult> Delete([FromRoute] string userNameOrId) { if (string.IsNullOrWhiteSpace(userNameOrId)) { return(ErrorResult .BadRequest($"The identifier '{userNameOrId}' provided in the url path is invalid. Must be a valid email address or GUID.", ResultErrorCode.ValidationError) .ActionResult()); } var userId = await userService .GetUserIdAsync(userNameOrId) .ConfigureAwait(false); if (string.IsNullOrEmpty(userId)) { return(ErrorResult .NotFound($"The user '{userNameOrId}' could not be found.") .ActionResult()); } var user = await usersRepository .GetAsync(userId) .ConfigureAwait(false); if (user is null) { return(ErrorResult .NotFound($"The specified User could not be found in this TeamCloud Instance.") .ActionResult()); } if (user.IsAdmin()) { var otherAdmins = await usersRepository .ListAdminsAsync() .AnyAsync(a => a.Id != user.Id) .ConfigureAwait(false); if (!otherAdmins) { return(ErrorResult .BadRequest($"The TeamCloud instance must have at least one Admin user. To delete this user you must first add another Admin user.", ResultErrorCode.ValidationError) .ActionResult()); } } var currentUserForCommand = await userService .CurrentUserAsync() .ConfigureAwait(false); var command = new OrchestratorTeamCloudUserDeleteCommand(currentUserForCommand, user); return(await orchestrator .InvokeAndReturnAccepted(command) .ConfigureAwait(false)); }
public async Task <IActionResult> Post([FromBody] UserDefinition userDefinition) { if (userDefinition is null) { throw new ArgumentNullException(nameof(userDefinition)); } var validation = new UserDefinitionTeamCloudValidator().Validate(userDefinition); if (!validation.IsValid) { return(ErrorResult .BadRequest(validation) .ActionResult()); } var userId = await userService .GetUserIdAsync(userDefinition.Identifier) .ConfigureAwait(false); if (string.IsNullOrEmpty(userId)) { return(ErrorResult .NotFound($"The user '{userDefinition.Identifier}' could not be found.") .ActionResult()); } var user = await usersRepository .GetAsync(userId) .ConfigureAwait(false); if (user != null) { return(ErrorResult .Conflict($"The user '{userDefinition.Identifier}' already exists on this TeamCloud Instance. Please try your request again with a unique user or call PUT to update the existing User.") .ActionResult()); } user = new Model.Internal.Data.User { Id = userId, Role = Enum.Parse <TeamCloudUserRole>(userDefinition.Role, true), Properties = userDefinition.Properties, UserType = UserType.User }; var currentUserForCommand = await userService .CurrentUserAsync() .ConfigureAwait(false); var command = new OrchestratorTeamCloudUserCreateCommand(currentUserForCommand, user); return(await orchestrator .InvokeAndReturnAccepted(command) .ConfigureAwait(false)); }
public async Task <IActionResult> Put([FromBody] ProviderData providerData) { if (providerData is null) { throw new ArgumentNullException(nameof(providerData)); } var validation = new ProviderDataValidator().Validate(providerData); if (!validation.IsValid) { return(ErrorResult .BadRequest(validation) .ActionResult()); } var provider = await providersRepository .GetAsync(ProviderId) .ConfigureAwait(false); if (provider is null) { return(ErrorResult .NotFound($"A Provider with the ID '{ProviderId}' could not be found in this TeamCloud Instance") .ActionResult()); } var oldProviderData = await providerDataRepository .GetAsync(providerData.Id) .ConfigureAwait(false); if (oldProviderData is null) { return(ErrorResult .NotFound($"The Provider Data '{providerData.Id}' could not be found..") .ActionResult()); } var newProviderData = new ProviderDataDocument { ProviderId = provider.Id, Scope = ProviderDataScope.System }; newProviderData.PopulateFromExternalModel(providerData); var updateResult = await orchestrator .UpdateAsync(newProviderData) .ConfigureAwait(false); var returnUpdateResult = updateResult.PopulateExternalModel(); return(DataResult <ProviderData> .Ok(returnUpdateResult) .ActionResult()); }
public async Task <IActionResult> Get([FromRoute] string userNameOrId) { if (!ProjectId.HasValue) { return(ErrorResult .BadRequest($"Project Id provided in the url path is invalid. Must be a valid GUID.", ResultErrorCode.ValidationError) .ActionResult()); } if (string.IsNullOrWhiteSpace(userNameOrId)) { return(ErrorResult .BadRequest($"The identifier '{userNameOrId}' provided in the url path is invalid. Must be a valid email address or GUID.", ResultErrorCode.ValidationError) .ActionResult()); } var project = await projectsRepository .GetAsync(ProjectId.Value) .ConfigureAwait(false); if (project is null) { return(ErrorResult .NotFound($"A Project with the ID '{ProjectId.Value}' could not be found in this TeamCloud Instance.") .ActionResult()); } if (!Guid.TryParse(userNameOrId, out var userId)) { var idLookup = await userService .GetUserIdAsync(userNameOrId) .ConfigureAwait(false); if (!idLookup.HasValue || idLookup.Value == Guid.Empty) { return(ErrorResult .NotFound($"A User with the email '{userNameOrId}' could not be found.") .ActionResult()); } userId = idLookup.Value; } var user = project?.Users?.FirstOrDefault(u => u.Id == userId); if (user is null) { return(ErrorResult .NotFound($"The specified User could not be found in this Project.") .ActionResult()); } return(DataResult <User> .Ok(user) .ActionResult()); }
public async Task <IActionResult> Post([FromBody] UserDefinition userDefinition) { if (userDefinition is null) { throw new ArgumentNullException(nameof(userDefinition)); } var validation = new UserDefinitionTeamCloudAdminValidator().Validate(userDefinition); if (!validation.IsValid) { return(ErrorResult .BadRequest(validation) .ActionResult()); } var adminUsers = await usersRepository .ListAdminsAsync() .AnyAsync() .ConfigureAwait(false); if (adminUsers) { return(ErrorResult .BadRequest($"The TeamCloud instance already has one or more Admin users. To add additional users to the TeamCloud instance POST to api/users.", ResultErrorCode.ValidationError) .ActionResult()); } var userId = await userService .GetUserIdAsync(userDefinition.Identifier) .ConfigureAwait(false); if (string.IsNullOrEmpty(userId)) { return(ErrorResult .NotFound($"The user '{userDefinition.Identifier}' could not be found.") .ActionResult()); } var user = new User { Id = userId, Role = Enum.Parse <TeamCloudUserRole>(userDefinition.Role, true), Properties = userDefinition.Properties, UserType = UserType.User }; // no users exist in the database yet and the cli calls this api implicitly immediatly // after the teamcloud instance is created to add the instance creator as an admin user // thus, we can assume the calling user and the user from the payload are the same var command = new OrchestratorTeamCloudUserCreateCommand(user, user); return(await orchestrator .InvokeAndReturnAccepted(command) .ConfigureAwait(false)); }
public async Task <IActionResult> EnsureProjectAndProviderAsync(Func <ProjectDocument, ProviderDocument, Task <IActionResult> > callback) { try { if (callback is null) { throw new ArgumentNullException(nameof(callback)); } if (string.IsNullOrEmpty(ProjectIdentifier)) { return(ErrorResult .BadRequest($"Project name or id provided in the url path is invalid. Must be a valid project name or id (guid).", ResultErrorCode.ValidationError) .ToActionResult()); } if (string.IsNullOrEmpty(ProviderId)) { return(ErrorResult .BadRequest($"Provider id provided in the url path is invalid. Must be a valid non-empty string.", ResultErrorCode.ValidationError) .ToActionResult()); } var project = await ProjectRepository .GetAsync(ProjectIdentifier) .ConfigureAwait(false); if (project is null) { return(ErrorResult .NotFound($"A Project with the name or id '{ProjectIdentifier}' could not be found.") .ToActionResult()); } var provider = await ProviderRepository .GetAsync(ProviderId) .ConfigureAwait(false); if (provider is null) { return(ErrorResult .NotFound($"A Provider with the id '{ProviderId}' could not be found..") .ToActionResult()); } return(await callback(project, provider) .ConfigureAwait(false)); } catch (Exception exc) { return(ErrorResult .ServerError(exc) .ToActionResult()); } }
public async Task <IActionResult> Put([FromBody] TeamCloudInstance teamCloudInstance) { if (teamCloudInstance is null) { return(ErrorResult .BadRequest("Request body must not be empty.", ResultErrorCode.ValidationError) .ToActionResult()); } if (!teamCloudInstance.TryValidate(out var validationResult, serviceProvider: HttpContext.RequestServices)) { return(ErrorResult .BadRequest(validationResult) .ToActionResult()); } var teamCloudInstanceDocument = await teamCloudRepository .GetAsync() .ConfigureAwait(false); if (teamCloudInstanceDocument is null) { return(ErrorResult .NotFound("The TeamCloud instance could not be found.") .ToActionResult()); } if (!string.IsNullOrEmpty(teamCloudInstance.Version)) { teamCloudInstanceDocument.Version = teamCloudInstance.Version; } if (!(teamCloudInstance.ResourceGroup is null)) { teamCloudInstanceDocument.ResourceGroup = teamCloudInstance.ResourceGroup; } if (teamCloudInstance.Tags?.Any() ?? false) { teamCloudInstanceDocument.MergeTags(teamCloudInstance.Tags); } if (teamCloudInstance.Applications?.Any() ?? false) { teamCloudInstanceDocument.Applications = teamCloudInstance.Applications; } var currentUser = await UserService .CurrentUserAsync() .ConfigureAwait(false); return(await Orchestrator .InvokeAndReturnActionResultAsync <TeamCloudInstanceDocument, TeamCloudInstance>(new OrchestratorTeamCloudInstanceSetCommand(currentUser, teamCloudInstanceDocument), Request) .ConfigureAwait(false)); }
public async Task <IActionResult> Put([FromRoute] string providerId, [FromBody] Provider provider) { if (provider is null) { throw new ArgumentNullException(nameof(provider)); } if (string.IsNullOrWhiteSpace(providerId)) { return(ErrorResult .BadRequest($"The identifier '{providerId}' provided in the url path is invalid. Must be a valid provider ID.", ResultErrorCode.ValidationError) .ActionResult()); } var validation = new ProviderValidator().Validate(provider); if (!validation.IsValid) { return(ErrorResult .BadRequest(validation) .ActionResult()); } if (!providerId.Equals(provider.Id, StringComparison.OrdinalIgnoreCase)) { return(ErrorResult .BadRequest(new ValidationError { Field = "id", Message = $"Provider's id does match the identifier provided in the path." }) .ActionResult()); } var oldProvider = await providersRepository .GetAsync(provider.Id) .ConfigureAwait(false); if (oldProvider is null) { return(ErrorResult .NotFound($"A Provider with the ID '{provider.Id}' could not be found on this TeamCloud Instance.") .ActionResult()); } var currentUserForCommand = await userService .CurrentUserAsync() .ConfigureAwait(false); oldProvider.PopulateFromExternalModel(provider); var command = new OrchestratorProviderUpdateCommand(currentUserForCommand, oldProvider); return(await orchestrator .InvokeAndReturnAccepted(command) .ConfigureAwait(false)); }
public async Task <IActionResult> Put([FromBody] Dictionary <string, string> tags) { if (!ProjectId.HasValue) { return(ErrorResult .BadRequest($"Project Id provided in the url path is invalid. Must be a valid GUID.", ResultErrorCode.ValidationError) .ActionResult()); } var tag = tags.FirstOrDefault(); if (tag.Key is null) { return(ErrorResult .BadRequest() .ActionResult()); } var project = await projectsRepository .GetAsync(ProjectId.Value) .ConfigureAwait(false); if (project is null) { return(ErrorResult .NotFound($"A Project with the ID '{ProjectId.Value}' could not be found in this TeamCloud Instance.") .ActionResult()); } if (!project.Tags.ContainsKey(tag.Key)) { return(ErrorResult .NotFound($"A Tag with the key '{tag.Key}' could not be found in this Project.") .ActionResult()); } // TODO: return(new OkResult()); // var command = new ProjectUserUpdateCommand(CurrentUser, user, ProjectId.Value); // var commandResult = await orchestrator // .InvokeAsync(command) // .ConfigureAwait(false); // if (commandResult.Links.TryGetValue("status", out var statusUrl)) // return StatusResult // .Accepted(commandResult.CommandId.ToString(), statusUrl, commandResult.RuntimeStatus.ToString(), commandResult.CustomStatus) // .ActionResult(); throw new Exception("This shoudn't happen, but we need to decide to do when it does."); }