jenkins-bot has submitted this change and it was merged.
Change subject: Add API list=novainstances
......................................................................
Add API list=novainstances
Change-Id: I9dedd527bd7a4ca611fb1aa85bf73dd202a679f2
---
M OpenStackManager.php
A api/ApiListNovaInstances.php
M nova/OpenStackNovaController.php
M nova/OpenStackNovaProject.php
4 files changed, 107 insertions(+), 1 deletion(-)
Approvals:
MaxSem: Looks good to me, approved
jenkins-bot: Verified
diff --git a/OpenStackManager.php b/OpenStackManager.php
index e341810..bf503df 100644
--- a/OpenStackManager.php
+++ b/OpenStackManager.php
@@ -196,6 +196,7 @@
$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';
@@ -302,6 +303,7 @@
$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..be90e63
--- /dev/null
+++ b/api/ApiListNovaInstances.php
@@ -0,0 +1,89 @@
+<?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() {
+ $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' );
+ }
+
+ if ( !$this->getUser()->isLoggedIn() ) {
+ $this->dieUsage( 'Must be logged in to use this API',
'notloggedin' );
+ }
+
+ $user = new OpenStackNovaUser();
+ if ( !$user->exists() ) {
+ $this->dieUsage( 'NovaUser does not exist', 'baduser' );
+ }
+ $controller = OpenStackNovaController::newFromUser( $user );
+ $controller->setProject( $project->getName() );
+ $controller->setRegion( $params['region'] ); // validated by API
+ $instances = $controller->getInstances();
+ foreach ( $instances as $instance ) {
+ $info = array(
+ 'name' => $instance->getInstanceName(),
+ 'state' => $instance->getInstanceState(),
+ 'ip' => $instance->getInstancePrivateIPs(),
+ 'id' => $instance->getInstanceId(),
+ 'floatingip' =>
$instance->getInstancePublicIPs(),
+ 'securitygroups' =>
$instance->getSecurityGroups(),
+ 'imageid' => $instance->getImageId(),
+ );
+
+ // 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/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 9bd8d3b..511b67f 100644
--- a/nova/OpenStackNovaProject.php
+++ b/nova/OpenStackNovaProject.php
@@ -668,6 +668,21 @@
}
/**
+ * 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.
--
To view, visit https://gerrit.wikimedia.org/r/165894
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I9dedd527bd7a4ca611fb1aa85bf73dd202a679f2
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/OpenStackManager
Gerrit-Branch: wmf/1.25wmf2
Gerrit-Owner: Legoktm <[email protected]>
Gerrit-Reviewer: MaxSem <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits