上一篇文章
C# 如何控制 Azure Traffic Manager - 新增 Custom Header , 停用 ExternalTargetEndpoint , 改 FQDN , 列出所有 ExternalTargetEndpoint 我們談到如何 控制
Azure Traffic Manager ,但是有一個小遺憾,經過目前版本測試 我不能改 cutomer header 但是山不轉我轉,有沒有可能,我直接刪除那個 ExternalEndpoint 然後再把它加回去呢?
經過我測試一下,是可以的但是寫法有點 tricky .
這邊就分享一下 code ,大概講解一下 ,我原本有一個 endpoint 叫做 client1 , 並且相關參數為
IsEnable:True
Name:client1
CustomHeader:{"test":"aaaa"}
FQDN:tm.ina9.win
RoutingPriority:6
然後基於這,我要把這砍掉,然後變成 其他資料,一樣取名叫做 client1 後,新的資料是
IsEnable:True
Name:client1
CustomHeader:{"testheader":"app_edited_20201223"}
FQDN:tm2.ina9.win
RoutingPriority:6
這裡面我主要是修改 FQDN , CustomHeader ,詳細我都寫在註解裡面了
code
var credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(
"aaaaaaa-xxxx-cccc-eeee-123412341234",
"p-Password_5Hs96D4Lk_9MtA_CNDA_",
"aaaaaaa-dddd-dddd-cccc-ba7182d7e4d7", AzureEnvironment.AzureGlobalCloud);
var azure = Azure
.Configure()
.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
.Authenticate(credentials)
.WithDefaultSubscription();
//Read
var trafficManagerProfile = azure.TrafficManagerProfiles.GetById("/subscriptions/aaaaaaaa-bbbb-cccc-dddd-ffffffffffff/resourceGroups/spinus/providers/Microsoft.Network/trafficManagerProfiles/site1");
Console.WriteLine("-- ALL Settings Before--");
var externalEndpoints = trafficManagerProfile.ExternalEndpoints;
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("-");
}
//關鍵是這裡
//再它們的程式設計中,刪除是用 Without 的關鍵字,並不是 remove or delete
//因為刪除要先 Apply 才會生效
//之後它的定義就是要先 Define 就是 新增一個腳本,記得要先 FromRegion 之後才能繼續設定
//這跟它們的 sdk 設計邏輯有關係
trafficManagerProfile.Update().WithoutEndpoint("client1").Apply().Update()
.DefineExternalTargetEndpoint("client1")
.ToFqdn("tm2.ina9.win").FromRegion(Region.AsiaEast).WithRoutingPriority(6).WithCustomHeader("testheader", "app_edited_20201223").Attach().Apply();
Console.WriteLine("-- ALL Settings After--");
var externalEndpointsAfter = trafficManagerProfile.ExternalEndpoints;
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("-");
}
result
大概更新後,1~ 3 分鐘 他的測試健康探查就會更新了
這些都是花錢測試的阿,說多都是淚..T.T..
後記:
我之後有回報給 微軟那邊的 github 有得到bug 修正回應 ,靜候之後版本吧,應該會修正 update traffic manager custom header 問題
Cannot update traffic manager CustomHeader · Issue #1187 · Azure/azure-libraries-for-net (github.com)
reference : https://github.com/Azure/azure-libraries-for-net