r80039 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r80038‎ | r80039 | r80040 >
Date:21:09, 11 January 2011
Author:laner
Status:deferred
Tags:
Comment:
Adding missing files
Modified paths:
  • /trunk/extensions/OpenStackManager/OpenStackNovaAddress.php (added) (history)
  • /trunk/extensions/OpenStackManager/SpecialNovaAddress.php (added) (history)

Diff [purge]

Index: trunk/extensions/OpenStackManager/SpecialNovaAddress.php
@@ -0,0 +1,333 @@
 2+<?php
 3+class SpecialNovaAddress extends SpecialNova {
 4+
 5+ var $adminNova;
 6+ var $userNova;
 7+ var $userLDAP;
 8+
 9+ function __construct() {
 10+ parent::__construct( 'NovaAddress' );
 11+ }
 12+
 13+ function execute( $par ) {
 14+ global $wgRequest, $wgUser;
 15+ global $wgOpenStackManagerNovaAdminKeys;
 16+
 17+ if ( ! $wgUser->isLoggedIn() ) {
 18+ $this->notLoggedIn();
 19+ return false;
 20+ }
 21+ $this->userLDAP = new OpenStackNovaUser();
 22+ if ( ! $this->userLDAP->exists() ) {
 23+ $this->noCredentials();
 24+ return true;
 25+ }
 26+ $adminCredentials = $wgOpenStackManagerNovaAdminKeys;
 27+ $this->adminNova = new OpenStackNovaController( $adminCredentials );
 28+
 29+ $action = $wgRequest->getVal( 'action' );
 30+ if ( $action == "allocate" ) {
 31+ $this->allocateAddress();
 32+ } else if ( $action == "release" ) {
 33+ $this->releaseAddress();
 34+ } else if ( $action == "associate" ) {
 35+ $this->associateAddress();
 36+ } else if ( $action == "disassociate" ) {
 37+ $this->disassociateAddress();
 38+ } else {
 39+ $this->listAddresses();
 40+ }
 41+ }
 42+
 43+ function allocateAddress() {
 44+ global $wgRequest, $wgOut;
 45+
 46+ $this->setHeaders();
 47+ $wgOut->setPagetitle( wfMsg( 'openstackmanager-allocateaddress' ) );
 48+
 49+ $project = $wgRequest->getText( 'project' );
 50+ $userCredentials = $this->userLDAP->getCredentials( $project );
 51+ $this->userNova = new OpenStackNovaController( $userCredentials );
 52+ if ( ! $wgRequest->wasPosted() ) {
 53+ $out = Html::element( 'p', array(), wfMsgExt( 'openstackmanager-allocateaddress-confirm', array(), $project ) );
 54+ $wgOut->addHTML( $out );
 55+ }
 56+ $addressInfo = Array();
 57+ $addressInfo['project'] = array(
 58+ 'type' => 'hidden',
 59+ 'default' => $project,
 60+ );
 61+ $addressInfo['action'] = array(
 62+ 'type' => 'hidden',
 63+ 'default' => 'allocate',
 64+ );
 65+
 66+ $addressForm = new SpecialNovaAddressForm( $addressInfo, 'openstackmanager-novaaddress' );
 67+ $addressForm->setTitle( SpecialPage::getTitleFor( 'NovaAddress' ) );
 68+ $addressForm->setSubmitID( 'novaaddress-form-allocateaddresssubmit' );
 69+ $addressForm->setSubmitCallback( array( $this, 'tryAllocateSubmit' ) );
 70+ $addressForm->show();
 71+
 72+ return true;
 73+ }
 74+
 75+ function releaseAddress() {
 76+ global $wgOut, $wgRequest;
 77+
 78+ $this->setHeaders();
 79+ $wgOut->setPagetitle( wfMsg( 'openstackmanager-releaseaddress' ) );
 80+
 81+ $project = $wgRequest->getText( 'project' );
 82+ $userCredentials = $this->userLDAP->getCredentials( $project );
 83+ $this->userNova = new OpenStackNovaController( $userCredentials );
 84+ $ip = $wgRequest->getText( 'ip' );
 85+ if ( ! $wgRequest->wasPosted() ) {
 86+ $out = Html::element( 'p', array(), wfMsgExt( 'openstackmanager-releaseaddress-confirm', array(), $ip ) );
 87+ $wgOut->addHTML( $out );
 88+ }
 89+ $addressInfo = Array();
 90+ $addressInfo['project'] = array(
 91+ 'type' => 'hidden',
 92+ 'default' => $project,
 93+ );
 94+ $addressInfo['ip'] = array(
 95+ 'type' => 'hidden',
 96+ 'default' => $ip,
 97+ );
 98+ $addressInfo['action'] = array(
 99+ 'type' => 'hidden',
 100+ 'default' => 'release',
 101+ );
 102+ $addressForm = new SpecialNovaAddressForm( $addressInfo, 'openstackmanager-novaaddress' );
 103+ $addressForm->setTitle( SpecialPage::getTitleFor( 'NovaAddress' ) );
 104+ $addressForm->setSubmitID( 'novaaddress-form-releaseaddresssubmit' );
 105+ $addressForm->setSubmitCallback( array( $this, 'tryReleaseSubmit' ) );
 106+ $addressForm->setSubmitText( 'confirm' );
 107+ $addressForm->show();
 108+
 109+ return true;
 110+ }
 111+
 112+ function associateAddress() {
 113+ global $wgOut, $wgRequest;
 114+
 115+ $this->setHeaders();
 116+ $wgOut->setPagetitle( wfMsg( 'openstackmanager-associateaddress' ) );
 117+
 118+ $ip = $wgRequest->getText( 'ip' );
 119+ $project = $wgRequest->getText( 'project' );
 120+ $userCredentials = $this->userLDAP->getCredentials( $project );
 121+ $this->userNova = new OpenStackNovaController( $userCredentials );
 122+ $instances = $this->userNova->getInstances();
 123+ $instance_keys = array();
 124+ foreach ( $instances as $instance ) {
 125+ if ( $instance->getOwner() == $project ) {
 126+ $instancename = $instance->getInstanceName();
 127+ $instanceid = $instance->getInstanceId();
 128+ $instance_keys["$instancename"] = $instanceid;
 129+ }
 130+ }
 131+ $addressInfo = array();
 132+ $addressInfo['project'] = array(
 133+ 'type' => 'hidden',
 134+ 'default' => $project,
 135+ );
 136+ $addressInfo['ip'] = array(
 137+ 'type' => 'hidden',
 138+ 'default' => $ip,
 139+ );
 140+ $addressInfo['instanceid'] = array(
 141+ 'type' => 'select',
 142+ 'label-message' => 'openstackmanager-instancename',
 143+ 'options' => $instance_keys,
 144+ );
 145+ $addressInfo['action'] = array(
 146+ 'type' => 'hidden',
 147+ 'default' => 'associate',
 148+ );
 149+ $addressForm = new SpecialNovaAddressForm( $addressInfo, 'openstackmanager-novaaddress' );
 150+ $addressForm->setTitle( SpecialPage::getTitleFor( 'NovaAddress' ) );
 151+ $addressForm->setSubmitID( 'novaaddress-form-releaseaddresssubmit' );
 152+ $addressForm->setSubmitCallback( array( $this, 'tryAssociateSubmit' ) );
 153+ $addressForm->show();
 154+
 155+ }
 156+
 157+ function disassociateAddress() {
 158+ global $wgOut, $wgRequest;
 159+
 160+ $this->setHeaders();
 161+ $wgOut->setPagetitle( wfMsg( 'openstackmanager-disassociateaddress' ) );
 162+
 163+ $project = $wgRequest->getText( 'project' );
 164+ $userCredentials = $this->userLDAP->getCredentials( $project );
 165+ $this->userNova = new OpenStackNovaController( $userCredentials );
 166+ $ip = $wgRequest->getText( 'ip' );
 167+ if ( ! $wgRequest->wasPosted() ) {
 168+ $out = Html::element( 'p', array(), wfMsgExt( 'openstackmanager-disassociateaddress-confirm', array(), $ip ) );
 169+ $wgOut->addHTML( $out );
 170+ }
 171+ $addressInfo = Array();
 172+ $addressInfo['project'] = array(
 173+ 'type' => 'hidden',
 174+ 'default' => $project,
 175+ );
 176+ $addressInfo['ip'] = array(
 177+ 'type' => 'hidden',
 178+ 'default' => $ip,
 179+ );
 180+ $addressInfo['action'] = array(
 181+ 'type' => 'hidden',
 182+ 'default' => 'disassociate',
 183+ );
 184+ $addressForm = new SpecialNovaAddressForm( $addressInfo, 'openstackmanager-novaaddress' );
 185+ $addressForm->setTitle( SpecialPage::getTitleFor( 'NovaAddress' ) );
 186+ $addressForm->setSubmitID( 'novaaddress-form-disassociateaddresssubmit' );
 187+ $addressForm->setSubmitCallback( array( $this, 'tryDisassociateSubmit' ) );
 188+ $addressForm->setSubmitText( 'confirm' );
 189+ $addressForm->show();
 190+
 191+ }
 192+
 193+ function listAddresses() {
 194+ global $wgOut, $wgUser;
 195+
 196+ $this->setHeaders();
 197+ $wgOut->setPagetitle( wfMsg( 'openstackmanager-addresslist' ) );
 198+
 199+ $userProjects = $this->userLDAP->getProjects();
 200+ $out = '';
 201+ $sk = $wgUser->getSkin();
 202+ $header = Html::element( 'th', array(), wfMsg( 'openstackmanager-address' ) );
 203+ $header .= Html::element( 'th', array(), wfMsg( 'openstackmanager-instanceid' ) );
 204+ $header .= Html::element( 'th', array(), wfMsg( 'openstackmanager-instancename' ) );
 205+ $header .= Html::element( 'th', array(), wfMsg( 'openstackmanager-actions' ) );
 206+ $addresses = $this->adminNova->getAddresses();
 207+ $projectArr = array();
 208+ foreach ( $addresses as $address ) {
 209+ $ip = $address->getPublicIP();
 210+ $instanceid = $address->getInstanceId();
 211+ $project = $address->getProject();
 212+ $addressOut = Html::element( 'td', array(), $ip );
 213+ if ( $instanceid ) {
 214+ $addressOut .= Html::element( 'td', array(), $instanceid );
 215+ $instance = $this->adminNova->getInstance( $instanceid );
 216+ $instancename = $instance->getInstanceName();
 217+ $addressOut .= Html::element( 'td', array(), $instancename );
 218+ } else {
 219+ $addressOut .= Html::element( 'td', array(), '' );
 220+ $addressOut .= Html::element( 'td', array(), '' );
 221+ }
 222+ if ( $instanceid ) {
 223+ $msg = wfMsg( 'openstackmanager-reassociateaddress' );
 224+ } else {
 225+ $msg = wfMsg( 'openstackmanager-releaseaddress' );
 226+ $link = $sk->link( $this->getTitle(), $msg, array(),
 227+ array( 'action' => 'release', 'ip' => $ip, 'project' => $project ), array() );
 228+ $actions = Html::rawElement( 'li', array(), $link );
 229+ $msg = wfMsg( 'openstackmanager-associateaddress' );
 230+ }
 231+ $link = $sk->link( $this->getTitle(), $msg, array(),
 232+ array( 'action' => 'associate', 'ip' => $ip, 'project' => $project ), array() );
 233+ $actions .= Html::rawElement( 'li', array(), $link );
 234+ if ( $instanceid ) {
 235+ $msg = wfMsg( 'openstackmanager-disassociateaddress' );
 236+ $link = $sk->link( $this->getTitle(), $msg, array(),
 237+ array( 'action' => 'disassociate', 'ip' => $ip, 'project' => $project ), array() );
 238+ $actions .= Html::rawElement( 'li', array(), $link );
 239+ }
 240+ $actions = Html::rawElement( 'ul', array(), $actions );
 241+ $addressOut .= Html::rawElement( 'td', array(), $actions );
 242+ $projectArr["$project"] = Html::rawElement( 'tr', array(), $addressOut );
 243+ }
 244+ foreach ( $userProjects as $project ) {
 245+ $out .= Html::element( 'h2', array(), $project );
 246+ $out .= $sk->link( $this->getTitle(), wfMsg( 'openstackmanager-allocateaddress' ), array(), array( 'action' => 'allocate', 'project' => $project ), array() );
 247+ if ( isset( $projectArr["$project"] ) ) {
 248+ $projectOut = $header;
 249+ $projectOut .= $projectArr["$project"];
 250+ $out .= Html::rawElement( 'table',
 251+ array( 'id' => 'novainstancelist', 'class' => 'wikitable' ), $projectOut );
 252+ }
 253+ }
 254+ $wgOut->addHTML( $out );
 255+ }
 256+
 257+ function tryAllocateSubmit( $formData, $entryPoint = 'internal' ) {
 258+ global $wgOut, $wgUser;
 259+
 260+ $address = $this->userNova->allocateAddress();
 261+ if ( ! $address ) {
 262+ $out = Html::element( 'p', array(), wfMsg( 'openstackmanager-allocateaddressfailed' ) );
 263+ $wgOut->addHTML( $out );
 264+ return false;
 265+ }
 266+ $ip = $address->getPublicIP();
 267+ $out = Html::element( 'p', array(), wfMsgExt( 'openstackmanager-allocatedaddress' , array(), $ip ) );
 268+ $out .= '<br />';
 269+ $sk = $wgUser->getSkin();
 270+ $out .= $sk->link( $this->getTitle(), wfMsg( 'openstackmanager-backaddresslist' ), array(), array(), array() );
 271+ $wgOut->addHTML( $out );
 272+
 273+ return true;
 274+ }
 275+
 276+ function tryReleaseSubmit( $formData, $entryPoint = 'internal' ) {
 277+ global $wgOut, $wgUser;
 278+
 279+ $ip = $formData['ip'];
 280+ $success = $this->userNova->releaseAddress( $ip );
 281+ if ( $success ) {
 282+ $out = Html::element( 'p', array(), wfMsgExt( 'openstackmanager-releasedaddress', array(), $ip ) );
 283+ } else {
 284+ $out = Html::element( 'p', array(), wfMsgExt( 'openstackmanager-releaseaddressfailed', array(), $ip ) );
 285+ }
 286+ $out .= '<br />';
 287+ $sk = $wgUser->getSkin();
 288+ $out .= $sk->link( $this->getTitle(), wfMsg( 'openstackmanager-backaddresslist' ), array(), array(), array() );
 289+ $wgOut->addHTML( $out );
 290+
 291+ return true;
 292+ }
 293+
 294+ function tryAssociateSubmit( $formData, $entryPoint = 'internal' ) {
 295+ global $wgOut, $wgUser;
 296+
 297+ $instanceid = $formData['instanceid'];
 298+ $ip = $formData['ip'];
 299+ $address = $this->userNova->associateAddress( $instanceid, $ip );
 300+ if ( $address ) {
 301+ $out = Html::element( 'p', array(), wfMsgExt( 'openstackmanager-associatedaddress', array(), $ip, $instanceid ) );
 302+ } else {
 303+ $out = Html::element( 'p', array(), wfMsgExt( 'openstackmanager-associateaddressfailed', array(), $ip, $instanceid ) );
 304+ }
 305+ $out .= '<br />';
 306+ $sk = $wgUser->getSkin();
 307+ $out .= $sk->link( $this->getTitle(), wfMsg( 'openstackmanager-backaddresslist' ), array(), array(), array() );
 308+ $wgOut->addHTML( $out );
 309+
 310+ return true;
 311+ }
 312+
 313+ function tryDisassociateSubmit( $formData, $entryPoint = 'internal' ) {
 314+ global $wgOut, $wgUser;
 315+
 316+ $ip = $formData['ip'];
 317+ $address = $this->userNova->disassociateAddress( $ip );
 318+ if ( $address ) {
 319+ $out = Html::element( 'p', array(), wfMsgExt( 'openstackmanager-disassociatedaddress', array(), $ip ) );
 320+ } else {
 321+ $out = Html::element( 'p', array(), wfMsgExt( 'openstackmanager-disassociateaddressfailed', array(), $ip ) );
 322+ }
 323+ $out .= '<br />';
 324+ $sk = $wgUser->getSkin();
 325+ $out .= $sk->link( $this->getTitle(), wfMsg( 'openstackmanager-backaddresslist' ), array(), array(), array() );
 326+ $wgOut->addHTML( $out );
 327+
 328+ return true;
 329+ }
 330+
 331+}
 332+
 333+class SpecialNovaAddressForm extends HTMLForm {
 334+}
Property changes on: trunk/extensions/OpenStackManager/SpecialNovaAddress.php
___________________________________________________________________
Added: svn:eol-style
1335 + native
Index: trunk/extensions/OpenStackManager/OpenStackNovaAddress.php
@@ -0,0 +1,32 @@
 2+<?php
 3+
 4+# TODO: Make this an abstract class, and make the EC2 API a subclass
 5+class OpenStackNovaAddress {
 6+
 7+ var $address;
 8+
 9+ function __construct( $apiInstanceResponse ) {
 10+ $this->address = $apiInstanceResponse;
 11+ }
 12+
 13+ function getInstanceId() {
 14+ # instanceId returns as: instanceid (project)
 15+ $info = explode( ' ', $this->address->instanceId );
 16+ if ( $info[0] == 'None' ) {
 17+ return '';
 18+ } else {
 19+ return $info[0];
 20+ }
 21+ }
 22+
 23+ function getPublicIP() {
 24+ return $this->address->publicIp;
 25+ }
 26+
 27+ function getProject() {
 28+ # instanceId returns as: instanceid (project)
 29+ $info = explode( ' ', $this->address->instanceId );
 30+ return str_replace( array('(',')'), '', $info[1] );
 31+ }
 32+
 33+}
Property changes on: trunk/extensions/OpenStackManager/OpenStackNovaAddress.php
___________________________________________________________________
Added: svn:eol-style
134 + native

Status & tagging log