VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/CloudUserProfileListImpl.cpp@ 73173

Last change on this file since 73173 was 73173, checked in by vboxsync, 7 years ago

bugref:9152. Fixed several issues in the code.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.6 KB
Line 
1/* $Id: CloudUserProfileListImpl.cpp 73173 2018-07-17 11:47:51Z vboxsync $ */
2/** @file
3 * ICloudUserProfileList COM class implementations.
4 */
5
6/*
7 * Copyright (C) 2008-2018 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
19#include <iprt/path.h>
20#include <iprt/cpp/utils.h>
21#include <VBox/com/array.h>
22
23#include "CloudUserProfileListImpl.h"
24#include "VirtualBoxImpl.h"
25#include "Global.h"
26#include "ProgressImpl.h"
27#include "MachineImpl.h"
28#include "AutoCaller.h"
29#include "Logging.h"
30
31using namespace std;
32
33////////////////////////////////////////////////////////////////////////////////
34//
35// SimpleConfigFile implementation
36//
37////////////////////////////////////////////////////////////////////////////////
38SimpleConfigFile::SimpleConfigFile(VirtualBoxBase *pSetError, const char *pszFilename)
39 : GeneralTextScript(pSetError, pszFilename)
40{
41 LogRel(("SimpleConfigFile::SimpleConfigFile(VirtualBoxBase *pSetError,...)\n"));
42}
43
44SimpleConfigFile::~SimpleConfigFile()
45{
46 LogRel(("SimpleConfigFile::~SimpleConfigFile()\n"));
47}
48
49HRESULT SimpleConfigFile::parse()
50{
51 LogRel(("Starting to parse the existing profiles\n"));
52 HRESULT hrc = S_OK;
53 hrc = GeneralTextScript::parse();
54 if (SUCCEEDED(hrc))
55 {
56 size_t lines = getLineNumbersOfScript();
57 LogRel(("Initial parsing (GeneralTextScript::parse()) was succeeded \n"));
58 LogRel(("Read lines is %d \n", lines));
59 size_t startSectionPos=0;
60 bool fInSection = false;
61 std::map <Utf8Str, Utf8Str> sectionConfiguration;
62 Utf8Str strSectionName("Default section ");
63
64 for (size_t i=0; i<lines; ++i)
65 {
66 //find the beginning of section, it starts from the line like "[section name]"
67 //next find the end of section, it ends up when we find the beginning
68 //of the next section or reach the end of the contents.
69 //Go through the found lines and split each of them up by "=".
70 //Each correct line must look like "key=value"
71 //if there is not the character "=" in the line one is skipped
72 //class Utf8Str contains the function parseKeyValue() for this action
73 LogRel(("Parsing the line %d \n", i));
74 Utf8Str strLineContent = getContentOfLine(i);
75 if (strLineContent.isEmpty() || strLineContent.startsWith("#"))
76 continue;
77
78 LogRel(("Content of the line %d is %s \n", i, strLineContent.c_str()));
79
80 if ( strLineContent.startsWith("[") && strLineContent.endsWith("]") )
81 {
82 LogRel(("Found section in the line %d\n", i));
83 if ( fInSection == true )
84 {
85 if ( i > startSectionPos )//at least we can have here 1 line in the section
86 {
87 LogRel(("Add section \"%s\" to the map \n", strSectionName.c_str()));
88 mSections.insert(make_pair(strSectionName, sectionConfiguration));
89 sectionConfiguration.clear();
90 strSectionName.append(Utf8StrFmt("%d",i).c_str());
91 }
92 }
93
94 strSectionName = strLineContent.substr(strLineContent.find("[")+1,
95 strLineContent.find("]")-1);
96 LogRel(("Section name is \"%s\" \n", strSectionName.c_str()));
97 fInSection = true;
98 startSectionPos = i+1;
99 }
100 else
101 {
102 if ( fInSection == true )
103 {
104 LogRel(("Continue to parse section \"%s\" \n", strSectionName.c_str()));
105 if ( i >= startSectionPos )
106 {
107 LogRel(("Extracting key and value:\n"));
108 Utf8Str key, value;
109 size_t offEnd = strLineContent.parseKeyValue(key, value);
110 if (offEnd == strLineContent.length())
111 {
112 LogRel(("%s=%s \n", key.c_str(), value.c_str()));
113 sectionConfiguration.insert(make_pair(key,value));
114 }
115 else
116 {
117 //else something goes wrong, skip the line
118 LogRel(("Coudn't extract key and value from the line %d\n", i));
119 }
120 }
121 }
122 else
123 {
124 LogRel(("Goes to the next line %d\n", i+1));
125 }
126 }
127 }
128
129 if (fInSection == false)//there is not any section
130 {
131 LogRel(("There are not any sections in the config\n"));
132 hrc = VWRN_NOT_FOUND;
133 }
134 else //the last section hasn't a close tag (the close tag is just the beginning of next section)
135 {
136 //just insert the last section into the map
137 LogRel(("Add the last section %s to the map \n", strSectionName.c_str()));
138 mSections.insert(make_pair(strSectionName, sectionConfiguration));
139 }
140 }
141
142 return hrc;
143}
144
145HRESULT SimpleConfigFile::addSection(const Utf8Str &aSectionName, const std::map <Utf8Str, Utf8Str>& section)
146{
147 HRESULT hrc = S_OK;
148 if (aSectionName.isEmpty())
149 hrc = VERR_INVALID_PARAMETER;
150 else
151 mSections.insert(make_pair(aSectionName, section));
152
153 return hrc;
154}
155
156std::vector <Utf8Str> SimpleConfigFile::getSectionsNames() const
157{
158 std::vector <Utf8Str> res;
159 std::map < Utf8Str, std::map <Utf8Str, Utf8Str> >::const_iterator cit = mSections.begin();
160 while (cit!=mSections.end())
161 {
162 res.push_back(cit->first);
163 ++cit;
164 }
165 return res;
166}
167
168std::map <Utf8Str, Utf8Str> SimpleConfigFile::getSectionByName (const Utf8Str &strSectionName) const
169{
170 std::map <Utf8Str, Utf8Str> res;
171 std::map < Utf8Str, std::map <Utf8Str, Utf8Str> >::const_iterator cit;
172 if ( (cit=mSections.find(strSectionName)) != mSections.end() )
173 {
174 res=cit->second;
175 }
176 return res;
177}
178
179HRESULT SimpleConfigFile::updateSection (const Utf8Str &strSectionName,
180 const std::map <Utf8Str, Utf8Str> &newSection)
181{
182 HRESULT hrc = S_OK;
183 std::map <Utf8Str, Utf8Str> oldSection = getSectionByName(strSectionName);
184 if (oldSection.empty())
185 {
186 //add new section
187 hrc = addSection(strSectionName, newSection);
188 }
189 else
190 {
191 //update old section by new values or add new pair key/value if there isn't such key
192 std::map <Utf8Str, Utf8Str>::const_iterator cit = newSection.begin();
193 while (cit != newSection.end())
194 {
195 oldSection[cit->first] = cit->second;
196 ++cit;
197 }
198
199 }
200 return hrc;
201}
202
203bool SimpleConfigFile::isSectionExist(const Utf8Str &strSectionName) const
204{
205 return ((mSections.find(strSectionName) == mSections.end()) ? false : true);
206}
207
208////////////////////////////////////////////////////////////////////////////////
209//
210// ICloudUserProfileList implementation
211//
212////////////////////////////////////////////////////////////////////////////////
213CloudUserProfileList::CloudUserProfileList()
214 : mParent(NULL)
215{
216}
217
218CloudUserProfileList::~CloudUserProfileList()
219{
220 LogRel(("CloudUserProfileListImpl::~CloudUserProfileListImpl()\n"));
221 unconst(mParent) = NULL;
222}
223
224HRESULT CloudUserProfileList::FinalConstruct()
225{
226 return BaseFinalConstruct();
227}
228
229void CloudUserProfileList::FinalRelease()
230{
231 uninit();
232
233 BaseFinalRelease();
234}
235
236void CloudUserProfileList::uninit()
237{
238 /* Enclose the state transition Ready->InUninit->NotReady */
239 AutoUninitSpan autoUninitSpan(this);
240 if (autoUninitSpan.uninitDone())
241 return;
242
243 unconst(mParent) = NULL;
244}
245
246HRESULT CloudUserProfileList::init(VirtualBox *aParent)
247{
248 /* Enclose the state transition NotReady->InInit->Ready */
249 AutoInitSpan autoInitSpan(this);
250 AssertReturn(autoInitSpan.isOk(), E_FAIL);
251
252 unconst(mParent) = aParent;
253
254 autoInitSpan.setSucceeded();
255 return S_OK;
256}
257
258
259HRESULT CloudUserProfileList::getSupportedPropertiesNames(std::vector<com::Utf8Str> &aPropertiesNames)
260{
261 LogRel(("CloudUserProfileList::getSupportedPropertiesNames:\n"));
262 aPropertiesNames.clear();
263 return VERR_NOT_IMPLEMENTED;
264}
265
266HRESULT CloudUserProfileList::readProfiles(const Utf8Str &strConfigPath)
267{
268 LogRel(("CloudUserProfileList::readProfiles: %s\n", strConfigPath.c_str()));
269 return VERR_NOT_IMPLEMENTED;
270}
271
272HRESULT CloudUserProfileList::getProvider(CloudProviderId_T *aProvider)
273{
274 *aProvider = CloudProviderId_Unknown;
275 LogRel(("CloudUserProfileList::getProvider: %d\n", *aProvider));
276 return VERR_NOT_IMPLEMENTED;
277}
278
279HRESULT CloudUserProfileList::createProfile(const com::Utf8Str &aProfileName,
280 const std::vector<com::Utf8Str> &aNames,
281 const std::vector<com::Utf8Str> &aValues)
282{
283 LogRel(("CloudUserProfileList::createProfile: %s, %d, %d\n", aProfileName.c_str(), aNames.size(), aValues.size()));
284 return VERR_NOT_IMPLEMENTED;
285}
286
287HRESULT CloudUserProfileList::updateProfile(const com::Utf8Str &aProfileName,
288 const std::vector<com::Utf8Str> &aNames,
289 const std::vector<com::Utf8Str> &aValues)
290{
291 LogRel(("CloudUserProfileList::updateProfile: %s, %d, %d\n", aProfileName.c_str(), aNames.size(), aValues.size()));
292 return VERR_NOT_IMPLEMENTED;
293}
294
295HRESULT CloudUserProfileList::getStoredProfilesNames(std::vector<com::Utf8Str> &aProfilesNames)
296{
297
298 LogRel(("CloudUserProfileList::getStoredProfilesNames:\n"));
299 aProfilesNames.clear();
300 return VERR_NOT_IMPLEMENTED;
301}
302
303HRESULT CloudUserProfileList::getProfileProperties(const com::Utf8Str &aProfileName,
304 std::vector<com::Utf8Str> &aReturnNames,
305 std::vector<com::Utf8Str> &aReturnValues)
306{
307 LogRel(("CloudUserProfileList::getProfileProperties: %s\n", aProfileName.c_str()));
308 aReturnNames.clear();
309 aReturnValues.clear();
310 return VERR_NOT_IMPLEMENTED;
311}
312
313HRESULT CloudUserProfileList::getPropertyDescription(const com::Utf8Str &aName,
314 com::Utf8Str &aDescription)
315{
316 LogRel(("CloudUserProfileList::getPropertyDescription: %s, %s\n", aName.c_str(), aDescription.c_str()));
317 return VERR_NOT_IMPLEMENTED;
318}
319
320HRESULT CloudUserProfileList::createCloudClient(const com::Utf8Str &aProfileName,
321 ComPtr<ICloudClient> &aCloudClient)
322{
323 LogRel(("CloudUserProfileList::createCloudClient: %s\n", aProfileName.c_str()));
324
325 if (aCloudClient.isNull())
326 {
327 LogRel(("aCloudClient is NULL\n"));
328 }
329
330 return VERR_NOT_IMPLEMENTED;
331}
332
333////////////////////////////////////////////////////////////////////////////////
334//
335// CloudConnectionOCI implementation
336//
337////////////////////////////////////////////////////////////////////////////////
338static struct
339{
340 const char *pszOCIConfigEntry, *pszDesciption;
341} const g_aOCIConfigEntryToDescription[] =
342{
343 { "user", "OCID of the user calling the API." },
344 { "tenancy", "OCID of your tenancy." },
345 { "compartment", "OCID of your compartment." },
346 { "fingerprint", "Fingerprint for the key pair being used." },
347 { "key_file", "Full path and filename of the private key."
348 "If you encrypted the key with a passphrase, you must also include "
349 "the pass_phrase entry in the config file."},
350 { "pass_phrase", "Passphrase used for the key, if it is encrypted." },
351 { "region", "An Oracle Cloud Infrastructure region" },
352};
353
354
355OCIUserProfileList::OCIUserProfileList()
356{
357 LogRel(("OCIUserProfileList::OCIUserProfileList()\n"));
358 mpProfiles = new SimpleConfigFile(mParent);
359 LogRel(("Succeeded create SimpleConfigFile\n"));
360}
361
362OCIUserProfileList::~OCIUserProfileList()
363{
364 LogRel(("OCIUserProfileList::~OCIUserProfileList()\n"));
365 if (mpProfiles)
366 delete mpProfiles;
367}
368
369HRESULT OCIUserProfileList::createCloudClient(const com::Utf8Str &aProfileName,
370 ComPtr<ICloudClient> &aCloudClient)
371{
372 HRESULT hrc = S_OK;
373 CloudProviderId_T providerId;
374 hrc = getProvider(&providerId);
375
376 ComObjPtr<CloudClient> ptrCloudClient;
377 hrc = ptrCloudClient.createObject();
378 if (SUCCEEDED(hrc))
379 {
380 AutoReadLock wlock(this COMMA_LOCKVAL_SRC_POS);
381 hrc = ptrCloudClient->initCloudClient(this, mParent, providerId, aProfileName);
382 if (SUCCEEDED(hrc))
383 {
384 hrc = ptrCloudClient.queryInterfaceTo(aCloudClient.asOutParam());
385 }
386 }
387
388 return hrc;
389}
390
391HRESULT OCIUserProfileList::readProfiles(const Utf8Str &strConfigPath)
392{
393 LogRel(("Reading profiles from %s\n", strConfigPath.c_str()));
394 HRESULT hrc = S_OK;
395 if ( !strConfigPath.isEmpty() )
396 {
397 mStrConfigPath = strConfigPath;
398 hrc = mpProfiles->read(mStrConfigPath);
399 if (SUCCEEDED(hrc))
400 {
401 LogRel(("Successfully read the profiles from the config %s\n", mStrConfigPath.c_str()));
402 hrc = mpProfiles->parse();
403 if (FAILED(hrc))
404 {
405 throw hrc;
406 }
407 LogRel(("Successfully parsed %d profiles\n", mpProfiles->getNumberOfSections()));
408 }
409 }
410 else
411 {
412 LogRel(("Empty path to config file\n"));
413 hrc = VERR_INVALID_PARAMETER;
414 }
415
416 return hrc;
417}
418
419HRESULT OCIUserProfileList::createProfile(const com::Utf8Str &aProfileName,
420 const std::vector<com::Utf8Str> &aNames,
421 const std::vector<com::Utf8Str> &aValues)
422{
423 HRESULT hrc = S_OK;
424
425 if (!mpProfiles->isSectionExist(aProfileName))
426 {
427 std::map <Utf8Str, Utf8Str> newProfile;
428
429 for (size_t i=0;i<aNames.size();++i)
430 {
431 com::Utf8Str newValue = (i<aValues.size()) ? aValues.at(i) : "";
432 newProfile[aNames.at(i)] = newValue;
433 }
434
435 hrc = mpProfiles->addSection(aProfileName, newProfile);
436 }
437 else
438 hrc = VERR_ALREADY_EXISTS;
439
440 return hrc;
441}
442
443HRESULT OCIUserProfileList::updateProfile(const com::Utf8Str &aProfileName,
444 const std::vector<com::Utf8Str> &aNames,
445 const std::vector<com::Utf8Str> &aValues)
446{
447 HRESULT hrc = S_OK;
448 if (mpProfiles->isSectionExist(aProfileName))
449 {
450 std::map <Utf8Str, Utf8Str> newProfile;
451
452 for (size_t i=0;i<aNames.size();++i)
453 {
454 com::Utf8Str newValue = (i<aValues.size()) ? aValues.at(i) : "";
455 newProfile[aNames.at(i)] = newValue;
456 }
457
458 hrc = mpProfiles->updateSection(aProfileName, newProfile);
459 }
460 else
461 hrc = VERR_NOT_FOUND;
462
463 return hrc;
464}
465
466HRESULT OCIUserProfileList::getStoredProfilesNames(std::vector<com::Utf8Str> &aProfilesNames)
467{
468 HRESULT hrc = S_OK;
469 aProfilesNames = mpProfiles->getSectionsNames();
470 if (aProfilesNames.empty())
471 hrc = VERR_NOT_FOUND;
472
473 return hrc;
474}
475
476HRESULT OCIUserProfileList::getProfileProperties(const com::Utf8Str &aProfileName,
477 std::vector<com::Utf8Str> &aReturnNames,
478 std::vector<com::Utf8Str> &aReturnValues)
479{
480 HRESULT hrc = S_OK;
481
482 if (mpProfiles->isSectionExist(aProfileName))
483 {
484 std::map <Utf8Str, Utf8Str> profile;
485 hrc = i_getProfileProperties(aProfileName, profile);
486 if (SUCCEEDED(hrc))
487 {
488 aReturnNames.clear();
489 aReturnValues.clear();
490 std::map <Utf8Str, Utf8Str>::const_iterator cit = profile.begin();
491 while (cit!=profile.end())
492 {
493 aReturnNames.push_back(cit->first);
494 aReturnValues.push_back(cit->second);
495 ++cit;
496 }
497 }
498 }
499 else
500 hrc = VERR_NOT_FOUND;
501
502 return hrc;
503}
504
505HRESULT OCIUserProfileList::getSupportedPropertiesNames(std::vector<com::Utf8Str> &aPropertiesNames)
506{
507 HRESULT hrc = S_OK;
508 for (size_t i = 0; i < RT_ELEMENTS(g_aOCIConfigEntryToDescription); ++i)
509 aPropertiesNames.push_back(g_aOCIConfigEntryToDescription[i].pszOCIConfigEntry);
510 return hrc;
511}
512
513HRESULT OCIUserProfileList::getPropertyDescription(const com::Utf8Str &aName, com::Utf8Str &aDescription)
514{
515 HRESULT hrc = S_OK;
516 for (size_t i = 0; i < RT_ELEMENTS(g_aOCIConfigEntryToDescription); ++i)
517 if (aName.contains(g_aOCIConfigEntryToDescription[i].pszOCIConfigEntry, Utf8Str::CaseInsensitive))
518 {
519 aDescription = g_aOCIConfigEntryToDescription[i].pszDesciption;
520 }
521 return hrc;
522}
523
524
525HRESULT OCIUserProfileList::i_createProfile(const com::Utf8Str &aProfileName,
526 const std::map <Utf8Str, Utf8Str> &aProfile)
527{
528 HRESULT hrc = S_OK;
529
530 hrc = mpProfiles->addSection(aProfileName, aProfile);
531
532 return hrc;
533}
534
535HRESULT OCIUserProfileList::i_updateProfile(const com::Utf8Str &aProfileName,
536 const std::map <Utf8Str, Utf8Str> &aProfile)
537{
538 HRESULT hrc = S_OK;
539 if (mpProfiles->isSectionExist(aProfileName))
540 hrc = mpProfiles->updateSection(aProfileName, aProfile);
541 else
542 hrc = VERR_NOT_FOUND;
543
544 return hrc;
545}
546
547HRESULT OCIUserProfileList::i_getProfileProperties(const com::Utf8Str &aProfileName,
548 std::map <Utf8Str, Utf8Str> &aProfile)
549{
550 HRESULT hrc = S_OK;
551 std::map <Utf8Str, Utf8Str> defProfile = mpProfiles->getSectionByName("DEFAULT");
552 std::map <Utf8Str, Utf8Str> reqProfile = mpProfiles->getSectionByName(aProfileName);
553
554 std::map <Utf8Str, Utf8Str>::iterator itDefProfile = defProfile.begin();
555 while (itDefProfile != defProfile.end())
556 {
557 std::map <Utf8Str, Utf8Str>::iterator itProfile = reqProfile.find(itDefProfile->first);
558 if (itProfile == reqProfile.end())
559 {
560 //Add a key=value pair from defProfile into the reqProfile if the key doesn't exist in the reqProfile.
561 reqProfile.insert(*itDefProfile);
562 }
563 ++itDefProfile;
564 }
565
566 if (!reqProfile.empty())
567 aProfile = reqProfile;
568 else
569 hrc = VERR_NOT_FOUND;
570
571 return hrc;
572}
573
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