veryLongSatelliteName = 'SatelliteWithVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVery' \
                        'VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVery' \
                        'VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongName'

repository.create(factory.configurationItem('Infrastructure/Satellite', 'core.Directory'))

for i in range(1, 12):
    satelliteId = 'Infrastructure/Satellite/LocalSatellite' + str(i)
    repository.create(factory.configurationItem(satelliteId, 'xl.Satellite',
                                                {'address': '127.0.0.1', 'protocolPort': '8380',
                                                 'encrypted': 'false'}))

    hostId = 'Infrastructure/satellite-host' + str(i)
    satelliteHost = repository.create(factory.configurationItem(hostId, 'overthere.LocalHost',
                                                                {'os': 'UNIX', 'satellite': satelliteId}))

    env = repository.create(factory.configurationItem('Environments/EnvWithSatellite' + str(i), 'udm.Environment',
                                                      {'dictionaries': ['Environments/Defaults'],
                                                       'members': [satelliteHost.id]}))


    def createDeployTask(appId):
        deplToSat = deployment.prepareInitial(appId, env.id)
        deplToSat = deployment.prepareAutoDeployeds(deplToSat)
        deployment.createDeployTask(deplToSat)


    for appVersion in range(1, 12):
        appId = 'Applications/SatelliteApp/' + str(appVersion) + '.0'
        createDeployTask(appId)

    createDeployTask('Applications/AppForHistory/1.0')
    createDeployTask('Applications/AppForHistory/2.0')
    createDeployTask('Applications/AppForHistory/3.0')
    createDeployTask('Applications/LongRunningApp/1.0')
    createDeployTask('Applications/TestApp/1.0')
    createDeployTask('Applications/AppForUpdateScenario/1.0')
    createDeployTask('Applications/AppForUpdateScenario/2.0')
    createDeployTask('Applications/AppForUpdateScenario/3.0')
    createDeployTask('Applications/MultiCommandApp/1.0')

repository.create(factory.configurationItem('Infrastructure/Satellite/MisconfiguredSatellite', 'xl.Satellite',
                                            {'address': '127.0.0.1', 'protocolPort': '9999',
                                             'encrypted': 'false'}))

repository.create(factory.configurationItem('Infrastructure/Satellite/' + veryLongSatelliteName, 'xl.Satellite',
                                            {'address': '127.0.0.1', 'protocolPort': '8380',
                                             'encrypted': 'false'}))
repository.create(
    factory.configurationItem('Infrastructure/Satellite/XLSatelliteGroup', 'xl.SatelliteGroup',
                              {'satellites': ['Infrastructure/Satellite/LocalSatellite1',
                                              'Infrastructure/Satellite/LocalSatellite2',
                                              'Infrastructure/Satellite/LocalSatellite3',
                                              'Infrastructure/Satellite/LocalSatellite4',
                                              'Infrastructure/Satellite/LocalSatellite5',
                                              'Infrastructure/Satellite/LocalSatellite6',
                                              'Infrastructure/Satellite/LocalSatellite7',
                                              'Infrastructure/Satellite/LocalSatellite8',
                                              'Infrastructure/Satellite/LocalSatellite9',
                                              'Infrastructure/Satellite/LocalSatellite10',
                                              'Infrastructure/Satellite/LocalSatellite11',
                                              'Infrastructure/Satellite/' + veryLongSatelliteName,
                                              'Infrastructure/Satellite/MisconfiguredSatellite']}))

satelliteGroup = repository.create(
    factory.configurationItem('Infrastructure/Satellite/LocalSatelliteGroup', 'xl.SatelliteGroup',
                              {'satellites': ['Infrastructure/Satellite/LocalSatellite1',
                                              'Infrastructure/Satellite/LocalSatellite2']}))

repository.create(factory.configurationItem('Infrastructure/group-host',
                                            'overthere.LocalHost', {'os': 'UNIX', 'satellite': satelliteGroup.id}))

repository.create(factory.configurationItem('Environments/Group', 'udm.Environment',
                                            {'dictionaries': ['Environments/Defaults'],
                                             'members': ['Infrastructure/group-host']}))
multiHostedSatelliteGroup = repository.create(
    factory.configurationItem('Infrastructure/Satellite/MultiHostedSatelliteGroup', 'xl.SatelliteGroup',
                              {'satellites': ['Infrastructure/Satellite/LocalSatellite1']}))

multiHostedTeam = repository.create(factory.configurationItem('Infrastructure/Satellite/SatelliteGroupTeam',
                                                              'core.Directory'))
for i in range(1, 15):
    repository.create(factory.configurationItem(multiHostedTeam.id + '/groupTeam-host-' + str(i),
                                                'overthere.LocalHost',
                                                {'os': 'UNIX', 'satellite': multiHostedSatelliteGroup.id}))
multiHostedSatellite = repository.create(
    factory.configurationItem('Infrastructure/Satellite/MultiHostedSatellite', 'xl.Satellite',
                              {'address': '127.0.0.1', 'protocolPort': '8380',
                               'encrypted': 'false'}))

for teamLetter in ['A', 'B', 'C', 'D']:
    team = repository.create(factory.configurationItem('Infrastructure/Satellite/Team' + teamLetter, 'core.Directory'))
    for i in range(1, 10):
        createdHost = repository.create(factory.configurationItem(team.id + '/team-' + teamLetter + '-host-' + str(i),
                                                                  'overthere.LocalHost',
                                                                  {'os': 'UNIX', 'satellite': multiHostedSatellite.id}))
        createdHost.satellite = multiHostedSatellite.id
        repository.update(createdHost)

security.grant("read", "Cis_reader_only", ['Infrastructure/Satellite'])
security.grant("read", "Cis_reader_only", ['Infrastructure/Satellite/TeamA'])
security.grant("read", "Cis_reader_only", ['Infrastructure/Satellite/SatelliteGroupTeam'])
security.grant("read", "Task_View_Only", ['Infrastructure/Satellite'])

security.grant("controltask#execute", "Cis_reader_only", ['Infrastructure/Satellite/TeamB'])
security.grant("controltask#execute", "Cis_reader_only", ['Infrastructure/Satellite/TeamC'])
security.grant("controltask#execute", "Cis_reader_only", ['Infrastructure/Satellite/TeamD'])
security.grant("controltask#execute", "Cis_reader_only", ['Infrastructure/Satellite/SatelliteGroupTeam'])

# Prepare deployment tasks for satellite group
host6 = repository.read('Infrastructure/host6')
host6.satellite = 'Infrastructure/Satellite/LocalSatelliteGroup'
repository.update(host6)

jbossHost = repository.read('Infrastructure/jboss-host')
jbossHost.satellite = 'Infrastructure/Satellite/LocalSatellite1'
repository.update(jbossHost)

envWithSatGroup = repository.create(factory.configurationItem('Environments/EnvWithSatelliteGroup', 'udm.Environment',
                                                              {'dictionaries': ['Environments/Defaults'],
                                                               'members': [host6.id]}))

for appVersion in range(1, 12):
    appId = 'Applications/SatelliteApp/' + str(appVersion) + '.0'
    deplToSat = deployment.prepareInitial(appId, envWithSatGroup.id)
    deplToSat = deployment.prepareAutoDeployeds(deplToSat)
    deployment.createDeployTask(deplToSat)

# Create explorer.Satellite cis
for i in range(1, 2):
    satellite = 'Infrastructure/Satellite/ExpSatellite' + str(i)
    repository.create(factory.configurationItem(
        satellite,
        'explorer.Satellite',
        {
            'address': '127.0.0.1',
            'protocolPort': '8380',
            'encrypted': 'false'
        }
    ))

# Create explorer.SatelliteGroup cis
for i in range(1, 2):
    satellite = 'Infrastructure/Satellite/ExpSatelliteGroup' + str(i)
    repository.create(factory.configurationItem(satellite,'explorer.SatelliteGroup'))
