def export_import_content(): source = Portal('http://portaldev.esri.com', 'admin', 'esri.agp') target = Portal('http://wittm.esri.com', 'admin', 'esri.agp') file_path = 'C:\\temp\\export' web_content = source.search(q=portalpy.WEB_ITEM_FILTER) save_items(source, web_content, file_path, indent=4) load_items(target, file_path)
def daily_item_stats(): portal = Portal('http://www.arcgis.com') today = datetime.utcnow() weekago = today - timedelta(days=1) q = 'modified:[' + portal_time(weekago) + ' TO ' + portal_time(today) + ']' results = portal.search(['type', 'count(type)'], q, group_fields=['type'], num=5000) pprint(results, indent=2)
def copy_items_query(): source = Portal('http://www.arcgis.com') target = Portal('http://wittm.esri.com', 'wmathot', 'wmathot') items = source.search(q='h1n1') copied_items = copy_items(items, source, target, 'wmathot', 'Copied Items (h1n1)') for sourceid in copied_items.keys(): print 'Copied ' + sourceid + ' to ' + copied_items[sourceid]
def count_item_types(): portal = Portal('http://portaldev.esri.com', 'admin', 'esri.agp') counts = portal.search(properties=['type', 'count(type)'], group_fields=['type'],\ sort_field='count(type)',\ sort_order='desc') pprint(counts, indent=2)
def create_user_group_item_reports(): portal = Portal('http://portaldev.esri.com', 'admin', 'esri.agp') item_fields = ['id', 'title', 'owner', 'numViews'] items = portal.search(item_fields, sort_field='numViews', sort_order='desc') csvfile = csv.writer(open('items-report.csv', 'wb')) csvfile.writerow(item_fields) for item in items: row = [item[field] for field in item_fields] csvfile.writerow(row) groups_fields = ['id', 'title', 'owner'] groups = portal.groups(groups_fields) csvfile = csv.writer(open('groups-report.csv', 'wb')) csvfile.writerow(groups_fields) for group in groups: row = [group[field] for field in groups_fields] csvfile.writerow(row) user_fields = ['username', 'fullName', 'email', 'role'] users = portal.org_users(user_fields) csvfile = csv.writer(open('users-report.csv', 'wb')) csvfile.writerow(user_fields) for user in users: row = [user[field] for field in user_fields] csvfile.writerow(row)
def count_tags(): portal = Portal('http://www.geoplatform.gov') results = portal.search(['tags']) tags = unpack(results, flatten=True) counts = dict((tag, tags.count(tag)) for tag in tags) sorted_counts = sorted(counts.iteritems(), key=itemgetter(1), reverse=True) pprint(sorted_counts, indent=2)
def copy_items_with_related(): source = Portal('http://dev.arcgis.com') target = Portal('http://wittm.esri.com', 'wmathot', 'wmathot') items = source.search(q='type:"Web Mapping Application"', num=5) copied_items = copy_items(items, source, target, 'wmathot', 'Web Apps 5', \ relationships=['WMA2Code', 'MobileApp2Code']) for sourceid in copied_items.keys(): print 'Copied ' + sourceid + ' to ' + copied_items[sourceid]
def hide_user(): portal = Portal('http://portaldev.arcgis.com', 'admin', 'esri.agp') user_to_hide = 'wmathot' portal.update_user(user_to_hide, {'access': 'private'}) groups = portal.groups(['id'], 'owner:' + user_to_hide) for group in groups: portal.update_group(group['id'], {'access': 'private'}) items = portal.search(['id'], 'owner:' + user_to_hide) portal.share_items(user_to_hide, items, None, False, False)
def create_demographics_group(): arcgisonline = Portal('http://www.arcgis.com') portal = Portal('http://wittm.esri.com', 'admin', 'esri.agp') owner = portal.logged_in_user()['username'] items = arcgisonline.search(q='demographics owner:esri ' + WEBMAP_ITEM_FILTER) copied_items = copy_items(items, arcgisonline, portal, owner) group_id = portal.create_group({'title': 'Demographics', 'access': 'public', 'tags': 'demographics'}, thumbnail='http://bit.ly/WEaIh5') for item_id in copied_items.values(): portal.share_items(owner, [item_id], [group_id], True, True)
def average_coverage_by_item_type(): portal = Portal('http://portaldev.esri.com', 'admin', 'esri.agp') def avg_area(extents): extents = filter(None, extents) if extents: areas = [] for e in extents: if e: areas.append((e[1][0] - e[0][0]) * (e[1][1] - e[0][1])) return sum(area for area in areas) / len(areas) return 0 portal.aggregate_functions['avg_area'] = avg_area results = portal.search(['type', 'avg_area(extent)'], group_fields=['type']) pprint(results, indent=2)
def find_public_content_with_weak_descriptions(): portal = Portal('http://portaldev.esri.com', 'admin', 'esri.agp') public_items = portal.search(q='access:public') weak_items = [] for item in public_items: snippet = item.get('snippet') description = item.get('description') thumbnail = item.get('thumbnail') if not snippet or not description or not thumbnail or len(snippet) < 20 \ or len(description) < 50: weak_items.append(item) for weak_item in weak_items: owner = weak_item['owner'] email = portal.user(owner)['email'] print owner + ', ' + email + ', ' + weak_item['id']
def find_hostname_references(): hostname = 'wh94.fltplan.com' portal = Portal('http://portaldev.esri.com', 'admin', 'esri.agp') hostname_references = [] url_items = portal.search(['id', 'type', 'url'], portalpy.URL_ITEM_FILTER) for item in url_items: if parse_hostname(item['url']) == hostname: hostname_references.append((item['id'], item['type'], item['url'])) webmaps = portal.webmaps() for webmap in webmaps: urls = webmap.urls(normalize=True) for url in urls: if parse_hostname(url) == hostname: hostname_references.append((webmap.id, 'Web Map', url)) pprint(hostname_references, indent=2)
def find_hostname_references(): hostname = 'wh94.fltplan.com' portal = Portal('http://portaldev.esri.com', 'admin', 'esri.agp') hostname_references = [] url_items = portal.search(['id','type','url'], portalpy.URL_ITEM_FILTER) for item in url_items: if parse_hostname(item['url']) == hostname: hostname_references.append((item['id'], item['type'], item['url'])) webmaps = portal.webmaps() for webmap in webmaps: urls = webmap.urls(normalize=True) for url in urls: if parse_hostname(url) == hostname: hostname_references.append((webmap.id, 'Web Map', url)) pprint(hostname_references, indent=2)
def create_demographics_group(): arcgisonline = Portal('http://www.arcgis.com') portal = Portal('http://wittm.esri.com', 'admin', 'esri.agp') owner = portal.logged_in_user()['username'] items = arcgisonline.search(q='demographics owner:esri ' + WEBMAP_ITEM_FILTER) copied_items = copy_items(items, arcgisonline, portal, owner) group_id = portal.create_group( { 'title': 'Demographics', 'access': 'public', 'tags': 'demographics' }, thumbnail='http://bit.ly/WEaIh5') for item_id in copied_items.values(): portal.share_items(owner, [item_id], [group_id], True, True)
def copy_group_with_shared_content(): source = Portal('http://www.arcgis.com') target = Portal('http://wittm.esri.com', 'admin', 'esri.agp') target_owner = target.logged_in_user()['username'] group_id = '2394b887a80347fb8544610cfa30489c' # Copy the groups groups = source.groups(q='id:' + group_id) copied_groups = copy_groups(groups, source, target) print 'Copied ' + str(len(copied_groups)) + ' groups' # Copy the items items = source.search(q='group:' + group_id) copied_items = copy_items(items, source, target, target_owner, 'Copied Items (' + group_id + ')') print 'Copied ' + str(len(copied_items)) + ' items' # Share the items in the target portal for item_id in copied_items.keys(): sharing = source.user_item(item_id)[1] # If we have access to the full sharing properties of the source # item, then copy all of them, otherwise just share with the group if sharing and sharing['access'] != 'private': target_item_id = copied_items[item_id] target_group_ids = [copied_groups[id] for id in sharing['groups'] \ if id in copied_groups] share_org = (sharing['access'] == 'org') share_public = (sharing['access'] == 'public') if not target.is_multitenant(): share_public = (share_public or share_org) target.share_items(target_owner, [target_item_id], target_group_ids, \ share_org or share_public, \ share_public) else: target_item_id = copied_items[item_id] target_group_id = copied_groups[group_id] target.share_items(target_owner, [target_item_id], \ [target_group_id])
def update_hostname_references(): portal = Portal('http://portaldev.esri.com', 'admin', 'esri.agp') hostname_map = {'wh94.fltplan.com:8080': 'wh94.fltplan.com'} url_items = portal.search(['id','type','url'], portalpy.URL_ITEM_FILTER) for item in url_items: url = item.get('url') if url: url = normalize_url(url) host = parse_hostname(url, include_port=True) if host in hostname_map: url = url.replace(host, hostname_map[host]) portal.update_item(item['id'], {'url': url}) webmaps = portal.webmaps() for webmap in webmaps: is_update = False for url in webmap.urls(): normalized_url = normalize_url(url) host = parse_hostname(normalized_url, include_port=True) if host in hostname_map: new_url = normalized_url.replace(host, hostname_map[host]) webmap.data = webmap.data.replace(url, new_url) is_update = True if is_update: portal.update_webmap(webmap)
def update_hostname_references(): portal = Portal('http://portaldev.esri.com', 'admin', 'esri.agp') hostname_map = {'wh94.fltplan.com:8080': 'wh94.fltplan.com'} url_items = portal.search(['id', 'type', 'url'], portalpy.URL_ITEM_FILTER) for item in url_items: url = item.get('url') if url: url = normalize_url(url) host = parse_hostname(url, include_port=True) if host in hostname_map: url = url.replace(host, hostname_map[host]) portal.update_item(item['id'], {'url': url}) webmaps = portal.webmaps() for webmap in webmaps: is_update = False for url in webmap.urls(): normalized_url = normalize_url(url) host = parse_hostname(normalized_url, include_port=True) if host in hostname_map: new_url = normalized_url.replace(host, hostname_map[host]) webmap.data = webmap.data.replace(url, new_url) is_update = True if is_update: portal.update_webmap(webmap)
def main(): exit_err_code = 1 starting_cwd = os.getcwd() # Print/get script arguments results = print_args() if not results: sys.exit(exit_err_code) portal_address, adminuser, password, id_mapping_file_path, search_query = results total_success = True title_break_count = 100 section_break_count = 75 print '=' * title_break_count print 'Update Portal GUIDs' print '=' * title_break_count if not os.path.exists(id_mapping_file_path): print '\nFile {} does not exist. Exiting.'.format(id_mapping_file_path) sys.exit(0) try: portal = Portal(portal_address, adminuser, password) print '\n{}'.format('-' * section_break_count) print '- Searching for portal items...\n' items_temp = portal.search(q=search_query, sort_field='owner') items = [] for item in items_temp: if not item['owner'].startswith('esri_'): items.append(item) for item in items: print format_item_info(item) print '\nFound {} items.'.format(len(items)) if len(items) > 0: user_response = raw_input( "\nDo you want to continue with the update? Enter 'y' to continue: " ) if validate_user_repsonse_yesno(user_response): # Open id mapping file file_dir = os.path.dirname(id_mapping_file_path) file_name = os.path.basename(id_mapping_file_path) if len(file_dir) == 0: file_dir = os.getcwd() os.chdir(file_dir) id_mapping = json.load(open(file_name)) print '\n{}'.format('-' * section_break_count) print '- Updating item and item data...\n' for item in items: print format_item_info(item) for id_map in id_mapping: search = id_map.get(search_key) replace = id_map.get(replace_key) update_item_properties(portal, item, search, replace) update_item_data(portal, item, search, replace) except: total_success = False # Get the traceback object tb = sys.exc_info()[2] tbinfo = traceback.format_tb(tb)[0] # Concatenate information together concerning the error # into a message string pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + \ "\nError Info:\n" + str(sys.exc_info()[1]) # Print Python error messages for use in Python / Python Window print print "***** ERROR ENCOUNTERED *****" print pymsg + "\n" finally: os.chdir(starting_cwd) print '\nDone.' if total_success: sys.exit(0) else: sys.exit(exit_err_code)
def publish_portal(portaladdress,contentpath,adminuser,adminpassword, users, hostname_map, specified_groups, id_mapping_file): os.chdir(contentpath) portal_properties = json.load(open("portal_properties.json")) portaladmin = Portal(portaladdress, adminuser, adminpassword) print_script_header(portaladmin, portal_processing, users, specified_groups) # Create Portal log folder for this portal portalLogPath = os.path.join(contentpath, portalLogFolder, get_servername_from_url(portaladmin.url)) makeFolder(portalLogPath) # ------------------------------------------------------------------------ # Get info about the groups on disk to filter items based on group membership # ------------------------------------------------------------------------ # Get the groups on disk grps_on_disk = get_groups_on_disk(contentpath) # Validate that each specified group is valid if specified_groups: invalid_specified_groups = [] for specified_group in specified_groups: found = False for grp in grps_on_disk.itervalues(): if specified_group == grp: found = True if not found: invalid_specified_groups.append(specified_group) if len(invalid_specified_groups) > 0: print '\n***ERROR: the following specified groups do not exist; NOTE: group owner and group title must match exactly including case:\n' print invalid_specified_groups return # ------------------------------------------------------------------------ # Create users # ------------------------------------------------------------------------ print titleBreak print 'Create users...\n' # Only create users if this is not an online organization if portaladmin.properties()['id'] == '0123456789ABCDEF': # All portals have this id. for username, userinfo in users.iteritems(): create_user(portaladmin, contentpath, userinfo) else: print '\tThis is an online organization. Users must already have been created.' # ------------------------------------------------------------------------ # Create user folders # ------------------------------------------------------------------------ print '\n'+ titleBreak print 'Create user folders...\n' for username, userinfo in users.iteritems(): print '\nUser: '******'target_username'] create_user_folders(portaladmin, contentpath, userinfo) # ------------------------------------------------------------------------ # Create groups and add users to groups # ------------------------------------------------------------------------ print "\n" + titleBreak print "Creating groups ...\n" oldGrpID_newGrpID = {} for username, userinfo in users.iteritems(): newGroups = publish_user_groups(portaladmin, contentpath, userinfo, users) for key,val in newGroups.iteritems(): oldGrpID_newGrpID[key] = {'id': val} # ------------------------------------------------------------------------ # Publish Items and Update their sharing info # ------------------------------------------------------------------------ print "\n" + titleBreak print "Publishing items and setting sharing ..." print titleBreak for username, userinfo in users.iteritems(): username = userinfo['target_username'] password = userinfo['target_password'] userfoldername = userinfo['username'] # --------------------------------------- # Publish all the users' items # --------------------------------------- usercontentpath = os.path.join(contentpath, userfoldername) newItems, origItemSourceIDs = publish_user_items(portaladmin, username, usercontentpath, source_hostname, new_hostname, new_port, specified_groups, id_mapping_file, grps_on_disk) # Dump info about new items to JSON to use for resetting IDs of related items dump_newItem_info(newItems, origItemSourceIDs, os.path.join(portalLogPath, username)) # --------------------------------------- # Share the users' items with the # appropriate groups # --------------------------------------- userItemsPath = os.path.join(contentpath, userfoldername, "items") share_items(portaladmin, userItemsPath, newItems, origItemSourceIDs, originGroupToDestGroups) # Dump info about all items (old, new ids) into json os.chdir(portalLogPath) json.dump(origIDToNewID, open('oldID_newID.json','w')) json.dump(oldGrpID_newGrpID, open('oldGrpID_newGrpID.json', 'w')) # ------------------------------------------------------------------------ # Post publishing processing: Update URLs and item ids # ------------------------------------------------------------------------ print "\n" + titleBreak print "Update URLs and Item IDs..." print titleBreak + "\n" # Add the group ids to the dictionary of old/new ids origIDToNewID.update(oldGrpID_newGrpID) # Create list of items to update # (exclude any items owned by esri_ accounts) items_all = portaladmin.search() items_to_update = [] for item in items_all: if not item["owner"].startswith("esri_"): items_to_update.append(item) # Update update_post_publish(portaladmin, hostname_map, origIDToNewID, items_to_update) # Comment out: this functionality is now available out of the box # # ------------------------------------------------------------------------ # # Share items in default web apps template and default gallery # # ------------------------------------------------------------------------ # # Share the items in the default web apps template and # # default gallery template built-in group with the # # 'OpsServer' user 'AFMI Web Application Templates' and # # 'AFMI Gallery Templates' # print "\n" + sectionBreak # print "Share the items in the default web apps and gallery template groups..." # group_owner = 'OpsServer' # if users.get(group_owner): # share_templates(portaladdress, users[group_owner]['target_username'], users[group_owner]['target_password']) # else: # print "-Skipping...user {} was not created. Can perform same operation through portal 'Edit Settings'.".format(group_owner) print "\nDONE: Finished posting content to portal."
def publish_portal(portaladdress, contentpath, adminuser, adminpassword, users, hostname_map, specified_groups, id_mapping_file): os.chdir(contentpath) portal_properties = json.load(open("portal_properties.json")) portaladmin = Portal(portaladdress, adminuser, adminpassword) print_script_header(portaladmin, portal_processing, users, specified_groups) # Create Portal log folder for this portal portalLogPath = os.path.join(contentpath, portalLogFolder, get_servername_from_url(portaladmin.url)) makeFolder(portalLogPath) # ------------------------------------------------------------------------ # Get info about the groups on disk to filter items based on group membership # ------------------------------------------------------------------------ # Get the groups on disk grps_on_disk = get_groups_on_disk(contentpath) # Validate that each specified group is valid if specified_groups: invalid_specified_groups = [] for specified_group in specified_groups: found = False for grp in grps_on_disk.itervalues(): if specified_group == grp: found = True if not found: invalid_specified_groups.append(specified_group) if len(invalid_specified_groups) > 0: print '\n***ERROR: the following specified groups do not exist; NOTE: group owner and group title must match exactly including case:\n' print invalid_specified_groups return # ------------------------------------------------------------------------ # Create users # ------------------------------------------------------------------------ print titleBreak print 'Create users...\n' # Only create users if this is not an online organization if portaladmin.properties( )['id'] == '0123456789ABCDEF': # All portals have this id. for username, userinfo in users.iteritems(): create_user(portaladmin, contentpath, userinfo) else: print '\tThis is an online organization. Users must already have been created.' # ------------------------------------------------------------------------ # Create user folders # ------------------------------------------------------------------------ print '\n' + titleBreak print 'Create user folders...\n' for username, userinfo in users.iteritems(): print '\nUser: '******'target_username'] create_user_folders(portaladmin, contentpath, userinfo) # ------------------------------------------------------------------------ # Create groups and add users to groups # ------------------------------------------------------------------------ print "\n" + titleBreak print "Creating groups ...\n" oldGrpID_newGrpID = {} for username, userinfo in users.iteritems(): newGroups = publish_user_groups(portaladmin, contentpath, userinfo, users) for key, val in newGroups.iteritems(): oldGrpID_newGrpID[key] = {'id': val} # ------------------------------------------------------------------------ # Publish Items and Update their sharing info # ------------------------------------------------------------------------ print "\n" + titleBreak print "Publishing items and setting sharing ..." print titleBreak for username, userinfo in users.iteritems(): username = userinfo['target_username'] password = userinfo['target_password'] userfoldername = userinfo['username'] # --------------------------------------- # Publish all the users' items # --------------------------------------- usercontentpath = os.path.join(contentpath, userfoldername) newItems, origItemSourceIDs = publish_user_items( portaladmin, username, usercontentpath, source_hostname, new_hostname, new_port, specified_groups, id_mapping_file, grps_on_disk) # Dump info about new items to JSON to use for resetting IDs of related items dump_newItem_info(newItems, origItemSourceIDs, os.path.join(portalLogPath, username)) # --------------------------------------- # Share the users' items with the # appropriate groups # --------------------------------------- userItemsPath = os.path.join(contentpath, userfoldername, "items") share_items(portaladmin, userItemsPath, newItems, origItemSourceIDs, originGroupToDestGroups) # Dump info about all items (old, new ids) into json os.chdir(portalLogPath) json.dump(origIDToNewID, open('oldID_newID.json', 'w')) json.dump(oldGrpID_newGrpID, open('oldGrpID_newGrpID.json', 'w')) # ------------------------------------------------------------------------ # Post publishing processing: Update URLs and item ids # ------------------------------------------------------------------------ print "\n" + titleBreak print "Update URLs and Item IDs..." print titleBreak + "\n" # Add the group ids to the dictionary of old/new ids origIDToNewID.update(oldGrpID_newGrpID) # Create list of items to update # (exclude any items owned by esri_ accounts) items_all = portaladmin.search() items_to_update = [] for item in items_all: if not item["owner"].startswith("esri_"): items_to_update.append(item) # Update update_post_publish(portaladmin, hostname_map, origIDToNewID, items_to_update) # Comment out: this functionality is now available out of the box # # ------------------------------------------------------------------------ # # Share items in default web apps template and default gallery # # ------------------------------------------------------------------------ # # Share the items in the default web apps template and # # default gallery template built-in group with the # # 'OpsServer' user 'AFMI Web Application Templates' and # # 'AFMI Gallery Templates' # print "\n" + sectionBreak # print "Share the items in the default web apps and gallery template groups..." # group_owner = 'OpsServer' # if users.get(group_owner): # share_templates(portaladdress, users[group_owner]['target_username'], users[group_owner]['target_password']) # else: # print "-Skipping...user {} was not created. Can perform same operation through portal 'Edit Settings'.".format(group_owner) print "\nDONE: Finished posting content to portal."
def main(): exit_err_code = 1 # Print/get script arguments results = print_args() if not results: sys.exit(exit_err_code) portal_address, adminuser, password = results total_success = True title_break_count = 100 section_break_count = 75 search_query = None print '=' * title_break_count print 'Validate Hosted Service Sources' print '=' * title_break_count source_items = [] hosted_items = [] root_folder_path = None root_folder_path = tempfile.mkdtemp() print 'Temporary directory: {}'.format(root_folder_path) orig_dir = os.getcwd() try: portal = Portal(portal_address, adminuser, password) items = portal.search() # --------------------------------------------------------------------- # Get info about hosted service source items # (currently service definitions) # --------------------------------------------------------------------- for item in items: if item['type'] == 'Service Definition': print '\nDownloading and extracting Service Definition item {}'.format( item['id']) # Download .sd file download_root_path = os.path.join(root_folder_path, item['id']) os.mkdir(download_root_path) download_path = portal.item_datad(item['id'], download_root_path) # Extract serviceconfiguration.json file from downloaded .sd file file_name = 'serviceconfiguration.json' extract_path = download_path.replace('.sd', '') #print extract_path os.mkdir(extract_path) err_stat = extractFromSDFile(download_path, extract_path, file_name) print 'Extract status: {}'.format(err_stat) # Open extract .json file file_path = findFilePath(extract_path, file_name) os.chdir(os.path.dirname(file_path)) service_config = json.load(open(file_name)) # [{id: val, owner: val, title: val, type: val # service_config: {stuff from .json file}}] d = { 'id': item['id'], 'owner': item['owner'], 'title': item['title'], 'type': item['type'], 'service_config': service_config } source_items.append(d) # --------------------------------------------------------------------- # Get info about hosted service items # --------------------------------------------------------------------- print '\nDetermine what hosted services exist...' h_service_items = get_hosted_service_items(portal, items) for item in h_service_items: d = { 'id': item['id'], 'owner': item['owner'], 'title': item['title'], 'type': item['type'], 'url': item['url'] } hosted_items.append(d) # --------------------------------------------------------------------- # For each hosted service find the associated source item # --------------------------------------------------------------------- print '=' * section_break_count print '\nDetermine which source items are associated with each hosted service...' print '=' * section_break_count num_hosted_no_match = 0 num_hosted_match = 0 num_hosted_mismatch_owner = 0 write_str = "\tid: {:<34}owner: {:<20}type: {:<25}service: {:<50}\n" for hosted_d in hosted_items: found = False found_num = 0 # Get last components of URL (i.e., SRTM_V2_56020/FeatureServer) hosted_url = '/'.join(hosted_d['url'].split('/')[-2:]) print '\n{}'.format('-' * 100) print 'Hosted Service Item: Title - "{}"\n'.format( hosted_d['title']) hosted_str = write_str.format(hosted_d['id'], hosted_d['owner'], hosted_d['type'], hosted_url) print hosted_str # Look for match in source items print '\tMatching Source Item:' for source_d in source_items: src_service_info = source_d['service_config']['service'] src_service_name = src_service_info['serviceName'] src_service_type = src_service_info['type'] src_service_url = '{}/{}'.format(src_service_name, src_service_type) if hosted_url == src_service_url: found = True found_num += 1 match_str = write_str.format(source_d['id'], source_d['owner'], source_d['type'], src_service_url) print '\n\tTitle: "{}"'.format(source_d['title']) print match_str if source_d['owner'] != hosted_d['owner']: print '*** ERROR: owner does not match hosted service item owner.' num_hosted_mismatch_owner += 1 if found_num == 0: print '*** ERROR: no matching source item found.' if found_num > 1: print '*** ERROR: there is more then one matching source item found.' if found: num_hosted_match += 1 else: num_hosted_no_match += 1 # --------------------------------------------------------------------- # For each source item find the associated hosted service # --------------------------------------------------------------------- print '=' * section_break_count print '\nDetermine which hosted services are associated with each source item...' print '=' * section_break_count num_source_no_match = 0 num_source_match = 0 num_source_mismatch_owner = 0 write_str = "\tid: {:<34}owner: {:<20}type: {:<25}service: {:<50}\n" for source_d in source_items: found = False found_num = 0 src_service_info = source_d['service_config']['service'] src_service_name = src_service_info['serviceName'] src_service_type = src_service_info['type'] src_service_url = '{}/{}'.format(src_service_name, src_service_type) print '\n{}'.format('-' * 100) print 'Source Item: Title - "{}"\n'.format(source_d['title']) source_str = write_str.format(source_d['id'], source_d['owner'], source_d['type'], src_service_url) print source_str # Look for match in source items print '\tMatching Hosted Service:' for hosted_d in hosted_items: # Get last components of URL (i.e., SRTM_V2_56020/FeatureServer) hosted_url = '/'.join(hosted_d['url'].split('/')[-2:]) if hosted_url == src_service_url: found = True found_num += 1 match_str = write_str.format(hosted_d['id'], hosted_d['owner'], hosted_d['type'], hosted_url) print '\n\tTitle: "{}"'.format(hosted_d['title']) print match_str if hosted_d['owner'] != source_d['owner']: print '*** ERROR: owner does not match associated source owner.' num_source_mismatch_owner += 1 if found_num == 0: print '*** ERROR: no matching hosted service found.' if found_num > 1: print '*** ERROR: there is more then one hosted service found.' if found: num_source_match += 1 else: num_source_no_match += 1 print '\n{}'.format('=' * section_break_count) print 'Summary:\n' print 'Number of hosted services: {}'.format(len(hosted_items)) print 'With matching source item: {}'.format(num_hosted_match) print 'With NO matching source item: {}'.format(num_hosted_no_match) print 'With mis-matching owners: {}'.format(num_hosted_mismatch_owner) print '\nNumber of source items: {}'.format(len(source_items)) print 'With matching hosted service: {}'.format(num_source_match) print 'With NO matching hosted service: {}'.format(num_source_no_match) print 'With mis-matching owners: {}'.format(num_source_mismatch_owner) except: total_success = False # Get the traceback object tb = sys.exc_info()[2] tbinfo = traceback.format_tb(tb)[0] # Concatenate information together concerning the error # into a message string pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + \ "\nError Info:\n" + str(sys.exc_info()[1]) # Print Python error messages for use in Python / Python Window print print "***** ERROR ENCOUNTERED *****" print pymsg + "\n" finally: os.chdir(orig_dir) if root_folder_path: shutil.rmtree(root_folder_path) print '\nDone.' if total_success: sys.exit(0) else: sys.exit(exit_err_code)
def main(): exit_err_code = 1 starting_cwd = os.getcwd() # Print/get script arguments results = print_args() if not results: sys.exit(exit_err_code) portal_address, adminuser, password, id_mapping_file_path, search_query = results total_success = True title_break_count = 100 section_break_count = 75 print '=' * title_break_count print 'Update Portal GUIDs' print '=' * title_break_count if not os.path.exists(id_mapping_file_path): print '\nFile {} does not exist. Exiting.'.format(id_mapping_file_path) sys.exit(0) try: portal = Portal(portal_address, adminuser, password) print '\n{}'.format('-' * section_break_count) print '- Searching for portal items...\n' items_temp = portal.search(q=search_query, sort_field='owner') items = [] for item in items_temp: if not item['owner'].startswith('esri_'): items.append(item) for item in items: print format_item_info(item) print '\nFound {} items.'.format(len(items)) if len(items) > 0: user_response = raw_input("\nDo you want to continue with the update? Enter 'y' to continue: ") if validate_user_repsonse_yesno(user_response): # Open id mapping file file_dir = os.path.dirname(id_mapping_file_path) file_name = os.path.basename(id_mapping_file_path) if len(file_dir) == 0: file_dir = os.getcwd() os.chdir(file_dir) id_mapping = json.load(open(file_name)) print '\n{}'.format('-' * section_break_count) print '- Updating item and item data...\n' for item in items: print format_item_info(item) for id_map in id_mapping: search = id_map.get(search_key) replace = id_map.get(replace_key) update_item_properties(portal, item, search, replace) update_item_data(portal, item, search, replace) except: total_success = False # Get the traceback object tb = sys.exc_info()[2] tbinfo = traceback.format_tb(tb)[0] # Concatenate information together concerning the error # into a message string pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + \ "\nError Info:\n" + str(sys.exc_info()[1]) # Print Python error messages for use in Python / Python Window print print "***** ERROR ENCOUNTERED *****" print pymsg + "\n" finally: os.chdir(starting_cwd) print '\nDone.' if total_success: sys.exit(0) else: sys.exit(exit_err_code)
feature_items, feature_groups from portalpy.provision import load_groups, copy_items logging.basicConfig(level=logging.WARN) # Setup portal connections and file paths portal = Portal('http://wittm.esri.com', 'admin', 'esri.agp') arcgisonline = Portal('http://www.arcgis.com') groups_csv = '.\\provision_groups.csv' # Configure the portal configure_portal(portal, 'Demo Portal', 'A portal used for demonstrations.') create_basemap_gallery_group(portal, 'Demo Basemaps', copy_filter='-bing -osm') # Create groups from CSV, then copy/share like-tagged items from arcgis online items_to_feature = [] owner = portal.logged_in_user()['username'] groups = load_groups(portal, groups_csv, 'csv')[0] for group in groups: tags = group['tags'].split(',') tags_expr = ' OR '.join('tags:"%s"' % tag.strip() for tag in tags) item_query = WEB_ITEM_FILTER + ' AND (' + tags_expr + ')' items = arcgisonline.search(q=item_query, num=5) item_id_map = copy_items(items, arcgisonline, portal, owner) portal.share_items(owner, item_id_map.values(), [group['id']], True, True) items_to_feature.append(item_id_map[items[0]['id']]) # Feature items and groups (clear_existing=True) feature_items(portal, items_to_feature, True) feature_groups(portal, random.sample(groups, 5), True)
def main(): output_root = None # Get script parameters results = check_args() if not results: sys.exit(0) portal_address, adminuser, password, src_ids = results try: # Create portal connection object portal = Portal(portal_address, adminuser, password) # Check if any specified GUIDs do not exist invalid_guids = validate_guids(portal, src_ids) if len(invalid_guids) > 0: raise Exception( 'ERROR: The following portal items do not exist: {}'.format( invalid_guids)) # Create list of users users = [org_user['username'] for org_user in portal.org_users()] target_users = [user for user in users if user not in exclude_users] # ----------------------------------------------------------------- # Extract portal items # ----------------------------------------------------------------- print '\n\n{}\nExtracting select portal items...\n{}\n'.format( sec_char * sec_len, sec_char * sec_len) # Create temporary extract folder in OS users' temp directory output_root = os.path.join( tempfile.gettempdir(), os.path.basename(sys.argv[0]).split('.')[0] + '_Extract') os.makedirs(output_root) print 'Extract folder: {}'.format(output_root) # Extract specified portal item(s) for src_id in src_ids: src_item = portal.item(src_id) os.chdir(output_root) print '- Extracting item {} "{}" ({}) user account {}...'.format( src_item['id'], src_item['title'], src_item['type'], src_item['owner']) PortalContentExtract.extract_item(portal, src_item['id'], src_item['owner']) # Create list of paths to individual extracted portal item folders src_item_paths = [ os.path.join(output_root, src_id) for src_id in src_ids ] # ----------------------------------------------------------------- # Publish extracted portal items for each user # ----------------------------------------------------------------- print '\n\n{}\nPublish extracted items to each portal' \ 'user account...\n{}'.format(sec_char * sec_len, sec_char * sec_len) print 'NOTE: not publishing to the following users:' print exclude_users for target_user in target_users: print '\n\nUser Account: {}'.format(target_user) # Get info about user folders target_user_folders = portal.folders(target_user) for src_item_path in src_item_paths: # Get info about the source item os.chdir(src_item_path) src_item_json = json.load(open('item.json')) item_title = src_item_json['title'] item_type = src_item_json['type'] item_id = src_item_json['id'] item_owner = src_item_json['owner'] item_folder_id = src_item_json['ownerFolder'] # Create folder in user account for item item_folder_name = get_folder_name(portal, item_owner, item_folder_id) if item_folder_name: if not has_folder(portal, target_user, item_folder_name): print 'Creating target folder "{}" in account ' \ '{}...'.format(item_folder_name, target_user) portal.create_folder(target_user, item_folder_name) # Check if user already owns item user_items = portal.search( q='owner:{} AND type:{} AND title:{}'.format( target_user, item_type, item_title)) # Add item if item does not exist in user account or # update item if it already exists if len(user_items) == 0: print '\n- Add item "{}" ({}) to user account {}...'.format( item_title, item_type, portal.logged_in_user()['username']) item, orig_id = provision.load_item(portal, src_item_path) print '- Reassign item to user account {}, ' \ 'folder "{}"...'.format(target_user, item_folder_name) portal.reassign_item(item.get('id'), target_user, item_folder_name) else: for user_item in user_items: if user_item['id'] <> item_id: print '\n- Update existing item {} ' \ '"{}" ({}) user account {}...'.format( user_item['id'], user_item['title'], user_item['type'], user_item['owner']) item, orig_id = provision.load_item( portal, src_item_path, user_item['id']) print '- Reassign item to user account {}, ' \ 'folder "{}"...'.format(target_user, item_folder_name) portal.reassign_item(item.get('id'), target_user, item_folder_name) else: print '*** No need to update item {}; ' \ 'user is owner of extracted item.'.format( user_item['id']) print '\n\nDone.' except: # Get the traceback object tb = sys.exc_info()[2] tbinfo = traceback.format_tb(tb)[0] # Concatenate information together concerning the error # into a message string pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + \ "\nError Info:\n" + str(sys.exc_info()[1]) # Print Python error messages for use in Python / Python Window print print "***** ERROR ENCOUNTERED *****" print pymsg + "\n" finally: # Change directory to starting directory, otherwise the # delete will fail. os.chdir(start_dir) # Delete temp extracted folder/files if output_root: if os.path.exists(output_root): shutil.rmtree(output_root)
def main(): exit_err_code = 1 # Print/get script arguments results = print_args() if not results: sys.exit(exit_err_code) portal_address, adminuser, password = results total_success = True title_break_count = 100 section_break_count = 75 search_query = None print '=' * title_break_count print 'Validate Hosted Service Sources' print '=' * title_break_count source_items = [] hosted_items = [] root_folder_path = None root_folder_path = tempfile.mkdtemp() print 'Temporary directory: {}'.format(root_folder_path) orig_dir = os.getcwd() try: portal = Portal(portal_address, adminuser, password) items = portal.search() # --------------------------------------------------------------------- # Get info about hosted service source items # (currently service definitions) # --------------------------------------------------------------------- for item in items: if item['type'] == 'Service Definition': print '\nDownloading and extracting Service Definition item {}'.format(item['id']) # Download .sd file download_root_path = os.path.join(root_folder_path, item['id']) os.mkdir(download_root_path) download_path = portal.item_datad(item['id'], download_root_path) # Extract serviceconfiguration.json file from downloaded .sd file file_name = 'serviceconfiguration.json' extract_path = download_path.replace('.sd', '') #print extract_path os.mkdir(extract_path) err_stat = extractFromSDFile(download_path, extract_path, file_name) print 'Extract status: {}'.format(err_stat) # Open extract .json file file_path = findFilePath(extract_path, file_name) os.chdir(os.path.dirname(file_path)) service_config = json.load(open(file_name)) # [{id: val, owner: val, title: val, type: val # service_config: {stuff from .json file}}] d = { 'id': item['id'], 'owner': item['owner'], 'title': item['title'], 'type': item['type'], 'service_config': service_config } source_items.append(d) # --------------------------------------------------------------------- # Get info about hosted service items # --------------------------------------------------------------------- print '\nDetermine what hosted services exist...' h_service_items = get_hosted_service_items(portal, items) for item in h_service_items: d = { 'id': item['id'], 'owner': item['owner'], 'title': item['title'], 'type': item['type'], 'url': item['url'] } hosted_items.append(d) # --------------------------------------------------------------------- # For each hosted service find the associated source item # --------------------------------------------------------------------- print '=' * section_break_count print '\nDetermine which source items are associated with each hosted service...' print '=' * section_break_count num_hosted_no_match = 0 num_hosted_match = 0 num_hosted_mismatch_owner = 0 write_str = "\tid: {:<34}owner: {:<20}type: {:<25}service: {:<50}\n" for hosted_d in hosted_items: found = False found_num = 0 # Get last components of URL (i.e., SRTM_V2_56020/FeatureServer) hosted_url = '/'.join(hosted_d['url'].split('/')[-2:]) print '\n{}'.format('-' * 100) print 'Hosted Service Item: Title - "{}"\n'.format(hosted_d['title']) hosted_str = write_str.format( hosted_d['id'], hosted_d['owner'], hosted_d['type'], hosted_url) print hosted_str # Look for match in source items print '\tMatching Source Item:' for source_d in source_items: src_service_info = source_d['service_config']['service'] src_service_name = src_service_info['serviceName'] src_service_type = src_service_info['type'] src_service_url = '{}/{}'.format(src_service_name, src_service_type) if hosted_url == src_service_url: found = True found_num += 1 match_str = write_str.format( source_d['id'], source_d['owner'], source_d['type'], src_service_url) print '\n\tTitle: "{}"'.format(source_d['title']) print match_str if source_d['owner'] != hosted_d['owner']: print '*** ERROR: owner does not match hosted service item owner.' num_hosted_mismatch_owner += 1 if found_num == 0: print '*** ERROR: no matching source item found.' if found_num > 1: print '*** ERROR: there is more then one matching source item found.' if found: num_hosted_match += 1 else: num_hosted_no_match += 1 # --------------------------------------------------------------------- # For each source item find the associated hosted service # --------------------------------------------------------------------- print '=' * section_break_count print '\nDetermine which hosted services are associated with each source item...' print '=' * section_break_count num_source_no_match = 0 num_source_match = 0 num_source_mismatch_owner = 0 write_str = "\tid: {:<34}owner: {:<20}type: {:<25}service: {:<50}\n" for source_d in source_items: found = False found_num = 0 src_service_info = source_d['service_config']['service'] src_service_name = src_service_info['serviceName'] src_service_type = src_service_info['type'] src_service_url = '{}/{}'.format(src_service_name, src_service_type) print '\n{}'.format('-' * 100) print 'Source Item: Title - "{}"\n'.format(source_d['title']) source_str = write_str.format( source_d['id'], source_d['owner'], source_d['type'], src_service_url) print source_str # Look for match in source items print '\tMatching Hosted Service:' for hosted_d in hosted_items: # Get last components of URL (i.e., SRTM_V2_56020/FeatureServer) hosted_url = '/'.join(hosted_d['url'].split('/')[-2:]) if hosted_url == src_service_url: found = True found_num += 1 match_str = write_str.format( hosted_d['id'], hosted_d['owner'], hosted_d['type'], hosted_url) print '\n\tTitle: "{}"'.format(hosted_d['title']) print match_str if hosted_d['owner'] != source_d['owner']: print '*** ERROR: owner does not match associated source owner.' num_source_mismatch_owner += 1 if found_num == 0: print '*** ERROR: no matching hosted service found.' if found_num > 1: print '*** ERROR: there is more then one hosted service found.' if found: num_source_match += 1 else: num_source_no_match += 1 print '\n{}'.format('=' * section_break_count) print 'Summary:\n' print 'Number of hosted services: {}'.format(len(hosted_items)) print 'With matching source item: {}'.format(num_hosted_match) print 'With NO matching source item: {}'.format(num_hosted_no_match) print 'With mis-matching owners: {}'.format(num_hosted_mismatch_owner) print '\nNumber of source items: {}'.format(len(source_items)) print 'With matching hosted service: {}'.format(num_source_match) print 'With NO matching hosted service: {}'.format(num_source_no_match) print 'With mis-matching owners: {}'.format(num_source_mismatch_owner) except: total_success = False # Get the traceback object tb = sys.exc_info()[2] tbinfo = traceback.format_tb(tb)[0] # Concatenate information together concerning the error # into a message string pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + \ "\nError Info:\n" + str(sys.exc_info()[1]) # Print Python error messages for use in Python / Python Window print print "***** ERROR ENCOUNTERED *****" print pymsg + "\n" finally: os.chdir(orig_dir) if root_folder_path: shutil.rmtree(root_folder_path) print '\nDone.' if total_success: sys.exit(0) else: sys.exit(exit_err_code)
def most_active_publishers(): portal = Portal('http://portaldev.esri.com', 'admin', 'esri.agp') results = portal.search(['owner','count(owner)'], group_fields=['owner'], scope='org') pprint(results, indent=2)
def highest_rated_publishers(): portal = Portal('http://portaldev.esri.com', 'admin', 'esri.agp') results = portal.search(['owner','count(avgRating)','avg(avgRating)'], \ 'numratings:{0 TO 99999}',\ group_fields=['owner']) pprint(results, indent=2)
def most_rated_publishers(): portal = Portal('http://portaldev.esri.com', 'admin', 'esri.agp') results = portal.search(['owner', 'sum(numRatings)'], group_fields=['owner']) pprint(results, indent=2)
def main(): exit_err_code = 1 # Print/get script arguments results = print_args() if not results: sys.exit(exit_err_code) portal_address, adminuser, password = results total_success = True title_break_count = 100 section_break_count = 75 search_query = None print '=' * title_break_count print 'Prepare Items for Extract' print '=' * title_break_count try: portal = Portal(portal_address, adminuser, password) items = portal.search(q=search_query, sort_field='owner') # --------------------------------------------------------------------- # Prepare hosted service items # --------------------------------------------------------------------- # Add new tag to hosted service so we can identify the original # hosted service after the portal items are published to a new portal new_tags = ['Hosted Service'] print '\n{}'.format('-' * section_break_count) print '- Prepare Hosted Service Items (Add tags: {})...'.format(', '.join(new_tags)) items_to_prep = get_hosted_service_items(portal, items) for item_to_prep in items_to_prep: print '\n {}'.format(format_hosted_item_info(item_to_prep)) tags = item_to_prep.get('tags') for new_tag in new_tags: if new_tag not in tags: tags.append(new_tag) # NOTE: have to pass new tags as string and not as a list resp = portal.update_item(item_to_prep['id'], {'tags':', '.join(tags)}) if not resp: print '***ERROR encountered during "update_item".' total_success = False except: total_success = False # Get the traceback object tb = sys.exc_info()[2] tbinfo = traceback.format_tb(tb)[0] # Concatenate information together concerning the error # into a message string pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + \ "\nError Info:\n" + str(sys.exc_info()[1]) # Print Python error messages for use in Python / Python Window print print "***** ERROR ENCOUNTERED *****" print pymsg + "\n" finally: print '\nDone.' if total_success: sys.exit(0) else: sys.exit(exit_err_code)
def main(): output_root = None # Get script parameters results = check_args() if not results: sys.exit(0) portal_address, adminuser, password, src_ids = results try: # Create portal connection object portal = Portal(portal_address, adminuser, password) # Check if any specified GUIDs do not exist invalid_guids = validate_guids(portal, src_ids) if len(invalid_guids) > 0: raise Exception( 'ERROR: The following portal items do not exist: {}'.format( invalid_guids)) # Create list of users users = [org_user['username'] for org_user in portal.org_users()] target_users = [user for user in users if user not in exclude_users] # ----------------------------------------------------------------- # Extract portal items # ----------------------------------------------------------------- print '\n\n{}\nExtracting select portal items...\n{}\n'.format( sec_char * sec_len, sec_char * sec_len) # Create temporary extract folder in OS users' temp directory output_root = os.path.join(tempfile.gettempdir(), os.path.basename( sys.argv[0]).split('.')[0] + '_Extract' ) os.makedirs(output_root) print 'Extract folder: {}'.format(output_root) # Extract specified portal item(s) for src_id in src_ids: src_item = portal.item(src_id) os.chdir(output_root) print '- Extracting item {} "{}" ({}) user account {}...'.format( src_item['id'], src_item['title'], src_item['type'], src_item['owner']) PortalContentExtract.extract_item( portal, src_item['id'], src_item['owner']) # Create list of paths to individual extracted portal item folders src_item_paths = [os.path.join(output_root, src_id) for src_id in src_ids] # ----------------------------------------------------------------- # Publish extracted portal items for each user # ----------------------------------------------------------------- print '\n\n{}\nPublish extracted items to each portal' \ 'user account...\n{}'.format(sec_char * sec_len, sec_char * sec_len) print 'NOTE: not publishing to the following users:' print exclude_users for target_user in target_users: print '\n\nUser Account: {}'.format(target_user) # Get info about user folders target_user_folders = portal.folders(target_user) for src_item_path in src_item_paths: # Get info about the source item os.chdir(src_item_path) src_item_json = json.load(open('item.json')) item_title = src_item_json['title'] item_type = src_item_json['type'] item_id = src_item_json['id'] item_owner = src_item_json['owner'] item_folder_id = src_item_json['ownerFolder'] # Create folder in user account for item item_folder_name = get_folder_name(portal, item_owner, item_folder_id) if item_folder_name: if not has_folder(portal, target_user, item_folder_name): print 'Creating target folder "{}" in account ' \ '{}...'.format(item_folder_name, target_user) portal.create_folder(target_user, item_folder_name) # Check if user already owns item user_items = portal.search( q='owner:{} AND type:{} AND title:{}'.format( target_user, item_type, item_title)) # Add item if item does not exist in user account or # update item if it already exists if len(user_items) == 0: print '\n- Add item "{}" ({}) to user account {}...'.format( item_title, item_type, portal.logged_in_user()['username']) item, orig_id = provision.load_item(portal, src_item_path) print '- Reassign item to user account {}, ' \ 'folder "{}"...'.format(target_user, item_folder_name) portal.reassign_item(item.get('id'), target_user, item_folder_name) else: for user_item in user_items: if user_item['id'] <> item_id: print '\n- Update existing item {} ' \ '"{}" ({}) user account {}...'.format( user_item['id'], user_item['title'], user_item['type'], user_item['owner']) item, orig_id = provision.load_item( portal, src_item_path, user_item['id']) print '- Reassign item to user account {}, ' \ 'folder "{}"...'.format(target_user, item_folder_name) portal.reassign_item(item.get('id'), target_user, item_folder_name) else: print '*** No need to update item {}; ' \ 'user is owner of extracted item.'.format( user_item['id']) print '\n\nDone.' except: # Get the traceback object tb = sys.exc_info()[2] tbinfo = traceback.format_tb(tb)[0] # Concatenate information together concerning the error # into a message string pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + \ "\nError Info:\n" + str(sys.exc_info()[1]) # Print Python error messages for use in Python / Python Window print print "***** ERROR ENCOUNTERED *****" print pymsg + "\n" finally: # Change directory to starting directory, otherwise the # delete will fail. os.chdir(start_dir) # Delete temp extracted folder/files if output_root: if os.path.exists(output_root): shutil.rmtree(output_root)
def main(): totalSuccess = True # ------------------------------------------------- # Check arguments # ------------------------------------------------- results = check_args() if not results: sys.exit(exitErrCode) server, port, adminuser, password, useSSL = results if debug: print server, port, adminuser, password, useSSL print print '=' * 100 print ' Remap portal ids stored within ArcGIS Server services' print '=' * 100 print try: # ------------------------------------------------- # Get portal items with URLs # ------------------------------------------------- if useSSL: protocol = 'https' else: protocol = 'http' # Create portal object portal_address = '{}://{}:7443/arcgis'.format(protocol, server) portal = Portal(portal_address, adminuser, password) if not portal: raise Exception('ERROR: Could not create "portal" object.') print '\n- Retrieving portal item information from portal...' portal_url_items = getPortalURLItems(portal) if not portal_url_items: raise Exception( 'ERROR: There are no URL portal items. Have you published the portal content?' ) # ------------------------------------------------- # Get all services that exist on server # ------------------------------------------------- print '\n- Retrieving list of ArcGIS Server services...' allServices = getServiceList(server, port, adminuser, password) # Remove certain services from collection excludeServices = ['SampleWorldCities.MapServer'] services = [ service for service in allServices if service not in excludeServices ] if len(services) == 0: raise Exception( 'ERROR: There are no user published ArcGIS Server services. Have you published the ArcGIS Server services?' ) # ------------------------------------------------- # Update portal item ids with service portal properties json # ------------------------------------------------- portalItemIDsToDelete = [] print '\n- Remap portal ids on each ArcGIS Server service...\n' totalNumIDsNotFound = 0 for service in services: time.sleep(0.5) print '\t' + ('-' * 75) print '\tService: ' + service folder, serviceNameType = parseService(service) numIDsFoundForService = 0 # Get the service info info = getServiceInfo(server, port, adminuser, password, folder, serviceNameType) # Get the service portal properties json and update the item ids print '\n\t- Retrieving information about associated portal items stored in the server JSON...' servicePortalPropsOrig = info.get('portalProperties') if not servicePortalPropsOrig: raise Exception( 'ERROR: The service ' + service + ' does not ' + 'have any portal properties ("portalProperties" JSON key/value). ' + 'Did you federate the server?') if servicePortalPropsOrig: servicePortalProps = copy.deepcopy(servicePortalPropsOrig) servicePortalItemsOrig = servicePortalProps.get('portalItems') servicePortalItems = copy.deepcopy(servicePortalItemsOrig) if not servicePortalItems: totalSuccess = False print '\n\t**** ERROR: this service does not have any associated portal items.' continue if servicePortalItems: print '\n\t- Associated portal items...' for servicePortalItem in servicePortalItems: orig_id = servicePortalItem['itemID'] # Get service search string serviceSearchStr = getServiceSearchString( service, servicePortalItem) print '\n\t - ' + serviceSearchStr + ': original item id = ' + orig_id # Get new portal item id new_id = findPortalItemID(server, serviceSearchStr, portal_url_items) if new_id: if (new_id <> orig_id): numIDsFoundForService = numIDsFoundForService + 1 servicePortalItem['itemID'] = new_id portalItemIDsToDelete.append(orig_id) print '\t\tFound new item id - ' + new_id else: print '\t\tItem IDs match, not processing.' else: print '\n\t**** WARNING: new item id not found.' servicePortalProps['portalItems'] = servicePortalItems info['portalProperties'] = servicePortalProps if doUpdateService: print '\n\n\t- Updating portal item information stored within service JSON (service will be restarted automatically)...' if numIDsFoundForService == 0: print '\n\t**** WARNING: there were no new ids found for this service so there is no need to update the service JSON info.' continue success, status = editServiceInfo( server, port, adminuser, password, folder, serviceNameType, info) if success: print '\t\tDone.' else: totalSuccess = False print '**** ERROR: Update of service was not successful.' print 'status: ' + str(status) if doDeleteItems: print #print '=' * 100 print '\n\n-Deleting portal items that were remapped to original portal item...' if len(portalItemIDsToDelete) == 0: print '\n**** ERROR: No portal items to delete; which means there were no portal items ' print '\t owned by ' + portal.logged_in_user( )['username'] + ' that were remapped to original portal item.\n' # Get list of all portal ids so we can verify that the portal item exists before we delete portal_items = portal.search(['id']) time.sleep(5) for portalItemID in portalItemIDsToDelete: time.sleep(2) itemFound = False print ' -Deleting id ' + portalItemID + '...' # Delete if item exists for portal_item in portal_items: if portal_item['id'] == portalItemID: itemFound = True results = portal.delete_item( portalItemID, portal.logged_in_user()['username']) if results: print '\tDone.' else: totalSuccess = False print '**** ERROR: Deletion of service was not successful.' if not itemFound: print '\tItem ' + portalItemID + ' does not exist. Skipping...' except: totalSuccess = False # Get the traceback object tb = sys.exc_info()[2] tbinfo = traceback.format_tb(tb)[0] # Concatenate information together concerning the error into a message string pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str( sys.exc_info()[1]) # Print Python error messages for use in Python / Python Window print print "***** ERROR ENCOUNTERED *****" print pymsg + "\n" finally: print print if totalSuccess: print "Remap of portal item ids on services was completed successfully." sys.exit(0) else: print "ERROR: Remap of portal item ids on services was _NOT_ completed successfully." sys.exit(1)
def most_reviewed_publishers(): portal = Portal('http://portaldev.esri.com', 'admin', 'esri.agp') results = portal.search(['owner','sum(numComments)'], group_fields=['owner']) pprint(results, indent=2)
def most_active_publishers(): portal = Portal('http://portaldev.esri.com', 'admin', 'esri.agp') results = portal.search(['owner', 'count(owner)'], group_fields=['owner'], scope='org') pprint(results, indent=2)
def main(): totalSuccess = True # ------------------------------------------------- # Check arguments # ------------------------------------------------- results = check_args() if not results: sys.exit(exitErrCode) server, port, adminuser, password, useSSL = results if debug: print server, port, adminuser, password, useSSL print print '=' * 100 print ' Remap portal ids stored within ArcGIS Server services' print '=' * 100 print try: # ------------------------------------------------- # Get portal items with URLs # ------------------------------------------------- if useSSL: protocol = 'https' else: protocol = 'http' # Create portal object portal_address = '{}://{}:7443/arcgis'.format(protocol, server) portal = Portal(portal_address, adminuser, password) if not portal: raise Exception('ERROR: Could not create "portal" object.') print '\n- Retrieving portal item information from portal...' portal_url_items = getPortalURLItems(portal) if not portal_url_items: raise Exception('ERROR: There are no URL portal items. Have you published the portal content?') # ------------------------------------------------- # Get all services that exist on server # ------------------------------------------------- print '\n- Retrieving list of ArcGIS Server services...' allServices = getServiceList(server, port, adminuser, password) # Remove certain services from collection excludeServices = ['SampleWorldCities.MapServer'] services = [service for service in allServices if service not in excludeServices] if len(services) == 0: raise Exception('ERROR: There are no user published ArcGIS Server services. Have you published the ArcGIS Server services?') # ------------------------------------------------- # Update portal item ids with service portal properties json # ------------------------------------------------- portalItemIDsToDelete = [] print '\n- Remap portal ids on each ArcGIS Server service...\n' totalNumIDsNotFound = 0 for service in services: time.sleep(0.5) print '\t' + ('-' * 75) print '\tService: ' + service folder, serviceNameType = parseService(service) numIDsFoundForService = 0 # Get the service info info = getServiceInfo(server, port, adminuser, password, folder, serviceNameType) # Get the service portal properties json and update the item ids print '\n\t- Retrieving information about associated portal items stored in the server JSON...' servicePortalPropsOrig = info.get('portalProperties') if not servicePortalPropsOrig: raise Exception('ERROR: The service ' + service + ' does not ' + 'have any portal properties ("portalProperties" JSON key/value). ' + 'Did you federate the server?') if servicePortalPropsOrig: servicePortalProps = copy.deepcopy(servicePortalPropsOrig) servicePortalItemsOrig = servicePortalProps.get('portalItems') servicePortalItems = copy.deepcopy(servicePortalItemsOrig) if not servicePortalItems: totalSuccess = False print '\n\t**** ERROR: this service does not have any associated portal items.' continue if servicePortalItems: print '\n\t- Associated portal items...' for servicePortalItem in servicePortalItems: orig_id = servicePortalItem['itemID'] # Get service search string serviceSearchStr = getServiceSearchString(service, servicePortalItem) print '\n\t - ' + serviceSearchStr + ': original item id = ' + orig_id # Get new portal item id new_id = findPortalItemID(server, serviceSearchStr, portal_url_items) if new_id: if (new_id <> orig_id): numIDsFoundForService = numIDsFoundForService + 1 servicePortalItem['itemID'] = new_id portalItemIDsToDelete.append(orig_id) print '\t\tFound new item id - ' + new_id else: print '\t\tItem IDs match, not processing.' else: print '\n\t**** WARNING: new item id not found.' servicePortalProps['portalItems'] = servicePortalItems info['portalProperties'] = servicePortalProps if doUpdateService: print '\n\n\t- Updating portal item information stored within service JSON (service will be restarted automatically)...' if numIDsFoundForService == 0: print '\n\t**** WARNING: there were no new ids found for this service so there is no need to update the service JSON info.' continue success, status = editServiceInfo(server, port, adminuser, password, folder, serviceNameType, info) if success: print '\t\tDone.' else: totalSuccess = False print '**** ERROR: Update of service was not successful.' print 'status: ' + str(status) if doDeleteItems: print #print '=' * 100 print '\n\n-Deleting portal items that were remapped to original portal item...' if len(portalItemIDsToDelete) == 0: print '\n**** ERROR: No portal items to delete; which means there were no portal items ' print '\t owned by ' + portal.logged_in_user()['username'] + ' that were remapped to original portal item.\n' # Get list of all portal ids so we can verify that the portal item exists before we delete portal_items = portal.search(['id']) time.sleep(5) for portalItemID in portalItemIDsToDelete: time.sleep(2) itemFound = False print ' -Deleting id ' + portalItemID + '...' # Delete if item exists for portal_item in portal_items: if portal_item['id'] == portalItemID: itemFound = True results = portal.delete_item(portalItemID, portal.logged_in_user()['username']) if results: print '\tDone.' else: totalSuccess = False print '**** ERROR: Deletion of service was not successful.' if not itemFound: print '\tItem ' + portalItemID + ' does not exist. Skipping...' except: totalSuccess = False # Get the traceback object tb = sys.exc_info()[2] tbinfo = traceback.format_tb(tb)[0] # Concatenate information together concerning the error into a message string pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()[1]) # Print Python error messages for use in Python / Python Window print print "***** ERROR ENCOUNTERED *****" print pymsg + "\n" finally: print print if totalSuccess: print "Remap of portal item ids on services was completed successfully." sys.exit(0) else: print "ERROR: Remap of portal item ids on services was _NOT_ completed successfully." sys.exit(1)