<!--
%\VignetteEngine{knitr::knitr}
%\VignetteIndexEntry{Working with the Digital Ocean API}
%\VignetteEncoding{UTF-8}
-->

```{r echo=FALSE}
knitr::opts_chunk$set(
  comment = "#>",
  warning = FALSE,
  message = FALSE
)
```


Working with the Digital Ocean API
======

## Create a DO account

If you don't already have one, [create a DO account](https://www.digitalocean.com/?refcode=0740f5169634). 
By using this link, you'll start with $10 in credits (enough for >600 hours of 
computing on a 1 gb machine), and if you become a digital ocean customer we'll 
get some DO credits for us to offset our costs for testing. Thanks :)

## Authenticate

The best way to authenticate is to generate a personal access token 
(<https://cloud.digitalocean.com/settings/tokens/new>) and save it in an 
environment variable called `DO_PAT`.  If you don't do this, you'll be prompted 
to authenticate in your browser the first time you use analogsea.

## SSH keys

`analogsea` allows you to interact with your droplet(s) from R via SSH. To do 
this you need to setup SSH keys with Digital Ocean. Make sure you provide Digitial 
Ocean your public key at <https://cloud.digitalocean.com/ssh_keys>. GitHub has 
some good advice on  creating a new public key if you don't already have one: 
<https://help.github.com/articles/generating-ssh-keys/>.

Note that when using ssh, you'll likely get warnings like 

> The authenticity of host can't be established ...

This is normal, don't be worried about this.

Note that if you want to connect over SSH to a droplet you have to 
create the droplet with an SSH key with the `ssh_keys` parameter. If you
don't you can still interact with the droplet via the Digital Ocean API, 
but you can't access the droplet over SSH. 

## Create a droplet

`droplet_create()` will create a droplet on your account. You can run it as 
below without any inputs, and it will use sensible defaults:

* Memory size of 1gb
* Ubuntu 18.04 box
* Region sfo3
* Uses your ssh key
* Don't use ipv6
* Don't allow backups
* Don't allow private networking

You can set all of these options in your `.Rprofile` file like 
`options(do_size = "8gb")` for a default of 8 GB.

The name given to the droplet is picked at random from a list of 1000 random names.

You can of course set any of these parameters.

```{r eval=FALSE}
droplet_create()
```

You can also create many droplets at once:

```{r eval=FALSE}
droplets_create()
```

## Get a droplet or droplets

Listing droplets can be done in singular or plural fashion. `droplet()` accepts 
a droplet ID, while `droplets()` list all droplets.

If you don't have any droplets yet, you will get an empty list running 
`droplets()`, and you of course can't pass in a droplet ID number to `droplet()` 
if you don't have any droplets yet.

```{r}
library("analogsea")
```

```{r eval=FALSE}
droplets()
#> named list()
```

Create a droplet

```{r}
droplet_create()
```

After creating a droplet and running `droplets()` again, we see a list of 
our droplet(s)

```{r echo=FALSE}
drops <- droplets()[-1]
```

```{r eval=FALSE}
(drops <- droplets())
```

Or we can pass in a droplet id to `droplet()`. There is a `print.droplet()` method 
that is used to print a brief summary of each droplet. 

```{r}
droplet(drops[[1]]$id)
```

Get more detailed information on your droplet with `summary()`. This is a 
`summary.droplet()` method, that is just a little more verbose than the 
`print.droplet()` method 

```{r}
droplet(drops[[1]]$id) %>% summary
```


## Actions on droplets

### Delete

You can delete a droplet with `droplet_delete()`. Be careful, as this completely 
removes your droplet. Backup your droplet or make an image if you want to use 
the droplet later.

```{r}
droplet_create() %>%
  droplet_delete()
```

### Actions

List actions on a droplet, newer ones at the top. Here, list actions

```{r}
drops[[1]] %>% droplet_actions()
```

Then rename and list actions again

```{r eval=FALSE}
drops[[1]] %>%
  droplet_rename(name = "droppy") %>%
  droplet_wait() %>%
  droplet_actions()
#> Waiting for rename ...
#> [[1]]
#> <action> rename (166715389)
#>   Status: completed
#>   Resource: droplet 31859471
#> 
#> [[2]]
#> <action> create (166715005)
#>   Status: completed
#>   Resource: droplet 31859471
```

### Snapshot

Making a snapshot of a droplet can be done with `droplet_snapshot()`. This 
action requires that you turn off the droplet first, then take the snapshot. 
First, create a droplet

```{r eval=FALSE}
d <- droplet_create(size = "2gb")
```

Then power off, and take a snapshot, which gives an action object describing 
that the snapshot is in progress.

```{r eval=FALSE}
d %>%
  droplet_power_off() %>%
  droplet_wait() %>%
  droplet_snapshot(name = "mynewsnap")
#> Waiting for power_off ...................................................
#> <action> snapshot (166715834)
#>   Status: in-progress
#>   Resource: droplet 31859617
```

## Regions

The `regions()` function lists region slug names, full names, available sizes,
whether the region is available at all, and features.

This helps you get an overview of region details, which you can select from
when creating droplets

```{r}
regions()
```

## Sizes

The `sizes()` function lists size slug names, associated memory, vcpus, disk 
size, prices, and regions where the size is available.

This helps you get an overview of sizes, which you can select from
when creating droplets

```{r}
sizes()
```

## Keys

We suggest you use SSH keys to interact with Digital Ocean from `analogsea`. 
There are a variety of functions for working with SSH keys.

List your keys

```{r}
keys()
```

Get a key by id

```{r}
key(keys()[[1]]$id)
```

You can also create a key, rename a key, and delete a key

```{r eval=FALSE}
k <- key_create("key", readLines("~/.ssh/id_rsa.pub"))
k <- key_rename(k, "new_name")
key_delete(k)
```

Note that if you're on Windows you may experience some problems connecting 
over SSH. We hope to resolve these  problems as soon as possible.

## Images

The `images()` function can list both your own private images, and public images. 
If `public=FALSE` only your private images are listed, while if `public=TRUE`, 
your private images are listed along with publicly avaialble images.

```{r}
images(page = 4, per_page = 5)
```

You can also do various actions on images. First, you can pass in an image ID to 
the `image()` function to get an image object.

```{r}
img <- images(per_page = 1)[[1]]
image(img$id)
```

You can rename an image

```{r eval=FALSE}
img %>% image_rename(name = "analog")
```

You can transfer an image to another region

```{r eval=FALSE}
image(img$id) %>% image_transfer(region = "sfo3")
```

## Domains

You can use domain names for your droplets on Digital Ocean. `analogsea` has 
a variety of functions to work with domain names.

List domain names

```{r}
domains()
```

Create a new domain name

```{r}
dom <- paste0(sample(words, 1), ".info")
domain_create(name = dom, ip_address = "127.0.0.1")
```

Get a single domain by domain name

```{r}
domain(dom)
```

Create a domain record, list records and delete the one just created

```{r}
domain(dom) %>%
  domain_record_create(type = "TXT", name = "hello", data = "world")
records <- domain(dom) %>% domain_records()
domain_record_delete(records[[length(records)]])
```

List records

```{r}
domain(dom) %>% domain_records()
```

Delete a domain name, returns nothing if delete is successful

```{r}
domain(dom) %>% domain_delete()
```
