Esempio n. 1
0
 private static PolicyDto CreatePolicyDto(PolicyClaim policyClaim)
 {
     return(new PolicyDto()
     {
         PolicyName = $"PolicyName{policyClaim}",
         PolicyKey = $"PolicyKey{policyClaim}",
         PolicyType = policyClaim
     });
 }
        async Task <PolicyDto> IRelayManagementApiClient.CreatePolicykeyAsync(string tenantId, PolicyClaim policyClaim)
        {
            // Check for empty strings and null checks - Consider making private method to make the empty check
            if (string.IsNullOrWhiteSpace(tenantId))
            {
                throw new ArgumentNullException(tenantId);
            }
            var subscriptionId = !string.IsNullOrWhiteSpace(_applicationsSettings.AzureSubscriptionId) ?
                                 _applicationsSettings.AzureSubscriptionId : throw new InvalidOperationException(nameof(_applicationsSettings.AzureSubscriptionId));
            var resourceGroupName = !string.IsNullOrWhiteSpace(_applicationsSettings.ResourceGroupname) ?
                                    _applicationsSettings.ResourceGroupname : throw new InvalidOperationException(nameof(_applicationsSettings.ResourceGroupname));
            var relayNamespaceName = !string.IsNullOrWhiteSpace(_applicationsSettings.RelayNameSpace) ?
                                     _applicationsSettings.RelayNameSpace : throw new InvalidOperationException(nameof(_applicationsSettings.RelayNameSpace));

            var hybridConnectionName = $"{tenantId}Hybrid";
            var policyName           = Guid.NewGuid().ToString();
            var relativeUrlString    = $"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Relay/namespaces/{relayNamespaceName}/hybridConnections/{hybridConnectionName}/authorizationRules/{policyName}";
            var relativeUrl          = new Uri($"{relativeUrlString}?api-version=2017-04-01", UriKind.Relative);
            var httpRequestMessage   = new HttpRequestMessage
            {
                Method     = new HttpMethod(HttpMethods.Put),
                Content    = new StringContent(JsonConvert.SerializeObject(new CreateHybridConnectionPolicyRequest(new string[] { policyClaim.ToString() }))),
                RequestUri = relativeUrl
            };
            HttpResponseMessage response = await _httpClient.SendAsync(httpRequestMessage);

            response.EnsureSuccessStatusCode();

            return(new PolicyDto()
            {
                PolicyName = policyName, PolicyKey = await GetPolicyKeyAsync(relativeUrlString), PolicyType = policyClaim
            });
        }