Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
258 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...