master
cpp 324 lines 9.59 KB
Raw
1 /*++
2
3 Copyright (c) Microsoft. All rights reserved.
4
5 Module Name:
6
7 ContainerSettings.cpp
8
9 Abstract:
10
11 This file contains the implementation of the WinRT wrapper for the WSLC SDK ContainerSettings class.
12
13 --*/
14
15 #include "precomp.h"
16 #include "ContainerSettings.h"
17 #include "Microsoft.WSL.Containers.ContainerSettings.g.cpp"
18
19 namespace winrt::Microsoft::WSL::Containers::implementation {
20
21 ContainerSettings::ContainerSettings(hstring const& imageName) : m_imageName(winrt::to_string(imageName))
22 {
23 if (imageName.empty())
24 {
25 throw winrt::hresult_invalid_argument(L"Image name cannot be empty");
26 }
27 }
28
29 hstring ContainerSettings::ImageName()
30 {
31 return winrt::to_hstring(m_imageName);
32 }
33
34 void ContainerSettings::ImageName(hstring const& value)
35 {
36 if (m_containerSettings)
37 {
38 throw winrt::hresult_illegal_state_change(L"Cannot change image name after container has been initialized");
39 }
40
41 if (value.empty())
42 {
43 throw winrt::hresult_invalid_argument(L"Image name cannot be empty");
44 }
45
46 m_imageName = winrt::to_string(value);
47 }
48
49 hstring ContainerSettings::Name()
50 {
51 return winrt::to_hstring(m_name);
52 }
53
54 void ContainerSettings::Name(hstring const& value)
55 {
56 if (m_containerSettings)
57 {
58 throw winrt::hresult_illegal_state_change(L"Cannot change container name after container has been initialized");
59 }
60
61 m_name = winrt::to_string(value);
62 }
63
64 winrt::Microsoft::WSL::Containers::ProcessSettings ContainerSettings::InitProcess()
65 {
66 return m_initProcess;
67 }
68
69 void ContainerSettings::InitProcess(winrt::Microsoft::WSL::Containers::ProcessSettings const& value)
70 {
71 if (m_containerSettings)
72 {
73 throw winrt::hresult_illegal_state_change(L"Cannot change init process after container has been initialized");
74 }
75
76 m_initProcess = value;
77 }
78
79 winrt::Windows::Foundation::IReference<winrt::Microsoft::WSL::Containers::ContainerNetworkingMode> ContainerSettings::NetworkingMode()
80 {
81 return m_networkingMode;
82 }
83
84 void ContainerSettings::NetworkingMode(winrt::Windows::Foundation::IReference<winrt::Microsoft::WSL::Containers::ContainerNetworkingMode> const& value)
85 {
86 if (m_containerSettings)
87 {
88 throw winrt::hresult_illegal_state_change(L"Cannot change networking mode after container has been initialized");
89 }
90
91 if (value && value.Value() != ContainerNetworkingMode::None && value.Value() != ContainerNetworkingMode::Bridged)
92 {
93 throw winrt::hresult_invalid_argument(L"Invalid networking mode");
94 }
95
96 m_networkingMode = value;
97 }
98
99 hstring ContainerSettings::HostName()
100 {
101 return winrt::to_hstring(m_hostName);
102 }
103
104 void ContainerSettings::HostName(hstring const& value)
105 {
106 if (m_containerSettings)
107 {
108 throw winrt::hresult_illegal_state_change(L"Cannot change host name after container has been initialized");
109 }
110
111 m_hostName = winrt::to_string(value);
112 }
113
114 hstring ContainerSettings::DomainName()
115 {
116 return winrt::to_hstring(m_domainName);
117 }
118
119 void ContainerSettings::DomainName(hstring const& value)
120 {
121 if (m_containerSettings)
122 {
123 throw winrt::hresult_illegal_state_change(L"Cannot change domain name after container has been initialized");
124 }
125
126 m_domainName = winrt::to_string(value);
127 }
128
129 bool ContainerSettings::EnableAutoRemove()
130 {
131 return WI_IsFlagSet(m_containerFlags, WSLC_CONTAINER_FLAG_AUTO_REMOVE);
132 }
133
134 void ContainerSettings::EnableAutoRemove(bool value)
135 {
136 if (m_containerSettings)
137 {
138 throw winrt::hresult_illegal_state_change(L"Cannot change container settings after container has been initialized");
139 }
140
141 WI_UpdateFlag(m_containerFlags, WSLC_CONTAINER_FLAG_AUTO_REMOVE, value);
142 }
143
144 bool ContainerSettings::EnableGpu()
145 {
146 return WI_IsFlagSet(m_containerFlags, WSLC_CONTAINER_FLAG_ENABLE_GPU);
147 }
148
149 void ContainerSettings::EnableGpu(bool value)
150 {
151 if (m_containerSettings)
152 {
153 throw winrt::hresult_illegal_state_change(L"Cannot change container settings after container has been initialized");
154 }
155
156 WI_UpdateFlag(m_containerFlags, WSLC_CONTAINER_FLAG_ENABLE_GPU, value);
157 }
158
159 bool ContainerSettings::Privileged()
160 {
161 return WI_IsFlagSet(m_containerFlags, WSLC_CONTAINER_FLAG_PRIVILEGED);
162 }
163
164 void ContainerSettings::Privileged(bool value)
165 {
166 if (m_containerSettings)
167 {
168 throw winrt::hresult_illegal_state_change(L"Cannot change container settings after container has been initialized");
169 }
170
171 WI_UpdateFlag(m_containerFlags, WSLC_CONTAINER_FLAG_PRIVILEGED, value);
172 }
173
174 winrt::Windows::Foundation::Collections::IVector<winrt::Microsoft::WSL::Containers::ContainerPortMapping> ContainerSettings::PortMappings()
175 {
176 return m_portMappings;
177 }
178
179 void ContainerSettings::PortMappings(winrt::Windows::Foundation::Collections::IVector<winrt::Microsoft::WSL::Containers::ContainerPortMapping> const& value)
180 {
181 if (m_containerSettings)
182 {
183 throw winrt::hresult_illegal_state_change(L"Cannot change port mappings after container has been initialized");
184 }
185
186 if (!value)
187 {
188 throw winrt::hresult_error(E_POINTER, L"Value cannot be null");
189 }
190
191 m_portMappings = value;
192 }
193
194 winrt::Windows::Foundation::Collections::IVector<winrt::Microsoft::WSL::Containers::ContainerVolume> ContainerSettings::Volumes()
195 {
196 return m_volumes;
197 }
198
199 void ContainerSettings::Volumes(winrt::Windows::Foundation::Collections::IVector<winrt::Microsoft::WSL::Containers::ContainerVolume> const& value)
200 {
201 if (m_containerSettings)
202 {
203 throw winrt::hresult_illegal_state_change(L"Cannot change volumes after container has been initialized");
204 }
205
206 if (!value)
207 {
208 throw winrt::hresult_error(E_POINTER, L"Value cannot be null");
209 }
210
211 m_volumes = value;
212 }
213
214 winrt::Windows::Foundation::Collections::IVector<winrt::Microsoft::WSL::Containers::ContainerNamedVolume> ContainerSettings::NamedVolumes()
215 {
216 return m_namedVolumes;
217 }
218
219 void ContainerSettings::NamedVolumes(winrt::Windows::Foundation::Collections::IVector<winrt::Microsoft::WSL::Containers::ContainerNamedVolume> const& value)
220 {
221 if (m_containerSettings)
222 {
223 throw winrt::hresult_illegal_state_change(L"Cannot change named volumes after container has been initialized");
224 }
225
226 if (!value)
227 {
228 throw winrt::hresult_error(E_POINTER, L"Value cannot be null");
229 }
230
231 m_namedVolumes = value;
232 }
233
234 WslcContainerSettings* ContainerSettings::ToStructPointer()
235 {
236 if (!m_containerSettings)
237 {
238 m_containerSettings = std::make_unique<WslcContainerSettings>();
239 winrt::check_hresult(WslcInitContainerSettings(m_imageName.c_str(), m_containerSettings.get()));
240
241 if (!m_name.empty())
242 {
243 winrt::check_hresult(WslcSetContainerSettingsName(m_containerSettings.get(), m_name.c_str()));
244 }
245
246 if (m_initProcess)
247 {
248 winrt::check_hresult(WslcSetContainerSettingsInitProcess(m_containerSettings.get(), GetStructPointer(m_initProcess)));
249 }
250
251 if (m_networkingMode)
252 {
253 winrt::check_hresult(WslcSetContainerSettingsNetworkingMode(
254 m_containerSettings.get(), static_cast<WslcContainerNetworkingMode>(m_networkingMode.Value())));
255 }
256
257 if (!m_hostName.empty())
258 {
259 winrt::check_hresult(WslcSetContainerSettingsHostName(m_containerSettings.get(), m_hostName.c_str()));
260 }
261
262 if (!m_domainName.empty())
263 {
264 winrt::check_hresult(WslcSetContainerSettingsDomainName(m_containerSettings.get(), m_domainName.c_str()));
265 }
266
267 winrt::check_hresult(WslcSetContainerSettingsFlags(m_containerSettings.get(), m_containerFlags));
268
269 if (m_portMappings.Size() > 0)
270 {
271 m_portMappingsStructs.clear();
272 m_portMappingsStructs.reserve(m_portMappings.Size());
273 for (auto const& portMapping : m_portMappings)
274 {
275 if (!portMapping)
276 {
277 throw winrt::hresult_error(E_POINTER, L"Port mappings collection contains a null element");
278 }
279 m_portMappingsStructs.push_back(GetStruct(portMapping));
280 }
281
282 winrt::check_hresult(WslcSetContainerSettingsPortMappings(
283 m_containerSettings.get(), m_portMappingsStructs.data(), static_cast<uint32_t>(m_portMappingsStructs.size())));
284 }
285
286 if (m_volumes.Size() > 0)
287 {
288 m_volumesStructs.clear();
289 m_volumesStructs.reserve(m_volumes.Size());
290 for (auto const& volume : m_volumes)
291 {
292 if (!volume)
293 {
294 throw winrt::hresult_error(E_POINTER, L"Volumes collection contains a null element");
295 }
296 m_volumesStructs.push_back(GetStruct(volume));
297 }
298
299 winrt::check_hresult(WslcSetContainerSettingsVolumes(
300 m_containerSettings.get(), m_volumesStructs.data(), static_cast<uint32_t>(m_volumesStructs.size())));
301 }
302
303 if (m_namedVolumes.Size() > 0)
304 {
305 m_namedVolumesStructs.clear();
306 m_namedVolumesStructs.reserve(m_namedVolumes.Size());
307 for (auto const& namedVolume : m_namedVolumes)
308 {
309 if (!namedVolume)
310 {
311 throw winrt::hresult_error(E_POINTER, L"Named volumes collection contains a null element");
312 }
313 m_namedVolumesStructs.push_back(GetStruct(namedVolume));
314 }
315
316 winrt::check_hresult(WslcSetContainerSettingsNamedVolumes(
317 m_containerSettings.get(), m_namedVolumesStructs.data(), static_cast<uint32_t>(m_namedVolumesStructs.size())));
318 }
319 }
320
321 return m_containerSettings.get();
322 }
323
324 } // namespace winrt::Microsoft::WSL::Containers::implementation