R/set_adl_token.R

Defines functions set_adl_token

Documented in set_adl_token

#' Set Azure Data Lake Token
#'
#' Setting the Azure Data Lake Token to the global environment to be read by other
#' flyrod functions.
#' This must be set before any funciton will run.
#'
#' It is required that a service principal in AD is set up.
#'
#'
#' @param tenant A string representing the tenant id. _Required parameter._
#' @param client_id A string representing the client id. _Required parameter._
#' @param client_secret A string representing the client secret. _Required parameter._
#' @return no value is returned, but ADL_TOKEN is set as an environment variable
#' @importFrom curl new_handle handle_setform curl_fetch_memory
#' @importFrom jsonlite fromJSON
#' @examples
#' \dontrun{
#'  set_adl_token(tenant = "abc123", client_id = "abc123", client_secret = "abc123")
#' }
#'
#' @export
set_adl_token <- function(tenant, client_id, client_secret){
  if(missing(tenant)){
     stop("tenant is missing")
  }else if(missing(client_id)){
    stop("client_id is missing")
  }else if(missing(client_secret)){
    stop("client_secret is missing")
  }else{
  h <- curl::new_handle()
  curl::handle_setform(h,
                 "grant_type"="client_credentials",
                 "resource"="https://management.core.windows.net/",
                 "client_id"=client_id,
                 "client_secret"=client_secret)
  req <- curl::curl_fetch_memory(paste0("https://login.windows.net/", tenant, "/oauth2/token"),
                                 handle = h)
  token <- jsonlite::fromJSON(rawToChar(req$content))$access_token
  Sys.setenv(ADL_TOKEN = token)
  message("ADL_TOKEN has been set as an environment variable.
          If you want to view it use Sys.getenv(\"ADL_TOKEN\")")
  }
}
alexhallam/flyrod documentation built on Nov. 20, 2019, 7:33 a.m.