Last active
August 29, 2015 14:25
-
-
Save cesargalindo/8ecb9e5f44847e635409 to your computer and use it in GitHub Desktop.
Migrate Edge User missing from Dev Portal to Dev Portal Database
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 | |
/* | |
* This script presumes that you place the following in composer.json: | |
* { | |
* "require": { | |
* "guzzlehttp/guzzle": "~4.0" | |
* } | |
* } | |
* and then run composer install in that directory. | |
*/ | |
use GuzzleHttp\Client; | |
/* ==== CONFIGURATION SECTION ==== */ | |
// What org do you want to sync? | |
$org = 'ADD-ORG-NAME-HERE'; | |
// What are the credentials for an orgadmin within that org? | |
$user = 'ADDHERE'; | |
$pass = 'ADDHERE'; | |
// What's the connection string for the database? | |
// Hint: look in ~/.drush/pantheon.aliases.drushrc.php | |
//$connection = 'mysql://pantheon:[email protected]:31337/pantheon'; | |
$connection = 'mysql://USERNAME:PASSWORD@localhost/DATABSE'; | |
/* ==== END CONFIGURATION ==== */ | |
$parts = parse_url($connection); | |
$db_user = $parts['user']; | |
$db_pass = $parts['pass']; | |
$db_host = $parts['host']; | |
$db_port = $parts['port']; | |
$db_name = ltrim($parts['path'], '/'); | |
$dsn = "mysql:host=$db_host;port=$db_port;dbname=$db_name"; | |
$PDO = new PDO($dsn, $db_user, $db_pass, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); | |
require_once 'vendor/autoload.php'; | |
$client = new Client([ | |
'base_url' => ['https://api.enterprise.apigee.com/v1/o/{org}/', ['org' => $org]], | |
'defaults' => [ | |
'headers' => ['Accept' => 'application/json'], | |
'auth' => [$user, $pass], | |
'allow_redirects' => true, | |
], | |
]); | |
// Export missing mint developers | |
$tmp_file = '/tmp/missing-mint-developers.txt'; | |
exec('echo "Contains missing developers - ' . date("F j, Y, g:i a") .'" > '. $tmp_file); | |
$tmp_file2 = '/tmp/mint-developers.txt'; | |
exec('echo "Mint developers - ' . date("F j, Y, g:i a") .'" > '. $tmp_file2); | |
// Retrieve list of all users in local db "mail" | |
try { | |
$emails = $PDO->query('SELECT mail,name FROM users WHERE uid > 0 ORDER by uid DESC')->fetchAll(); | |
foreach ($emails as $row) { | |
$local_users[$row["mail"]] = 1; | |
$local_names[$row["name"]] = 1; | |
} | |
} | |
catch (PDOexception $e) { | |
echo "Error is: " . $e-> etmessage(); | |
} | |
// start with first user in the system | |
$response = $client->get('developers'); | |
$edge_developers = json_decode($response->getBody(), TRUE); | |
$total = count($edge_developers); | |
while ($total > 1) { | |
$j = 0; | |
$drupal_developers = array(); | |
// TODO: - make sure sequences value is incremented - higher than current uid #### | |
$uid = $PDO->query('SELECT value FROM sequences')->fetchColumn(); | |
$mail_query = $PDO->prepare('SELECT uid FROM users WHERE mail = :mail OR name = :name'); | |
$users_query = $PDO->prepare('INSERT INTO users (uid, name, pass, mail, created, status, init, uuid) VALUES (:uid, :name, :pass, :mail, :created, :status, :init, :uuid)'); | |
$fn_data_query = $PDO->prepare('INSERT INTO field_data_field_first_name (entity_type, bundle, deleted, entity_id, revision_id, language, delta, field_first_name_value) VALUES (\'user\', \'user\', 0, :uid, :vid, \'und\', 0, :name)'); | |
$fn_rev_query = $PDO->prepare('INSERT INTO field_revision_field_first_name (entity_type, bundle, deleted, entity_id, revision_id, language, delta, field_first_name_value) VALUES (\'user\', \'user\', 0, :uid, :vid, \'und\', 0, :name)'); | |
$ln_data_query = $PDO->prepare('INSERT INTO field_data_field_last_name (entity_type, bundle, deleted, entity_id, revision_id, language, delta, field_last_name_value) VALUES (\'user\', \'user\', 0, :uid, :vid, \'und\', 0, :name)'); | |
$ln_rev_query = $PDO->prepare('INSERT INTO field_revision_field_last_name (entity_type, bundle, deleted, entity_id, revision_id, language, delta, field_last_name_value) VALUES (\'user\', \'user\', 0, :uid, :vid, \'und\', 0, :name)'); | |
$user_roles = $PDO->prepare('INSERT INTO users_roles (uid, rid) VALUES (:uid, :rid)'); | |
foreach ($edge_developers as $edge_developer) { | |
$j++; | |
exec('echo ' . $edge_developer . ' >> '. $tmp_file2); | |
if (!$local_users[$edge_developer]) { | |
try { | |
$response = $client->get('developers/' . rawurlencode($edge_developer)); | |
$details = json_decode($response->getBody(), TRUE); | |
exec('echo ' . $edge_developer . ' ' . $details['userName'] . ' >> '. $tmp_file); | |
echo "Missing developer: " . $edge_developer . "\n"; | |
echo "[$j/$total] Processed $edge_developer\n"; | |
echo "uid = $uid \n"; | |
echo "name = " . $details['userName'] . "\n"; | |
echo "pass = " . user_password() . "\n"; | |
echo "mail = " . $edge_developer . "\n"; | |
echo "status = " . $details['status'] . "\n"; | |
if ($local_names[$details['userName']]) { | |
echo "==####=======> Duplicate username: " . $details['userName'] . " <========####==\n"; | |
} | |
else { | |
$users_query->execute([ | |
':uid' => $uid, | |
':name' => $details['userName'], | |
':pass' => user_password(), | |
':mail' => $edge_developer, | |
':status' => ($details['status'] == 'active' ? 1 : 0), | |
':init' => $edge_developer, | |
':created' => time(), | |
':uuid' => GUID(), | |
]); | |
$fn_data_query->execute([ | |
':uid' => $uid, | |
':vid' => $uid, | |
':name' => $details['firstName'], | |
]); | |
$fn_rev_query->execute([ | |
':uid' => $uid, | |
':vid' => $uid, | |
':name' => $details['firstName'], | |
]); | |
$ln_data_query->execute([ | |
':uid' => $uid, | |
':vid' => $uid, | |
':name' => $details['lastName'], | |
]); | |
$ln_rev_query->execute([ | |
':uid' => $uid, | |
':vid' => $uid, | |
':name' => $details['lastName'], | |
]); | |
// 10 = self serve role | |
$user_roles->execute([ | |
':uid' => $uid, | |
':rid' => 10, | |
]); | |
echo "[$j/$total] Processed $edge_developer\n"; | |
$uid++; | |
$PDO->exec('UPDATE sequences SET value = ' . $uid); | |
} | |
} catch | |
(Exception $e) { | |
exec('echo "$edge_developer . " >> ' . $tmp_file); | |
echo "#### missing developer in mint >> " . $edge_developer . "\n"; | |
} | |
} | |
$last_developer = $edge_developer; | |
} | |
// Get new developer list - count | |
$response = $client->get('developers?count=9999&startKey=' . $last_developer); | |
$edge_developers = json_decode($response->getBody(), TRUE); | |
$total = count($edge_developers); | |
echo "\n ###### NEW TOTAL: $total #######\n"; | |
} | |
exec ('cp -rp ' . $tmp_file . ' /Users/ApigeeCorporation/Downloads/.'); | |
exec ('cp -rp ' . $tmp_file2 . ' /Users/ApigeeCorporation/Downloads/.'); | |
echo "Done!\n"; | |
function user_password() { | |
$allowable_characters = 'abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789'; | |
$len = strlen($allowable_characters) - 1; | |
$pass = ''; | |
for ($i = 0; $i < 10; $i++) { | |
$pass .= $allowable_characters[mt_rand(0, $len)]; | |
} | |
return $pass; | |
} | |
function GUID() | |
{ | |
if (function_exists('com_create_guid') === true) | |
{ | |
return trim(com_create_guid(), '{}'); | |
} | |
return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535)); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment