Skip to content

Instantly share code, notes, and snippets.

@reubenmiller
Created November 15, 2023 08:54
Show Gist options
  • Select an option

  • Save reubenmiller/ec26d0573ef627cf487b1b3131439e68 to your computer and use it in GitHub Desktop.

Select an option

Save reubenmiller/ec26d0573ef627cf487b1b3131439e68 to your computer and use it in GitHub Desktop.
go-c8y-cli: copy managed objects across tenants

Copy managed objects across tenants

Long Version

  1. Activate a session to the source tenant

    set-session
    
  2. Save the data to file

    c8y devices list --pageSize 10 > tenant1.json
  3. Change to the target tenant (where the data will be created in)

  4. Create the managed objects (removing protected properties using a default view)

    cat tenant1.json \
    | c8y util show --view ignoreinbuilt_id \
    | c8y util show --select '**,!owner' \
    | c8y inventory create --template "input.value" --dry

    If you are doing a lot of data, and what to make sure you don't create duplicates when you rerun the one-liner, then you can use the caching mechanism (available on all commands, see https://goc8ycli.netlify.app/docs/concepts/caching/)

    This is the same example but using the global caching options:

    cat tenant1.json \
    | c8y util show --view ignoreinbuilt_id \
    | c8y util show --select '**,!owner' \
    | c8y inventory create --template "input.value" --cache --cacheTTL 1d --dry

Short version

  1. Change to the source tenant

    set-session
    
  2. List the managed object from the current tenant, and use the results to create the managed objects in a session tenant (using --session <session>)

    c8y devices list --pageSize 10 \
    | c8y util show --view ignoreinbuilt_id \
    | c8y util show --select '**,!owner' \
    | c8y inventory create --template "input.value" --cache --cacheTTL 1d --dry --session secondtenant.json
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment