Esempio n. 1
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            ExecutionContext context)
        {
            ILogger            log    = null;
            IConfigurationRoot root   = null;
            LocalConfig        config = new LocalConfig();

            try
            {
                if (File.Exists(String.Format($"{context.FunctionAppDirectory}/secrets.json")))
                {
                    //secrets.json exists use it and environment variables
                    var builder = new ConfigurationBuilder()
                                  .SetBasePath(context.FunctionAppDirectory)
                                  .AddJsonFile("secrets.json", optional: true, reloadOnChange: true)
                                  .AddEnvironmentVariables("FUNC_");


                    root = builder.Build();
                    ConfigurationBinder.Bind(root, config);
                }
                else if (File.Exists(String.Format("{0}/{1}", context.FunctionAppDirectory, "local.settings.json")))
                {
                    //use for local testing...do not use in production
                    //remember to add the storage connection string
                    var builder = new ConfigurationBuilder()
                                  .SetBasePath(context.FunctionAppDirectory)
                                  .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                                  .AddEnvironmentVariables("FUNC_");

                    root = builder.Build();
                    ConfigurationBinder.Bind(root, config);

                    config.StorageConnectionString = root.GetConnectionString("StorageConnectionString");
                }
                else
                {
                    //no secrets or local.settings.json files...use only environment variables
                    var builder = new ConfigurationBuilder()
                                  .AddEnvironmentVariables("FUNC_");

                    root = builder.Build();
                    ConfigurationBinder.Bind(root, config);
                }
            }
            catch (Exception ex)
            {
                log?.LogError(ex.Message);
            }

            string       requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            DeviceConfig dconfig     = JsonConvert.DeserializeObject <DeviceConfig>(requestBody);

            dconfig = ConfigurationResolver.Configure(dconfig, config);

            try
            {
                string           template   = dconfig.GetTemplate();
                string           funcType   = req.Query["type"];
                DeviceDeployment deployment = new DeviceDeployment(dconfig.IoTHubConnectionString, template);

                if (string.IsNullOrEmpty(funcType))
                {
                    string connectionString = await DeployFull(config, dconfig, deployment);

                    return(!string.IsNullOrEmpty(connectionString) ? (ActionResult) new OkObjectResult(connectionString) : new BadRequestObjectResult("Invalid connection string to return."));
                }
                else if (funcType.ToLowerInvariant() == "update")
                {
                    string luss = await UpdateTableAsync(config, dconfig);

                    Device device = await deployment.GetDevice(dconfig.DeviceId);
                    await UpdateModuleAsync(device, dconfig.Container.ModuleId, luss, config.ServiceUrl, deployment);

                    return(new OkResult());
                }
                else
                {
                    return(new BadRequestObjectResult("Invalid type parameter in query string."));
                }
            }
            catch (Exception ex)
            {
                log?.LogError(ex.Message);
                return(new BadRequestObjectResult("Failed to provision."));
            }
        }