Beispiel #1
0
        /// <summary>
        /// GetDataFromAzure_AuthAAD function to get data from azure sql using AzureAD Authentication
        /// </summary>
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequestMessage httpRequestMessage, ILogger log)
        {
            log.LogInformation($"GetDataFromAzure_AuthAAD Function:HTTP trigger function processed request at : { DateTime.Now}");
            DataSet publishMetadta = new DataSet();
            string  errormessage   = string.Empty;

            try
            {
                //get the content from httpRequestMessage
                var jsonContent = await httpRequestMessage.Content.ReadAsStringAsync();

                //Get the service bus message values from httpRequestMessage
                MessageModel messageModel = JsonConvert.DeserializeObject <MessageModel>(jsonContent);
                //Get the Configuration values from Function app Configuration
                ConfigurationModel configurationModel = new ConfigurationModel();
                //Validating message values from httprequestMessage
                if (ModelValidation.ValidateMessageModel(messageModel, ref errormessage))
                {
                    log.LogInformation($"Service bus Message values returned from the HttpRequestMessage body: {messageModel}");
                    log.LogInformation($"r_object_id:{messageModel.r_object_id}");
                    log.LogInformation($"lifecyclestage:{messageModel.lifecyclestage}");
                    var configurationJSON = ConfigurationManager.AppSettings[Constants.CONFIGURATION_SETTINGS];
                    configurationModel = JsonConvert.DeserializeObject <ConfigurationModel>(configurationJSON);
                    //Get the Azure SQL connection string value based on the lifecyclestage
                    if (messageModel.lifecyclestage == Constants.WIP)
                    {
                        configurationModel.connStrAzure = ConfigurationManager.ConnectionStrings[Constants.WIP_SQLCONNECTIONSTRING].ConnectionString;
                    }
                    else if (messageModel.lifecyclestage == Constants.STAGING)
                    {
                        configurationModel.connStrAzure = ConfigurationManager.ConnectionStrings[Constants.STAGING_SQLCONNECTIONSTRING].ConnectionString;
                    }
                    else if (messageModel.lifecyclestage == Constants.ACTIVE)
                    {
                        configurationModel.connStrAzure = ConfigurationManager.ConnectionStrings[Constants.ACTIVE_SQLCONNECTIONSTRING].ConnectionString;
                    }
                    else
                    {
                        log.LogError($"Please provide valid lifecyclestage name: {messageModel.lifecyclestage}");
                        return(new HttpResponseMessage(HttpStatusCode.BadRequest)
                        {
                            Content = new StringContent($"Please provide valid lifecyclestage name : {messageModel.lifecyclestage}", Encoding.UTF8, Constants.JSON)
                        });
                    }

                    //Validating configuration values
                    if (ModelValidation.ValidateConfigurationModel(configurationModel, ref errormessage))
                    {
                        log.LogInformation($"Configuration values returned from,function app configuration app settings: {configurationModel}");
                        //Get access token for Azure SQL
                        var accessToken = await AuthTokenHelper.GetSqlTokenAsync(configurationModel.TenantId, configurationModel.SqlEndPointURI);

                        publishMetadta = GetDataFromAzureSql(configurationModel, messageModel, accessToken, log);
                        if (publishMetadta != null)
                        {
                            log.LogInformation($"Table count: { publishMetadta.Tables.Count}");
                            string publishMetadtaJSON = JsonConvert.SerializeObject(publishMetadta, Formatting.Indented);
                            //return dataset in the response
                            log.LogInformation($"GetDataFromAzure_AuthAAD Function successfully processed.");
                            return(new HttpResponseMessage(HttpStatusCode.OK)
                            {
                                Content = new StringContent(publishMetadtaJSON, Encoding.UTF8, Constants.JSON)
                            });
                        }
                        else
                        {
                            log.LogError($"Content not available in azure sql for the given r_object_id : {messageModel.r_object_id}");
                            return(new HttpResponseMessage(HttpStatusCode.InternalServerError)
                            {
                                Content = new StringContent($"Content not available in azure sql for the given r_object_id : {messageModel.r_object_id}", Encoding.UTF8, Constants.JSON)
                            });
                        }
                    }
                    else
                    {
                        //configuration values has empty or null return badrequest response
                        log.LogError($"{errormessage}");
                        return(new HttpResponseMessage(HttpStatusCode.BadRequest)
                        {
                            Content = new StringContent($"{errormessage}", Encoding.UTF8, Constants.JSON)
                        });
                    }
                }
                else
                {
                    //Message model values has empty or null return badrequest response
                    log.LogError($"{errormessage}");
                    return(new HttpResponseMessage(HttpStatusCode.BadRequest)
                    {
                        Content = new StringContent($"{errormessage}", Encoding.UTF8, Constants.JSON)
                    });
                }
            }
            catch (Exception ex)
            {
                log.LogError($"Exception occurred in GetDataFromAzure_Function,Error : {ex.Message}, Details:{ex.InnerException}");
                publishMetadta = null;
                return(new HttpResponseMessage(HttpStatusCode.InternalServerError)
                {
                    Content = new StringContent(ex.Message, Encoding.UTF8, Constants.JSON)
                });
            }
        }