jenkins-bot has submitted this change and it was merged.

Change subject: Add API action for instance reboots
......................................................................


Add API action for instance reboots

Change-Id: I72994ddedc6aa00c1273599d2dd39d771984cf03
---
M OpenStackManager.i18n.php
M OpenStackManager.php
A api/ApiNovaInstance.php
A api/ApiNovaProjects.php
M modules/ext.openstack.css
A modules/ext.openstack.js
M special/SpecialNova.php
M special/SpecialNovaInstance.php
8 files changed, 245 insertions(+), 8 deletions(-)

Approvals:
  Catrope: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/OpenStackManager.i18n.php b/OpenStackManager.i18n.php
index d3af614..bb2a174 100644
--- a/OpenStackManager.i18n.php
+++ b/OpenStackManager.i18n.php
@@ -115,7 +115,7 @@
        'openstackmanager-rebootinstancewithname' => 'Reboot instance $1 ($2)',
        'openstackmanager-rebootinstancequestion' => 'Are you sure you wish to 
reboot instance "$1"?',
        'openstackmanager-rebootedinstance' => 'Rebooted instance $1.',
-       'openstackmanager-rebootinstancefailed' => 'Failed to reboot instance.',
+       'openstackmanager-rebootinstancefailed' => 'Failed to reboot instance 
$1.',
 
        'openstackmanager-launchtime' => 'Launch time',
        'openstackmanager-createinstance' => 'Add instance',
@@ -614,6 +614,9 @@
 
 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].
 
+Parameters:
+* $1 - the human-readable name for the same instance
+
 See also:
 * {{msg-mw|Openstackmanager-rebootedinstance}}',
        'openstackmanager-launchtime' => 'Used as heading cell in "resource" 
table, in Special:NovaInstance.',
diff --git a/OpenStackManager.php b/OpenStackManager.php
index 528ba46..5954b77 100644
--- a/OpenStackManager.php
+++ b/OpenStackManager.php
@@ -168,6 +168,8 @@
 $wgAutoloadClasses['SpecialNovaSudoer'] = $dir . 
'special/SpecialNovaSudoer.php';
 $wgAutoloadClasses['SpecialNovaPuppetGroup'] = $dir . 
'special/SpecialNovaPuppetGroup.php';
 $wgAutoloadClasses['SpecialNova'] = $dir . 'special/SpecialNova.php';
+$wgAutoloadClasses['ApiNovaInstance'] = $dir . 'api/ApiNovaInstance.php';
+$wgAutoloadClasses['ApiNovaProjects'] = $dir . 'api/ApiNovaProjects.php';
 $wgAutoloadClasses['Spyc'] = $dir . 'Spyc.php';
 $wgAutoloadClasses['OpenStackManagerNotificationFormatter'] = $dir . 
'OpenStackManagerNotificationFormatter.php';
 $wgAutoloadClasses['OpenStackManagerEvent'] = $dir . 
'OpenStackManagerEvent.php';
@@ -210,8 +212,14 @@
 
 $wgResourceModules['ext.openstack'] = array(
        'styles' => 'ext.openstack.css',
+       'scripts' => 'ext.openstack.js',
+       'dependencies' => array( 'jquery.spinner' ),
+       'messages' => array( 'openstackmanager-rebootinstancefailed', 
'openstackmanager-rebootedinstance' ),
 ) + $commonModuleInfo;
 
+$wgAPIModules['novainstance'] = 'ApiNovaInstance';
+$wgAPIModules['novaprojects'] = 'ApiNovaProjects';
+
 # Schema changes
 $wgHooks['LoadExtensionSchemaUpdates'][] = 'efOpenStackSchemaUpdates';
 
diff --git a/api/ApiNovaInstance.php b/api/ApiNovaInstance.php
new file mode 100644
index 0000000..c858ef9
--- /dev/null
+++ b/api/ApiNovaInstance.php
@@ -0,0 +1,91 @@
+<?
+class ApiNovaInstance extends ApiBase {
+       var $userLDAP;
+       var $userNova;
+       var $params;
+
+       public function canExecute() {
+               if ( ! $this->userLDAP->exists() ) {
+                       $this->dieUsageMsg( 'openstackmanager-nonovacred' );
+               }
+               if ( ! $this->userLDAP->inProject( $this->params['project'] ) ) 
{
+                       $this->dieUsageMsg( 'openstackmanager-noaccount' );
+               }
+               if ( ! $this->userLDAP->inRole( 'projectadmin', 
$this->params['project'] ) ) {
+                       $this->dieUsageMsg( 'openstackmanager-needrole' );
+               }
+       }
+
+       function execute() {
+               $this->params = $this->extractRequestParams();
+
+               $this->userLDAP = new OpenStackNovaUser();
+               $this->canExecute();
+               $this->userNova = OpenStackNovaController::newFromUser( 
$this->userLDAP );
+               $this->userNova->setProject( $this->params['project'] );
+               $this->userNova->setRegion( $this->params['region'] );
+
+               switch( $this->params['subaction'] ) {
+               case 'reboot':
+                       $success = $this->userNova->rebootInstance( 
$this->params['instanceid'] );
+                       if ( ! $success ) {
+                               $this->dieUsageMsg( array( 
'openstackmanager-rebootinstancefailed', $this->params['instanceid'] ) );
+                       }
+                       $this->getResult()->addValue( null, 
$this->getModuleName(), array ( 'instancestate' => 'rebooting' ) );
+                       break;
+               }
+       }
+
+       public function getPossibleErrors() {
+               return array(
+                       array( 'openstackmanager-rebootinstancefailed', 
'instance' ),
+               );
+       }
+
+       // Face parameter.
+       public function getAllowedParams() {
+               return array(
+                       'subaction' => array (
+                               ApiBase::PARAM_TYPE => 'string',
+                               ApiBase::PARAM_REQUIRED => true
+                       ),
+                       'instanceid' => array (
+                               ApiBase::PARAM_TYPE => 'string',
+                               ApiBase::PARAM_REQUIRED => true
+                       ),
+                       'project' => array (
+                               ApiBase::PARAM_TYPE => 'string',
+                               ApiBase::PARAM_REQUIRED => true
+                       ),
+                       'region' => array (
+                               ApiBase::PARAM_TYPE => 'string',
+                               ApiBase::PARAM_REQUIRED => true
+                       ),
+               );
+       }
+ 
+       public function getParamDescription() {
+               return array_merge( parent::getParamDescription(), array(
+                       'subaction' => 'The subaction to perform.',
+                       'instanceid' => 'The Nova instance ID to perform a 
subaction on',
+                       'project' => 'The project in which the instance exists',
+                       'region' => 'The region in which the instance exists',
+               ) );
+       }
+
+       public function getDescription() {
+               return 'Perform actions on instances.';
+       }
+
+       public function getExamples() {
+               return array(
+                       
'api.php?action=novainstancereboot&instanceid=eb195097-8539-4e66-b0b5-be8347d8caec&project=testing&region=mars'
+                       => 'Reboot instance id 
eb195097-8539-4e66-b0b5-be8347d8caec in project testing in region mars',
+               );
+       }
+
+       public function mustBePosted() {
+               return true;
+       }
+
+}
diff --git a/api/ApiNovaProjects.php b/api/ApiNovaProjects.php
new file mode 100644
index 0000000..7c2c144
--- /dev/null
+++ b/api/ApiNovaProjects.php
@@ -0,0 +1,52 @@
+<?php
+class ApiNovaProjects extends ApiBase {
+
+       public function execute() {
+               $params = $this->extractRequestParams();
+
+               $projectNames = array();
+               if ( array_key_exists( 'getall', $params ) && $params['getall'] 
) {
+                       $projects = OpenStackNovaProject::getAllProjects();
+                       foreach ( $projects as $project ) {
+                               $projectNames[] = $project->getProjectName();
+                       }
+               } else {
+                       $userLDAP = new OpenStackNovaUser();
+                       $projectNames = $userLDAP->getProjects();
+               }
+
+               $this->getResult()->setIndexedTagName( $projectNames, 'project' 
);
+               $this->getResult()->addValue( null, $this->getModuleName(), 
$projectNames );
+       }
+
+       public function getAllowedParams() {
+               return array(
+                       'getall' => array (
+                               ApiBase::PARAM_TYPE => 'boolean',
+                       ),
+               );
+       }
+
+       public function getParamDescription() {
+               return array(
+                       'getall' => 'Fetch all projects, not just the projects 
associated with this user.',
+               );
+       }
+
+       public function getDescription() {
+               return array(
+                       'Get a list of OpenStack projects for this user or a 
list of all projects.',
+               );
+       }
+
+       public function getExamples() {
+               return array(
+                       'api.php?action=novaprojects&getall=true',
+               );
+       }
+
+       public function getVersion() {
+               return __CLASS__ . ': 1.0';
+       }
+
+}
diff --git a/modules/ext.openstack.css b/modules/ext.openstack.css
index b2861a8..30adad1 100644
--- a/modules/ext.openstack.css
+++ b/modules/ext.openstack.css
@@ -8,3 +8,28 @@
 #novaaction  {
 font-size: 67%;
 }
+.OpenStack_warning {
+/* @embed */
+background-image: url(images/warning-32.png);
+       background-position: center center;
+       background-repeat: no-repeat;
+       padding: 16px;
+       position: relative;
+       top: -16px;
+}
+.OpenStack_error {
+/* @embed */
+background-image: url(images/critical-32.png);
+       background-position: center center;
+       background-repeat: no-repeat;
+       padding: 16px;
+       position: relative;
+       top: -16px;
+}
+.OpenStack_success {
+/* @embed */
+background-image: url(images/tick-32.png);
+display: inline-block;
+height: 20px;
+width: 20px;
+}
diff --git a/modules/ext.openstack.js b/modules/ext.openstack.js
new file mode 100644
index 0000000..67aaedb
--- /dev/null
+++ b/modules/ext.openstack.js
@@ -0,0 +1,56 @@
+( function ( mw, $ ) {
+       'use strict';
+
+       $( '.novainstanceaction' ).click( function( event ) {
+               var $actionLink = $(this),
+                       $href = $actionLink.prop( 'href' ).split('&'),
+                       $spinner = $.createSpinner( {
+                               size: 'small',
+                               type: 'inline'
+                       } ),
+                       args = {},
+                       argarr,
+                       i,
+                       $state,
+                       $instancename;
+               $actionLink.hide().after( $spinner );
+               for ( i = 0; i < $href.length; i++ ) {
+                       argarr = $href[i].split( '=' );
+                       args[argarr[0]] = argarr[1];
+               }
+
+               if ( args.action === 'reboot' ) {
+                       event.preventDefault();
+                       $state = $( event.target ).closest( 'tr' ).find( 
'.novainstancestate' );
+                       $instancename = $( event.target ).closest( 'tr' ).find( 
'.novainstancename' );
+                       $.ajax({
+                               url: mw.config.get( 'wgServer' ) + 
mw.config.get( 'wgScriptPath' ) + '/api.php?',
+                               data: {
+                                       'action'      : 'novainstance',
+                                       'subaction'   : 'reboot',
+                                       'format'      : 'json',
+                                       'instanceid'  : args.instanceid,
+                                       'project'     : args.project,
+                                       'region'      : args.region
+                               },
+                               dataType: 'json',
+                               type: 'POST',
+                               success: function ( data ) {
+                                       $spinner.remove();
+                                       if ( data.error !== undefined ) {
+                                               mw.notify( mw.msg( 
'openstackmanager-rebootinstancefailed', $instancename.text() ) );
+                                       } else {
+                                               mw.notify( mw.msg( 
'openstackmanager-rebootedinstance', $instancename.text() ) );
+                                               $state.text( 
data.novainstance.instancestate );
+                                       }
+                                       $actionLink.show();
+                               },
+                               error: function () {
+                                       $spinner.remove();
+                                       mw.notify( mw.msg( 
'openstackmanager-rebootinstancefailed', $instancename.text() ) );
+                                       $actionLink.show();
+                               }
+                       });
+               }
+       } );
+}( mediaWiki, jQuery ) );
diff --git a/special/SpecialNova.php b/special/SpecialNova.php
index a727d95..9d87f34 100644
--- a/special/SpecialNova.php
+++ b/special/SpecialNova.php
@@ -167,11 +167,11 @@
                return Linker::link( $title, $resource );
        }
 
-       function createActionLink( $msg, $params, $title = null ) {
+       function createActionLink( $msg, $params, $title = null, 
$attribs=array() ) {
                if ( !$title ) {
                        $title = $this->getTitle();
                }
-               return Linker::link( $title, $this->msg( $msg )->escaped(), 
array(), $params );
+               return Linker::link( $title, $this->msg( $msg )->escaped(), 
$attribs, $params );
        }
 
        public static function createNovaKeyActionLink( $msg, $params ) {
diff --git a/special/SpecialNovaInstance.php b/special/SpecialNovaInstance.php
index 8a9152f..190b547 100644
--- a/special/SpecialNovaInstance.php
+++ b/special/SpecialNovaInstance.php
@@ -476,7 +476,7 @@
         */
        function listInstances() {
                $this->setHeaders();
-               $this->getOutput()->addModuleStyles( 'ext.openstack' );
+               $this->getOutput()->addModules( 'ext.openstack' );
                $this->getOutput()->setPagetitle( $this->msg( 
'openstackmanager-instancelist' ) );
 
                if ( $this->getUser()->isAllowed( 'listall' ) ) {
@@ -527,9 +527,9 @@
                 */
                foreach ( $instances as $instance ) {
                        $instanceRow = array();
-                       $this->pushResourceColumn( $instanceRow, 
$instance->getInstanceName() );
+                       $this->pushResourceColumn( $instanceRow, 
$instance->getInstanceName(), array( 'class' => 'novainstancename' ) );
                        $this->pushRawResourceColumn( $instanceRow, 
$this->createResourceLink( $instance->getInstanceId() ) );
-                       $this->pushResourceColumn( $instanceRow, 
$instance->getInstanceState() );
+                       $this->pushResourceColumn( $instanceRow, 
$instance->getInstanceState(), array( 'class' => 'novainstancestate' ) );
                        $this->pushRawResourceColumn( $instanceRow, 
$this->createResourceList( $instance->getInstancePrivateIPs() ) );
                        $this->pushRawResourceColumn( $instanceRow, 
$this->createResourceList( $instance->getInstancePublicIPs() ) );
                        $this->pushRawResourceColumn( $instanceRow, 
$this->createResourceList( $instance->getSecurityGroups() ) );
@@ -562,7 +562,9 @@
                                                'instanceid' => 
$instance->getInstanceOSId(),
                                                'project' => $projectName,
                                                'region' => $region
-                                       )
+                                       ),
+                                       null,
+                                       array( 'class' => 'novainstanceaction' )
                                );
                                $actions[] = $this->createActionLink(
                                        'openstackmanager-configure',
@@ -696,7 +698,7 @@
                        OpenStackManagerEvent::storeEventInfo( 'reboot', 
$this->getUser(), $this->userNova->getInstance( $instanceid ), 
$formData['project'] );
                        $this->getOutput()->addWikiMsg( 
'openstackmanager-rebootedinstance', $instanceid );
                } else {
-                       $this->getOutput()->addWikiMsg( 
'openstackmanager-rebootinstancefailed' );
+                       $this->getOutput()->addWikiMsg( 
'openstackmanager-rebootinstancefailed', $instanceid );
                }
 
                $out = '<br />';

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I72994ddedc6aa00c1273599d2dd39d771984cf03
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/OpenStackManager
Gerrit-Branch: master
Gerrit-Owner: Ryan Lane <[email protected]>
Gerrit-Reviewer: Catrope <[email protected]>
Gerrit-Reviewer: Ori.livneh <[email protected]>
Gerrit-Reviewer: Siebrand <[email protected]>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to