Ejemplo n.º 1
0
        public static ResourceAlertDTO ConvertToDTO(ResourceAlert resource)
        {
            var resourceAlertDTO = new ResourceAlertDTO()
            {
                alertId              = Guid.NewGuid().ToString(),
                alertStatus          = "Resolved",
                currentHealthStatus  = "Available",
                previousHealthStatus = "null",
                eventTimestamp       = resource.eventTime,
                resourceId           = resource.data.resourceUri,
                id             = Guid.NewGuid().ToString(),
                resourceType   = resource.data.resourceType,
                subscriptionId = resource.data.subscriptionId
            };

            return(resourceAlertDTO);
        }
        public static void Run([QueueTrigger("activity-alert-queue", Connection = "AzureWebJobsStorage")] string myQueueItem, ILogger log)
        {
            var resourceHealthAlert = JsonConvert.DeserializeObject <ResourceAlert>(myQueueItem);

            var validResourceTypes = new List <string> {
                "microsoft.compute/virtualmachines", "microsoft.storage/storageaccounts",
                "microsoft.network/connections", "microsoft.network/expressroutecircuits", "microsoft.network/loadbalancers", "microsoft.network/virtualnetworkgateways"
            };
            string operationName = resourceHealthAlert.data.operationName.ToLower();
            bool   contains      = validResourceTypes.Contains(operationName.Replace("/write", "").Replace("/delete", ""), StringComparer.OrdinalIgnoreCase);

            if (contains)
            {
                //log.LogInformation($"Processing operation: " + operationName);
                var          collectionId = GetEnvironmentVariable("CosmosDb_Collection");
                var          databaseId   = GetEnvironmentVariable("CosmosDb_Database");
                CosmosClient client       = new CosmosClient(GetEnvironmentVariable("CosmosDb_Uri"), GetEnvironmentVariable("CosmosDb_Key"));

                var resourceAlertObj = AlertHelper.ConvertToDTO(resourceHealthAlert);

                if (operationName.Contains("write"))
                {
                    //log.LogInformation($"Creating record in cosmos db....");
                    //log.LogInformation($"PartitionKey: " + resourceAlertObj.resourceId);

                    resourceHealthAlert.data.location = ""; //TODO - ADD RESOURCE LOCATION HERE

                    ItemResponse <ResourceAlertDTO> response = client.GetContainer(databaseId, collectionId).CreateItemAsync(resourceAlertObj, new PartitionKey(resourceAlertObj.resourceId)).Result;
                }
                if (operationName.Contains("delete"))
                {
                    //log.LogInformation($"Deleting record from cosmos db....");
                    ResourceAlertDTO item = client.GetContainer(databaseId, collectionId).GetItemLinqQueryable <ResourceAlertDTO>(true).Where(b => b.resourceId == resourceAlertObj.resourceId).AsEnumerable().FirstOrDefault();

                    //log.LogInformation($"Item Id: " + item.id);
                    //log.LogInformation($"PartitionKey: " + resourceAlertObj.resourceId);

                    ItemResponse <ResourceAlertDTO> deletedItem = client.GetContainer(databaseId, collectionId).DeleteItemAsync <ResourceAlertDTO>(item.id, new PartitionKey(item.resourceId)).Result;
                }
            }

            //log.LogInformation($"Resource Health Alert Processed");
        }