Ejemplo n.º 1
0
        public async Task <ActionResult <Instance> > Post(string appId, [FromBody] Instance instance)
        {
            (Application appInfo, ActionResult appInfoError) = await GetApplicationOrErrorAsync(appId);

            int instanceOwnerPartyId = int.Parse(instance.InstanceOwner.PartyId);

            if (appInfoError != null)
            {
                return(appInfoError);
            }

            if (string.IsNullOrWhiteSpace(instance.InstanceOwner.PartyId))
            {
                return(BadRequest("Cannot create an instance without an instanceOwner.PartyId."));
            }

            // Checking that user is authorized to instantiate.
            XacmlJsonRequestRoot request  = DecisionHelper.CreateDecisionRequest(appInfo.Org, appInfo.Id.Split('/')[1], HttpContext.User, "instantiate", instanceOwnerPartyId, null);
            XacmlJsonResponse    response = await _pdp.GetDecisionForRequest(request);

            if (response?.Response == null)
            {
                _logger.LogInformation($"// Instances Controller // Authorization of instantiation failed with request: {JsonConvert.SerializeObject(request)}.");
                return(Forbid());
            }

            bool authorized = DecisionHelper.ValidatePdpDecision(response.Response, HttpContext.User);

            if (!authorized)
            {
                return(Forbid());
            }

            Instance storedInstance = new Instance();

            try
            {
                DateTime creationTime = DateTime.UtcNow;
                string   userId       = GetUserId();

                Instance instanceToCreate = CreateInstanceFromTemplate(appInfo, instance, creationTime, userId);
                storedInstance = await _instanceRepository.Create(instanceToCreate);
                await DispatchEvent(InstanceEventType.Created, storedInstance);

                _logger.LogInformation($"Created instance: {storedInstance.Id}");
                storedInstance.SetPlatformSelflink(_storageBaseAndHost);

                return(Created(storedInstance.SelfLinks.Platform, storedInstance));
            }
            catch (Exception storageException)
            {
                _logger.LogError($"Unable to create {appId} instance for {instance.InstanceOwner.PartyId} due to {storageException}");

                // compensating action - delete instance
                await _instanceRepository.Delete(storedInstance);

                _logger.LogError($"Deleted instance {storedInstance.Id}");
                return(StatusCode(500, $"Unable to create {appId} instance for {instance.InstanceOwner.PartyId} due to {storageException.Message}"));
            }
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> Post(string appId, [FromBody] Instance instance)
        {
            // check if metadata exists
            Application appInfo = GetApplicationOrError(appId, out ActionResult appInfoError);

            if (appInfoError != null)
            {
                return(appInfoError);
            }

            if (string.IsNullOrWhiteSpace(instance.InstanceOwner.PartyId))
            {
                return(BadRequest("Cannot create an instance without an instanceOwner.PartyId."));
            }

            Instance storedInstance = new Instance();

            try
            {
                DateTime creationTime = DateTime.UtcNow;
                string   userId       = GetUserId();

                Instance instanceToCreate = CreateInstanceFromTemplate(appInfo, instance, creationTime, userId);
                storedInstance = await _instanceRepository.Create(instanceToCreate);
                await DispatchEvent(InstanceEventType.Created.ToString(), storedInstance);

                _logger.LogInformation($"Created instance: {storedInstance.Id}");

                AddSelfLinks(Request, storedInstance);

                return(Created(storedInstance.SelfLinks.Platform, storedInstance));
            }
            catch (Exception storageException)
            {
                _logger.LogError($"Unable to create {appId} instance for {instance.InstanceOwner.PartyId} due to {storageException}");

                // compensating action - delete instance
                await _instanceRepository.Delete(storedInstance);

                _logger.LogError($"Deleted instance {storedInstance.Id}");
                return(StatusCode(500, $"Unable to create {appId} instance for {instance.InstanceOwner.PartyId} due to {storageException.Message}"));
            }
        }
Ejemplo n.º 3
0
        public async Task <ActionResult> Delete(Guid instanceGuid, int instanceOwnerId, bool?hard)
        {
            string instanceId = $"{instanceOwnerId}/{instanceGuid}";

            Instance instance;

            try
            {
                instance = await _instanceRepository.GetOne(instanceId, instanceOwnerId);
            }
            catch (DocumentClientException dce)
            {
                if (dce.Error.Code.Equals("NotFound"))
                {
                    return(NotFound($"Didn't find the object that should be deleted with instanceId={instanceId}"));
                }

                return(StatusCode(500, $"Unknown database exception in delete: {dce}"));
            }
            catch (Exception e)
            {
                return(StatusCode(500, $"Unknown exception in delete: {e}"));
            }

            if (hard.HasValue && hard == true)
            {
                try
                {
                    await _instanceRepository.Delete(instance);

                    return(Ok(true));
                }
                catch (Exception e)
                {
                    return(StatusCode(500, $"Unknown exception in delete: {e}"));
                }
            }
            else
            {
                instance.InstanceState.IsDeleted = true;
                instance.LastChangedBy           = User.Identity.Name;
                instance.LastChangedDateTime     = DateTime.UtcNow;

                try
                {
                    Instance softDeletedInstance = await _instanceRepository.Update(instance);

                    return(Ok(softDeletedInstance));
                }
                catch (Exception e)
                {
                    return(StatusCode(500, $"Unknown exception in delete: {e}"));
                }
            }
        }
Ejemplo n.º 4
0
        public async Task <ActionResult> Delete(Instance model)
        {
            var res = await _repository.Delete(model.Id);

            if (res.Success)
            {
                return(RedirectToAction("Index"));
            }
            ModelState.AddModelError("", res.Message);
            return(View(model));
        }
Ejemplo n.º 5
0
 public static void Delete(Instance obj)
 {
     repository.Delete(obj);
 }
Ejemplo n.º 6
0
        public async Task <ActionResult> Post(string appId, int?instanceOwnerId)
        {
            // check if metadata exists
            Application appInfo = GetApplicationOrError(appId, out ActionResult appInfoError);

            if (appInfoError != null)
            {
                return(appInfoError);
            }

            List <Part> parts = ReadAndCheckContent(Request, appInfo, out ActionResult contentError);

            if (contentError != null)
            {
                return(contentError);
            }

            // extract instance template. it should, if it exists, be first part in list
            Instance instanceTemplate = await ExtractInstanceTemplateFromParts(parts);

            // get instanceOwnerId from one out of three possible places
            int ownerId = GetOrLookupInstanceOwnerId(instanceOwnerId, instanceTemplate, out ActionResult instanceOwnerIdError);

            if (instanceOwnerIdError != null)
            {
                return(instanceOwnerIdError);
            }

            if (instanceTemplate == null)
            {
                instanceTemplate = new Instance();
            }

            Instance storedInstance = null;

            try
            {
                DateTime creationTime = DateTime.UtcNow;
                string   userId       = null;

                Instance instanceToCreate = CreateInstanceFromTemplate(appInfo, instanceTemplate, ownerId, creationTime, userId);
                storedInstance = await _instanceRepository.Create(instanceToCreate);
                await DispatchEvent(InstanceEventType.Created.ToString(), storedInstance);

                logger.LogInformation($"Created instance: {storedInstance.Id}");

                if (parts.Any())
                {
                    storedInstance = await SaveDataElementsAndUpdateInstance(parts, storedInstance, creationTime, userId);
                }

                AddSelfLinks(Request, storedInstance);

                return(Ok(storedInstance));
            }
            catch (Exception storageException)
            {
                logger.LogError($"Unable to create {appId} instance for {ownerId} due to {storageException}");

                // compensating action - delete instance
                await _instanceRepository.Delete(storedInstance);

                logger.LogError($"Deleted instance {storedInstance.Id}");

                return(StatusCode(500, $"Unable to create {appId} instance for {ownerId} due to {storageException.Message}"));
            }
        }