Skip to content

Commit

Permalink
Organizations commands
Browse files Browse the repository at this point in the history
  • Loading branch information
mikevanwinkle committed Dec 8, 2014
1 parent 7ed7681 commit fc8590c
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 105 deletions.
44 changes: 44 additions & 0 deletions php/Terminus/Organization.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
namespace Terminus;

use Terminus\User;
use Terminus\Site;

class Organization {

public function __construct( $org ) {
// if the org id is passed in then we need to fetch it from the user object
if (is_string($org)) {
$user = User::instance();
$orgs = $user->organizations();
$org = $orgs->$org;
}

// hydrate the object
$properties = get_object_vars($org);
foreach (get_object_vars($org) as $key => $value) {
if(!property_exists($this,$key)) {
$this->$key = $properties[$key];
}
}

return $this;
}

public function addSite( Site $site ) {
$path = sprintf("organizations/%s/sites/%s", $this->id, $site->getId());
$method = 'PUT';
$user = User::id();
$response = \Terminus_Command::request('users', $user, $path, $method);
return $response['data'];
}

public function removeSite( Site $site ) {
$path = sprintf("organizations/%s/sites/%s", $this->id, $site->getId());
$method = 'DELETE';
$user = User::id();
$response = \Terminus_Command::request('users', $user, $path, $method);
return $response['data'];
}

}
57 changes: 57 additions & 0 deletions php/Terminus/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
namespace Terminus;

use \Terminus\Organization;

class User {
static $instance;
public $id;
private $organizations;

public function __construct($id = null) {
if (null===$id) {
$this->id = Session::getValue('user_uuid');
} else {
$this->id = $id;
}
self::$instance = $this;
return $this;
}

static function instance($id = null) {
if (self::$instance) {
new Self($id);
}
return self::$instance;
}

public function organizations() {
if (!$this->organizations) {
$path = 'organizations';
$method = "GET";
$response = \Terminus_Command::request('users', $this->id, $path, $method);
$this->organizations = $response['data'];
}
return $this->organizations;
}

public function sites($organization=null) {
if ($organization) {
$path = sprintf("organizations/%s/sites", $organization);
} else {
$path = "sites";
}
$method = 'GET';
$response = \Terminus_Command::request('users', $this->id, $path, $method);
return $response['data'];
}

public function getId() {
return $this->id;
}

public static function id() {
$user = self::instance();
return $user->getId();
}
}
173 changes: 69 additions & 104 deletions php/commands/organizations.php
Original file line number Diff line number Diff line change
@@ -1,130 +1,95 @@
<?php

/**
* Print the pantheon art
*
*/

use \Terminus\User;
use \Terminus\Utils;
use \Terminus\Auth;
use \Terminus\SiteFactory;
use \Terminus\Organization;
use \Guzzle\Http\Client;
use \Terminus\Loggers\Regular as Logger;

class Organizations_Command extends Terminus_Command {

/**
* Commands specific to an environment
*
* <commands>...
* [--site=<value>]
* : specify the site on which the command should be performed
* [--env=<value>]
* : Specificy the environment of a site previously set with --site=
*
* [--<flag>=<value>]
* : Additional Drush flag(s) to pass in to the command.
*/
function __invoke(array $args, array $assoc_args ) {
if (empty($args) || (!array_key_exists("site", $assoc_args)) || (!array_key_exists("org", $assoc_args))) {
Terminus::error("You need to specify a task to perform, site and environment on which to perform.");
} else {
$this->_handleFuncArg($args, $assoc_args);
}
$this->_execute($args, $assoc_args);
public function __construct() {
parent::__construct();
}

/**
* API call to get a user's organizations.
*/
// function list($args, $assoc_args) {
// //TODO: format output
// return $this->terminus_request("user", $this->_uuid, 'organizations', "GET");
// }

/**
* API call to get sites within an organization.
*
* Available only to organization admins.
*/
function sites($args, $assoc_args) {
//TODO: format output
return $this->terminus_request("user", $this->_uuid, 'organizations/'. $this->_org .'/sites', "GET");
}

/**
* API call to add a site into an organization.
* [--site=<value>]
* : specify the site on which the command should be performed (may be name or UUID)
* @subcommand list
*
* Available only to organization admins.
*/
function site_add($args, $assoc_args) {
if (array_key_exists("site", $assoc_args)) {
$site_uuid = $this->_validateSiteUuid($assoc_args["site"]);
}
if (empty($site_uuid)) {
Terminus::error("You must specify the site to remove with --site=");
return false;
}
//TODO: format output
return $this->terminus_request("user", $this->_uuid, 'organizations/' . $this->_org . '/sites/' . $site_uuid, "PUT");
*/
public function all($args, $assoc_args) {
$user = new User();
$data = array();
foreach ( $user->organizations() as $org_id => $org) {
$data[] = array(
'name' => $org->name,
'id' => $org_id,
);
}

$this->handleDisplay($data);
}

/**
* API call to remove a site from an organization.
* [--site=<value>]
* : specify the site on which the command should be performed (may be name or uuid)
* List an organizations sites
*
* Available only to organization admins.
*/
function site_remove($args, $assoc_args) {
if (array_key_exists("site", $assoc_args)) {
$site_uuid = $this->_validateSiteUuid($assoc_args["site"]);
}
if (empty($site_uuid)) {
Terminus::error("You must specify the site to remove with --site=");
return false;
}
//TODO: format output
return $this->terminus_request("user", $this->_uuid, 'organizations/'. $this->_org .'/sites/'. $site_uuid, "DELETE");
}

/**
* API call to get users within an organization.
* @subcommand sites
*
* Available only to organization admins.
*/
function users($args, $assoc_args) {
//TODO: format output
return $this->terminus_request("user", $this->_uuid, 'organizations/'. $this->_org .'/users', "GET");
}

/**
* API call to add a user to an organization.
* ## Options
*
* Available only to organization admins.
* [--org=<org>] : Organization name or Id
* [--add=<site>] : Site to add to organization
* [--remove=<site>] : Site to remove from organization
*
* @todo: promote/demote
*/
function useradd($args, $assoc_args) {
$user_to_add = array_shift($args);
if ($admin) {
$path .= '?admin=1';
public function sites($args, $assoc_args) {
$orgs = array();
$user = new User();

foreach ($user->organizations() as $id => $org) {
$orgs[$id] = $org->name;
}
//TODO: format output
return $this->terminus_request("user", $this->_uuid, 'organizations/'. $this->_org .'/users/'. $user_to_add, "PUT");
}

/**
* API call to remove a user from an organization.
*
* Available only to organization admins.
*/
function userremove($args, $assoc_args) {
$user_to_delete = array_shift($args);
//TODO: format output
return $this->terminus_request("user", $this->_uuid, 'organizations/'. $organization_uuid .'/users/'. $user_to_delete, "DELETE");
}
if (empty(@$assoc_args['org'])) {
$selected_org = Terminus::menu($orgs,false,"Choose an organization");
} else {
$selected_org = @$assoc_args['org'];
}

$org = new Organization($selected_org);

if (@$assoc_args['add']) {
$add = SiteFactory::instance(@$assoc_args['add']);

}
Terminus::confirm("Are you sure you want to add %s to %s ?", $assoc_args, array($add->getName(), $org->name));
$org->addSite($add);
Terminus::success("Added site!");
return true;
}

if (@$assoc_args['remove']) {
$remove = SiteFactory::instance(@$assoc_args['remove']);

Terminus::confirm("Are you sure you want to remove %s to %s ?", $assoc_args, array($remove->getName(), $org->name));
$org->removeSite($remove);
Terminus::success("Removed site!");
return true;
}

$sites = $user->sites($selected_org);
$data = array();
foreach ($sites as $site) {
$data[] = array(
'name' => $site->name,
'service level' => @$site->service_level,
'framework' => @$site->framework,
'created' => date('Y-m-d H:i:s', $site->created),
);
}
$this->handleDisplay($data);
}

}
Terminus::add_command( 'organizations', 'Organizations_Command' );
2 changes: 1 addition & 1 deletion scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
set -ex

# Basic lint test
for f in $( git diff-tree $TRAVIS_COMMIT --name-only -r | grep php ) ; do php -l $f ; done
for f in $( git diff-tree $TRAVIS_COMMIT --name-status -r | grep php | grep -v "^D" | awk '{print $2}') ; do php -l $f ; done

# Run the unit tests
# phpunit
Expand Down

0 comments on commit fc8590c

Please sign in to comment.