git clone https://github.com/CSC-DevOps/Provision
cd Provision
then npm install
# Mac/Linux
export NCSU_DOTOKEN="xxx"
# Windows
setx NCSU_DOTOKEN xxx
node index.js
If everything is set correctly, you should see output like the following:
Your token is: xxxxxx
Calls remaining 4931
A very brief overview on REST APIs:
A RESTful architecture, is an architectural style (Fielding) for providing resources to clients using a set of request verbs and resource locations. A REST API allows a client to access resources provided by a service.
A useful workshop on using the GitHub REST APIs can be done for more practice.
The goal of REST APIs is to provide painless communication and interoperability between applications and services. REST APIs typically allow (often with some sort of authentication) a manipulation of the various types of entities associated with a service.
For example, if an API was created to enable an application to get information about user accounts, create new ones, update them, or delete them, the REST API could simply be defined by combining the following HTTP VERBS (GET, POST, PUT, DELETE), with resource paths, such as /users/.
HTTP Verb | Action | Example | Result |
---|---|---|---|
GET | Retrieve record(s) | GET /users/ | Retrieves all users |
POST | Create record | POST /users/ | Creates a new user |
PUT | Update record | PUT /users/6 | Updates user 6 |
DELETE | Delete record(s) | DELETE /users/1 | Deletes user with id 1 |
Query parameters can be provided to provide additional information to the request. For example, GET /users?country=France
would return a list of all users in France.
A response typically is composed of several parts: including headers, status code, and body.
Headers will may useful information, such as rate-limits quotas, or properties of the response, such as whether it is encoded or compressed in a particular format.
The status code will allow you to verify the success of an operation or indicate different types of failures. Additional error information may be sent via the request body.
The body will typically contain a JSON-formated object or an array. Some actions, such as deleting an object, may not return any data in the response body.
When sending a request, a JSON-formatted object is sent attached via the request body to the server.
Let's see how a javascript client application can communicate with a server using a REST API call.
In a browser, visit http://httpbin.org/
Open the developer's tool console (in your Web browser) and execute this code inside of the console.
Note: If you try this same code from a different page, you'll get a Cross-Origin Resource Sharing (CORS) error.
fetch("https://httpbin.org/anything")
.then(data => data.json())
.then(result => console.log(result));
This constructs as simple GET
request to https://httpbin.org/anything
and returns a copy of what was sent.
Here are the same examples as above, but written to work with curl:
curl --request GET https://httpbin.org/anything
curl --request PUT -H "Content-Type: application/json" --data '{"coffee":1,"milk":1,"sugar":1,"chocolate":1}' https://httpbin.org/anything
You will be experimenting with code for provisioning a new server from a cloud provider at a particular region, that is initialized with a specified virtual machine image, using the digitalocean api v2.
The code makes use of the got api for making http requests, suitable for interacting with a REST API.
Complete the remaining 7 steps specified in the code comments of index.js
.
- List regions.
- List VM images.
- Create droplet.
- Get droplet ip
- Ping ip
- Destroy droplet
- Ping ip, make sure dead.
You can use curl
to help debug your calls:
curl -X GET -H 'Content-Type: application/json' -H "Authorization: Bearer $NCSU_DOTOKEN" "https://api.digitalocean.com/v2/images"
Note: For Windows, use %NCSU_DOTOKEN%
in your curl command to expand the environment variable.