Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Enable/Disable Azure Function programmatically

Is there a way to programmatically enable/disable an Azure function?

I can enable/disable a function using the portal under the "Manage" section, which causes a request to be sent to https://<myfunctionapp>.scm.azurewebsites.net/api/functions/<myfunction>

The JSON payload looks a bit like:

{
   "name":"SystemEventFunction",
   "config":{
      "disabled":true,
      "bindings":[ 
         // the bindings for this function
       ]
   }
  // lots of other properties (mostly URIs)
}

I'm creating a management tool outside of the portal that will allow users to enable and disable functions.

Hoping I can avoid creating the JSON payload by hand, so I'm wondering if there is something in an SDK (WebJobs??) that has this functionality.

like image 967
Cloud SME Avatar asked Feb 22 '17 19:02

Cloud SME


People also ask

How do I enable and disable Azure function programmatically?

The recommended way to disable a function is with an app setting in the format AzureWebJobs. <FUNCTION_NAME>. Disabled set to true . You can create and modify this application setting in a number of ways, including by using the Azure CLI and from your function's Overview tab in the Azure portal.

How do I enable Azure function app?

Sign in to the Azure portal, then search for and select Function App. Select the function you want to verify. In the left navigation under Functions, select App keys.

How do I manually trigger Azure function?

Navigate to your function app in the Azure portal, select App Keys, and then the _master key. In the Edit key section, copy the key value to your clipboard, and then select OK. After copying the _master key, select Code + Test, and then select Logs.


1 Answers

Further to @James Z.'s answer, I've created the following class in C# that allows you to programmatically disable / enable an Azure function.

The functionsSiteRoot constructor argument is the Kudu root of your Functions application, eg https://your-functions-web-app.scm.azurewebsites.net/api/vfs/site/wwwroot/

The username and password can be obtained from "Get publish profile" in the App Service settings for your Functions.

public class FunctionsHelper : IFunctionsHelper
{
    private readonly string _username;
    private readonly string _password;
    private readonly string _functionsSiteRoot;
    private WebClient _webClient;

    public FunctionsHelper(string username, string password, string functionsSiteRoot)
    {
        _username = username;
        _password = password;
        _functionsSiteRoot = functionsSiteRoot;

        _webClient = new WebClient
        {
            Headers = { ["ContentType"] = "application/json" },
            Credentials = new NetworkCredential(username, password),
            BaseAddress = functionsSiteRoot
        };
    }

    public void StopFunction(string functionName)
    {
        SetFunctionState(functionName, isDisabled: true);
    }

    public void StartFunction(string functionName)
    {
        SetFunctionState(functionName, isDisabled: false);
    }

    private void SetFunctionState(string functionName, bool isDisabled)
    {
        var functionJson =
            JsonConvert.DeserializeObject<FunctionSettings>(_webClient.DownloadString(GetFunctionJsonUrl(functionName)));
        functionJson.disabled = isDisabled;
        _webClient.Headers["If-Match"] = "*";
        _webClient.UploadString(GetFunctionJsonUrl(functionName), "PUT", JsonConvert.SerializeObject(functionJson));
    }

    private static string GetFunctionJsonUrl(string functionName)
    {
        return $"{functionName}/function.json";
    }
}

internal class FunctionSettings
{
    public bool disabled { get; set; }
    public List<Binding> bindings { get; set; }
}

internal class Binding
{
    public string name { get; set; }
    public string type { get; set; }
    public string direction { get; set; }
    public string queueName { get; set; }
    public string connection { get; set; }
    public string accessRights { get; set; }
}
like image 197
DavidGouge Avatar answered Nov 15 '22 08:11

DavidGouge