def createHardwareProfile(self): """ Create hardware profile """ response = None postdata = cherrypy.request.json hwProfileDict = postdata['hardwareProfile'] settingsDict = postdata['settingsDict'] \ if 'settingsDict' in postdata else {} if 'osInfo' in settingsDict and settingsDict['osInfo']: settingsDict['osInfo'] = OsInfo.getFromDict(settingsDict['osInfo']) hwProfile = HardwareProfile.getFromDict(hwProfileDict) try: HardwareProfileManager().createHardwareProfile( cherrypy.request.db, hwProfile, settingsDict=settingsDict) except Exception as ex: self.getLogger().exception( 'hardware profile WS API createHardwareProfile() failed') self.getLogger().exception(ex) self.handleException(ex) response = self.errorResponse(str(ex)) return self.formatResponse(response)
def updateHardwareProfile(self, hardwareProfileId): """ Handle PUT to "hardwareprofiles/:(hardwareProfileId)" """ response = None try: postdata = cherrypy.request.json hw_profile = HardwareProfile.getFromDict(postdata) hw_profile.setId(hardwareProfileId) hp_mgr = HardwareProfileManager() hp_mgr.updateHardwareProfile(cherrypy.request.db, hw_profile) except HardwareProfileNotFound as ex: self.handleException(ex) code = self.getTortugaStatusCode(ex) response = self.notFoundErrorResponse(str(ex), code) except Exception as ex: self._logger.exception( 'hardware profile WS API updateHardwareProfile() failed') self.handleException(ex) response = self.errorResponse(str(ex)) return self.formatResponse(response)
def updateHardwareProfile(self, hardwareProfileId): ''' Handle PUT to "hardwareprofiles/:(hardwareProfileId)" ''' response = None try: postdata = cherrypy.request.json hwProfile = HardwareProfile.getFromDict(postdata) # Make sure the id is synced hwProfile.setId(hardwareProfileId) hpMgr = HardwareProfileManager() hpMgr.updateHardwareProfile(hwProfile) except HardwareProfileNotFound as ex: self.handleException(ex) code = self.getTortugaStatusCode(ex) response = self.notFoundErrorResponse(str(ex), code) except Exception as ex: self.getLogger().exception( 'hardware profile WS API updateHardwareProfile() failed') self.handleException(ex) response = self.errorResponse(str(ex)) return self.formatResponse(response)
def test_deserialization(): name = 'testEXAMPLE' cfg_name = 'configEXAMPLE' hp_dict = { 'name': name, 'default_resource_adapter_config': cfg_name, } hp = HardwareProfile.getFromDict(hp_dict) assert hp.getName() == name assert hp.getDefaultResourceAdapterConfig() == cfg_name
def getHardwareProfileById(self, id_, optionDict=None): """ Get hardware profile by name """ postdata = json.dumps(optionDict or {}) url = 'v1/hardwareProfiles/id/%s' % (id_) try: (response, responseDict) = self.sendSessionRequest(url, method='POST', data=postdata) return HardwareProfile.getFromDict( responseDict.get('hardwareprofile')) except TortugaException as ex: raise except Exception as ex: raise TortugaException(exception=ex)
def getHardwareProfileById(self, id_, optionDict: Optional[Union[dict, None]] = None): """ Get hardware profile by name """ url = 'hardwareprofiles/%d' % (id_) if optionDict: for key, value in optionDict.items(): if not value: continue url += '&include={}'.format(key) try: responseDict = self.get(url) return HardwareProfile.getFromDict( responseDict.get('hardwareprofile')) except TortugaException as ex: raise except Exception as ex: raise TortugaException(exception=ex)
def runCommand(self): self.parseArgs() if self.getArgs().name and self.getArgs().deprecated_name: self.getParser().error( 'argument name: not allowed with argument --name') name = self.getArgs().name \ if self.getArgs().name else self.getArgs().deprecated_name if self.getArgs().jsonTemplatePath: # load from template if self.getArgs().jsonTemplatePath and \ not os.path.exists(self.getArgs().jsonTemplatePath): raise InvalidCliRequest( _('Cannot read template from %s') % (self.getArgs().jsonTemplatePath)) try: with open(self.getArgs().jsonTemplatePath) as fp: buf = json.load(fp) tmpl_dict = buf['hardwareProfile'] except Exception as exc: raise InvalidProfileCreationTemplate( 'Invalid profile creation template: {}'.format(exc)) else: tmpl_dict = {} # build up dict from scratch if name: tmpl_dict['name'] = name if self.getArgs().description: tmpl_dict['description'] = self.getArgs().description if hasattr(self.getArgs(), 'osInfo'): tmpl_dict['os'] = { 'name': getattr(self.getArgs(), 'osInfo').getName(), 'version': getattr(self.getArgs(), 'osInfo').getVersion(), 'arch': getattr(self.getArgs(), 'osInfo').getArch(), } if self.getArgs().nameFormat: tmpl_dict['nameFormat'] = self.getArgs().nameFormat elif 'nameFormat' not in tmpl_dict: tmpl_dict['nameFormat'] = 'compute-#NN' if self.getArgs().tags: tmpl_dict['tags'] = parse_tags(self.getArgs().tags) settings_dict = { 'defaults': self.getArgs().bUseDefaults, 'osInfo': getattr(self.getArgs(), 'osInfo') if hasattr( self.getArgs(), 'osInfo') else None, } hw_profile_spec = HardwareProfile.getFromDict(tmpl_dict) api = HardwareProfileWsApi(username=self.getUsername(), password=self.getPassword(), baseurl=self.getUrl(), verify=self._verify) api.createHardwareProfile(hw_profile_spec, settings_dict)