Skip to content

Instantly share code, notes, and snippets.

@mmerce
Last active April 1, 2019 19:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmerce/e6c91dcf7dec95e026e782e0c55363f9 to your computer and use it in GitHub Desktop.
Save mmerce/e6c91dcf7dec95e026e782e0c55363f9 to your computer and use it in GitHub Desktop.
Moving from one project to another
{"name": "Move from projects to project",
"description": "Moves the non-script resources from a list of existing projects to a single destination project",
"inputs": [
{
"default": "",
"description": "Destination project ID",
"name": "destination",
"type": "project-id"
},
{
"default": "",
"description": "Origin project IDs list",
"name": "origin",
"type": "list"
},
{
"default": "",
"description": "If true, only logs information about the projects and resources to be moved.",
"name": "dry-run",
"type": "boolean"
},
{
"default": ["library", "script", "execution"],
"description": "List of resource types that will not be moved",
"name": "excluded-res",
"type": "list"
}
]}
(define irregulars {"anomalies" "anomaly"
"pca" "pca"
"libraries" "library"
"timeseries" "timeseries"})
(define (is-project? resource)
(= (resource-type resource) "project"))
(define (singular-type res-type)
(irregulars res-type (subs res-type 0 -1)))
(define (move-to-project destination origins dry-run excluded-res)
(if (is-project? destination)
(let (origins (if (empty? origins)
(list-projects {})
(if (list? origins)
origins
(list origins)))
status (if dry-run
" simulated"
" completed")
all-projects (and (map is-project? origins)))
(if all-projects
(for (project-id origins)
(let (project (fetch project-id)
full-res-types (keys (project "stats" {}))
res-counts (iterate (acc {} res-type full-res-types)
(if (and (> (project ["stats" res-type "count"] 0) 0)
(not (member? (singular-type res-type) excluded-res)))
(assoc acc res-type (project ["stats" res-type "count"] 0))
acc)))
(when dry-run (log-info "Origin project: " (project "name") "\n"))
(for (res-type (keys res-counts))
(when (> (project ["stats" res-type "count"] 0) 0)
(let (res-ids (map resource-id (resources (singular-type res-type) {"project" project-id}))
res-num (count res-ids))
(if dry-run
(log-info "Resources (" res-type "): " res-num)
(for (resource res-ids)
(update resource {"project" destination}))))))))
(log-info "Only projects can be set as origins"))
(log-info "Moving from " origins " to " destination status))
(log-info "No project set as destination")))
(move-to-project destination origin dry-run excluded-res)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment