2016-07-12 67 views
0

我創建使用Microsoft CREST API用下面的代碼新的訂閱:如何在創建新的Azure訂閱後設置貢獻者角色?

WebClient client = new WebClient(); 
SATokenFormatReseller SA_Token = GetSAResellerToken(); 
client.Headers[HttpRequestHeader.Accept] = "application/json"; 
client.Headers[HttpRequestHeader.Authorization] = "Bearer " + SA_Token.access_token; 
client.Headers[HttpRequestHeader.ContentType] = "application/json"; 
client.Headers.Add("api-version", ApiVersion); 
client.Headers.Add("x-ms-correlation-id", Guid.NewGuid().ToString()); 
client.Headers.Add("x-ms-tracking-id", Guid.NewGuid().ToString()); 
var reqOrderObj = CreateReqOrderObject(service, mpn); 
var reqOrderJson = JsonConvert.SerializeObject(reqOrderObj, Newtonsoft.Json.Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); 
string res = client.UploadString("https://api.cp.microsoft.com/" + TenantId + "/orders", "POST", reqOrderJson); 

成功創建訂閱,我試圖使用管理蔚藍核心API來設置新的訂閱貢獻者角色之後,這樣:

public bool CreateRoleAssignment(string subscriptionId, string tenantId, string principalId) 
{ 
    bool roleGranted = false; 
    var azureToken = GetAzureAuthTokenForCustomerTenant(NativeClientId, PartnerCenterUser, PartnerCenterPassword, tenantId); 
    string responseContent = String.Empty; 
    var roleAssignmentId = Guid.NewGuid().ToString(); 
    var correlationId = Guid.NewGuid().ToString(); 
    var request = (HttpWebRequest)HttpWebRequest.Create(string.Format("https://management.azure.com/subscriptions/{0}/providers/Microsoft.Authorization/roleAssignments/{1}?api-version=2015-07-01", subscriptionId, roleAssignmentId)); 

    request.Method = "PUT"; 
    request.Accept = "application/json"; 
    request.ContentType = "application/json"; 

    request.Headers.Add("x-ms-correlation-id", correlationId); 
    request.Headers.Add("x-ms-tracking-id", Guid.NewGuid().ToString()); 
    request.Headers.Add("Authorization", "Bearer " + azureToken.AccessToken); 

    string content = Json.Encode(CreateRoleAssignmentRequestData(subscriptionId, principalId)); 

    using (var writer = new StreamWriter(request.GetRequestStream())) 
    { 
     writer.Write(content); 
    } 

    try 
    { 
     var response = request.GetResponse(); 
     using (var reader = new StreamReader(response.GetResponseStream())) 
     { 
      responseContent = reader.ReadToEnd(); 
      roleGranted = true; 
     } 
    } 
    catch (WebException webException) 
    { 
     using (var reader = new StreamReader(webException.Response.GetResponseStream())) 
     { 
      responseContent = reader.ReadToEnd(); 
      roleGranted = false; 
     } 
    } 

    return roleGranted; 
} 

但是,迴應是訂閱不存在。

經過一段時間後,如果我運行同一段代碼(CreateRoleAssignment),則該角色將成功提供給訂閱。

如何在創建訂閱後實現此過程?

+0

難道是因爲訂閱變得活躍需要一些時間嗎?當我註冊一個新的訂閱時,我被告知可能需要長達30分鐘才能激活訂閱。 –

+0

準確地說,我不知道這是個問題,但我該如何處理這種情況,我應該有一個解決方案progammaticaly?某種異步輪詢或並行任務可能是? –

+0

我會說繼續異步投票。 –

回答

1

正如@GauravMantri所說,使用異步輪詢作爲下面的示例代碼。

var azureToken = GetAzureAuthTokenForCustomerTenant(NativeClientId, PartnerCenterUser, PartnerCenterPassword, tenantId); 
string responseContent = String.Empty; 
var roleAssignmentId = Guid.NewGuid().ToString(); 
var correlationId = Guid.NewGuid().ToString(); 
string url = string.Format("https://management.azure.com/subscriptions/{0}/providers/Microsoft.Authorization/roleAssignments/{1}?api-version=2015-07-01", subscriptionId, roleAssignmentId); 
string content = Json.Encode(CreateRoleAssignmentRequestData(subscriptionId, principalId)); 

using (var client = new HttpClient()) 
{ 
    client.DefaultRequestHeaders.Clear(); 
    client.DefaultRequestHeaders.Add("Accept", "application/json"); 
    client.DefaultRequestHeaders.Add("Content-Type", "application/json"); 
    client.DefaultRequestHeaders.Add("x-ms-correlation-id", correlationId); 
    client.DefaultRequestHeaders.Add("x-ms-tracking-id", Guid.NewGuid().ToString()); 
    client.DefaultRequestHeaders.Add("Authorization", "Bearer " + azureToken.AccessToken); 
    var response = await client.PutAsync(url, content); 
    while(!response.IsSuccessStatusCode) 
    { 
     response = await client.PutAsync(url, content); 
    } 
    Console.WriteLine(response.Content.ReadAsStringAsync().Result); 
}