Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
adding Cory's example and README
- Loading branch information
1 parent
8f0ae57
commit 24daa6d
Showing
15 changed files
with
251 additions
and
0 deletions.
There are no files selected for viewing
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# Meraki Dashboard with Google Scripts & Forms | ||
|
||
## Overview | ||
|
||
This learning lab demonstrates the power of Meraki APIs with Google Scripts and Forms. | ||
|
||
By building a simple Google Form and attaching a script written in JavaScript, a Meraki Dashboard administrator can easily be created. This is incredibly helpful when running workshops that require several administrators to have access to a lab network. | ||
|
||
# Instructions | ||
## Create a Google Form | ||
https://forms.google.com | ||
|
||
### The first two questions should be as follows: | ||
- Name | ||
|
||
[SAMPLE] | ||
|
||
<img src="images/Meraki API Registration Form Screenshot.png" alt="Form" width=400/> | ||
|
||
## Link the form to a Google Script | ||
|
||
<img src="images/GoogleScriptsMenu.png" alt="Google Scripts Menu" width=400/> | ||
|
||
## Paste the contents of this repository's `code.gs` file into the Google Scripts IDE. | ||
|
||
- [code.gs](code.gs) | ||
- update the **API_KEY**, **ORG_ID** and **SHARD** to match your settings. | ||
- save your changes | ||
|
||
<img src="images/GoogleScriptsIDE.png" alt="Google Scripts IDE" width=400/> | ||
|
||
## Add a Trigger to launch the script when the Form is submitted. | ||
|
||
<img src="images/GoogleScriptsTriggersMenu.png" alt="Triggers Menu" width=400/> | ||
|
||
### Configure the trigger | ||
- Run: **onFormSubmit** | ||
- Events: **From form** | ||
- --> On form submit | ||
|
||
<img src="images/GoogleScriptsTriggers.png" alt="Triggers" width=600/> | ||
|
||
## Test the API calls | ||
Several additional functions are included in this code to allow you to test with sample data and collect information. | ||
- Run --> *Select a **test** function* | ||
|
||
<img src="images/GoogleScriptsRunMenu.png" alt="Run Menu" width=400/> | ||
|
||
Feel free to modify the sample JSON data defined in the `testAddMerakiAdmin` function. | ||
|
||
``` | ||
function testAddMerakiAdmin(){ | ||
var data = { | ||
"name":"Google Scripts Demo", | ||
"email":"GoogleScriptsDemo@meraki.com", // change this to an email you have access to! | ||
"orgAccess":"full" | ||
}; | ||
addMerakiAdmin(API_KEY,ORG_ID,SHARD,data); | ||
} | ||
``` | ||
|
||
|
||
## View the results | ||
- View --> Logs | ||
|
||
<img src="images/GoogleScriptsLogsMenu.png" alt="Logs Menu" width=400/> | ||
|
||
<img src="images/GoogleScriptsLogs.png" alt="Logs" width=400/> | ||
|
||
|
||
## Google Form | ||
Now that the API calls are working, test the Google Form by hitting the preview button. | ||
|
||
<img src="images/Google Form Preview button.png" alt="Logs" width=300/> | ||
|
||
Complete the form with a valid email address. | ||
|
||
<img src="images/GoogleScriptsForm.png" alt="Logs" width=400/> | ||
|
||
If everything worked, you should get an email from Meraki asking to complete the admin account verification. | ||
|
||
<img src="images/Meraki Account Verification Email.png" alt="Logs Menu" width=600/> | ||
|
||
|
||
## Verify Meraki account is created | ||
- Meraki Dashboard --> Organization --> Administrators | ||
|
||
<img src="images/Meraki Admin User screenshot - demo.png" alt="Admin Users" width=600/> | ||
|
||
## SUCCESS! | ||
|
||
You have now used the Meraki APIs to dynamically create Meraki administrator accounts. With Google Apps, you do not even need to host a server to run the application. Cool! | ||
|
||
|
||
|
||
### Meraki API Resources | ||
http://developers.meraki.com/ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
/* ##################################################### | ||
Cisco Meraki API Workshop Registration Google Script | ||
- This script collects a single response from a Google Form | ||
* Name | ||
- Creates an admin account with Meraki API | ||
There are additional test functions which are useful to run API calls and view the output in the log without using the form. | ||
Please update your environment variables below for proper operation. | ||
Written by: | ||
Cory Guynn | ||
2017 | ||
Cisco Meraki | ||
InternetOfLEGO.com | ||
/ ##################################################### */ | ||
|
||
|
||
|
||
/* ##################################################### | ||
Enter Your Environment Variables | ||
/ ##################################################### */ | ||
|
||
// Update your API Key. This is in your Meraki Dashboard profile. Your Org must have APIs enabled. | ||
var API_KEY = 'YourAPIKey'; | ||
|
||
// Update your Org ID (use testGetMerakiOrgs(); to learn the org IDs) | ||
var ORG_ID = 'YourOrgID'; | ||
|
||
// Update Shard Number (you can see this when looking at the URL when you login to Meraki Dashboard or by pulling the SNMP settings via API call) | ||
// https://nXXX.meraki.com/api/v0/organizations/{{organizationId}}/admins | ||
var SHARD = 'nXXX'; // if you are confused, you can try 'dashboard', but sometimes redirects are problematic. | ||
|
||
// Update the orgAccess. By default, the new account has FULL ADMIN ACCESS to the Organization!!! | ||
// Nice for hackathons, bad for production. The API call can also include network paramters and tags. | ||
var PERMISSIONS = 'full'; | ||
|
||
|
||
// ************* | ||
// API CALLS TO MERAKI | ||
// ************* | ||
|
||
|
||
// Add Meraki Admin | ||
function addMerakiAdmin(apiKey, orgId, shard, payload){ | ||
var headers = { | ||
"x-cisco-meraki-api-key": apiKey | ||
}; | ||
var options = | ||
{ | ||
"method" : "post", | ||
"payload": payload, | ||
"headers": headers, | ||
"content-type": "application/json", | ||
"contentLength": payload.length, | ||
"followRedirects": true, | ||
"muteHttpExceptions":true | ||
}; | ||
response = UrlFetchApp.fetch('https://'+shard+'.meraki.com/api/v0/organizations/'+orgId+'/admins', options); | ||
var result = JSON.parse(response.getContentText()); | ||
Logger.log(result); | ||
Logger.log(response.getResponseCode()); | ||
} | ||
|
||
// Test function (use this to test the API call without using the form) | ||
function testAddMerakiAdmin(){ | ||
var data = { | ||
"name":"Google Scripts Demo", | ||
"email":"GoogleScriptsDemo@meraki.com", // change this to an email you have access to! | ||
"orgAccess":"full" | ||
}; | ||
addMerakiAdmin(API_KEY,ORG_ID,SHARD,data); | ||
} | ||
|
||
|
||
// Get Meraki Admins -- Not used in the form, but really helpful to see if admins are getting created | ||
function getMerakiAdmins(apiKey,orgId){ | ||
var payload; | ||
var headers = { | ||
"x-cisco-meraki-api-key": apiKey | ||
}; | ||
var url = 'https://dashboard.meraki.com/api/v0/organizations/'+orgId+'/admins'; | ||
var options = { | ||
'method': 'get', | ||
'headers': headers, | ||
'payload': payload | ||
}; | ||
var response = UrlFetchApp.fetch(url, options); | ||
var json = response.getContentText(); | ||
var data = JSON.parse(json); | ||
|
||
Logger.log("URL JSON: "+ JSON.stringify(data)); | ||
} | ||
|
||
// Test function (Use this to test the API call since it includes the environment variables) | ||
function testGetMerakiAdmins(){ | ||
getMerakiAdmins(API_KEY,ORG_ID); | ||
} | ||
|
||
// Get the Meraki Organizations and IDs this API key hass access to. | ||
function getMerakiOrgs(apiKey){ | ||
var headers = { | ||
"x-cisco-meraki-api-key": apiKey | ||
}; | ||
var url = 'https://dashboard.meraki.com/api/v0/organizations'; | ||
var options = { | ||
'method': 'get', | ||
'headers': headers | ||
}; | ||
var response = UrlFetchApp.fetch(url, options); | ||
var json = response.getContentText(); | ||
var data = JSON.parse(json); | ||
|
||
Logger.log("Meraki Orgs: "+ JSON.stringify(data)); | ||
} | ||
|
||
// Test function | ||
function testGetMerakiOrgs(){ | ||
getMerakiOrgs(API_KEY); | ||
} | ||
|
||
|
||
// ************* | ||
// Form Handler | ||
// ************* | ||
|
||
// Collects input from form submition | ||
// (must run this via form or an error will occur) | ||
|
||
function onFormSubmit(e) { | ||
var form = FormApp.getActiveForm(); | ||
var formResponse = e.response; | ||
var itemResponses = formResponse.getItemResponses(); | ||
|
||
var admin = {}; | ||
admin.name = itemResponses[0].getResponse(); | ||
admin.email = formResponse.getRespondentEmail(); | ||
|
||
Logger.log("onFormSubmit - name: "+admin.name); | ||
Logger.log("onFormSubmit - email: "+admin.email); | ||
|
||
// Create admin account | ||
var payload = { | ||
"name":admin.name, | ||
"email":admin.email, | ||
"orgAccess":PERMISSIONS | ||
}; | ||
addMerakiAdmin(API_KEY, ORG_ID, SHARD, payload); | ||
} |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.