1111# ' publisher priviliges.
1212# ' @param from_key An API key on the originating "from" server. The API key must
1313# ' belong to a user with collaborator access to the content to be promoted.
14- # ' @param app_name The name of the content on the originating "from" server.
14+ # ' @param name The name of the content on the originating "from" server.
1515# ' If content with the same name is found on the destination server,
1616# ' the content will be updated. If no content on the destination server
1717# ' has a matching name, a new endpoint will be created.
@@ -21,7 +21,7 @@ promote <- function(from,
2121 to ,
2222 to_key ,
2323 from_key ,
24- app_name ) {
24+ name ) {
2525
2626 # TODO Validate Inputs
2727
@@ -30,70 +30,73 @@ promote <- function(from,
3030 to_client <- Connect $ new(host = to , api_key = to_key )
3131
3232 # find app on "from" server
33- from_app <- from_client $ get_apps(list (name = app_name ))
33+ from_app <- from_client $ get_apps(list (name = name ))
3434 if (length(from_app ) != 1 ) {
35- stop(sprintf(' Found %d apps matching app name %s on %s. Content must have a unique name.' , length(from_app ), app_name , from ))
35+ stop(sprintf(' Found %d apps matching app name %s on %s. Content must have a unique name.' , length(from_app ), name , from ))
3636 }
3737
3838 # download bundle
3939 bundle <- from_client $ download_bundle(from_app [[1 ]]$ bundle_id )
4040
4141 # find or create app to update
42- to_app <- to_client $ get_apps(list (name = app_name ))
43- if (length(to_app ) > 1 ) {
44- stop(sprintf(' Found %d apps matching %s on %s, content must have a unique name.' , length(to_app ), app_name , to ))
45- } else if (length(to_app ) == 0 ) {
46- # create app
47- to_app <- to_client $ create_app(app_name )
48- warning(sprintf(' Creating NEW app %d with name %s on %s' , to_app $ id , app_name , to ))
49- } else {
50- to_app <- to_app [[1 ]]
51- warning(sprintf(' Updating EXISTING app %d with name %s on %s' , to_app $ id , app_name , to ))
52- }
42+ to_app <- content_ensure(connect = to_client , name = name )
5343
54- task_id <- deploy_bundle(
55- connect = to_client ,
56- bundle = bundle ,
57- app_id = to_app $ id
58- )
44+ bundle_id <- to_client $ content_upload(bundle_path = bundle , guid = to_app [[" guid" ]])[[" bundle_id" ]]
45+ task_id <- to_client $ content_deploy(guid = to_app [[" guid" ]], bundle_id = bundle_id )[[" task_id" ]]
5946
6047 poll_task(connect = to_client , task_id = task_id )
6148
62- to_app_url <- app $ url
49+ to_app_url <- to_app $ url
6350
6451 return (to_app_url )
6552}
6653
67- # ' @export
68- content_ensure <- function (connect , name = random_name(), title = name , ... ) {
54+ content_ensure <- function (connect , name = random_name(), title = name , guid = NULL , ... ) {
6955
70- content <- connect $ get_apps( list ( name = name ))
71- if (length( content ) > 1 ) {
72- stop( glue :: glue( " Found {length(to_content)} content items " ,
73- " matching {content_name} on {connect$host} " ,
74- " , content must have a unique name. " ))
75- } else if (length( content ) == 0 ) {
76- # create app
77- content <- connect $ content_create(
78- name = name ,
79- title = title ,
80- ...
81- )
82- message( glue :: glue( " Creating NEW content {content$guid} " ,
83- " with name {name} on {connect$host} " ))
56+ if ( ! is.null( guid )) {
57+ # guid-based deployment
58+ # just in case we get a 404 back...
59+ content <- tryCatch( connect $ get_content( guid = guid ), error = function ( e ){ return ( NULL )})
60+ if (is.null( content )) {
61+ warning( glue :: glue(
62+ " guid {guid} was not found on {connect$host}. " ,
63+ " Creating new content with name {name} " ))
64+ content <- connect $ content_create(
65+ name = name ,
66+ title = title ,
67+ ...
68+ )
69+ }
8470 } else {
85- content <- content [[1 ]]
86- message(glue :: glue(" Found EXISTING content {content$guid} with " ,
87- " name {name} on {connect$host}" ))
71+ # name-based deployment
72+ content <- connect $ get_apps(list (name = name ))
73+ if (length(content ) > 1 ) {
74+ stop(glue :: glue(" Found {length(to_content)} content items " ,
75+ " matching {name} on {connect$host}" ,
76+ " , content must have a unique name." ))
77+ } else if (length(content ) == 0 ) {
78+ # create app
79+ content <- connect $ content_create(
80+ name = name ,
81+ title = title ,
82+ ...
83+ )
84+ message(glue :: glue(" Creating NEW content {content$guid} " ,
85+ " with name {name} on {connect$host}" ))
86+ } else {
87+ content <- content [[1 ]]
88+ message(glue :: glue(" Found EXISTING content {content$guid} with " ,
89+ " name {name} on {connect$host}" ))
90+ # update values...? need a PUT endpoint
91+ }
8892 }
8993 return (content )
9094}
9195
92- random_name <- function (length = 13 ) {
96+ random_name <- function (length = 25 ) {
9397 tolower(paste(sample(LETTERS , length , replace = TRUE ), collapse = " " ))
9498}
9599
96- # ' @export
97100dir_bundle <- function (path = " ." , filename = " bundle.tar.gz" ) {
98101 before_wd <- getwd()
99102 setwd(path )
@@ -104,18 +107,16 @@ dir_bundle <- function(path = ".", filename = "bundle.tar.gz") {
104107 return (fs :: path_abs(filename ))
105108}
106109
107- # ' @export
108- deploy_bundle <- function (connect , bundle , app_id ){
110+ deploy_bundle <- function (connect , bundle_path , guid ){
109111 # upload bundle
110- new_bundle_id <- connect $ upload_bundle( bundle , app_id )
112+ new_bundle_id <- connect $ content_upload( bundle_path = bundle_path , guid = guid )[[ " bundle_id " ]]
111113
112114 # activate bundle
113- task_id <- connect $ activate_bundle( app_id , new_bundle_id )
115+ task_id <- connect $ content_deploy( guid = guid , bundle_id = new_bundle_id )[[ " task_id " ]]
114116
115117 return (task_id )
116118}
117119
118- # ' @export
119120poll_task <- function (connect , task_id , wait = 1 ) {
120121 finished <- FALSE
121122 code <- - 1
0 commit comments