Dynamically set schedule in Azure Function - json

I have following function.json for my Azure function whose schedule is set to run at 9.30 daily. What I want is to dynamically set the schedule attribute of this json. This scenario arises when a client who is using my application enters date, on that date-scheduler should run.
{
"bindings": [
{
"name": "myTimer",
"type": "timerTrigger",
"direction": "in",
"schedule": "0 30 9 * * *" //Want to set dynamically
}
],
"disabled": false
}
Is this possible?
(Also note, I don't want to use Azure Scheduler due to budgeting reasons)

You could modify your function.json to fetch the cron expression from app settings.
"schedule": "%TriggerSchedule%"
Define TriggerSchedule in your appsettings. You could modify your appsettings dynamically and the function trigger would align to it.

Use Kudu API to change function.json
https://github.com/projectkudu/kudu/wiki/REST-API
PUT https://{functionAppName}.scm.azurewebsites.net/api/vfs/{pathToFunction.json},
Headers: If-Match: "*",
Body: new function.json content
Then send request to apply changes
POST https://{functionAppName}.scm.azurewebsites.net/api/functions/synctriggers
Or you can use Queue Trigger with "initialVisibilityDelay" messages. In this case you need to write your own code to implement a scheduler.

Its an old question but still relevant. I recently came across a similar issue. Azure function has a built in functionality that you can use. Its called Eternal orchestrations in Durable Functions (Azure Functions).
You can do something like
[FunctionName("Periodic_Cleanup_Loop")]
public static async Task Run([OrchestrationTrigger] IDurableOrchestrationContext
context)
{
await context.CallActivityAsync("DoCleanup", null);
// sleep for one hour between cleanups
DateTime nextCleanup = context.CurrentUtcDateTime.AddHours(1);
await context.CreateTimer(nextCleanup, CancellationToken.None);
context.ContinueAsNew(null);
}
More lnfo can be found at https://learn.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-eternal-orchestrations?tabs=csharp

Related

How to modify json payload request and add new json parameter with Charles proxy

Could anyone advise if there is any way to modify the existing json payload request while adding another parameter to the existing request. I have tried using breakpoints but having trouble due to request timeout occurs.
for example the existing payload
{
"Type": "Games",
"System": "Playstation"
}
and I would like to modify while adding a new parameter
{
"Type": "Games",
"System": "Playstation",
"Name": "Call of duty"
}
Would it be possible to use rewrite or any available workarounds with Charles?
Thanks.
You can do this fairly easily with the 'Rewrite' tool/feature.
This article will give you a more detail about modifying elements of Requests/Response bodies if you are interested
It might not a direct question, but you can achieve it with simple JS code from Scripting Tool
JS Code:
function onResponse(context, url, request, response) {
// Update status Code
response.statusCode = 500;
// Done
return response;
}
Disclaimer: I'm a creator of Proxyman.

Microsoft Graph does not retrieve the Group's Plan Id

After creating a Group using POST /v1.0/groups with the body:
{
"description": "hello",
"displayName": "group_for_restore",
"groupTypes": [
"Unified"
],
"mailEnabled": true,
"mailNickname": "group_for_restore",
"securityEnabled": false,
"visibility": "Public"
}
A request to GET /v1.0/groups/{group-id}/planner/plans does not retrieve any plans.
As far as I know, after creating a Group, a Plan will be created too. On the web interface you can see that this plan is correctly created and shown, but it does not appear in JSON response:
{
"#odata.context": "https://graph.microsoft.com/v1.0/$metadata#Collection(microsoft.graph.plannerPlan)",
"#odata.count": 0,
"value": []
}
After clicking on it in the web interface you can easily get the plan using the request above.
Is it ok to do such magic steps to fetch plan id?
The Web App is provisioning the Group's Plan the first time you attempt to access it. When creating a Group through the API however, you'll need to create a new Plan yourself:
POST https://graph.microsoft.com/v1.0/planner/plans
Content-type: application/json
{
"owner": "group-id",
"title": "title-value"
}
Keep in mind this important note from the documentation:
When creating a new plan, make a group its owner by simply setting the owner property on a plan object.

Read values from local.settings.json in VS 2017 Azure Function development

I am writing an Azure function in VS 2017. I need to set up a few custom configuration parameters. I added them in local.settings.json under Values.
{
"IsEncrypted":false,
"Values" : {
"CustomUrl" : "www.google.com",
"Keys": {
"Value1":"1",
"Value2" :"2"
}
}
}
Now, ConfigurationManager.AppSettings["CustomUrl"] returns null.
I'm using:
.NET Framework 4.7
Microsoft.NET.Sdk.Functions 1.0.5
System.Configuration.ConfigurationManager 4.4.0
Azure.Functions.Cli 1.0.4
Am I missing something?
Environment.GetEnvironmentVariable("key")
I was able to read values from local.settings.json using the above line of code.
Firstly, I create a sample and do a test with your local.settings.json data, as Mikhail and ahmelsayed said, it works fine.
Besides, as far as I know, Values collection is expected to be a Dictionary, if it contains any non-string values, it can cause Azure function can not read values from local.settings.json.
My Test:
ConfigurationManager.AppSettings["CustomUrl"] returns null with the following local.settings.json.
{
"IsEncrypted": false,
"Values": {
"CustomUrl": "www.google.com",
"testkey": {
"name": "kname1",
"value": "kval1"
}
}
}
If you are using TimeTrigger based Azure function than you can access your key (created in local.settings.json) from Azure Function as below.
[FunctionName("BackupTableStorageFunction")]
public static void Run([TimerTrigger("%BackUpTableStorageTriggerTime%")]TimerInfo myTimer, TraceWriter log, CancellationToken cancellationToken)
Using .Net 6 (and probably some earlier versions) it is possible to inject IConfiguration into the constructor of the function.
public Function1(IConfiguration configuration)
{
string setting = _configuration.GetValue<string>("MySetting");
}
MySetting must be in the Values section of local.settings.json:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
"MySetting": "value"
}
}
It works with Application settings in Azure Function App as well.
Azure function copies the binaries to the bin folder and runs using the azure function cli, so it searches for the local.settings.json, so make sure you have set the "Copy to Output Directory" to "Copy Always"
Hey you mmight be able to read the properties while debugging, but once you go and try to deploy that in azure, those properties are not going to work anymore. Azure functions does not allow nested properties, you must use all of them inline in the "Values" option or in "ConnectionStrings".
Look at this documentation as reference:
https://learn.microsoft.com/en-us/azure/azure-functions/functions-how-to-use-azure-function-app-settings
var value = Environment.GetEnvironmentVariable("key", EnvironmentVariableTarget.Process); should be the more appropriate answer, though EnvironmentVariableTarget.Process is the default value but it's more meaningful here.
Look at its EnvironmentVariableTarget declaration.
//
// Summary:
// Specifies the location where an environment variable is stored or retrieved in
// a set or get operation.
public enum EnvironmentVariableTarget
{
//
// Summary:
// The environment variable is stored or retrieved from the environment block associated
// with the current process.
Process = 0,
//
// Summary:
// The environment variable is stored or retrieved from the HKEY_CURRENT_USER\Environment
// key in the Windows operating system registry. This value should be used on .NET
// implementations running on Windows systems only.
User = 1,
//
// Summary:
// The environment variable is stored or retrieved from the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session
// Manager\Environment key in the Windows operating system registry. This value
// should be used on .NET implementations running on Windows systems only.
Machine = 2
}

Is it possible to integrate Amazon QuickSight dashboard graphs to a web application?

I need to display live interactive graphs based on customer data present in MySQL,for generating the graphs, I am planning to use Amazon Quick Sight but i would like to know whether the generated graphs can be integrated with my web application UI ?
Datasource MYSQL is hosted in AWS.
Any other better design solution is also most welcome :)
I don't think so. Even if you want to share the dashboard to
someone, you need to create a user in QuickSight. Any more than 1
user will be charged by AWS.
The dashboard cannot be public and you need to login to view the
dashboard. If it was public, you could have embedded it in your
webpage as an iframe. But you cannot.
So, I think you are having limited options here, when it comes to
QuickSight.
You can always using D3 or Google Charts to display the data by
exposing REST services for your data in MySQL.
If you have a huge database, you may want to consider indexing the
data to Elasticsearch and perform queries on it.
Check if Kibana + Elasticsearch works out of the box for you.
Good luck!
Update: Dec 28 2018
Amazon announced in Nov 2018, that Amazon QuickSight dashboards can now be embedded in applications. Read more here at this AWS QuickSight Update.
AWS has enabled the embedding of the Dashboards into web apps. The feature was released on 27th Nov 2018. Here are a few helpful links:
1. https://aws.amazon.com/blogs/big-data/embed-interactive-dashboards-in-your-application-with-amazon-quicksight/
2. https://docs.aws.amazon.com/quicksight/latest/user/embedded-dashboards-setup.html
Note: This answer is applicable only if you are using AWS Cognito
In order to generate Quicksight secure dashboard URL, follow the below steps:
Step 1: Create a new Identity Pool. Go to https://console.aws.amazon.com/cognito/home?region=u-east-1 , click ‘Create new Identity Pool’
Give an appropriate name.
Go to the Authentication Providers section, select Cognito.
Give the User Pool ID(your User pool ID) and App Client ID (go to App
Clients in user pool and copy id).
Click ‘Create Pool’. Then click ‘Allow’ to create roles of the
identity pool in IAM.
Step 2: Assign Custom policy to the Identity Pool Role
Create a custom policy with the below JSON.
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "quicksight:RegisterUser",
"Resource": "*",
"Effect": "Allow"
},
{
"Action": "quicksight:GetDashboardEmbedUrl",
"Resource": "*",
"Effect": "Allow"
},
{
"Action": "sts:AssumeRole",
"Resource": "*",
"Effect": "Allow"
}
]
}
Note: if you want to restrict the user to only one dashboard, replace the * with the dashboard ARN name in quicksight:GetDashboardEmbedUrl,
then goto the roles in IAM.
select the IAM role of the Identity pool and assign the custom policy
to the role.
Step 3: Configuration for generating the temporary IAM(STS) user
Login to your application with the user credentials.
For creating temporary IAM user, we use Cognito credentials.
When user logs in, Cognito generates 3 token IDs - IDToken,
AccessToken, RefreshToken. These tokens will be sent to your application server.
For creating a temporary IAM user, we use Cognito Access Token and credentials will look like below.
AWS.config.region = 'us-east-1';
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId:"Identity pool ID",
Logins: {
'cognito-idp.us-east-1.amazonaws.com/UserPoolID': AccessToken
}
});
For generating temporary IAM credentials, we call sts.assume role
method with the below parameters.
var params = {
RoleArn: "Cognito Identity role arn",
RoleSessionName: "Session name"
};
sts.assumeRole(params, function (err, data) {
if (err) console.log( err, err.stack); // an error occurred
else {
console.log(data);
})
You can add additional parameters like duration (in seconds) for the
user.
Now, we will get the AccessKeyId, SecretAccessKey and Session
Token of the temporary user.
Step 4: Register the User in Quicksight
With the help of same Cognito credentials used in Step 3, we will
register the user in quicksight by using the quicksight.registerUser
method with the below parameters
var params = {
AwsAccountId: “account id”,
Email: 'email',
IdentityType: 'IAM' ,
Namespace: 'default',
UserRole: ADMIN | AUTHOR | READER | RESTRICTED_AUTHOR | RESTRICTED_READER,
IamArn: 'Cognito Identity role arn',
SessionName: 'session name given in the assume role creation',
};
quicksight.registerUser(params, function (err, data1) {
if (err) console.log("err register user”); // an error occurred
else {
// console.log("Register User1”);
}
});
Now the user will be registered in quicksight.
Step5: Update AWS configuration with New credentials.
Below code shows how to configure the AWS.config() with new
credentials generated Step 3.
AWS.config.update({
accessKeyId: AccessToken,
secretAccessKey: SecretAccessKey ,
sessionToken: SessionToken,
"region": Region
});
Step6: Generate the EmbedURL for Dashboards:
By using the credentials generated in Step 3, we will call the
quicksight.getDashboardEmbedUrl with the below parameters
var params = {
AwsAccountId: "account ID",
DashboardId: "dashboard Id",
IdentityType: "IAM",
ResetDisabled: true,
SessionLifetimeInMinutes: between 15 to 600 minutes,
UndoRedoDisabled: True | False
}
quicksight.getDashboardEmbedUrl(params,
function (err, data) {
if (!err) {
console.log( data);
} else {
console.log(err);
}
}
);
Now, we will get the embed url for the dashboard.
Call the QuickSightEmbedding.embedDashboard from front end with the
help of the above generated url.
The result will be the dashboard embedded in your application with
filter controls.
I know this is a very late reply, but just in case someone else stumbles across this question... We use periscopedata.com to embed BI dashboards in our SaaS app. All that's needed is knowledge of SQL (to create the charts/dashboards) and enough dev knowledge to call their API endpoint to display the dash in your own app.

How do you cancel a PayPal subscription through their api?

On this page:
https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_api_ECRecurringPayments
it says that it's possible to cancel a PayPal subscription using their API. Knowing the SubscriptionId can anyone give me some code example how to do this?
Many thanks.
Did you manage to find an easy solution ? I'm looking for this as well. Thanks!
Update : After searching, the "ManageRecurringPaymentsProfileStatus" is very easy to use through a simple POST request.
Make sure that your user, password or signature are not visible (in other words, do this on your server and NOT on your client via javascript or html posts).
Below a simple working example in Python. It works and I'm now using it daily.
import urllib
from google.appengine.api import urlfetch
form_fields = {
"METHOD": "ManageRecurringPaymentsProfileStatus",
"PROFILEID": "xxx", # put your subscription ID here
"ACTION": "cancel",
"USER": "xxx", # Get USER, PWD, and SIGNATURE from your Paypal's account preferences
"PWD": "xxx",
"SIGNATURE": "xxx",
"VERSION": "54.0"
}
api_url = 'https://api-3t.sandbox.paypal.com/nvp' # remove the sandbox part for production
form_data = urllib.urlencode(form_fields)
result = urlfetch.fetch(url=api_url,
payload=form_data,
method=urlfetch.POST,
headers={'Content-Type': 'application/x-www-form-urlencoded'})
The response is a string that looks like this:
TIMESTAMP=2011%2d01%2d28T14%3a47%3a45Z&CORRELATIONID=148ebe1d25566&ACK=Failure&VERSION=54%2e0&BUILD=1704252&L_ERRORCODE0=11552&L_SHORTMESSAGE0=Invalid%20profile%20ID&L_LONGMESSAGE0=The%20profile%20ID%20is%20invalid&L_SEVERITYCODE0=Error
The 'ACK' field indicates 'Failure' or 'Success'.
In answer to comments below, note that it DOES enable me to cancel the subscriptions that have been created through a dynamically created link such as :
Subscribe
Note that I do not use the flag 'modify' at all.