Skip to content

Commit

Permalink
added droplets_create for many droplets at once, fix #112
Browse files Browse the repository at this point in the history
  • Loading branch information
sckott committed Dec 31, 2015
1 parent 0549e9c commit 74a8609
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Description: Provides a set of functions for interacting with the 'Digital
Ocean' 'API' at https://developers.digitalocean.com/documentation/v2/, including
creating images, destroying them, 'rebooting', getting details on regions, and
available images.
Version: 0.3.0.9500
Version: 0.3.0.9600
Authors@R: c(
person("Scott", "Chamberlain", role = c("aut", "cre"), email = "myrmecocystus@gmail.com"),
person("Hadley", "Wickham", role = "aut", email = "hadley@rstudio.com"),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export(droplet_upgrades_list)
export(droplet_upload)
export(droplet_wait)
export(droplets)
export(droplets_create)
export(image)
export(image_actions)
export(image_convert)
Expand Down
87 changes: 87 additions & 0 deletions R/droplets_create.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#' Create many new droplets.
#'
#' There are defaults for each of size, image, and region so that a quick one-liner with one
#' parameter is possible: simply specify the name of the droplet and your'e up and running.
#'
#' @export
#' @param names (character) Names of the droplets. The human-readable string you
#' wish to use when displaying the Droplet name. The name, if set to a domain
#' name managed in the DigitalOcean DNS management system, will configure a PTR
#' record for the Droplet. The name set during creation will also determine
#' the hostname for the Droplet in its internal configuration. Default: picks
#' a random name from \code{\link{words}} if none supplied.
#' @inheritParams droplet_create
#'
#' @details Note that if you exit the R session or kill the function call after it's
#' in waiting process (the string of ...), the droplet creation will continue.
#'
#' @return Two or more droplet objects
#'
#' @examples \dontrun{
#' # if no names given, creates two droplets with random names
#' droplets_create()
#'
#' # give names
#' droplets_create(names = c('drop1', 'drop2'))
#' droplets_create(names = c('drop3', 'drop4'))
#' }
droplets_create <- function(names = NULL,
size = getOption("do_size", "512mb"),
image = getOption("do_image", "ubuntu-14-04-x64"),
region = getOption("do_region", "sfo1"),
ssh_keys = getOption("do_ssh_keys", NULL),
backups = getOption("do_backups", NULL),
ipv6 = getOption("do_ipv6", NULL),
private_networking = getOption("do_private_networking", NULL),
user_data = NULL,
cloud_config = NULL,
wait = TRUE,
...) {

ssh_keys <- standardise_keys(ssh_keys)
if (length(ssh_keys) == 0) {
warning("You have not specified any ssh_keys. This is NOT recommended.",
" (You will receive an email with the root password in a few minutes",
call. = FALSE)
}

# Generate user_data if cloud_config specified
if (!is.null(cloud_config)) {
if (!is.null(user_data)) {
stop("You may only specify one of cloud_config and user_data.",
call. = FALSE)
}

user_data <- cloud_config(cloud_config, ssh_keys)
}

if (is.null(names)) names <- replicate(2, random_name())

res <- do_POST('droplets',
body = list(
names = names,
size = unbox(size),
image = unbox(image),
region = unbox(region),
ssh_keys = I(ssh_keys),
backups = unbox(backups),
ipv6 = unbox(ipv6),
private_networking = unbox(private_networking),
user_data = unbox(user_data)
), ...)
droplets <- lapply(res$droplets, function(z) droplet(z$id))

for (i in seq_along(droplets)) {
message("NB: This costs $", droplets[[i]]$size$price_hourly, " / hour ",
"until you droplet_delete() it")
}

if (wait) {
for (i in seq_along(droplets)) {
droplet_wait(droplets[[i]])
}
droplets
} else {
droplets
}
}
94 changes: 94 additions & 0 deletions man/droplets_create.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 74a8609

Please sign in to comment.