|
17 | 17 | "source": [ |
18 | 18 | "# Clone Portal users, groups and content\n", |
19 | 19 | "\n", |
20 | | - "This sample notebook can be used for cloning an entire Portal, from maybe a staging to a production environment. It clones the users, groups and the content. It does not copy over services and data though, and works at the tier of Portal items." |
| 20 | + "This sample notebook can be used for cloning a portal, from say, a staging to a production environment. It clones the users, groups and the content. It does not copy over services and data though, and works at the tier of portal items." |
21 | 21 | ] |
22 | 22 | }, |
23 | 23 | { |
|
48 | 48 | }, |
49 | 49 | "outputs": [], |
50 | 50 | "source": [ |
51 | | - "source = GIS(\"https://dev003328.esri.com/portal\", \"admin\", \"esri.agp\", verify_cert=False)\n", |
52 | | - "target = GIS(\"https://dev003327.esri.com/portal\", \"admin\", \"esri.agp\", verify_cert=False)\n", |
53 | | - "# source = GIS(\"portal or org url\", \"username\", \"password\")\n", |
54 | | - "# target = GIS(\"portal or org url\", \"username\", \"password\")\n", |
| 51 | + "source = GIS(\"portal url\", \"username\", \"password\")\n", |
| 52 | + "target = GIS(\"target portal url\", \"username\", \"password\")\n", |
55 | 53 | "target_admin_username = 'admin'" |
56 | 54 | ] |
57 | 55 | }, |
|
211 | 209 | } |
212 | 210 | ], |
213 | 211 | "source": [ |
214 | | - "for srcuser in sourceusers:\n", |
215 | | - " if not srcuser.username in systemusers:\n", |
216 | | - " if srcuser.username is not target_admin_username: #don't delete the account used to connect\n", |
| 212 | + "for srcuser in source_users:\n", |
| 213 | + " if not srcuser.username in system_users:\n", |
| 214 | + " #don't delete the account used to connect\n", |
| 215 | + " if srcuser.username != target_admin_username:\n", |
217 | 216 | " try:\n", |
218 | 217 | " targetusr = target.users.get(srcuser.username)\n", |
219 | 218 | " if targetusr is not None:\n", |
|
243 | 242 | "def copy_user(target, user, password):\n", |
244 | 243 | " # See if the user has firstName and lastName properties\n", |
245 | 244 | " try:\n", |
246 | | - " firstname = user.firstName\n", |
247 | | - " lastname = user.lastName\n", |
| 245 | + " first_name = user.firstName\n", |
| 246 | + " last_name = user.lastName\n", |
248 | 247 | " except:\n", |
249 | 248 | " # if not, split the fullName\n", |
250 | | - " fullName = user.fullName\n", |
251 | | - " firstname = fullName.split()[0]\n", |
| 249 | + " full_name = user.fullName\n", |
| 250 | + " first_name = full_name.split()[0]\n", |
252 | 251 | " try:\n", |
253 | | - " lastname = fullName.split()[1]\n", |
| 252 | + " last_name = full_name.split()[1]\n", |
254 | 253 | " except:\n", |
255 | | - " lastname = 'NoLastName'\n", |
| 254 | + " last_name = 'NoLastName'\n", |
256 | 255 | "\n", |
257 | 256 | " try:\n", |
258 | 257 | " # create user\n", |
259 | | - " target_user = target.users.create(user.username, password, firstname, lastname,\n", |
| 258 | + " target_user = target.users.create(user.username, password, first_name, last_name,\n", |
260 | 259 | " user.email, user.description, user.role)\n", |
261 | 260 | "\n", |
262 | 261 | " # update user properties\n", |
|
344 | 343 | "source": [ |
345 | 344 | "ignore_list = [target_admin_username,'system_publisher', 'esri_nav', 'esri_livingatlas', \n", |
346 | 345 | " 'esri_boundaries', 'esri_demographics']\n", |
347 | | - "for user in sourceusers:\n", |
| 346 | + "for user in source_users:\n", |
348 | 347 | " if not user.username in ignore_list:\n", |
349 | 348 | " print(\"Creating user: \" + user.username)\n", |
350 | 349 | " copy_user(target, user, 'TestPassword@123')" |
|
430 | 429 | } |
431 | 430 | ], |
432 | 431 | "source": [ |
433 | | - "targetusers = target.users.search()\n", |
434 | | - "targetusers" |
| 432 | + "target_users = target.users.search()\n", |
| 433 | + "target_users" |
435 | 434 | ] |
436 | 435 | }, |
437 | 436 | { |
|
483 | 482 | } |
484 | 483 | ], |
485 | 484 | "source": [ |
486 | | - "sourcegroups = source.groups.search()\n", |
487 | | - "sourcegroups" |
| 485 | + "source_groups = source.groups.search()\n", |
| 486 | + "source_groups" |
488 | 487 | ] |
489 | 488 | }, |
490 | 489 | { |
|
511 | 510 | } |
512 | 511 | ], |
513 | 512 | "source": [ |
514 | | - "targetgroups = target.groups.search()\n", |
515 | | - "targetgroups" |
| 513 | + "target_groups = target.groups.search()\n", |
| 514 | + "target_groups" |
516 | 515 | ] |
517 | 516 | }, |
518 | 517 | { |
|
531 | 530 | "outputs": [], |
532 | 531 | "source": [ |
533 | 532 | "groups_to_ignore = ['Basemaps']\n", |
534 | | - "for tg in targetgroups:\n", |
| 533 | + "for tg in target_groups:\n", |
535 | 534 | " if tg.title not in groups_to_ignore:\n", |
536 | | - " for sg in sourcegroups:\n", |
| 535 | + " for sg in source_groups:\n", |
537 | 536 | " if sg.title == tg.title and (not tg.owner in systemusers):\n", |
538 | 537 | " print(\"Cleaning up group {} in target Portal...\".format(tg.title))\n", |
539 | 538 | " tg.delete()\n", |
|
667 | 666 | } |
668 | 667 | ], |
669 | 668 | "source": [ |
670 | | - "targetgroups = target.groups.search()\n", |
671 | | - "targetgroups" |
| 669 | + "target_groups = target.groups.search()\n", |
| 670 | + "target_groups" |
672 | 671 | ] |
673 | 672 | }, |
674 | 673 | { |
|
722 | 721 | "metadata": {}, |
723 | 722 | "source": [ |
724 | 723 | "Copying items consists of multiple steps. The following section of the sample does the following\n", |
725 | | - " 1. Create a dictionary of itemIds and item objects for each user in each folder\n", |
726 | | - " 2. Prepare sharing information for each item" |
| 724 | + "\n", |
| 725 | + " 1. Create a dictionary of itemIds and `Item` objects for each user in each folder\n", |
| 726 | + " 2. Prepare sharing information for each item" |
727 | 727 | ] |
728 | 728 | }, |
729 | 729 | { |
|
800 | 800 | ], |
801 | 801 | "source": [ |
802 | 802 | "source_items_by_id = {}\n", |
803 | | - "for user in sourceusers:\n", |
804 | | - " if not user.username in systemusers: # ignore any 'system' Portal users\n", |
| 803 | + "for user in source_users:\n", |
| 804 | + " if not user.username in system_users: # ignore any 'system' Portal users\n", |
805 | 805 | " print(\"Collecting item ids for {}...\".format(user.username))\n", |
806 | | - " usercontent = user.items()\n", |
| 806 | + " user_content = user.items()\n", |
807 | 807 | " # Copy item ids from root folder first\n", |
808 | | - " for item in usercontent:\n", |
| 808 | + " for item in user_content:\n", |
809 | 809 | " source_items_by_id[item.itemid] = item \n", |
810 | 810 | " # Copy item ids from folders next\n", |
811 | 811 | " folders = user.folders\n", |
812 | 812 | " for folder in folders:\n", |
813 | | - " folderitems = user.items(folder=folder['title'])\n", |
814 | | - " for item in folderitems:\n", |
| 813 | + " folder_items = user.items(folder=folder['title'])\n", |
| 814 | + " for item in folder_items:\n", |
815 | 815 | " source_items_by_id[item.itemid] = item " |
816 | 816 | ] |
817 | 817 | }, |
|
831 | 831 | }, |
832 | 832 | "outputs": [], |
833 | 833 | "source": [ |
834 | | - "for group in sourcegroups:\n", |
| 834 | + "for group in source_groups:\n", |
835 | 835 | " if group.title not in groups_to_ignore:\n", |
836 | | - " if not group.owner in systemusers:\n", |
| 836 | + " if not group.owner in system_users:\n", |
837 | 837 | " target_group_id = copied_groups[group.groupid]\n", |
838 | 838 | " for group_item in group.content():\n", |
839 | | - " if not group_item.owner in systemusers:\n", |
| 839 | + " if not group_item.owner in system_users:\n", |
840 | 840 | " try:\n", |
841 | 841 | " item = source_items_by_id[group_item.itemid]\n", |
842 | 842 | " if item is not None:\n", |
|
5580 | 5580 | " print(\"**************\\n\"+user.username)\n", |
5581 | 5581 | " usercontent = user.items()\n", |
5582 | 5582 | " folders = user.folders\n", |
5583 | | - " for item in usercontent:\n", |
| 5583 | + " for item in user_content:\n", |
5584 | 5584 | " try:\n", |
5585 | 5585 | " copied_item = copy_item(target, user, None, item)\n", |
5586 | 5586 | " if copied_item is not None:\n", |
|
5598 | 5598 | " print(\"Error copying \" + item.title)\n", |
5599 | 5599 | "\n", |
5600 | 5600 | " for folder in folders:\n", |
5601 | | - " target.content.create_folder(folder['title'], user)\n", |
5602 | | - " folderitems = user.items(folder['title'])\n", |
| 5601 | + " target.content.create_folder(folder, user)\n", |
| 5602 | + " folder_items = user.items(folder['title'])\n", |
5603 | 5603 | " for item in folderitems:\n", |
5604 | 5604 | " try:\n", |
5605 | | - " #display(item)\n", |
5606 | | - " #print(item.__repr__())\n", |
5607 | 5605 | " copied_item = copy_item(target, user, folder['title'], item)\n", |
5608 | 5606 | " if copied_item is not None:\n", |
5609 | 5607 | " copied_items[item.itemid] = copied_item.itemid\n", |
|
5622 | 5620 | " # Copy the related items for this user (if specified)\n", |
5623 | 5621 | " if relationships:\n", |
5624 | 5622 | " for folder in folders:\n", |
5625 | | - " folderitems = usercontent[folder]\n", |
5626 | | - " for item in folderitems:\n", |
| 5623 | + " folder_items = user_content[folder]\n", |
| 5624 | + " for item in folder_items:\n", |
5627 | 5625 | " try:\n", |
5628 | 5626 | " copy_relationships(target, copied_items, item, \n", |
5629 | 5627 | " relationships, user, folder)\n", |
|
5634 | 5632 | ], |
5635 | 5633 | "metadata": { |
5636 | 5634 | "kernelspec": { |
5637 | | - "display_name": "Python [default]", |
| 5635 | + "display_name": "Python 3", |
5638 | 5636 | "language": "python", |
5639 | 5637 | "name": "python3" |
5640 | 5638 | }, |
|
5648 | 5646 | "name": "python", |
5649 | 5647 | "nbconvert_exporter": "python", |
5650 | 5648 | "pygments_lexer": "ipython3", |
5651 | | - "version": "3.5.2" |
| 5649 | + "version": "3.6.0" |
5652 | 5650 | } |
5653 | 5651 | }, |
5654 | 5652 | "nbformat": 4, |
|
0 commit comments