Here is a code sample demonstrating how to update service parameters after the service has been published.
#modify these to match your environment
serverHost = 'yourgisServer.com'
serverPort = 6080
serviceRelativePath = r'/services/serviceFolder/serviceName.MapServer'
username = 'yourAdminName'
password = 'yourAdminPW'
# properites you would like to apply to the service
newProperties = {
"minInstancesPerNode": 1,
"maxInstancesPerNode": 5,
"properties": {
"enableDynamicLayers": "true",
"dynamicDataWorkspaces": "[{\"id\":\"wsName\",\"workspaceFactory\":\"SDE\",\"lockVersion\":\"false\",\"workspaceConnection\":\"connectionProps\"}]"
}
}
import json
import urllib
import httplib
# ####################
# helper functions
# ####################
def getUpdatedDict(inDict,modDict):
def _getPathsAndValuesFromModDict(d,parentDict):
for k, v in d.iteritems():
if isinstance(v, dict):
_getPathsAndValuesFromModDict(v,parentDict)
else:
parentDict = v
_getPathsAndValuesFromModDict(modDict,inDict)
return inDict
def adminApiCall(strAdminApiUrl,objParams):
def _getToken():
tokenURL = "/arcgis/admin/generateToken"
params = urllib.urlencode({'username': username, 'password': password, 'client': 'requestip', 'f': 'json'})
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
httpConn = httplib.HTTPConnection(serverHost, serverPort)
httpConn.request("POST", tokenURL, params, headers)
response = httpConn.getresponse()
if (response.status != 200):
httpConn.close()
return
else:
data = response.read()
httpConn.close()
# Check that data returned is not an error object
if not _assertJsonSuccess(data):
return
# Extract the toke from it
token = json.loads(data)
return token['token']
def _assertJsonSuccess(data):
obj = json.loads(data)
if 'status' in obj and obj['status'] == "error":
#print "Error: JSON object returns an error. " + str(obj)
return False
else:
return True
token = _getToken()
if objParams:
objParams['token'] = token
objParams['f'] = 'json'
else:
objParams = {'token': token,'f': 'json'}
params = urllib.urlencode(objParams)
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
# Connect to URL and post parameters
httpConn = httplib.HTTPConnection(serverHost, serverPort)
httpConn.request("POST", r'/arcgis/admin'+ strAdminApiUrl, params, headers)
# Read response
response = httpConn.getresponse()
if (response.status != 200):
print response.status
httpConn.close()
output = "Error: Code "+str(response.status)+', '+str(response.reason)
else:
data = response.read()
# Check that data returned is not an error object
if not _assertJsonSuccess(data):
obj = json.loads(data)
output = obj['messages']
else:
dataObj = json.loads(data)
output = dataObj
httpConn.close()
try:
if output[0] == u'Authorization failed. This user account does not have the required privileges to access this application.':
return {'status':'error','msg':u'Authorization failed for user'}
except:
return output
# ####################
# end helper functions
# ####################
#fetch the parameters using the admin api
serviceInfo = adminApiCall(serviceRelativePath,{'f':'json'})
#update new parameters using the new properties
serviceInfo = getUpdatedDict(serviceInfo,newProperties)
#apply the updated params
adminApiCall('/services/test/testForDave.MapServer/edit',{'f':'json','service':json.dumps(serviceInfo)})
Modifying properties after the service is published is, in my opinion, easier than doing so via the sd or sddraft.