Yuvipanda has uploaded a new change for review. https://gerrit.wikimedia.org/r/166742
Change subject: Adds list=novaprojects and list=novainstances API ...................................................................... Adds list=novaprojects and list=novainstances API Squash of the following changes: I4667b02b2ab096677a3f04d131d5d4c24bb27dda I9dedd527bd7a4ca611fb1aa85bf73dd202a679f2 Ia9ed925c1e75fce29baf9a1b81cf2a23b0eb6e0f I1b56ba9688315b5b4ca17ad527db5e8f5e9c2393 Change-Id: I5cba45d61651c0bcfa6360a0110a87827cb31d98 --- M OpenStackManager.php A api/ApiListNovaInstances.php A api/ApiListNovaProjects.php M nova/OpenStackNovaController.php M nova/OpenStackNovaProject.php 5 files changed, 163 insertions(+), 2 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/OpenStackManager refs/changes/42/166742/1 diff --git a/OpenStackManager.php b/OpenStackManager.php index 011a4c1..bf503df 100644 --- a/OpenStackManager.php +++ b/OpenStackManager.php @@ -195,6 +195,8 @@ $wgAutoloadClasses['ApiNovaProjects'] = $dir . 'api/ApiNovaProjects.php'; $wgAutoloadClasses['ApiNovaProjectLimits'] = $dir . 'api/ApiNovaProjectLimits.php'; $wgAutoloadClasses['ApiNovaServiceGroups'] = $dir . 'api/ApiNovaServiceGroups.php'; +$wgAutoloadClasses['ApiListNovaProjects'] = $dir . 'api/ApiListNovaProjects.php'; +$wgAutoloadClasses['ApiListNovaInstances'] = $dir . 'api/ApiListNovaInstances.php'; $wgAutoloadClasses['Spyc'] = $dir . 'Spyc.php'; $wgAutoloadClasses['OpenStackManagerNotificationFormatter'] = $dir . 'OpenStackManagerNotificationFormatter.php'; $wgAutoloadClasses['OpenStackManagerEvent'] = $dir . 'OpenStackManagerEvent.php'; @@ -300,6 +302,8 @@ $wgAPIModules['novaprojects'] = 'ApiNovaProjects'; $wgAPIModules['novaservicegroups'] = 'ApiNovaServiceGroups'; $wgAPIModules['novaprojectlimits'] = 'ApiNovaProjectLimits'; +$wgAPIListModules['novaprojects'] = 'ApiListNovaProjects'; +$wgAPIListModules['novainstances'] = 'ApiListNovaInstances'; # Schema changes $wgHooks['LoadExtensionSchemaUpdates'][] = 'efOpenStackSchemaUpdates'; diff --git a/api/ApiListNovaInstances.php b/api/ApiListNovaInstances.php new file mode 100644 index 0000000..5350a20 --- /dev/null +++ b/api/ApiListNovaInstances.php @@ -0,0 +1,99 @@ +<?php + +class ApiListNovaInstances extends ApiQueryGeneratorBase { + + public function __construct( ApiQuery $query, $moduleName ) { + parent::__construct( $query, $moduleName, 'ni' ); + } + + public function execute() { + $this->run(); + } + + public function executeGenerator( $resultPageSet ) { + $this->run(); + } + + public function run() { + global $wgOpenStackManagerLDAPUsername; + global $wgOpenStackManagerLDAPUserPassword; + global $wgMemc; + + $params = $this->extractRequestParams(); + $project = OpenStackNovaProject::getProjectByName( $params['project'] ); + if ( !$project ) { + // This shouldn't be possible since the API should enforce valid names + $this->dieUsage( 'Invalid project specified.', 'badproject' ); + } + + $key = wfMemcKey( 'openstackmanager', 'apilistnovainstances', $params['region'], $params['project'] ); + $instancesInfo = $wgMemc->get( $key ); + if ( $instancesInfo === false ) { + $user = new OpenStackNovaUser( $wgOpenStackManagerLDAPUsername ); + $userNova = OpenStackNovaController::newFromUser( $user ); + $userNova->authenticate( $wgOpenStackManagerLDAPUsername, $wgOpenStackManagerLDAPUserPassword ); + + $userNova->setProject( $project->getName() ); + $userNova->setRegion( $params['region'] ); // validated by API + + $instances = $userNova->getInstances(); + $instancesInfo = array(); + foreach ( $instances as $instance ) { + $instancesInfo[ ] = array( + 'name' => $instance->getInstanceName(), + 'state' => $instance->getInstanceState(), + 'ip' => $instance->getInstancePrivateIPs(), + 'id' => $instance->getInstanceId(), + 'floatingip' => $instance->getInstancePublicIPs(), + 'securitygroups' => $instance->getSecurityGroups(), + 'imageid' => $instance->getImageId(), + ); + } + } + + // Cache info for 1 minute, not caching for longer since we do not invalidate + $wgMemc->set( $key, $instancesInfo, 1 * 60 ); + + foreach ( $instancesInfo as $info ) { + // UGH I hate XML + $this->getResult()->setIndexedTagName( $info['securitygroups'], 'group' ); + $this->getResult()->setIndexedTagName( $info['ip'], 'ip' ); + $this->getResult()->setIndexedTagName( $info['floatingip'], 'floatingip' ); + + $this->getResult()->addValue( array( 'query', $this->getModuleName() ), null, $info ); + } + + $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'instance' ); + } + + /** + * HACK: I can't figure out the proper way to do this + */ + private function getRegions() { + global $wgOpenStackManagerProxyGateways; + return array_keys( $wgOpenStackManagerProxyGateways ); + } + + public function getAllowedParams() { + return array( + 'project' => array( + ApiBase::PARAM_TYPE => OpenStackNovaProject::getAllProjectNames(), + ApiBase::PARAM_REQUIRED => true, + ), + 'region' => array( + ApiBase::PARAM_TYPE => $this->getRegions(), + ApiBase::PARAM_REQUIRED => true, + ) + ); + } + + public function getDescription() { + return array( + 'Returns a list of instances for the given project' + ); + } + + public function getExamples() { + return 'api.php?action=query&list=novainstances&niproject=cvn&niregion=eqiad'; + } +} diff --git a/api/ApiListNovaProjects.php b/api/ApiListNovaProjects.php new file mode 100644 index 0000000..830ba56 --- /dev/null +++ b/api/ApiListNovaProjects.php @@ -0,0 +1,39 @@ +<?php + +class ApiListNovaProjects extends ApiQueryGeneratorBase { + + public function __construct( ApiQuery $query, $moduleName ) { + parent::__construct( $query, $moduleName, 'np' ); + } + + public function execute() { + $this->run(); + } + + public function executeGenerator( $resultPageSet ) { + $this->run(); + } + + public function run() { + $projects = OpenStackNovaProject::getAllProjects(); + foreach ( $projects as $project ) { + $this->getResult()->addValue( + array( 'query', $this->getModuleName() ), + null, + $project->getName() + ); + } + + $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'project' ); + } + + public function getDescription() { + return array( + 'Returns a list of all the known projects' + ); + } + + public function getExamples() { + return 'api.php?action=query&list=novaprojects'; + } +} diff --git a/nova/OpenStackNovaController.php b/nova/OpenStackNovaController.php index 55d337d..df8b74b 100644 --- a/nova/OpenStackNovaController.php +++ b/nova/OpenStackNovaController.php @@ -238,7 +238,7 @@ } /** - * @return array + * @return OpenStackNovaInstance[] */ function getInstances() { $instancesarr = array(); diff --git a/nova/OpenStackNovaProject.php b/nova/OpenStackNovaProject.php index 09ccf37..511b67f 100644 --- a/nova/OpenStackNovaProject.php +++ b/nova/OpenStackNovaProject.php @@ -36,6 +36,10 @@ } } + public function getName() { + return $this->projectname; + } + /** * Fetch the project from LDAP and initialize the object * @return void @@ -664,12 +668,27 @@ } /** + * Get all project names + * + * @return string[] + */ + static function getAllProjectNames() { + $projects = self::getAllProjects(); + $names = array(); + foreach ( $projects as $project ) { + $names[] = $project->getName(); + } + + return $names; + } + + /** * Return all existing projects. Returns an empty array if no projects exist. This function * lazy loads the projects. Objects will be returned unloaded. If you wish to receive more * than just the project's name, you'll need to call the project's fetchProjectInfo() function. * * @static - * @return array + * @return OpenStackNovaProject[] */ static function getAllProjects() { global $wgAuth; -- To view, visit https://gerrit.wikimedia.org/r/166742 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I5cba45d61651c0bcfa6360a0110a87827cb31d98 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/OpenStackManager Gerrit-Branch: wmf/1.25wmf3 Gerrit-Owner: Yuvipanda <yuvipa...@gmail.com> Gerrit-Reviewer: Legoktm <legoktm.wikipe...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits