2017-02-22 126 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上發表評論!

相關問題