Exemple #1
0
        public async Task <HttpResponseMessage> SendMessageAsync(SlackPayload payload)
        {
            var serializedPayload = JsonConvert.SerializeObject(payload);

            Console.WriteLine(serializedPayload);
            var response = await _httpClient.PostAsync(_webhookUrl,
                                                       new StringContent(serializedPayload, Encoding.UTF8, "application/json"));

            return(response);
        }
        public static void Main(string[] args)
        {
            var credentialsPath = "./cred.azure";
            var configPath      = "./base.config.azure";

            if (args.Count() == 2)
            {
                credentialsPath = args[0];
                configPath      = args[1];
            }
            AzureCredentials credentials = AzureCredentials.FromFile(credentialsPath);

            Console.WriteLine("Starting to verify machine state");
            var azureList = Azure.Authenticate(credentials).Subscriptions.List();

            azureList.LoadAll();
            try
            {
                config = JsonConvert.DeserializeObject <ShutdownConfig>(File.ReadAllText(configPath));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            SlackPayload payload = new SlackPayload();

            foreach (var subscription in config.Subscriptions)
            {
                subscriptionInfo.GetOrAdd(subscription.SubscriptionId, subscription);
            }

            foreach (var subscription in config.Subscriptions)
            {
                AssertVms(credentials, subscription.SubscriptionId);
                if (MachinesStarted.Count > 0 || MachinesStopped.Count > 0)
                {
                    payload.Attachments.Add(ConfigureSlackMessage(azureList.Single(s => s.SubscriptionId == subscription.SubscriptionId).DisplayName, config.Simulate));
                }
                MachinesStarted    = new ConcurrentBag <string>();
                MachinesStopped    = new ConcurrentBag <string>();
                MachinesWithTag    = new ConcurrentBag <string>();
                MachinesWithoutTag = new ConcurrentBag <string>();
            }
            if (payload.Attachments.Count > 0 && config.SendSlackInfo)
            {
                SlackClient client   = new SlackClient(new Uri(config.SlackUrl));
                var         response = client.SendMessageAsync(payload).Result;
                Console.WriteLine(JsonConvert.SerializeObject(response));
            }
        }