|
4 | 4 | #nullable disable |
5 | 5 |
|
6 | 6 | using System; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using System.Threading; |
7 | 9 | using Azure.Core; |
8 | 10 | using Azure.Core.Pipeline; |
9 | 11 |
|
@@ -73,11 +75,152 @@ public ConnectionsClient(Uri endpoint, string subscriptionId, string resourceGro |
73 | 75 | ClientDiagnostics = new ClientDiagnostics(options, true); |
74 | 76 | _tokenCredential = credential; |
75 | 77 | _pipeline = HttpPipelineBuilder.Build(options, Array.Empty<HttpPipelinePolicy>(), new HttpPipelinePolicy[] { new BearerTokenAuthenticationPolicy(_tokenCredential, AuthorizationScopes) }, new ResponseClassifier()); |
76 | | - _endpoint = endpoint; |
| 78 | + _endpoint = new Uri("https://management.azure.com"); |
77 | 79 | _subscriptionId = subscriptionId; |
78 | 80 | _resourceGroupName = resourceGroupName; |
79 | 81 | _projectName = projectName; |
80 | 82 | _apiVersion = options.Version; |
81 | 83 | } |
| 84 | + |
| 85 | + /// <summary> Initializes a new instance of ConnectionsClient. </summary> |
| 86 | + /// <param name="clientDiagnostics"> The handler for diagnostic messaging in the client. </param> |
| 87 | + /// <param name="pipeline"> The HTTP pipeline for sending and receiving REST requests and responses. </param> |
| 88 | + /// <param name="tokenCredential"> The token credential to copy. </param> |
| 89 | + /// <param name="endpoint"> The Azure AI Studio project endpoint, in the form `https://<azure-region>.api.azureml.ms` or `https://<private-link-guid>.<azure-region>.api.azureml.ms`, where <azure-region> is the Azure region where the project is deployed (e.g. westus) and <private-link-guid> is the GUID of the Enterprise private link. </param> |
| 90 | + /// <param name="subscriptionId"> The Azure subscription ID. </param> |
| 91 | + /// <param name="resourceGroupName"> The name of the Azure Resource Group. </param> |
| 92 | + /// <param name="projectName"> The Azure AI Studio project name. </param> |
| 93 | + /// <param name="apiVersion"> The API version to use for this operation. </param> |
| 94 | + internal ConnectionsClient(ClientDiagnostics clientDiagnostics, HttpPipeline pipeline, TokenCredential tokenCredential, Uri endpoint, string subscriptionId, string resourceGroupName, string projectName, string apiVersion) |
| 95 | + { |
| 96 | + ClientDiagnostics = clientDiagnostics; |
| 97 | + _pipeline = pipeline; |
| 98 | + _tokenCredential = tokenCredential; |
| 99 | + _endpoint = new Uri("https://management.azure.com"); |
| 100 | + _subscriptionId = subscriptionId; |
| 101 | + _resourceGroupName = resourceGroupName; |
| 102 | + _projectName = projectName; |
| 103 | + _apiVersion = apiVersion; |
| 104 | + } |
| 105 | + |
| 106 | + /// <summary> List the details of all the connections (not including their credentials). </summary> |
| 107 | + /// <param name="category"> Category of the workspace connection. </param> |
| 108 | + /// <param name="withCredential"></param> |
| 109 | + /// <param name="includeAll"> Indicates whether to list datastores. Service default: do not list datastores. </param> |
| 110 | + /// <param name="target"> Target of the workspace connection. </param> |
| 111 | + /// <param name="cancellationToken"> The cancellation token to use. </param> |
| 112 | + internal virtual async Task<Response<ConnectionsListSecretsResponse>> GetDefaultConnectionAsync(ConnectionType category, bool? withCredential = null, bool? includeAll = null, string target = null, CancellationToken cancellationToken = default) |
| 113 | + { |
| 114 | + ConnectionsListResponse connections = await GetConnectionsAsync(category, includeAll, target, cancellationToken).ConfigureAwait(false); |
| 115 | + |
| 116 | + if (connections?.Value == null || connections.Value.Count == 0) |
| 117 | + { |
| 118 | + throw new InvalidOperationException("No connections found for the specified parameters."); |
| 119 | + } |
| 120 | + |
| 121 | + var secret = connections.Value[0]; |
| 122 | + return withCredential.GetValueOrDefault() |
| 123 | + ? await GetSecretsAsync(secret.Name, "ignored").ConfigureAwait(false) |
| 124 | + : await GetConnectionAsync(secret.Name).ConfigureAwait(false); |
| 125 | + } |
| 126 | + |
| 127 | + /// <summary> Get the details of a single connection. </summary> |
| 128 | + /// <param name="category"> Category of the workspace connection. </param> |
| 129 | + /// <param name="withCredential"></param> |
| 130 | + /// <param name="includeAll"> Indicates whether to list datastores. Service default: do not list datastores. </param> |
| 131 | + /// <param name="target"> Target of the workspace connection. </param> |
| 132 | + /// <param name="cancellationToken"> The cancellation token to use. </param> |
| 133 | + internal virtual Response<ConnectionsListSecretsResponse> GetDefaultConnection(ConnectionType category, bool? withCredential = null, bool? includeAll = null, string target = null, CancellationToken cancellationToken = default) |
| 134 | + { |
| 135 | + ConnectionsListResponse connections = GetConnections(category, includeAll, target, cancellationToken); |
| 136 | + |
| 137 | + if (connections?.Value == null || connections.Value.Count == 0) |
| 138 | + { |
| 139 | + throw new InvalidOperationException("No connections found for the specified parameters."); |
| 140 | + } |
| 141 | + |
| 142 | + var secret = connections.Value[0]; |
| 143 | + return withCredential.GetValueOrDefault() |
| 144 | + ? GetSecrets(secret.Name, "ignored") |
| 145 | + : GetConnection(secret.Name); |
| 146 | + } |
| 147 | + |
| 148 | + // CUSTOM: Fixed the request URI by removing "/agents/v1.0" |
| 149 | + internal HttpMessage CreateGetConnectionsRequest(string category, bool? includeAll, string target, RequestContext context) |
| 150 | + { |
| 151 | + var message = _pipeline.CreateMessage(context, ResponseClassifier200); |
| 152 | + var request = message.Request; |
| 153 | + request.Method = RequestMethod.Get; |
| 154 | + var uri = new RawRequestUriBuilder(); |
| 155 | + uri.Reset(_endpoint); |
| 156 | + uri.AppendRaw("/subscriptions/", false); |
| 157 | + uri.AppendRaw(_subscriptionId, true); |
| 158 | + uri.AppendRaw("/resourceGroups/", false); |
| 159 | + uri.AppendRaw(_resourceGroupName, true); |
| 160 | + uri.AppendRaw("/providers/Microsoft.MachineLearningServices/workspaces/", false); |
| 161 | + uri.AppendRaw(_projectName, true); |
| 162 | + uri.AppendPath("/connections", false); |
| 163 | + uri.AppendQuery("api-version", _apiVersion, true); |
| 164 | + if (category != null) |
| 165 | + { |
| 166 | + uri.AppendQuery("category", category, true); |
| 167 | + } |
| 168 | + if (includeAll != null) |
| 169 | + { |
| 170 | + uri.AppendQuery("includeAll", includeAll.Value, true); |
| 171 | + } |
| 172 | + if (target != null) |
| 173 | + { |
| 174 | + uri.AppendQuery("target", target, true); |
| 175 | + } |
| 176 | + request.Uri = uri; |
| 177 | + request.Headers.Add("Accept", "application/json"); |
| 178 | + return message; |
| 179 | + } |
| 180 | + |
| 181 | + internal HttpMessage CreateGetConnectionRequest(string connectionName, RequestContext context) |
| 182 | + { |
| 183 | + var message = _pipeline.CreateMessage(context, ResponseClassifier200); |
| 184 | + var request = message.Request; |
| 185 | + request.Method = RequestMethod.Get; |
| 186 | + var uri = new RawRequestUriBuilder(); |
| 187 | + uri.Reset(_endpoint); |
| 188 | + uri.AppendRaw("/subscriptions/", false); |
| 189 | + uri.AppendRaw(_subscriptionId, true); |
| 190 | + uri.AppendRaw("/resourceGroups/", false); |
| 191 | + uri.AppendRaw(_resourceGroupName, true); |
| 192 | + uri.AppendRaw("/providers/Microsoft.MachineLearningServices/workspaces/", false); |
| 193 | + uri.AppendRaw(_projectName, true); |
| 194 | + uri.AppendPath("/connections/", false); |
| 195 | + uri.AppendPath(connectionName, true); |
| 196 | + uri.AppendQuery("api-version", _apiVersion, true); |
| 197 | + request.Uri = uri; |
| 198 | + request.Headers.Add("Accept", "application/json"); |
| 199 | + return message; |
| 200 | + } |
| 201 | + |
| 202 | + internal HttpMessage CreateGetSecretsRequest(string connectionName, RequestContent content, RequestContext context) |
| 203 | + { |
| 204 | + var message = _pipeline.CreateMessage(context, ResponseClassifier200); |
| 205 | + var request = message.Request; |
| 206 | + request.Method = RequestMethod.Post; |
| 207 | + var uri = new RawRequestUriBuilder(); |
| 208 | + uri.Reset(_endpoint); |
| 209 | + uri.AppendRaw("/subscriptions/", false); |
| 210 | + uri.AppendRaw(_subscriptionId, true); |
| 211 | + uri.AppendRaw("/resourceGroups/", false); |
| 212 | + uri.AppendRaw(_resourceGroupName, true); |
| 213 | + uri.AppendRaw("/providers/Microsoft.MachineLearningServices/workspaces/", false); |
| 214 | + uri.AppendRaw(_projectName, true); |
| 215 | + uri.AppendPath("/connections/", false); |
| 216 | + uri.AppendPath(connectionName, true); |
| 217 | + uri.AppendPath("/listsecrets", false); |
| 218 | + uri.AppendQuery("api-version", _apiVersion, true); |
| 219 | + request.Uri = uri; |
| 220 | + request.Headers.Add("Accept", "application/json"); |
| 221 | + request.Headers.Add("Content-Type", "application/json"); |
| 222 | + request.Content = content; |
| 223 | + return message; |
| 224 | + } |
82 | 225 | } |
83 | 226 | } |
0 commit comments