Andrew Bogott has submitted this change and it was merged.

Change subject: Add the 'NovaResources' special page.
......................................................................


Add the 'NovaResources' special page.

This is meant to help users clean up their resource usages... it lists
only projects that a user is admin in, and only instances that the user
created.

Change-Id: Icf01bdce266833da547557b88dcd3a273f131e2a
---
M OpenStackManager.i18n.php
M OpenStackManager.php
M nova/OpenStackNovaInstance.php
A special/SpecialNovaResources.php
4 files changed, 170 insertions(+), 0 deletions(-)

Approvals:
  Andrew Bogott: Verified; Looks good to me, approved
  jenkins-bot: Checked



diff --git a/OpenStackManager.i18n.php b/OpenStackManager.i18n.php
index 030ea09..8b5beae 100644
--- a/OpenStackManager.i18n.php
+++ b/OpenStackManager.i18n.php
@@ -48,6 +48,9 @@
        'openstackmanager-novakey' => 'Nova key',
        'openstackmanager-novaproject' => 'Nova project',
 
+       'openstackmanager-ownedprojects' => 'You are an administrator for these 
projects:',
+       'openstackmanager-ownedinstances' =>  'You created these instances:',
+
        'openstackmanager-delete' => 'delete',
        'openstackmanager-configure' => 'configure',
        'openstackmanager-modify' => 'modify',
@@ -119,6 +122,7 @@
        'openstackmanager-rebootinstancefailed' => 'Failed to reboot instance 
$1.',
 
        'openstackmanager-launchtime' => 'Launch time',
+       'openstackmanager-instancecreator' => 'Instance creator',
        'openstackmanager-createinstance' => 'Add instance',
        'openstackmanager-createinstancepuppetwarning' => 'Warning: Adding 
Puppet configuration on instance creation can cause the instance to fail to 
build.
 Use this configuration with caution.
@@ -517,6 +521,8 @@
        'novainstance' => '{{doc-special|NovaInstance}}
 An instance is a virtual machine. In this particular case, a clone of an image 
for a virtual machine. 
[http://docs.openstack.org/diablo/openstack-compute/starter/content/Introduction-d1e2084.html
 More on OpenStack].
 {{Identical|Manage instance}}',
+       'openstackmanager-ownedprojects' => 'Precedes a list of projects in 
which the user has the projectadmin role.',
+       'openstackmanager-ownedinstances' =>  'Precedes a list of instances 
created by the user.',
        'novakey' => '{{doc-special|NovaKey}}',
        'novaproject' => '{{doc-special|NovaProject}}',
        'novaservicegroup' => '{{doc-special|NovaServiceGroup}}',
@@ -679,6 +685,7 @@
 See also:
 * {{msg-mw|Openstackmanager-rebootedinstance}}',
        'openstackmanager-launchtime' => 'Used as heading cell in "resource" 
table, in Special:NovaInstance.',
+       'openstackmanager-creator' => 'Used as heading cell in "resource" 
table, in Special:NovaResources.',
        'openstackmanager-createinstance' => 'Used as page title and as action 
link title, in Special:NovaInstance.',
        'openstackmanager-createinstancepuppetwarning' => 'Used in 
Special:NovaInstance.
 
diff --git a/OpenStackManager.php b/OpenStackManager.php
index 3b9571c..a7e174d 100644
--- a/OpenStackManager.php
+++ b/OpenStackManager.php
@@ -176,6 +176,7 @@
 $wgAutoloadClasses['SpecialNovaSudoer'] = $dir . 
'special/SpecialNovaSudoer.php';
 $wgAutoloadClasses['SpecialNovaProxy'] = $dir . 'special/SpecialNovaProxy.php';
 $wgAutoloadClasses['SpecialNovaPuppetGroup'] = $dir . 
'special/SpecialNovaPuppetGroup.php';
+$wgAutoloadClasses['SpecialNovaResources'] = $dir . 
'special/SpecialNovaResources.php';
 $wgAutoloadClasses['SpecialNova'] = $dir . 'special/SpecialNova.php';
 $wgAutoloadClasses['ApiNovaInstance'] = $dir . 'api/ApiNovaInstance.php';
 $wgAutoloadClasses['ApiNovaAddress'] = $dir . 'api/ApiNovaAddress.php';
@@ -210,6 +211,8 @@
 $wgJobClasses['addDNSHostToLDAP'] = 'OpenStackNovaHostJob';
 $wgSpecialPageGroups['NovaPuppetGroup'] = 'nova';
 $wgSpecialPages['NovaPuppetGroup'] = 'SpecialNovaPuppetGroup';
+$wgSpecialPageGroups['NovaResources'] = 'nova';
+$wgSpecialPages['NovaResources'] = 'SpecialNovaResources';
 
 $wgHooks['LDAPSetCreationValues'][] = 
'OpenStackNovaUser::LDAPSetCreationValues';
 $wgHooks['LDAPRetrySetCreationValues'][] = 
'OpenStackNovaUser::LDAPRetrySetCreationValues';
diff --git a/nova/OpenStackNovaInstance.php b/nova/OpenStackNovaInstance.php
index 68215aa..f246ca0 100644
--- a/nova/OpenStackNovaInstance.php
+++ b/nova/OpenStackNovaInstance.php
@@ -123,6 +123,15 @@
        }
 
        /**
+        * Get ID of the instance creator
+        *
+        * @return string
+        */
+       function getInstanceCreator() {
+               return OpenStackNovaController::_get_property( $this->instance, 
'user_id' );
+       }
+
+       /**
         * Get a human friendly name + id of this instance
         *
         * @return string
diff --git a/special/SpecialNovaResources.php b/special/SpecialNovaResources.php
new file mode 100644
index 0000000..7f67640
--- /dev/null
+++ b/special/SpecialNovaResources.php
@@ -0,0 +1,151 @@
+<?php
+
+/**
+ * Special page to interact with a Nova instance.
+ *
+ * @file
+ * @ingroup Extensions
+ */
+
+/**
+ * Class to handle [[Special:NovaResources]].
+ *
+ * This page lists things that are 'owned' by the current users:
+ *   Projects are incuded if the user is a project admin, and
+ *   instances are listed if the user created the instance.
+ *
+ */
+class SpecialNovaResources extends SpecialNova {
+       /**
+        * @var OpenStackNovaController
+        */
+       var $userNova;
+
+       /**
+        * @var OpenStackNovaUser
+        */
+       var $userLDAP;
+
+       function __construct() {
+               parent::__construct( 'NovaResources' );
+       }
+
+       function execute( $par ) {
+               if ( !$this->getUser()->isLoggedIn() ) {
+                       $this->notLoggedIn();
+                       return;
+               }
+               $this->userLDAP = new OpenStackNovaUser();
+               if ( ! $this->userLDAP->exists() ) {
+                       $this->noCredentials();
+                       return;
+               }
+               $this->checkTwoFactor();
+               $project = $this->getRequest()->getVal( 'project' );
+               $region = $this->getRequest()->getVal( 'region' );
+               $this->userNova = OpenStackNovaController::newFromUser( 
$this->userLDAP );
+               $this->userNova->setProject( $project );
+               $this->userNova->setRegion( $region );
+
+               $this->listInstances();
+       }
+
+       /**
+        * Default action
+        * @return void
+        */
+       function listInstances() {
+               $this->setHeaders();
+               $this->getOutput()->addModules( 'ext.openstack.Instance' );
+               $this->getOutput()->setPagetitle( $this->msg( 
'openstackmanager-ownedprojects' ) );
+
+               $projects = OpenStackNovaProject::getProjectsByName( 
$this->userLDAP->getProjects() );
+
+               $instanceOut = '';
+               $ownedProjects = array();
+               foreach ( $projects as $project ) {
+                       $projectName = $project->getProjectName();
+                       if ( $this->userLDAP->inRole( 'projectadmin', 
$projectName ) ) {
+                               $ownedProjects[] = $projectName;
+                       }
+                       $projectactions = array( 'projectadmin' => array() );
+                       $regions = '';
+                       $this->userNova->setProject( $projectName );
+                       $hasInstances = false;
+                       foreach ( $this->userNova->getRegions( 'compute' ) as 
$region ) {
+                               $regionactions = null;
+                               $instances = $this->getInstances( $projectName, 
$region );
+                               if ( $instances ) {
+                                       $hasInstances = true;
+                               }
+                               $regions .= $this->createRegionSection( 
$region, $projectName, $regionactions, $instances );
+                       }
+                       if ( $hasInstances ) {
+                           $instanceOut .= $this->createProjectSection( 
$projectName, $projectactions, $regions );
+                       }
+               }
+
+               $out = '';
+
+               foreach ( $ownedProjects as $ownedProject ) {
+                       $projectNameOut = $this->createResourceLink( 
$ownedProject );
+                       $out .= $projectNameOut . " ";
+               }
+
+               $out .= Html::rawElement( 'h1', array(), $this->msg( 
'openstackmanager-ownedinstances' )->text() );
+               $out .= $instanceOut;
+
+
+               $this->getOutput()->addHTML( $out );
+       }
+
+       function getInstances( $projectName, $region ) {
+               global $wgMemc;
+
+               $this->userNova->setRegion( $region );
+               $headers = array( 'openstackmanager-instancename', 
'openstackmanager-instanceid', 'openstackmanager-instancestate', 
'openstackmanager-instanceip', 'openstackmanager-projectname', 
'openstackmanager-launchtime', 'openstackmanager-instancecreator' );
+               $instances = $this->userNova->getInstances();
+               $instanceRows = array();
+               /**
+                * @var $instance OpenStackNovaInstance
+                */
+               foreach ( $instances as $instance ) {
+
+                       # Only display instances created by the current user.
+                       if ( $instance->getInstanceCreator() != 
$this->userLDAP->getUid() ) {
+                               continue;
+                       }
+
+                       $instanceRow = array();
+                       $this->pushResourceColumn( $instanceRow, 
$instance->getInstanceName(), array( 'class' => 'novainstancename' ) );
+                       $this->pushRawResourceColumn( $instanceRow, 
$this->createResourceLink( $instance->getInstanceId() ), array( 'class' => 
'novainstanceid' ) );
+                       $state = $instance->getInstanceState();
+                       $taskState = $instance->getInstanceTaskState();
+                       if ( $taskState ) {
+                               $stateDisplay = "$state ($taskState)";
+                       } else {
+                               $stateDisplay = $state;
+                       }
+                       $this->pushResourceColumn( $instanceRow, $stateDisplay, 
array( 'class' => 'novainstancestate' ) );
+                       $this->pushRawResourceColumn( $instanceRow, 
$this->createResourceList( $instance->getInstancePrivateIPs() ) );
+                       $this->pushResourceColumn( $instanceRow, $projectName );
+                       $this->pushResourceColumn( $instanceRow, 
$instance->getLaunchTime() );
+                       $this->pushResourceColumn( $instanceRow, 
$instance->getInstanceCreator() );
+                       $actions = array();
+                       $instanceDataAttributes = array(
+                               'data-osid' => $instance->getInstanceOSId(),
+                               'data-id' => $instance->getInstanceId(),
+                               'data-name' => $instance->getInstanceName(),
+                               'data-project' => $projectName,
+                               'data-region' => $region,
+                               'class' => 'novainstanceaction',
+                       );
+                       $instanceRows[] = $instanceRow;
+               }
+               if ( $instanceRows ) {
+                       return $this->createResourceTable( $headers, 
$instanceRows );
+               } else {
+                       return '';
+               }
+       }
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/96323
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Icf01bdce266833da547557b88dcd3a273f131e2a
Gerrit-PatchSet: 5
Gerrit-Project: mediawiki/extensions/OpenStackManager
Gerrit-Branch: master
Gerrit-Owner: Andrew Bogott <[email protected]>
Gerrit-Reviewer: Andrew Bogott <[email protected]>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to