Skip to content

Instantly share code, notes, and snippets.

@hasinhayder
Forked from stevebrun/Migration.js
Created March 21, 2017 09:00
Show Gist options
  • Save hasinhayder/f5e1dc5a76cb78ccfb39d567d25e9e9a to your computer and use it in GitHub Desktop.
Save hasinhayder/f5e1dc5a76cb78ccfb39d567d25e9e9a to your computer and use it in GitHub Desktop.
A script for migrating the primary domain of a Google Apps account.

#How to Migrate the Primary Domain of a Google Apps Account

##Instructions

###Step 1

Log into the administrator account. If you're reading this, that's probably your account.

###Step 2

Read the Google Apps Script Quickstart Guide to understand the basic concept behind using Google Apps Scripts within Drive.

Be sure to pay special attention to Step 2: Turn on the Directory API--because the migration script requires it.

###Step 3

Copy the code from Migration.js, and place it within a Google Apps Script of your very own.

###Step 4

Create a function like:

function migrateMe() {
  // call migrateDomain() with your organization and desired domain here
}

"Why is the organization name parameter necessary?" you may ask? Well, it's because without it, I was getting some weird error about an invalid firm_name or something. Just make sure that you provide a non-null-nor-empty-string value for that parameter.

###Step 5

Be sure that your 'migrateMe' method is selected in the drop-down next to the run and debug buttons at the top of the script editor, and then press that run button.

You can see whether or not the migration was a success by looking at the logs, which can be found from within the View menu.

If the migration failed, you can always resort to sprinkling Logger.log('%s', ...); throughout the code to follow the different customer values being sent and received.

##How I Figured This Out

Lots. Of. Googling.

Below are what I believe to be the best resources for reverse engineering my instructions for yourself, listed in the order I think best to read.

  1. Google Apps Administrator's page on how change your primary domain, for all the risks and alternatives.
  2. Google Developers' page on Google Apps Scripts and how they do, for getting a grip on what a Google Apps Script even is in the first place. Special shoutout to Step 2! Thanks for telling me how to enable those APIs, buddy.
  3. Google Developers' page on how method signatures are determined for Google services based on their HTTP interfaces. Without this page, I would have never figured out which methods to call on which objects when. Even then I still struggled—but I survived!
  4. And last but not least, Google Developers' page on the Directory API's HTTP interface and how to use it to change your primary domain.
// migrate the script executer's primary domain
// domainName - the desired domain to migrate to
function migrateDomain(organizationName, domainName) {
var customerId = 'my_customer';
var customer = AdminDirectory.Customers.get(customerId);
customer.customerDomain = domainName;
customer.postalAddress.organizationName = organizationName;
customer.customerCreationTime = undefined; //
AdminDirectory.Customers.patch(customer, customerId);
var result = AdminDirectory.Customers.get(customerId);
if (result.customerDomain === domainName) {
Logger.log("Success!");
} else {
Logger.log("Failure");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment