def create_portal_site():
    r = pcurl.get('https://' + fqdn +
                  ':7443/arcgis/portaladmin/createNewSite?f=json')
    payload = {
        'username':
        username,
        'password':
        passwd,
        'fullname':
        username,
        'email':
        '*****@*****.**',
        'securityQuestionIdx':
        1,
        'securityQuestionAns':
        '222',
        'contentStore':
        json.dumps({
            'type': 'fileStore',
            'provider': 'FileSystem',
            'connectionString': r['contentDirPath']
        }),
        'f':
        'json'
    }
    print u'---------------------------------------------------------------------------------------------'
    print u'                                     开始创建Portal站点'
    print u'---------------------------------------------------------------------------------------------'
    result = pcurl.post(
        'https://' + fqdn + ':7443/arcgis/portaladmin/createNewSite', payload)
    print u'---------------------------------------------------------------------------------------------'
    print u'                                     Portal站点创建完成'
    print u'---------------------------------------------------------------------------------------------'
    print json.dumps(result)
def get_server_admin_token():
    payload = {
        'username': username,
        'password': passwd,
        'f': 'json',
        'client': 'requestip'
    }
    result = pcurl.post('https://' + fqdn + ':6443/arcgis/admin/generateToken',
                        payload)
    return result['token']
def get_portal_token():
    payload = {
        'username': username,
        'password': passwd,
        'f': 'json',
        'client': 'referer',
        'referer': 'python://qqq'
    }
    result = pcurl.post(
        'https://' + fqdn + ':7443/arcgis/sharing/generateToken', payload)
    return result['token']
def unfederate(tt):
    result = pcurl.get(
        'https://' + fqdn +
        ':7443/arcgis/portaladmin/federation/servers?f=json&token=' + tt)
    print result
    for server in result['servers']:
        print u'---------------------------------------------------------------------------------------------'
        print u'                                取消Server联合', server['id']
        print u'---------------------------------------------------------------------------------------------'
        payload = {'token': tt, 'f': 'json'}
        r = pcurl.post(
            'https://' + fqdn +
            ':7443/arcgis/portaladmin/federation/servers/' + server['id'] +
            '/unfederate', payload)
        print json.dumps(r)
def create_server_site():
    r = pcurl.get('https://' + fqdn +
                  ':6443/arcgis/admin/createNewSite?f=json')
    payload = {
        'username': username,
        'password': passwd,
        'configStoreConnection': json.dumps(r['configStoreConnection']),
        'directories': json.dumps(r['directories']),
        'f': 'json'
    }
    print u'---------------------------------------------------------------------------------------------'
    print u'                                     开始创建Server站点'
    print u'---------------------------------------------------------------------------------------------'
    result = pcurl.post('https://' + fqdn + ':6443/arcgis/admin/createNewSite',
                        payload)
    print u'---------------------------------------------------------------------------------------------'
    print u'                                     开始创建Server站点'
    print u'---------------------------------------------------------------------------------------------'
    print json.dumps(result)
def config_server_remove_hosting(tt):
    result = pcurl.get(
        'https://' + fqdn +
        ':7443/arcgis/portaladmin/federation/servers?f=json&token=' + tt)
    print result
    for server in result['servers']:
        if server['isHosted']:
            print u'---------------------------------------------------------------------------------------------'
            print u'                           取消Server的托管', server['id']
            print u'---------------------------------------------------------------------------------------------'
            payload = {
                'serverRole': 'FEDERATED_SERVER',
                'token': tt,
                'f': 'json'
            }
            r = pcurl.post(
                'https://' + fqdn +
                ':7443/arcgis/portaladmin/federation/servers/' + server['id'] +
                '/update', payload)
            print json.dumps(r)
def config_hosting_server(tt):
    result = pcurl.get(
        'https://' + fqdn +
        ':7443/arcgis/portaladmin/federation/servers?f=json&token=' + tt)
    print result
    for server in result['servers']:
        if not server['isHosted']:
            print u'---------------------------------------------------------------------------------------------'
            print u'                            开始配置Server为托管服务器', server['id']
            print u'---------------------------------------------------------------------------------------------'
            payload = {
                'serverRole': 'HOSTING_SERVER',
                'token': tt,
                'f': 'json'
            }
            r = pcurl.post(
                'https://' + fqdn +
                ':7443/arcgis/portaladmin/federation/servers/' + server['id'] +
                '/update', payload)
            print json.dumps(r)
def delete_site(tt):
    payload = {'token': tt, 'f': 'json'}
    result = pcurl.post('https://' + fqdn + ':6443/arcgis/admin/deleteSite',
                        payload)
    return result