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); } }
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); } } }
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); } }