[Azure] C# 如何控制 Azure Traffic Manager - 新增 Custom Header , 停用 ExternalTargetEndpoint , 改 FQDN , 列出所有 ExternalTargetEndpoint

2020-12-22

最近在跟 Azure Traffic Manager 較勁, 折騰了一下,就筆記一下目前測試到的東西,說來都是淚阿,文件稀少,可能我用的方式比較冷門,就記錄一下..





1.首先你得先去 拿Azure  操控權限,詳情請參考  https://blog.no2don.com/2020/09/azurec-c-azure.html

2. 專案這邊 安裝該安裝的 nuget

https://www.nuget.org/packages/Microsoft.Azure.Management.Fluent/

https://www.nuget.org/packages/Microsoft.Azure.Management.ResourceManager.Fluent/

3. 介紹案例 ,我建立一個 新的 traffic manager 為 http://site1.trafficmanager.net ,並且我也在其中建立一個 為 client1


4. 列出所有的 ExternalTargetPoints

C# Code:

//Dependency: //https://www.nuget.org/packages/Microsoft.Azure.Management.Fluent/ //https://www.nuget.org/packages/Microsoft.Azure.Management.ResourceManager.Fluent/ //Get Info from https://blog.no2don.com/2020/09/azurec-c-azure.html var credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal( "99999999-4246-454f-b62c-1234567898765", "p-PasswordUTokens96D4Lk_9MtA_JNzW_", "ab194d5d-dddd-onma-dddd-ba7182d7e4d7", AzureEnvironment.AzureGlobalCloud); var azure = Azure .Configure() .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic) .Authenticate(credentials) .WithDefaultSubscription(); //Read var trafficManagerProfile = azure.TrafficManagerProfiles.GetById("/subscriptions/12345678-eeee-dddd-ffff-e7ad069b0cb5/resourceGroups/spinus/providers/Microsoft.Network/trafficManagerProfiles/site1"); Console.WriteLine("-- ALL Keys --"); var externalEndpoints = trafficManagerProfile.ExternalEndpoints; Console.WriteLine(JsonConvert.SerializeObject(externalEndpoints.Keys)); Console.WriteLine("-- ALL Settings --"); foreach (var endpoint in externalEndpoints) { Console.WriteLine("IsEnable:" + trafficManagerProfile.ExternalEndpoints[endpoint.Key].IsEnabled); Console.WriteLine("Name:" + trafficManagerProfile.ExternalEndpoints[endpoint.Key].Name); Console.WriteLine("CustomHeader:" + JsonConvert.SerializeObject(trafficManagerProfile.ExternalEndpoints[endpoint.Key].CustomHeaders)); Console.WriteLine("FQDN:" + trafficManagerProfile.ExternalEndpoints[endpoint.Key].Fqdn); Console.WriteLine("RoutingPriority:" + trafficManagerProfile.ExternalEndpoints[endpoint.Key].RoutingPriority); Console.WriteLine("-"); }

其中 GetById 的 ID 是你在 traffic manager > 屬性 > 識別辨識碼


Result:

5. 新增 Custom Header 

這地方,為何我不修改原本的呢? 因為,我怎麼測試都無法辦到,他永遠都會說 Duplicate custom header names for testheader are not allowed. 如果你要測試或是觀看你的操控行為,你可以到 traffic manager 活動紀錄中 看到。

但是新增是沒問題的

trafficManagerProfile.Update().UpdateExternalTargetEndpoint("client1") .WithCustomHeader("testappvalue", "donma_edited").Parent().Apply();

6.停用 ExternalTargetEndpoint

trafficManagerProfile.Update().UpdateExternalTargetEndpoint("client1").WithTrafficDisabled().Parent().Apply();

7.改 FQDN

trafficManagerProfile.Update().UpdateExternalTargetEndpoint("client1") .ToFqdn("192.168.1.1").Parent().Apply();

總結 , 其實看了一下,他的其他更改設定方法也是差不多套路,就先寫道這,如果有遇到雷要分享,我再寫文 :)


reference : https://github.com/Azure/azure-libraries-for-net


當麻許的碎念筆記 2014 | Donma Hsu Design.