VirtualBox

source: vbox/trunk/src/VBox/Frontends/VBoxManage/VBoxManageCloud.cpp@ 78138

Last change on this file since 78138 was 77886, checked in by vboxsync, 6 years ago

scm fix.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.6 KB
Line 
1/* $Id: VBoxManageCloud.cpp 77886 2019-03-26 16:37:38Z vboxsync $ */
2/** @file
3 * VBoxManageCloud - The cloud related commands.
4 */
5
6/*
7 * Copyright (C) 2006-2019 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#include <VBox/com/com.h>
19#include <VBox/com/string.h>
20#include <VBox/com/Guid.h>
21#include <VBox/com/array.h>
22#include <VBox/com/ErrorInfo.h>
23#include <VBox/com/errorprint.h>
24#include <VBox/com/VirtualBox.h>
25
26#include <iprt/ctype.h>
27#include <iprt/getopt.h>
28#include <iprt/stream.h>
29#include <iprt/string.h>
30#include <iprt/uuid.h>
31#include <iprt/file.h>
32#include <VBox/log.h>
33
34#include "VBoxManage.h"
35
36#include <list>
37
38using namespace com;//at least for Bstr
39
40/**
41 * Common Cloud options.
42 */
43typedef struct
44{
45 const char *pszProviderName;
46 const char *pszProfileName;
47} CLOUDCOMMONOPT;
48typedef CLOUDCOMMONOPT *PCLOUDCOMMONOPT;
49
50/**
51 * List all available cloud instances for the specified cloud provider.
52 * Available cloud instance is one which state whether "running" or "stopped".
53 *
54 * @returns RTEXITCODE
55 * @param a is the list of passed arguments
56 * @param iFirst is the position of the first unparsed argument in the arguments list
57 * @param pCommonOpts is a pointer to the structure CLOUDCOMMONOPT with some common
58 * arguments which have been already parsed before
59 */
60static RTEXITCODE listCloudInstances(HandlerArg *a, int iFirst, PCLOUDCOMMONOPT pCommonOpts)
61{
62 static const RTGETOPTDEF s_aOptions[] =
63 {
64 { "--compartment-id", 'c', RTGETOPT_REQ_STRING }
65 };
66 RTGETOPTSTATE GetState;
67 RTGETOPTUNION ValueUnion;
68 int vrc = RTGetOptInit(&GetState, a->argc, a->argv, s_aOptions, RT_ELEMENTS(s_aOptions), iFirst, 0);
69 AssertRCReturn(vrc, RTEXITCODE_FAILURE);
70
71 Utf8Str strCompartmentId;
72 int c;
73 while ((c = RTGetOpt(&GetState, &ValueUnion)) != 0)
74 {
75 switch (c)
76 {
77 case 'c':
78 strCompartmentId = ValueUnion.psz;
79 break;
80 case VINF_GETOPT_NOT_OPTION:
81 return errorUnknownSubcommand(ValueUnion.psz);
82 default:
83 return errorGetOpt(c, &ValueUnion);
84 }
85 }
86
87 HRESULT hrc = S_OK;
88 ComPtr<IVirtualBox> pVirtualBox = a->virtualBox;
89 ComPtr<ICloudProviderManager> pCloudProviderManager;
90 CHECK_ERROR2_RET(hrc, pVirtualBox,
91 COMGETTER(CloudProviderManager)(pCloudProviderManager.asOutParam()),
92 RTEXITCODE_FAILURE);
93 ComPtr<ICloudProvider> pCloudProvider;
94
95 CHECK_ERROR2_RET(hrc, pCloudProviderManager,
96 GetProviderByShortName(Bstr(pCommonOpts->pszProviderName).raw(), pCloudProvider.asOutParam()),
97 RTEXITCODE_FAILURE);
98 ComPtr<ICloudProfile> pCloudProfile;
99
100 CHECK_ERROR2_RET(hrc, pCloudProvider,
101 GetProfileByName(Bstr(pCommonOpts->pszProfileName).raw(), pCloudProfile.asOutParam()),
102 RTEXITCODE_FAILURE);
103
104 if (strCompartmentId.isNotEmpty())
105 {
106 CHECK_ERROR2_RET(hrc, pCloudProfile,
107 SetProperty(Bstr("compartment").raw(), Bstr(strCompartmentId).raw()),
108 RTEXITCODE_FAILURE);
109 }
110 else
111 {
112 RTPrintf("Parameter \'compartment\' is empty or absent.\n"
113 "Trying to get the compartment from the passed cloud profile \'%s\'\n", pCommonOpts->pszProfileName);
114 Bstr bStrCompartmentId;
115 CHECK_ERROR2_RET(hrc, pCloudProfile,
116 GetProperty(Bstr("compartment").raw(), bStrCompartmentId.asOutParam()),
117 RTEXITCODE_FAILURE);
118 strCompartmentId = bStrCompartmentId;
119 RTPrintf("Found the compartment \'%s\':\n", strCompartmentId.c_str());
120 }
121
122 Bstr bstrProfileName;
123 pCloudProfile->COMGETTER(Name)(bstrProfileName.asOutParam());
124
125 ComObjPtr<ICloudClient> oCloudClient;
126 CHECK_ERROR2_RET(hrc, pCloudProfile,
127 CreateCloudClient(oCloudClient.asOutParam()),
128 RTEXITCODE_FAILURE);
129
130 com::SafeArray<BSTR> arrayVMNames;
131 com::SafeArray<BSTR> arrayVMIds;
132 RTPrintf("Getting a list of available cloud instances...\n");
133 RTPrintf("Reply is in the form \'instance name\' = \'instance id\'\n");
134 CHECK_ERROR2_RET(hrc, oCloudClient,
135 ListInstances(CloudMachineState_Running,
136 ComSafeArrayAsOutParam(arrayVMNames),
137 ComSafeArrayAsOutParam(arrayVMIds)),
138 RTEXITCODE_FAILURE);
139 RTPrintf("List of available instances for the cloud profile \'%ls\' \nand compartment \'%s\':\n",
140 bstrProfileName.raw(), strCompartmentId.c_str());
141 size_t cIds = arrayVMIds.size();
142 size_t cNames = arrayVMNames.size();
143 for (size_t k = 0; k < cNames; k++)
144 {
145 Bstr value;
146 if (k < cIds)
147 value = arrayVMIds[k];
148 RTPrintf("\t%ls = %ls\n", arrayVMNames[k], value.raw());
149 }
150 CHECK_ERROR2_RET(hrc, oCloudClient,
151 ListInstances(CloudMachineState_Stopped,
152 ComSafeArrayAsOutParam(arrayVMNames),
153 ComSafeArrayAsOutParam(arrayVMIds)),
154 RTEXITCODE_FAILURE);
155 cNames = arrayVMNames.size();
156 cIds = arrayVMIds.size();
157 for (size_t k = 0; k < cNames; k++)
158 {
159 Bstr value;
160 if (k < cIds)
161 value = arrayVMIds[k];
162 RTPrintf("\t%ls=%ls\n", arrayVMNames[k], value.raw());
163 }
164
165 return SUCCEEDED(hrc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
166}
167
168/**
169 * List all available cloud images for the specified cloud provider.
170 *
171 * @returns RTEXITCODE
172 * @param a is the list of passed arguments
173 * @param iFirst is the position of the first unparsed argument in the arguments list
174 * @param pCommonOpts is a pointer to the structure CLOUDCOMMONOPT with some common
175 * arguments which have been already parsed before
176 */
177static RTEXITCODE listCloudImages(HandlerArg *a, int iFirst, PCLOUDCOMMONOPT pCommonOpts)
178{
179 static const RTGETOPTDEF s_aOptions[] =
180 {
181 { "--compartment-id", 'c', RTGETOPT_REQ_STRING }
182 };
183 RTGETOPTSTATE GetState;
184 RTGETOPTUNION ValueUnion;
185 int vrc = RTGetOptInit(&GetState, a->argc, a->argv, s_aOptions, RT_ELEMENTS(s_aOptions), iFirst, 0);
186 AssertRCReturn(vrc, RTEXITCODE_FAILURE);
187
188 Utf8Str strCompartmentId;
189 int c;
190 while ((c = RTGetOpt(&GetState, &ValueUnion)) != 0)
191 {
192 switch (c)
193 {
194 case 'c':
195 strCompartmentId = ValueUnion.psz;
196 break;
197 case VINF_GETOPT_NOT_OPTION:
198 return errorUnknownSubcommand(ValueUnion.psz);
199 default:
200 return errorGetOpt(c, &ValueUnion);
201 }
202 }
203
204 HRESULT hrc = S_OK;
205 ComPtr<IVirtualBox> pVirtualBox = a->virtualBox;
206 ComPtr<ICloudProviderManager> pCloudProviderManager;
207 CHECK_ERROR2_RET(hrc, pVirtualBox,
208 COMGETTER(CloudProviderManager)(pCloudProviderManager.asOutParam()),
209 RTEXITCODE_FAILURE);
210 ComPtr<ICloudProvider> pCloudProvider;
211
212 CHECK_ERROR2_RET(hrc, pCloudProviderManager,
213 GetProviderByShortName(Bstr(pCommonOpts->pszProviderName).raw(), pCloudProvider.asOutParam()),
214 RTEXITCODE_FAILURE);
215 ComPtr<ICloudProfile> pCloudProfile;
216
217 CHECK_ERROR2_RET(hrc, pCloudProvider,
218 GetProfileByName(Bstr(pCommonOpts->pszProfileName).raw(), pCloudProfile.asOutParam()),
219 RTEXITCODE_FAILURE);
220 if (strCompartmentId.isNotEmpty())
221 {
222 CHECK_ERROR2_RET(hrc, pCloudProfile,
223 SetProperty(Bstr("compartment").raw(), Bstr(strCompartmentId).raw()),\
224 RTEXITCODE_FAILURE);
225 }
226 else
227 {
228 RTPrintf("Parameter \'compartment\' is empty or absent.\n"
229 "Trying to get the compartment from the passed cloud profile \'%s\'\n", pCommonOpts->pszProfileName);
230 Bstr bStrCompartmentId;
231 CHECK_ERROR2_RET(hrc, pCloudProfile,
232 GetProperty(Bstr("compartment").raw(), bStrCompartmentId.asOutParam()),
233 RTEXITCODE_FAILURE);
234 strCompartmentId = bStrCompartmentId;
235 RTPrintf("Found the compartment \'%s\':\n", strCompartmentId.c_str());
236 }
237
238 Bstr bstrProfileName;
239 pCloudProfile->COMGETTER(Name)(bstrProfileName.asOutParam());
240
241 ComObjPtr<ICloudClient> oCloudClient;
242 CHECK_ERROR2_RET(hrc, pCloudProfile,
243 CreateCloudClient(oCloudClient.asOutParam()),
244 RTEXITCODE_FAILURE);
245 com::SafeArray<BSTR> arrayVMNames;
246 com::SafeArray<BSTR> arrayVMIds;
247 RTPrintf("Getting a list of available cloud images...\n");
248 RTPrintf("Reply is in the form \'image name\' = \'image id\'\n");
249 CHECK_ERROR2_RET(hrc, oCloudClient,
250 ListImages(CloudImageState_Available,
251 ComSafeArrayAsOutParam(arrayVMNames),
252 ComSafeArrayAsOutParam(arrayVMIds)),
253 RTEXITCODE_FAILURE);
254 RTPrintf("List of available images for the cloud profile \'%ls\' \nand compartment \'%s\':\n",
255 bstrProfileName.raw(), strCompartmentId.c_str());
256 size_t cNames = arrayVMNames.size();
257 size_t cIds = arrayVMIds.size();
258 for (size_t k = 0; k < cNames; k++)
259 {
260 Bstr value;
261 if (k < cIds)
262 value = arrayVMIds[k];
263 RTPrintf("\t%ls = %ls\n", arrayVMNames[k], value.raw());
264 }
265
266 return SUCCEEDED(hrc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
267}
268
269/**
270 * General function which handles the "list" commands
271 *
272 * @returns RTEXITCODE
273 * @param a is the list of passed arguments
274 * @param iFirst is the position of the first unparsed argument in the arguments list
275 * @param pCommonOpts is a pointer to the structure CLOUDCOMMONOPT with some common
276 * arguments which have been already parsed before
277 */
278static RTEXITCODE handleCloudLists(HandlerArg *a, int iFirst, PCLOUDCOMMONOPT pCommonOpts)
279{
280 if (a->argc < 1)
281 return errorNoSubcommand();
282
283 static const RTGETOPTDEF s_aOptions[] =
284 {
285 { "images", 1000, RTGETOPT_REQ_NOTHING },
286 { "instances", 1001, RTGETOPT_REQ_NOTHING },
287 { "networks", 1002, RTGETOPT_REQ_NOTHING },
288 { "subnets", 1003, RTGETOPT_REQ_NOTHING },
289 { "vcns", 1004, RTGETOPT_REQ_NOTHING },
290 { "objects", 1005, RTGETOPT_REQ_NOTHING }
291 };
292
293
294 Bstr bstrProvider(pCommonOpts->pszProviderName);
295 Bstr bstrProfile(pCommonOpts->pszProfileName);
296
297 /* check for required options */
298 if (bstrProvider.isEmpty())
299 return errorSyntax(USAGE_CLOUDPROFILE, "Parameter --provider is required");
300 if (bstrProfile.isEmpty())
301 return errorSyntax(USAGE_CLOUDPROFILE, "Parameter --profile is required");
302
303 RTGETOPTSTATE GetState;
304 int vrc = RTGetOptInit(&GetState, a->argc, a->argv, s_aOptions, RT_ELEMENTS(s_aOptions), iFirst, 0);
305 AssertRCReturn(vrc, RTEXITCODE_FAILURE);
306
307 int c;
308 RTGETOPTUNION ValueUnion;
309 while ((c = RTGetOpt(&GetState, &ValueUnion)) != 0)
310 {
311 switch (c)
312 {
313 case 1000:
314// setCurrentSubcommand(HELP_SCOPE_CLOUDIMAGE_CREATE);
315 return listCloudImages(a, GetState.iNext, pCommonOpts);
316 case 1001:
317// setCurrentSubcommand(HELP_SCOPE_CLOUDIMAGE_INFO);
318 return listCloudInstances(a, GetState.iNext, pCommonOpts);
319 case VINF_GETOPT_NOT_OPTION:
320 return errorUnknownSubcommand(ValueUnion.psz);
321
322 default:
323 return errorGetOpt(c, &ValueUnion);
324 }
325 }
326
327 return errorNoSubcommand();
328}
329
330RTEXITCODE handleCloud(HandlerArg *a)
331{
332 if (a->argc < 1)
333 return errorNoSubcommand();
334
335 static const RTGETOPTDEF s_aOptions[] =
336 {
337 /* common options */
338 { "--provider", 'v', RTGETOPT_REQ_STRING },
339 { "--profile", 'f', RTGETOPT_REQ_STRING },
340 { "list", 1000, RTGETOPT_REQ_NOTHING },
341 { "image", 1001, RTGETOPT_REQ_NOTHING },
342 { "instance", 1002, RTGETOPT_REQ_NOTHING },
343 { "network", 1003, RTGETOPT_REQ_NOTHING },
344 { "volume", 1004, RTGETOPT_REQ_NOTHING },
345 { "object", 1005, RTGETOPT_REQ_NOTHING }
346 };
347
348 RTGETOPTSTATE GetState;
349 int vrc = RTGetOptInit(&GetState, a->argc, a->argv, s_aOptions, RT_ELEMENTS(s_aOptions), 0, 0);
350 AssertRCReturn(vrc, RTEXITCODE_FAILURE);
351
352 CLOUDCOMMONOPT CommonOpts = { NULL, NULL };
353 int c;
354 RTGETOPTUNION ValueUnion;
355 while ((c = RTGetOpt(&GetState, &ValueUnion)) != 0)
356 {
357 switch (c)
358 {
359 case 'v': // --provider
360 CommonOpts.pszProviderName = ValueUnion.psz;
361 break;
362 case 'f': // --profile
363 CommonOpts.pszProfileName = ValueUnion.psz;
364 break;
365 /* Sub-commands: */
366 case 1000:
367// setCurrentSubcommand(HELP_SCOPE_CLOUDLIST);
368 return handleCloudLists(a, GetState.iNext, &CommonOpts);
369 case VINF_GETOPT_NOT_OPTION:
370 return errorUnknownSubcommand(ValueUnion.psz);
371
372 default:
373 return errorGetOpt(c, &ValueUnion);
374 }
375 }
376
377 return errorNoSubcommand();
378}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette