def copy_user_with_groups_and_items(): source = Portal('http://portaldev.arcgis.com', 'wmathot', 'wmathot') target = Portal('http://portaldev.esri.com', 'admin', 'esri.agp') source_owner = source.logged_in_user()['username'] target_owner = target.logged_in_user()['username'] # Copy the groups groups = source.groups(q='owner:' + source_owner) copied_groups = copy_groups(groups, source, target) print 'Copied ' + str(len(copied_groups)) + ' groups' # Copy the items copied_items = copy_user_contents(source, source_owner, target, target_owner) 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 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] target.share_items(target_owner, [target_item_id], target_group_ids,\ sharing['access'] == 'org',\ sharing['access'] == 'public')
def share_templates(portaladdress, username, password): # Create portal connection portal = Portal(portaladdress, username, password) print '\nShare web app templates with "AFMI Web Application Templates" group...' results = share_default_webapp_templates(portal, portal.logged_in_user()['username'], 'AFMI Web Application Templates') if results['success']: print '\tDone.' else: print 'ERROR: {}'.format(results['message']) print '\nShare web app templates with "AFMI Gallery Templates" group...' results = share_default_gallery_templates(portal, portal.logged_in_user()['username'], 'AFMI Gallery Templates') if results['success']: print '\tDone.' else: print 'ERROR: {}'.format(results['message'])
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 share_templates(portaladdress, username, password): # Create portal connection portal = Portal(portaladdress, username, password) print '\nShare web app templates with "AFMI Web Application Templates" group...' results = share_default_webapp_templates( portal, portal.logged_in_user()['username'], 'AFMI Web Application Templates') if results['success']: print '\tDone.' else: print 'ERROR: {}'.format(results['message']) print '\nShare web app templates with "AFMI Gallery Templates" group...' results = share_default_gallery_templates( portal, portal.logged_in_user()['username'], 'AFMI Gallery Templates') if results['success']: print '\tDone.' else: print 'ERROR: {}'.format(results['message'])
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 copy_users(): source = Portal('http://portaldev.arcgis.com', 'admin', 'esri.agp') target = Portal('http://wittm.esri.com', 'wmathot', 'wmathot') owners = ['admin', 'wmathot'] target_owner = target.logged_in_user()['username'] for owner in owners: groups = source.groups(q='owner:' + owner) copied_groups = copy_groups(groups, source, target, target_owner) copied_items = copy_user_contents(source, owner, target, target_owner) for item_id in copied_items.keys(): sharing = source.user_item(item_id)[1] if 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)
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 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 out_file = None # Check arguments results = check_args() if not results: sys.exit(exitErrCode) # Get parameter values portal_url, user, password, output_file, access_mode = results try: # Create portal object portal = Portal(portal_url, user, password) if not portal: raise Exception('ERROR: Could not create "portal" object.') if not portal.logged_in_user(): raise Exception( '\nERROR: Could not sign in with specified credentials.') # Get portal users portal_users = portal.org_users() out_file = open(output_file, access_mode) out_file_dir = os.path.dirname(output_file) for portal_user in portal_users: user_name = portal_user['username'] if user_name not in username_exclude: service_list_outfile = os.path.join( out_file_dir, '{}_servicelist.txt'.format(user_name)) service_list_qc_outfile = os.path.join( out_file_dir, '{}_qc_servicelist.txt'.format(user_name)) script_parameters = '{} {} {} {} {} {} {} > {}'.format( os.path.join(script_path, 'CreateServiceList.py'), portal_url, user, password, service_list_outfile, 'OVERWRITE', user_name, service_list_qc_outfile) out_file.write(script_parameters + '\n') print '\nDone. Output written to: {}\n'.format(output_file) 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: if hasattr(out_file, 'close'): out_file.close() if totalSuccess: sys.exit(0) else: sys.exit(1)
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 out_file = None # Check arguments results = check_args() if not results: sys.exit(exitErrCode) # Get parameter values portal_url, user, password, output_file, output_per_user = results try: access_mode = 'w' # Create portal object portal = Portal(portal_url, user, password) if not portal: raise Exception('ERROR: Could not create "portal" object.') if not portal.logged_in_user(): raise Exception('\nERROR: Could not sign in with specified credentials.') # Get portal users portal_users = portal.org_users() out_file = open(output_file, access_mode) out_file_dir = os.path.dirname(output_file) for portal_user in portal_users: user_name = portal_user['username'] if user_name in username_exclude: continue if output_per_user: service_list_outfile = os.path.join(out_file_dir, '{}_ServiceList.txt'.format(user_name)) service_list_qc_outfile = os.path.join(out_file_dir, '{}_CreateServiceList_output.txt'.format(user_name)) access_mode = 'OVERWRITE' redirect = '>' else: service_list_outfile = os.path.join(out_file_dir, 'ServiceList.txt') service_list_qc_outfile = os.path.join(out_file_dir, 'CreateServiceList_output.txt') access_mode = 'APPEND' redirect = '>>' script_parameters = '{} {} {} {} {} {} {} {} {}'.format( os.path.join(script_path, 'CreateServiceList.py'), portal_url, user, password, service_list_outfile, access_mode, user_name, redirect, service_list_qc_outfile) out_file.write(script_parameters + '\n') print '\nCreated output batch file: {}\n'.format(output_file) print 'Done.' 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: if hasattr(out_file, 'close'): out_file.close() if totalSuccess: sys.exit(0) else: sys.exit(1)
def main(): totalSuccess = True out_file = None # Check arguments results = check_args() if not results: sys.exit(exitErrCode) # Get parameter values portal_url, user, password, output_file, access_mode, owner, group = results try: # Create portal object portal = Portal(portal_url, user, password) if not portal: raise Exception('ERROR: Could not create "portal" object.') if not portal.logged_in_user(): raise Exception( '\nERROR: Could not sign in with specified credentials.') # Get portal items items = searchPortal(portal, owner, group) # Get portal item ids for all AGS services urlparts = urlparse.urlparse(portal_url) server = urlparts.hostname # NOTE: call to following function assumes AGS server is using # same username and password as portal service_portal_ids = get_service_portal_ids(server, 6443, user, password) all_urls = [] services = [] type_mapping = { 'FeatureServer': 'MapServer', 'NAServer': 'MapServer', 'MobileServer': 'MapServer', 'SchematicsServer': 'MapServer' } if items: for item in items: print '-' * 200 if 'Service' in item.get('typeKeywords'): if not 'Service Definition' in item.get('typeKeywords'): print_item_info2(item) url = item.get('url') all_urls.append(url) p_item_id = _get_item_id(url, service_portal_ids) print '\n\t\t{:<14}{:<32} {:<115}{:<25}'.format( 'Service', str(p_item_id), str(_get_item_title(portal, p_item_id)), str(_get_item_owner(portal, p_item_id))) print '\t\t{:<14}{:>32} {:<115}'.format( '', 'URL:', url) elif item.get('type') == 'Web Map': print_item_info2(item) urls = get_webmap_service_urls(portal, item) for url in urls: all_urls.append(url) p_item_id = _get_item_id(url, service_portal_ids) print '\n\t\t{:<14}{:<32} {:<115}{:<25}'.format( 'Service', str(p_item_id), str(_get_item_title(portal, p_item_id)), str(_get_item_owner(portal, p_item_id))) print '\t\t{:<14}{:>32} {:<115}'.format( '', 'URL:', url) elif item.get('type') == 'Operation View': print_item_info2(item) urls = get_opview_service_urls(portal, item, service_portal_ids) for url in urls: all_urls.append(url) elif item.get('type') == 'Web Mapping Application': print_item_info2(item) urls = get_webapp_service_urls(portal, item, service_portal_ids) for url in urls: all_urls.append(url) else: print_item_info2(item) print '\tSkipping service search for this item type...' if all_urls: for url in all_urls: url = _remove_layer_number(url) for search_str, replace_str in type_mapping.iteritems(): if url.find('/' + search_str) > 0: url = url.replace('/' + search_str, '/' + replace_str) url = '.'.join(url.rsplit('/', 1)) #print 'url line 271: ' + url if url.find('/rest/services/') > 0: service = url.split('/rest/services/')[1] if not service in services: services.append(service) if len(services) > 0: services.sort() out_file = open(output_file, access_mode) print '\n\n{}'.format('=' * 60) print 'Summary of services referenced by items:\n' for service in services: out_file.write(service + '\n') print '\t{}'.format(service) print '\n\tNumber of services: {}'.format(len(services)) print '\tService list written to file: {}'.format(output_file) else: print '\n***** ERROR: There are no services to write to file.' 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: if hasattr(out_file, 'close'): out_file.close() if totalSuccess: sys.exit(0) else: sys.exit(1)
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(): totalSuccess = True out_file = None # Check arguments results = check_args() if not results: sys.exit(exitErrCode) # Get parameter values portal_url, user, password, output_file, access_mode, owner, group = results global portal_netloc portal_netloc = urlparse.urlparse(portal_url).netloc try: # Create portal object portal = Portal(portal_url, user, password) if not portal: raise Exception('ERROR: Could not create "portal" object.') if not portal.logged_in_user(): raise Exception('\nERROR: Could not sign in with specified credentials.') # Get portal items items = searchPortal(portal, owner, group) # Get portal item ids for all AGS services urlparts = urlparse.urlparse(portal_url) server = urlparts.hostname # NOTE: call to following function assumes AGS server is using # same username and password as portal service_portal_ids = get_service_portal_ids(server, 6443, user, password) all_urls = [] services = [] type_mapping = {'FeatureServer':'MapServer', 'NAServer':'MapServer', 'MobileServer':'MapServer', 'SchematicsServer':'MapServer'} if items: for item in items: # if item['id'] <> 'faef542a74e141398c05c43379a67004': # continue print '-' * 200 if 'Service' in item.get('typeKeywords'): if not 'Service Definition' in item.get('typeKeywords'): print_item_info2(item) url = item.get('url') all_urls.append(url) p_item_id = _get_item_id(url, service_portal_ids) print '\n\t\t{:<14}{:<32} {:<115}{:<25}'.format('Service', str(p_item_id), str(_get_item_title(portal, p_item_id)), str(_get_item_owner(portal, p_item_id))) print '\t\t{:<14}{:>32} {:<115}'.format('','URL:',url) if do_url_checks: _check_url(url) elif item.get('type') == 'Web Map': print_item_info2(item) urls = get_webmap_service_urls(portal, item) for url in urls: all_urls.append(url) p_item_id = _get_item_id(url, service_portal_ids) print '\n\t\t{:<14}{:<32} {:<115}{:<25}'.format('Service', str(p_item_id), str(_get_item_title(portal, p_item_id)), str(_get_item_owner(portal, p_item_id))) print '\t\t{:<14}{:>32} {:<115}'.format('','URL:',url) if do_url_checks: _check_url(url) elif item.get('type') == 'Operation View': print_item_info2(item) urls = get_opview_service_urls(portal, item, service_portal_ids) for url in urls: all_urls.append(url) elif item.get('type') == 'Web Mapping Application': print_item_info2(item) if 'Story Map' in item.get('typeKeywords'): print '(Story Map)' urls = get_storymap_resources(portal, item, service_portal_ids) for url in urls: all_urls.append(url) else: urls = get_webapp_service_urls(portal, item, service_portal_ids) for url in urls: all_urls.append(url) else: print_item_info2(item) print '\tSkipping service search for this item type...' if all_urls: for url in all_urls: url = _remove_layer_number(url) if url.find('/Hosted/') == -1: for search_str, replace_str in type_mapping.iteritems(): if url.find('/' + search_str) > 0: url = url.replace('/' + search_str, '/' + replace_str) url = '.'.join(url.rsplit('/', 1)) #print 'url line 271: ' + url if url.find('/rest/services/') > 0: service = url.split('/rest/services/')[1] if not service in services: services.append(service) if len(services) > 0: services.sort() out_file = open(output_file, access_mode) print '\n\n{}'.format('=' * 60) print 'Summary of services referenced by items:\n' for service in services: out_file.write(service + '\n') print '\t{}'.format(service) print '\n\tNumber of services: {}'.format(len(services)) print '\tService list written to file: {}'.format(output_file) else: print '\n***** ERROR: There are no services to write to file.' 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: if hasattr(out_file, 'close'): out_file.close() if totalSuccess: sys.exit(0) else: sys.exit(1)
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)