Esempio n. 1
0
        public static void Run(payload geto365userlicense, TraceWriter log)
        {
            log.Info($"C# HTTP trigger function processed a request. Command used={geto365userlicense.Command}");
            double graphApiVersion    = double.Parse(GenericHelper.GetEnvironmentVariable("graphApiVersion"));
            string clientId           = GenericHelper.GetEnvironmentVariable("clientId");
            string clientSecret       = GenericHelper.GetEnvironmentVariable("clientSecret");
            string tenantId           = GenericHelper.GetEnvironmentVariable("tenantId");
            string allowedChannelName = GenericHelper.GetEnvironmentVariable("allowedChannelName");

            // assign the Slack payload "text" to be the UPN of the user that needs the license
            string username    = geto365userlicense.Text;
            string encUserName = Uri.UnescapeDataString(username);

            log.Info(encUserName);
            // assign the Slack payload "channel_name" to the allowed channel name for this code to be called from
            string channelName = geto365userlicense.Channel_Name;

            if (channelName == allowedChannelName)
            {
                // acquire Bearer Token for AD Application user through Graph API
                string token         = AuthenticationHelperRest.AcquireTokenBySpn(tenantId, clientId, clientSecret);
                string bearerToken   = "Bearer " + token;
                string skuPartNumber =
                    LicensingHelper.GetUserLicenseInfo(graphApiVersion, encUserName, bearerToken, log);

                var uri = Uri.UnescapeDataString(geto365userlicense.Response_Url);
                log.Info("in_channel response");
                var jsonPayload = new
                {
                    response_type = "in_channel",
                    text          = $"{encUserName} is licensed with the {skuPartNumber} license."
                };

                GenericHelper.SendMessageToSlack(uri, jsonPayload);
            }
        }
Esempio n. 2
0
        public static async Task <string> Run(HttpRequestMessage req, TraceWriter log, IAsyncCollector <payload> allocatevisio, IAsyncCollector <payload> allocatee3o365, IAsyncCollector <payload> geto365userlicense, IAsyncCollector <payload> deallocatee3o365, ICollector <string> outputDocument)
        {
            log.Info($"C# HTTP trigger function processed a request. Command used={req.RequestUri}");

            string jsonContent = await req.Content.ReadAsStringAsync();

            log.Info(jsonContent);
            string allowedChannelName = GenericHelper.GetEnvironmentVariable("allowedChannelName");
            string res = null;

            // assign the Slack payload "channel_name" to the allowed channel name for this code to be called from
            string  channelName = (jsonContent.Split('&')[4]).Split('=')[1];
            payload json        = new payload
            {
                Token        = (jsonContent.Split('&')[0]).Split('=')[1],
                Team_Id      = (jsonContent.Split('&')[1]).Split('=')[1],
                Team_Domain  = (jsonContent.Split('&')[2]).Split('=')[1],
                Channel_Id   = (jsonContent.Split('&')[3]).Split('=')[1],
                Channel_Name = (jsonContent.Split('&')[4]).Split('=')[1],
                User_Id      = (jsonContent.Split('&')[5]).Split('=')[1],
                User_Name    = (jsonContent.Split('&')[6]).Split('=')[1],
                Command      = (jsonContent.Split('&')[7]).Split('=')[1],
                Text         = (jsonContent.Split('&')[8]).Split('=')[1],
                Response_Url = (jsonContent.Split('&')[9]).Split('=')[1]
            };

            if (channelName == allowedChannelName)
            {
                string document = JsonConvert.SerializeObject(json);
                outputDocument.Add(document);

                string command = Uri.EscapeDataString(json.Command);

                switch (command)
                {
                case "%252Fallocatee3o365":
                    // add to allocateE3O365 queue
                    await allocatee3o365.AddAsync(json);

                    break;

                case "%252Fdeallocatee3o365":
                    // add to deallocatee3o365 queue
                    await deallocatee3o365.AddAsync(json);

                    break;

                case "%252Fgeto365userlicense":
                    // add to geto365userlicense queue
                    await geto365userlicense.AddAsync(json);

                    break;

                case "%252Fallocatevisio":
                    // add to allocatevisio queue
                    await allocatevisio.AddAsync(json);

                    break;
                }

                res = $"Hey, {json.User_Name}, I'm working on assigning the license. I'll let you know when I'm done...";
            }
            else
            {
                res = $"Hey, {json.User_Name}, you're calling the command from a channel that is not enabled...";
            }
            return(res);
        }
Esempio n. 3
0
        public static void Run(payload deallocatee3o365, TraceWriter log)
        {
            log.Info($"C# HTTP trigger function processed a request. Command used={deallocatee3o365.Command}");
            double graphApiVersion    = double.Parse(GenericHelper.GetEnvironmentVariable("graphApiVersion"));
            string clientId           = GenericHelper.GetEnvironmentVariable("clientId");
            string clientSecret       = GenericHelper.GetEnvironmentVariable("clientSecret");
            string tenantId           = GenericHelper.GetEnvironmentVariable("tenantId");
            string allowedChannelName = GenericHelper.GetEnvironmentVariable("allowedChannelName");

            // assign the Slack payload "text" to be the UPN of the user that needs the license
            string username = deallocatee3o365.Text;

            log.Info(username);

            // assign the Slack payload "channel_name" to the allowed channel name for this code to be called from
            string channelName = deallocatee3o365.Channel_Name;

            if (channelName == allowedChannelName && deallocatee3o365.User_Name == "david")
            {
                // acquire Bearer Token for AD Application user through Graph API
                string token       = AuthenticationHelperRest.AcquireTokenBySpn(tenantId, clientId, clientSecret);
                string bearerToken = "Bearer " + token;

                log.Info("Getting License SKUs...");
                // get information about all the O365 SKUs available
                JArray  skus        = LicensingHelper.GetO365Skus(graphApiVersion, bearerToken);
                JObject e1SkuObject = SubscriptionHelper.FilterSkusByPartNumber(skus, "STANDARDPACK");
                JObject e3SkuObject = SubscriptionHelper.FilterSkusByPartNumber(skus, "ENTERPRISEPACK");
                string  e1SkuId     = (string)e1SkuObject["skuId"];
                string  e3SkuId     = (string)e3SkuObject["skuId"];

                int usedLicenses      = e1SkuObject.GetValue("consumedUnits").Value <int>();
                int purchasedLicenses = e1SkuObject.SelectToken(@"prepaidUnits.enabled").Value <int>();

                // check if enough licenses available
                if (usedLicenses < purchasedLicenses)
                {
                    // enough licenses, so do it
                    log.Info("Setting License...");
                    string returnedUserName =
                        LicensingHelper.SetO365LicensingInfo(graphApiVersion, bearerToken, username, e1SkuId,
                                                             e3SkuId);

                    var uri         = Uri.UnescapeDataString(deallocatee3o365.Response_Url);
                    var jsonPayload = new
                    {
                        response_type = "in_channel",
                        text          =
                            $"There are {purchasedLicenses} available E1 licenses and {usedLicenses} already used. You have just used one more." +
                            $"Successfully assigned *E1* license to {returnedUserName}"
                    };

                    GenericHelper.SendMessageToSlack(uri, jsonPayload);
                }
                else
                {
                    // not enough licenses, notify user
                    log.Info(
                        $"There are {purchasedLicenses} available E1 licenses and {usedLicenses} already used. No licenses available for E1. Please log on to portal.office.com and buy new licenses.");

                    var uri         = Uri.UnescapeDataString(deallocatee3o365.Response_Url);
                    var jsonPayload = new
                    {
                        response_type = "in_channel",
                        text          =
                            $"There are {purchasedLicenses} available E1 licenses and {usedLicenses} already used. No licenses available for E1. Please log on to portal.office.com and buy new licenses."
                    };

                    GenericHelper.SendMessageToSlack(uri, jsonPayload);
                }
            }
        }
Esempio n. 4
0
        public static void Run(payload allocatevisio, TraceWriter log)
        {
            log.Info($"C# Queue trigger function processed: {allocatevisio.User_Name}");
            double graphApiVersion = double.Parse(GenericHelper.GetEnvironmentVariable("graphApiVersion"));
            string clientId        = GenericHelper.GetEnvironmentVariable("clientId");
            string clientSecret    = GenericHelper.GetEnvironmentVariable("clientSecret");
            string tenantId        = GenericHelper.GetEnvironmentVariable("tenantId");

            // assign the Slack payload "text" to be the UPN of the user that needs the license
            string username = allocatevisio.Text;

            log.Info(username);

            // acquire Bearer Token for AD Application user through Graph API
            string token       = AuthenticationHelperRest.AcquireTokenBySpn(tenantId, clientId, clientSecret);
            string bearerToken = "Bearer " + token;

            log.Info("Getting License SKUs...");
            // get information about all the O365 SKUs available
            JArray  skus           = LicensingHelper.GetO365Skus(graphApiVersion, bearerToken);
            JObject visioSkuObject = SubscriptionHelper.FilterSkusByPartNumber(skus, "VISIOCLIENT");

            string visioSkuId = (string)visioSkuObject["skuId"];

            int usedLicenses      = visioSkuObject.GetValue("consumedUnits").Value <int>();
            int purchasedLicenses = visioSkuObject.SelectToken(@"prepaidUnits.enabled").Value <int>();

            // check if enough licenses available
            if (usedLicenses < purchasedLicenses)
            {
                // enough licenses, so do it
                log.Info("Setting License...");
                string returnedUserName =
                    LicensingHelper.SetO365LicensingInfo(graphApiVersion, bearerToken, username, visioSkuId, "");

                var uri = Uri.UnescapeDataString(allocatevisio.Response_Url);

                var jsonPayload = new
                {
                    response_type = "in_channel",
                    text          =
                        $"There are {purchasedLicenses} available VISIO licenses and {usedLicenses} already used. You have just used one more. Successfully assigned *VISIO* license to {returnedUserName}"
                };

                GenericHelper.SendMessageToSlack(uri, jsonPayload);
            }
            else
            {
                // not enough licenses, notify user
                log.Info(
                    $"There are {purchasedLicenses} available VISIO licenses and {usedLicenses} already used. No licenses available for VISIO. Please log on to portal.office.com and buy new licenses.");
                var uri = Uri.UnescapeDataString(allocatevisio.Response_Url);

                log.Info(uri);
                var jsonPayload = new
                {
                    response_type = "in_channel",
                    text          =
                        $"There are {purchasedLicenses} available VISIO licenses and {usedLicenses} already used. No licenses available for VISIO. Please log on to portal.office.com and buy new licenses."
                };

                GenericHelper.SendMessageToSlack(uri, jsonPayload);
            }
        }