Skip to content

Commit

Permalink
Merge pull request #417 from pantheon-systems/site_tags
Browse files Browse the repository at this point in the history
Added site tags add|delete
  • Loading branch information
TeslaDethray committed Aug 20, 2015
2 parents 3944aa4 + ffdc8f3 commit 6b88af9
Show file tree
Hide file tree
Showing 13 changed files with 1,127 additions and 67 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ All notable changes to this project starting with the 0.6.0 release will be docu
- `site delete-branch` will delete a multidev environment's branch (For sites with multidev access) (#395)
- `site delete-env` now has an optional --remove-branch flag (#395)
- Environment variables for --site (TERMINUS_SITE), --org (TERMINUS_ORG), --env (TERMINUS_ENV), and user (TERMINUS_USER). User may import these themselves, or add them to the .env file in the user's current directory. (#407)
- `site tags <add|remove> --site=<site> --org=<org> --tag=<tag>` command will add tags to an organization site (#417)

### Fixed
- `organizations sites` shows all the organization's sites, not just the first 100 (#371)
Expand Down
109 changes: 109 additions & 0 deletions php/Terminus/Collections/SiteOrganizationMemberships.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace Terminus\Collections;
use Terminus\Request;
use Terminus\Session;
use Terminus\SiteOrganizationMembership;
use \TerminusCommand;

class SiteOrganizationMemberships {
private $site;
private $models = array();
private $workflows;

/**
* Object constructor. Saves site object
*
* @return [SiteOrganizationMemberships] $this
*/
public function __construct($options = array()) {
$this->site = $options['site'];
}

/**
* Adds this org as a member to the site
*
* @param [string] $site Name of site to add org to
* @return [Workflow] $workflow
**/
public function add($name, $role) {
$workflow = $this->site->workflows->create(
'add_site_organization_membership',
array('params' => array('organization_name' => $name, 'role' => $role))
);
return $workflow;
}

/**
* Lists all organizational members
*
* @return [array] $org_memberships SiteOrganizationMembership objects for each org member
*/
public function all() {
$org_memberships = array_values($this->models);
return $org_memberships;
}

/**
* Retrieves organization with given UUID or name, if such exists
*
* @param [string] $id User UUID or name
* @return [SiteOrganizationMembership] $this->models[$id] Org or null
*/
public function get($id) {
if(isset($this->models[$id])) {
return $this->models[$id];
}
return null;
}

/**
* Retrieves and fills in team member data
*
* @return [SiteOrganizationMemberships] $this
*/
public function fetch() {
$results = TerminusCommand::paged_request(
'sites/' . $this->site->get('id') . '/memberships/organizations'
);

foreach($results['data'] as $id => $org_membership_data) {
$org_membership_data = (array)$org_membership_data;
$org_membership_data['id'] = $org_membership_data['organization_id'];
$org_membership_data['site'] = $this->site;
$this->models[$id] = new SiteOrganizationMembership(
$this->site,
$org_membership_data
);
}

return $this;
}

/**
* Returns UUID of organization with given name
*
* @param [string] $email A name to search for
* @return [SiteOrganizationMembership] $orgs[$name]
*/
public function findByName($name) {
$orgs = array();
foreach($this->models as $org_member) {
$org = $org_member->getName();
if($org->name == $org) {
return $org_member;
}
}
return null;
}

/**
* Lists IDs of all organizational members of this site
*
* @return [array] $ids Array of organization UUIDs
*/
public function ids() {
$ids = array_keys($this->models);
return $ids;
}
}
2 changes: 1 addition & 1 deletion php/Terminus/Organization.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ 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)) {
$this->user = User::instance();
$this->user = new User();
$orgs = $this->user->organizations();
$org = $orgs->$org;
}
Expand Down
74 changes: 73 additions & 1 deletion php/Terminus/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use \Terminus\SiteUserMembership;
use Terminus\Collections\Environments;
use Terminus\Collections\SiteUserMemberships;
use Terminus\Collections\OrganizationSiteMemberships;
use Terminus\Collections\SiteOrganizationMemberships;
use Terminus\Collections\Workflows;

class Site {
Expand All @@ -22,6 +24,8 @@ class Site {
public $workflows;

private $features;
private $org_memberships;
private $tags;
private $user_memberships;

/**
Expand All @@ -36,7 +40,7 @@ public function __construct($attributes) {
$this->id = $attributes->id;
}

if (property_exists($attributes, 'information')) {
if (is_object($attributes) && property_exists($attributes, 'information')) {
# If the attributes has `information` property
# unwrap it and massage the data into proper format
$this->attributes = $attributes->information;
Expand All @@ -52,6 +56,7 @@ public function __construct($attributes) {
$this->metadata = @$this->attributes->metadata ?: new \stdClass();
# /deprecated properties

$this->org_memberships = new SiteOrganizationMemberships(array('site' => $this));
$this->user_memberships = new SiteUserMemberships(array('site' => $this));
$this->environmentsCollection = new Environments(array('site' => $this));
$this->workflows = new Workflows(array('owner' => $this, 'owner_type' => 'site'));
Expand Down Expand Up @@ -500,6 +505,35 @@ public function getFeature($feature) {
return null;
}

/**
* Adds a tag to the site
*
* @param [string] $tag Tag to apply
* @return [array] $response
*/
public function addTag($tag, $org) {
$params = array($tag => array('sites' => array($this->id)));
$response = TerminusCommand::simple_request(
sprintf('organizations/%s/tags', $org),
array('method' => 'put', 'data' => $params)
);
return $response;
}

/**
* Removes a tag to the site
*
* @param [string] $tag Tag to remove
* @return [array] $response
*/
public function removeTag($tag, $org) {
$response = TerminusCommand::simple_request(
sprintf('organizations/%s/tags/%s/sites?entity=%s', $org, $tag, $this->id),
array('method' => 'delete')
);
return $response;
}

/**
* Returns given attribute, if present
*
Expand All @@ -512,4 +546,42 @@ public function get($attribute) {
}
return null;
}

/**
* Returns tags from the site/org join
*
* @return [array] $tags Tags in string format
*/
public function getTags($org) {
if (isset($this->tags)) {
return $this->tags;
}
$org_site_member = new OrganizationSiteMemberships(
array('organization' => new Organization($org))
);
$tags = $org_site_member->fetch()->get($this->id)->get('tags');
return $tags;
}

/**
* Returns all organization members of this site
*
* @param [string] $uuid UUID of organization to check for
* @return [boolean] True if organization is a member of this site
*/
public function organizationIsMember($uuid) {
$org_ids = $this->org_memberships->fetch()->ids();
$org_is_member = in_array($uuid, $org_ids);
return $org_is_member;
}

/**
* Returns all organization members of this site
*
* @return [array] Array of SiteOrganizationMemberships
*/
public function getOrganizations() {
$orgs = $this->org_memberships->fetch()->all();
return $orgs;
}
}
80 changes: 80 additions & 0 deletions php/Terminus/SiteOrganizationMembership.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace Terminus;
use \ReflectionClass;
use \Terminus\Request;
use \Terminus\Collections\Bindings;

class SiteOrganizationMembership {
private $id;
private $attributes;
private $site;
private $organization;

/**
* Object constructor
*
* @param [stdClass] $attributes
* @param [array] $options
* @return [SiteOrganizationMembership] $this
*/
public function __construct($attributes, $options = array()) {
if(!is_array($options)) {
$options = get_object_vars($options);
}
foreach($options as $var_name => $value) {
$this->$var_name = $value;
}
$this->attributes = $attributes;
}

/**
* Returns attribute of given name
*
* @param [string] $attribute Name of attribute to retrieve
* @return [mixed] $this->attributes->$attribute or null
*/
public function get($attribute) {
if(isset($this->attributes->$attribute)) {
return $this->attributes->$attribute;
}
return null;
}

/**
* Returns organization object within SiteOrganizationMembership object
*
* @return [Organization] $this->organization Org associated with this SiteOrganizationMembership
*/
public function getOrganization() {
if (!isset($this->organization)) {
$this->organization = new Organization($this->id);
}
return $this->organization;
}
/**
* Remove membership of organization
*
* @return [Workflow] $workflow
**/
public function remove() {
$workflow = $this->site->workflows->create(
'remove_site_organization_membership',
array('params' => array('organization_id' => $this->id))
);
return $workflow;
}

/**
* Changes the role of the given member
*
* @param [string] $role Desired role for this organization
* @return [Workflow] $workflow
*/
public function setRole($role) {
$workflow = $this->site->workflows->create(
'update_site_organization_membership',
array('params' => array('organization_id' => $this->id, 'role' => $role)));
return $workflow;
}
}
21 changes: 21 additions & 0 deletions php/class-terminus-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,4 +393,25 @@ protected function workflowOutput($workflow) {
}
}

/**
* Outputs basic response success/failure messages
*
* @param [array] $response Array from response
* @param [array] $messages Array of response strings
* [string] success Displayed on success
* [string] failure Displayed on error
*/
protected function responseOutput($response, $messages = array()) {
$default_messages = array(
'success' => 'The operation has succeeded.',
'failure' => 'The operation was unsuccessful.',
);
$messages = array_merge($default_messages, $messages);
if ($response['status_code'] == 200) {
Terminus::success($messages['success']);
} else {
Terminus::error($messages['failure']);
}
}

}
Loading

0 comments on commit 6b88af9

Please sign in to comment.