Skip to content

Commit

Permalink
Add create_template
Browse files Browse the repository at this point in the history
Closes cloudyr#42
Add create template, remove template, describe template
  • Loading branch information
elimillera committed Mar 30, 2019
1 parent 62e3244 commit ae5fe56
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 0 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# aws.ec2 0.1.17

* Added provisional support for Launch Templates(#42)

# aws.ec2 0.1.16

* Added provisional support for EBS volumes and snapshots.
Expand Down
54 changes: 54 additions & 0 deletions R/create_template.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#' @title Create Launch Template
#' @description Create Instance Template, templates can be sent to run_instance()
#' @param name A name for the launch template, String, Minchar:3, Max:128
#' @param desc A description for the first version of the template.
#' @param templateData A named list of template data. See reference
#' @param clientToken Identifier to ensure idempotency.
#' @template dots
#' @example
#' \dontrun{
#' create_template(tempName = "testTemplate", tempDesc = "newDesc",
#' templateData = list(ImageId = "ami-1a2b3c4d", InstanceType = "t1.micro"),
#' clientToken = "123")
#' describe_template(tempName = "testTemplate")
#' delete_template(tempDesc = "testTemplate")
#' }
#' @keywords template
#' @references
#' <https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_CreateLaunchTemplate.html>
#' <https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_RequestLaunchTemplateData.html>
#' @export


create_template <-
function(
tempName = NULL,
tempDesc = NULL,
templateData = NULL,
clientToken = NULL,
...

) {
query <- list(Action = "CreateLaunchTemplate")
if(!is.null(tempName)) {
names(tempName) <- "LaunchTemplateName"
query <- c(query, tempName)
}
if(!is.null(tempDesc)) {
names(tempDesc) <- "VersionDescription"
query <- c(query, tempDesc)
}
if(!is.null(clientToken)) {
names(clientToken) <- "ClientToken"
query <- c(query, clientToken)
}
if(!is.null(templateData)) {
names(templateData) <- paste0("LaunchTemplateData.", names(templateData))
query <- c(query, templateData)
}

r <- ec2HTTP(query = query, ...)
return(unname(lapply(r, function(z) {
structure(flatten_list(z), class = "ec2_template")
})))
}
43 changes: 43 additions & 0 deletions R/delete_template.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#' @title Delete Launch Template
#' @description Delete Instance Template
#' @param name The name of the launch template to delete.
#' @param tempId The ID of the launch template to delete. Must specify
#' name or ID.
#' @template dots
#' @example
#' \dontrun{
#' create_template(tempName = "testTemplate", tempDesc = "newDesc",
#' templateData = list(ImageId = "ami-1a2b3c4d", InstanceType = "t1.micro"),
#' clientToken = "123")
#' describe_template(tempName = "testTemplate")
#' delete_template(tempDesc = "testTemplate")
#' }
#' @keywords template
#' @references
#' <https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_CreateLaunchTemplate.html>
#' <https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_RequestLaunchTemplateData.html>
#' @export

delete_template <- function(
tempName = NULL,
tempId = NULL,
...
) {
query <- list(Action = "DeleteLaunchTemplate")

if(!is.null(tempId)) {
names(tempId) <- "LaunchTemplateId"
query <- c(query, tempId)
}
else if(!is.null(tempName)) {
names(tempName) <- "LaunchTemplateName"
query <- c(query, tempName)
} else {
stop("delete_template wasn't passed a name or ID")
}

r <- ec2HTTP(query = query, ...)
return(unname(lapply(r, function(z) {
structure(flatten_list(z), class = "ec2_template_del")
})))
}
53 changes: 53 additions & 0 deletions R/describe_template.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#' @title Describe Launch Template
#' @description Describe Instance Template
#' @param tempName The name of the launch template to describe
#' @param tempId The ID of the launch template to delete. Must specify
#' name or ID.
#' @param filter One or more filters
#' @template dots
#' @example
#' \dontrun{
#' create_template(tempName = "testTemplate", tempDesc = "newDesc",
#' templateData = list(ImageId = "ami-1a2b3c4d", InstanceType = "t1.micro"),
#' clientToken = "123")
#' describe_template(tempName = "testTemplate")
#' delete_template(tempDesc = "testTemplate")
#' }
#' @keywords template
#' @references
#' <https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_CreateLaunchTemplate.html>
#' <https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_RequestLaunchTemplateData.html>
#' <https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_Filter.html>
#' @export

describe_template <-
function(
tempName = NULL,
tempId = NULL,
filter = NULL,
...
){

query <- list(Action = "DescribeLaunchTemplates")

if(!is.null(tempId)) {
names(tempId) <- "LaunchTemplateId.1"
query <- c(query, tempId)
}
else if(!is.null(tempName)) {
names(tempName) <- "LaunchTemplateName.1"
query <- c(query, tempName)
}
else {
stop("describe_template wasn't passed a name/Id")
}
if(!is.null(filter)) {
names(filter) <- "Filter.1"
query <- c(query, filter)
}

r <- ec2HTTP(query = query, ...)
return(unname(lapply(r, function(z) {
structure(flatten_list(z), class = "ec2_template_desc")
})))
}
6 changes: 6 additions & 0 deletions tests/testthat/test_template.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
context("Templates")

test_that("delete/describe stops if Id/Name isn't passed", {
expect_error(delete_template(), "delete_template wasn't passed")
expect_error(describe_template(), "describe_template wasn't passed")
})

0 comments on commit ae5fe56

Please sign in to comment.