Skip to content

Commit 800dd17

Browse files
Allow WSLC networks to be referenced by name, Id, or Id prefix (#42046)
* Allow WSLC networks to be referenced by name or Id Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Resolve network full Id before name to match engine precedence Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Add WSLC_E_NETWORK_PREFIX_AMBIGUOUS error code for ambiguous network prefixes Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Resolve built-in networks by ID or prefix at container create Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Resolve network prefixes via engine, reject host IDs, and check aliases before lookup Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Inspect networks by reference in a single engine round trip Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Move network resolution into WSLCSession and resolve container networks through it Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 83d3cca commit 800dd17

38 files changed

Lines changed: 738 additions & 133 deletions

‎doc/docs/api-reference/c/error-codes.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#define WSLC_E_CONTAINER_DELETED MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, WSLC_E_BASE + 19) /* 0x80040613 */
2424
#define WSLC_E_CAPABILITY_ADDITIONS_DISABLED MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, WSLC_E_BASE + 20) /* 0x80040614 */
2525
#define WSLC_E_RUNTIME_UPDATE_NEEDED MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, WSLC_E_BASE + 21) /* 0x80040615 */
26+
#define WSLC_E_NETWORK_PREFIX_AMBIGUOUS MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, WSLC_E_BASE + 22) /* 0x80040616 */
2627
```
2728
2829
| Symbol | Hex Value |
@@ -49,5 +50,6 @@
4950
| `WSLC_E_CONTAINER_DELETED` | `0x80040613` |
5051
| `WSLC_E_CAPABILITY_ADDITIONS_DISABLED` | `0x80040614` |
5152
| `WSLC_E_RUNTIME_UPDATE_NEEDED` | `0x80040615` |
53+
| `WSLC_E_NETWORK_PREFIX_AMBIGUOUS` | `0x80040616` |
5254
5355
---

‎doc/docs/api-reference/c/network-apis/wslcdeletesessionnetwork.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
# WslcDeleteSessionNetwork
22

33
```c
4-
STDAPI WslcDeleteSessionNetwork(_In_ WslcSession session, _In_z_ PCSTR name, _Outptr_opt_result_z_ PWSTR* errorMessage);
4+
STDAPI WslcDeleteSessionNetwork(_In_ WslcSession session, _In_z_ PCSTR nameOrId, _Outptr_opt_result_z_ PWSTR* errorMessage);
55
```
66
77
| Parameter | Type | Direction |
88
|---|---|---|
99
| `session` | `WslcSession` | in |
10-
| `name` | `PCSTR` | in |
10+
| `nameOrId` | `PCSTR` | in |
1111
| `errorMessage` | `PWSTR*` | out, optional |
1212
1313
Return value: `HRESULT`.
1414
15-
Returns `WSLC_E_NETWORK_NOT_FOUND` if the network does not exist, or `HRESULT_FROM_WIN32(ERROR_SHARING_VIOLATION)` if a container is connected to the network. Returns [`WSLC_E_RUNTIME_UPDATE_NEEDED`](../error-codes.md) if the installed WSL runtime does not support network management.
15+
`nameOrId` is the name, full ID, or unique ID prefix of the network to delete. An exact ID match takes precedence over a name match, and a name match takes precedence over an ID prefix match.
16+
17+
Returns `WSLC_E_NETWORK_NOT_FOUND` if no network matches, `WSLC_E_NETWORK_PREFIX_AMBIGUOUS` if an ID prefix matches more than one network, or `HRESULT_FROM_WIN32(ERROR_SHARING_VIOLATION)` if a container is connected to the network. Returns [`WSLC_E_RUNTIME_UPDATE_NEEDED`](../error-codes.md) if the installed WSL runtime does not support network management.
1618
1719
Example:
1820

‎doc/docs/api-reference/cpp/core-classes/session.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ auto network = session.CreateNetwork(NetworkSettings{L"backend"});
200200
201201
## Session::DeleteNetwork(hstring)
202202
203-
Deletes a network from the session. Throws `WSLC_E_NETWORK_NOT_FOUND` if the network does not exist.
203+
Deletes a network from the session by name, full ID, or unique ID prefix. An exact ID match takes precedence over a name match, and a name match takes precedence over an ID prefix match. Throws `WSLC_E_NETWORK_NOT_FOUND` if no network matches, or `WSLC_E_NETWORK_PREFIX_AMBIGUOUS` if an ID prefix matches more than one network.
204204
205205
```cpp
206206
session.DeleteNetwork(L"backend");

‎doc/docs/api-reference/cpp/enumerations/error.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@
1919
| `ContainerDeleted` | `-2147219949` | `0x80040613` | The container was deleted. |
2020
| `CapabilityAdditionsDisabled` | `-2147219948` | `0x80040614` | Capability additions are disabled by policy |
2121
| `RuntimeUpdateNeeded` | `-2147219947` | `0x80040615` | The requested feature requires a newer WSL runtime. |
22+
| `NetworkPrefixAmbiguous` | `-2147219946` | `0x80040616` | The network ID prefix matches more than one network. |

‎doc/docs/api-reference/csharp/core-classes/session.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public sealed class Session : IDisposable
3030
public IReadOnlyList<ImageInfo> GetImages();
3131
public IReadOnlyList<NetworkInfo> GetNetworks();
3232
public NetworkInfo CreateNetwork(NetworkSettings settings);
33-
public void DeleteNetwork(string name);
33+
public void DeleteNetwork(string nameOrId);
3434
public void Dispose();
3535
}
3636
```
@@ -249,9 +249,9 @@ Creates a bridge network in the session and returns it. Throws `HRESULT_FROM_WIN
249249
var network = session.CreateNetwork(new NetworkSettings("backend"));
250250
```
251251

252-
## Session.DeleteNetwork(string name)
252+
## Session.DeleteNetwork(string nameOrId)
253253

254-
Deletes a network from the session. Throws `WSLC_E_NETWORK_NOT_FOUND` if the network does not exist.
254+
Deletes a network from the session by name, full ID, or unique ID prefix. An exact ID match takes precedence over a name match, and a name match takes precedence over an ID prefix match. Throws `WSLC_E_NETWORK_NOT_FOUND` if no network matches, or `WSLC_E_NETWORK_PREFIX_AMBIGUOUS` if an ID prefix matches more than one network.
255255

256256
```csharp
257257
session.DeleteNetwork("backend");

‎doc/docs/api-reference/csharp/enumerations/error.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@
1919
| `ContainerDeleted` | `-2147219949` | `0x80040613` | The container was deleted. |
2020
| `CapabilityAdditionsDisabled` | `-2147219948` | `0x80040614` | Capability additions are disabled by policy |
2121
| `RuntimeUpdateNeeded` | `-2147219947` | `0x80040615` | The requested feature requires a newer WSL runtime. |
22+
| `NetworkPrefixAmbiguous` | `-2147219946` | `0x80040616` | The network ID prefix matches more than one network. |

‎localization/strings/en-US/Resources.resw‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2514,6 +2514,10 @@ For privacy information about this product please visit https://aka.ms/privacy.<
25142514
<value>Network not found: '{}'</value>
25152515
<comment>{FixedPlaceholder="{}"}Command line arguments, file names and string inserts should not be translated</comment>
25162516
</data>
2517+
<data name="MessageWslcNetworkPrefixAmbiguous" xml:space="preserve">
2518+
<value>Multiple networks match '{}'; use a longer ID or the network name.</value>
2519+
<comment>{FixedPlaceholder="{}"}Command line arguments, file names and string inserts should not be translated</comment>
2520+
</data>
25172521
<data name = "MessageWslcInvalidNetworkDriver" xml:space = "preserve" >
25182522
<value>Unsupported network driver: '{}'</value>
25192523
<comment>{FixedPlaceholder="{}"}Command line arguments, file names and string inserts should not be translated</comment>
@@ -3820,6 +3824,9 @@ On first run, creates the file with all settings commented out at their defaults
38203824
<data name="WSLCCLI_NetworkNameArgDescription" xml:space="preserve">
38213825
<value>Network name</value>
38223826
</data>
3827+
<data name="WSLCCLI_NetworkNameOrIdArgDescription" xml:space="preserve">
3828+
<value>Network name or ID</value>
3829+
</data>
38233830
<data name="WSLCCLI_NetworkListQuietArgDesc" xml:space="preserve">
38243831
<value>Only display network IDs</value>
38253832
</data>

‎src/windows/WslcSDK/winrt/Session.cpp‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -440,17 +440,17 @@ winrt::Microsoft::WSL::Containers::NetworkInfo Session::CreateNetwork(winrt::Mic
440440
return winrt::make<implementation::NetworkInfo>(networkInfo);
441441
}
442442

443-
void Session::DeleteNetwork(hstring const& name)
443+
void Session::DeleteNetwork(hstring const& nameOrId)
444444
{
445-
if (name.empty())
445+
if (nameOrId.empty())
446446
{
447-
throw winrt::hresult_invalid_argument(L"Network name cannot be empty");
447+
throw winrt::hresult_invalid_argument(L"Network name or ID cannot be empty");
448448
}
449449

450450
EnsureStarted();
451451

452452
wil::unique_cotaskmem_string errorMessage;
453-
auto hr = WslcDeleteSessionNetwork(ToHandle(), winrt::to_string(name).c_str(), errorMessage.put());
453+
auto hr = WslcDeleteSessionNetwork(ToHandle(), winrt::to_string(nameOrId).c_str(), errorMessage.put());
454454
THROW_MSG_IF_FAILED(hr, errorMessage);
455455
}
456456

‎src/windows/WslcSDK/winrt/Session.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ struct Session : SessionT<Session>
4545
winrt::Windows::Foundation::Collections::IVectorView<winrt::Microsoft::WSL::Containers::ImageInfo> GetImages();
4646
winrt::Windows::Foundation::Collections::IVectorView<winrt::Microsoft::WSL::Containers::NetworkInfo> GetNetworks();
4747
winrt::Microsoft::WSL::Containers::NetworkInfo CreateNetwork(winrt::Microsoft::WSL::Containers::NetworkSettings const& settings);
48-
void DeleteNetwork(hstring const& name);
48+
void DeleteNetwork(hstring const& nameOrId);
4949
winrt::event_token Terminated(winrt::Microsoft::WSL::Containers::SessionTerminationHandler const& handler);
5050
void Terminated(winrt::event_token const& token) noexcept;
5151
winrt::event_token ProcessCrashed(winrt::Microsoft::WSL::Containers::ProcessCrashHandler const& handler);

‎src/windows/WslcSDK/winrt/wslcsdk.idl‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ namespace Microsoft.WSL.Containers
9393

9494
IVectorView<NetworkInfo> GetNetworks();
9595
NetworkInfo CreateNetwork(NetworkSettings settings);
96-
void DeleteNetwork(String name);
96+
void DeleteNetwork(String nameOrId);
9797

9898
event SessionTerminationHandler Terminated;
9999
event ProcessCrashHandler ProcessCrashed;
@@ -453,5 +453,6 @@ namespace Microsoft.WSL.Containers
453453
ContainerDeleted = 0x80040613,
454454
CapabilityAdditionsDisabled = 0x80040614,
455455
RuntimeUpdateNeeded = 0x80040615,
456+
NetworkPrefixAmbiguous = 0x80040616,
456457
};
457458
}

0 commit comments

Comments
 (0)