2017-02-22 141 views
4

有没有办法以编程方式启用/禁用Azure功能?如何以编程方式启用/禁用Azure功能

我可以启用/使用的“管理”部分,这会导致一个请求要被发送到https://<myfunctionapp>.scm.azurewebsites.net/api/functions/<myfunction>

的JSON有效载荷下门户禁用功能看起来有点像:

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

我在门户之外创建一个管理工具,允许用户启用和禁用功能。

希望我可以避免手动创建JSON负载,所以我想知道是否有一些SDK(WebJobs ??)中有这样的功能。

+0

powershell将是最简单的方法 – 4c74356b41

+0

我能够使用Stop-AzureRmWebApp来停止整个Azure功能应用程序。我只需要暂停一下,所以不需要切换个别功能。 – Svend

回答

4

不,目前不可能。 function.json中的disabled元数据属性决定是否启用某个函数。当您在门户中启用/禁用时,门户只会更新该值。

不知道它是否能满足您的需求,但我会指出,还有一个可用于控制将被加载的函数集的数组(可用于记录here)。例如,如果您只希望启用10个​​函数中的2个,则可以将此属性设置为仅包含这2个函数名称的数组(例如"functions": [ "QueueProcessor", "GitHubWebHook" ]),并且只有那些将被加载/启用。但是,这与启用/禁用稍有不同,因为您将无法通过门户调用排除的功能,而您可以通过门户调用禁用的函数。

+0

[根据官方MS文档](https://docs.microsoft.com/zh-cn/azure/azure-functions/functions-host-json#functions),禁用函数的最佳方法是使用'function.json ',而不是'host.json'配置 – SliverNinja

5

除了@James Z.的回答,我已经在C#中创建了以下类,它允许您以编程方式禁用/启用Azure功能。

的functionsSiteRoot构造函数的参数是您的应用程序功能的捻根,例如https://your-functions-web-app.scm.azurewebsites.net/api/vfs/site/wwwroot/

的用户名和密码可以从获得在App服务设置为你的功能“获得发布配置文件”。

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

这真的有帮助,虽然我不得不做一些改变,第一不得不函数url:返回$“/ home/site/wwwroot/{functionName} /function.json”; 第二个json转换和序列化不起作用,我只需要做一个普通的搜索和替换: funcFile = funcFile.Replace(“\”disabled \“:true”,“\”disabled \“:false”) ; –

+0

禁用的属性需要在序列化json中的绑定之后。否则,函数无法加载。 – Sopuli

2

除了上面的@DavidGouge的回答,他发布的代码确实有效,我只是测试了它,并将其用于我的应用程序。但它需要一些调整:

  1. 从IFunctionsHelper中删除继承。我不确定那个界面是什么,但它不是必需的。

  2. 更改为绑定的类定义如下:

    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; } 
         public string schedule { get; set; } 
        } 
    

    之后,它会工作。

P.S.我会把这个作为对原始答案的评论,但是我没有足够的信誉在Stack Overflow上发表评论!

相关问题