Created
October 15, 2011 07:57
-
-
Save kaz29/1289240 to your computer and use it in GitHub Desktop.
PHPMatsuri2011 NiftyCloud Workshop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// メンバ変数を追加 | |
public $uses = array( | |
'NiftyCloudControllPanel.Instance', | |
'NiftyCloudControllPanel.KeyPair', | |
'NiftyCloudControllPanel.SecurityGroup', | |
); | |
/// アクションを追加 | |
public function run($id=null) | |
{ | |
// SSHキー一覧を取得 | |
$key_pairs = $this->KeyPair->find('list'); | |
if ( empty($key_pairs) ) { | |
$this->Session->setFlash(__('Could not get key pairs.', true)); | |
$this->redirect(array('action' => 'index')); | |
} | |
// Firewall一覧を取得 | |
$security_groups = $this->SecurityGroup->find('list'); | |
if ( empty($security_groups) ) { | |
$this->Session->setFlash(__('Could not get security groups.', true)); | |
$this->redirect(array('action' => 'index')); | |
} | |
$this->set(compact('key_pairs', 'security_groups')); | |
if ( $this->data ) { | |
$this->Instance->create(); | |
$this->Instance->set($this->data); | |
$result = $this->Instance->run(); | |
if ( $result === true ) { | |
$this->Session->setFlash(__('The instance has been created', true)); | |
$this->redirect(array('action' => 'index')); | |
} else { | |
$this->Session->setFlash(__('The instance could not be create. Please, try again.', true)); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// サーバー起動処理用のメソッドを追加 | |
public function run() | |
{ | |
if (is_null($this->api)) { | |
$config = Configure::read('nifty_cloud'); | |
$this->api = new NiftyCloud($config); | |
} | |
$params = array( | |
'ImageId' => $this->data[$this->alias]['image_id'], | |
'KeyName' => $this->data[$this->alias]['key_pair'], | |
'InstanceType' => $this->data[$this->alias]['instance_type'], | |
'InstanceId' => $this->data[$this->alias]['instanceid'], | |
'AccountingType' => $this->data[$this->alias]['accounting_type'], | |
'Password' => $this->data[$this->alias]['password'], | |
'SecurityGroup' => $this->data[$this->alias]['security_group'], | |
); | |
$api_result = $this->api->run_instances($params); | |
return !$this->api->isError(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once(ROOT.DS.'vendors'.DS.'NiftyCloud'.DS.'services'.DS.'niftycloud.class.php'); | |
class KeyPair extends AppModel { | |
public $useTable = false; | |
private $api = null; | |
public function find($type, $conditions=array()) | |
{ | |
if (is_null($this->api)) { | |
$config = Configure::read('nifty_cloud'); | |
$this->api = new NiftyCloud($config); | |
} | |
$result = false ; | |
$api_result = $this->api->describe_key_pairs(); | |
if ( $this->api->isError() ) { | |
$result = array(); | |
} else { | |
$result = array(); | |
foreach($api_result->keySet->item as $item) { | |
if ( $type === 'list' ) { | |
$result[(string)$item->keyName] = (string)$item->keyName; | |
} else { | |
$result[] = array( | |
$this->alias => array( | |
'name' => (string)$item->keyName, | |
) | |
); | |
} | |
} | |
} | |
return $result; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once(ROOT.DS.'vendors'.DS.'NiftyCloud'.DS.'services'.DS.'niftycloud.class.php'); | |
class SecurityGroup extends AppModel { | |
public $useTable = false; | |
private $api = null; | |
public function find($type, $conditions=array()) | |
{ | |
if (is_null($this->api)) { | |
$config = Configure::read('nifty_cloud'); | |
$this->api = new NiftyCloud($config); | |
} | |
$result = false ; | |
$api_result = $this->api->describe_security_groups(); | |
if ( $this->api->isError() ) { | |
$result = array(); | |
} else { | |
$result = array(); | |
foreach($api_result->securityGroupInfo->item as $item) { | |
if ( $type === 'list' ) { | |
$result[(string)$item->groupName] = (string)$item->groupName; | |
} else { | |
$result[] = array( | |
$this->alias => array( | |
'name' => (string)$item->groupName, | |
'description' => (string)$item->groupDescription, | |
) | |
); | |
} | |
} | |
} | |
return $result; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ファイルの最後に以下を追加 | |
<div class="actions"> | |
<h3><?php echo __('Actions'); ?></h3> | |
<ul> | |
<li><?php echo $this->Html->link(__('Create Instances', true), array('action' => 'run'));?></li> | |
</ul> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="instances form"> | |
<?php echo $this->Form->create('Instance');?> | |
<fieldset> | |
<legend><?php echo __('Run Instance'); ?></legend> | |
<?php | |
echo $this->Form->input('id', array('type'=>'hidden')); | |
echo $this->Form->input('image_id', array( | |
'type'=>'select', | |
'label'=>__('OSイメージ',true), | |
'options' => $images | |
)); | |
echo $this->Form->input('instanceid', array( | |
'label'=>__('インスタンスID'), | |
'type'=>'text' | |
)); | |
echo $this->Form->input('accounting_type', array( | |
'type'=>'select', | |
'label'=>__('課金方法',true), | |
'options' => array(1=>'月額',2=>'従量'), | |
'value'=>2 | |
)); | |
echo $this->Form->input('instance_type', array( | |
'type'=>'select', | |
'label'=>__('サーバータイプ',true), | |
'options' => $instance_types | |
)); | |
echo $this->Form->input('password', array( | |
'label'=>__('パスワード'), | |
'type'=>'password' | |
)); | |
echo $this->Form->input('password_confirm', array( | |
'label'=>__('パスワード(確認)'), | |
'type'=>'password' | |
)); | |
echo $this->Form->input('key_pair', array( | |
'type'=>'select', | |
'label'=>__('公開鍵',true), | |
'options' => $key_pairs | |
)); | |
echo $this->Form->input('security_group', array( | |
'type'=>'select', | |
'label'=>__('ファイアーウォール',true), | |
'options' => $security_groups | |
)); | |
?> | |
</fieldset> | |
<?php echo $this->Form->end(__('Submit', true));?> | |
</div> | |
<div class="actions"> | |
<h3><?php echo __('Actions'); ?></h3> | |
<ul> | |
<li><?php echo $this->Html->link(__('List Instances', true), array('action' => 'index'));?></li> | |
</ul> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment