repository.create(factory.configurationItem('Applications/Placeholders', 'core.Directory'))
repository.create(factory.configurationItem('Environments/Placeholders', 'core.Directory'))
repository.create(factory.configurationItem('Environments/Placeholders/Dictionaries', 'core.Directory'))
repository.create(factory.configurationItem('Infrastructure/Placeholders', 'core.Directory'))

repository.create(factory.configurationItem('Applications/Placeholders/PlaceholderNoDeployedsApp', 'udm.Application'))
repository.create(
    factory.configurationItem('Applications/Placeholders/PlaceholderNoDeployedsApp/1.0',
                              'placeholder.DeploymentPackage', {'dependencyResolution': '{{dependencyResolution}}'}))
repository.create(
    factory.configurationItem('Applications/Placeholders/PlaceholderNoDeployedsApp/2.0',
                              'placeholder.DeploymentPackage', {'dependencyResolution': '{{dependencyResolution}}'}))

repository.create(factory.configurationItem('Applications/Placeholders/PlaceholderAppWithDeployeds', 'udm.Application'))

repository.create(
    factory.configurationItem('Applications/Placeholders/PlaceholderAppWithDeployeds/1.0',
                              'placeholder.DeploymentPackage'))
repository.create(
    factory.configurationItem('Applications/Placeholders/PlaceholderAppWithDeployeds/1.0/Command', 'xl.Command',
                              {'commandLine': '{{cmd}}', 'secret': '{{secret}}', 'undoCommandLine': 'sleep 3'}))

repository.create(
    factory.configurationItem('Applications/Placeholders/PlaceholderAppWithDeployeds/2.0',
                              'placeholder.DeploymentPackage'))
repository.create(
    factory.configurationItem('Applications/Placeholders/PlaceholderAppWithDeployeds/2.0/Command', 'xl.Command',
                              {'commandLine': '{{cmd}}', 'secret': '{{secret}}', 'undoCommandLine': 'sleep 3'}))

repository.create(
    factory.configurationItem('Applications/Placeholders/PlaceholderAppWithEmbeddedDeployeds', 'udm.Application'))
repository.create(
    factory.configurationItem('Applications/Placeholders/PlaceholderAppWithEmbeddedDeployeds/1.0',
                              'placeholder.DeploymentPackage'))
repository.create(
    factory.configurationItem(
        'Applications/Placeholders/PlaceholderAppWithEmbeddedDeployeds/1.0/Primary-{{primaryName}}',
        'example.ExamplePrimary'))
repository.create(
    factory.configurationItem(
        'Applications/Placeholders/PlaceholderAppWithEmbeddedDeployeds/1.0/Primary-{{primaryName}}/Embedded-{{embeddedName}}',
        'example.PrimaryEmbeddedDeployed'))
repository.create(
    factory.configurationItem(
        'Applications/Placeholders/PlaceholderAppWithEmbeddedDeployeds/1.0/Primary-{{primaryName}}/Embedded-{{embeddedName}}/Secondary-{{secondaryName}}',
        'example.EmbeddedVolumeSpec'))

repository.create(
    factory.configurationItem('Applications/Placeholders/PlaceholderAppWithEmbeddedDeployeds/2.0',
                              'placeholder.DeploymentPackage'))
repository.create(
    factory.configurationItem('Applications/Placeholders/PlaceholderAppWithEmbeddedDeployeds/2.0/PrimaryPlaceholder',
                              'example.ExamplePrimary'))

repository.create(
    factory.configurationItem(
        'Applications/Placeholders/PlaceholderAppWithEmbeddedDeployeds/2.0/Primary-{{primaryName}}',
        'example.ExamplePrimary'))
repository.create(factory.configurationItem(
    'Applications/Placeholders/PlaceholderAppWithEmbeddedDeployeds/2.0/Primary-{{primaryName}}/Embedded-{{embeddedName}}',
    'example.PrimaryEmbeddedDeployed'))
repository.create(
    factory.configurationItem(
        'Applications/Placeholders/PlaceholderAppWithEmbeddedDeployeds/2.0/Primary-{{primaryName}}/Embedded-{{embeddedName}}/Secondary-{{secondaryName}}',
        'example.EmbeddedVolumeSpec'))

repository.create(
    factory.configurationItem('Environments/Placeholders/Dictionaries/Dutch-Dictionary', 'udm.Dictionary',
                              {
                                  'entries': {
                                      'cmd': 'date',
                                      'dependencyResolution': 'LATEST',
                                      'embeddedName': 'ENL',
                                      'primaryName': 'PNL',
                                      'secondaryName': 'SNL',
                                      'usr': 'anki',
                                      'PLACE_HOLDER': 'value'
                                  },
                                  'encryptedEntries': {
                                      'secret': '%%_it_is_a_secret_%%'
                                  }
                              }))

repository.create(
    factory.configurationItem('Infrastructure/Placeholders/amsterdam-host', 'overthere.LocalHost', {'os': 'UNIX'}))

repository.create(
    factory.configurationItem('Infrastructure/Placeholders/amsterdam-host/server', 'example.Server',
                              {'home': '/tmp/amsterdam'}))

repository.create(
    factory.configurationItem('Infrastructure/Placeholders/haarlem-host', 'overthere.LocalHost', {'os': 'UNIX'}))

repository.create(
    factory.configurationItem('Infrastructure/Placeholders/hilversum-host', 'overthere.LocalHost', {'os': 'UNIX'}))

repository.create(factory.configurationItem('Environments/Placeholders/Netherlands', 'xl.Environment',
                                            {
                                                'dictionaries': [
                                                    'Environments/Placeholders/Dictionaries/Dutch-Dictionary'],
                                                'members': [
                                                    'Infrastructure/Placeholders/amsterdam-host',
                                                    'Infrastructure/Placeholders/amsterdam-host/server',
                                                    'Infrastructure/Placeholders/haarlem-host',
                                                    'Infrastructure/Placeholders/hilversum-host'
                                                ]}))

repository.create(
    factory.configurationItem('Environments/Placeholders/Dictionaries/Croatian-Dictionary', 'udm.Dictionary',
                              {
                                  'entries': {
                                      'cmd': 'ls -al',
                                      'dependencyResolution': 'EXISTING',
                                      'embeddedName': 'ECR',
                                      'primaryName': 'PCR',
                                      'secondaryName': 'SCR',
                                      'usr': 'anki',
                                      'PLACE_HOLDER': 'value'
                                  },
                                  'encryptedEntries': {
                                      'secret': '!!_it_is_a_secret_!!'
                                  }
                              }))

repository.create(
    factory.configurationItem('Infrastructure/Placeholders/zagreb-host', 'overthere.LocalHost', {'os': 'UNIX'}))

repository.create(
    factory.configurationItem('Infrastructure/Placeholders/zagreb-host/server', 'example.Server',
                              {'home': '/tmp/zagreb'}))

repository.create(
    factory.configurationItem('Infrastructure/Placeholders/split-host', 'overthere.LocalHost', {'os': 'UNIX'}))

repository.create(
    factory.configurationItem('Infrastructure/Placeholders/dubrovnik-host', 'overthere.LocalHost', {'os': 'UNIX'}))

repository.create(factory.configurationItem('Environments/Placeholders/Croatia', 'xl.Environment',
                                            {
                                                'dictionaries': [
                                                    'Environments/Placeholders/Dictionaries/Croatian-Dictionary'],
                                                'members': [
                                                    'Infrastructure/Placeholders/zagreb-host',
                                                    'Infrastructure/Placeholders/zagreb-host/server',
                                                    'Infrastructure/Placeholders/dubrovnik-host',
                                                    'Infrastructure/Placeholders/split-host'
                                                ]
                                            }))


def initialDeployment(appId, envId):
    deplApp = deployment.prepareInitial(appId, envId)
    deplApp = deployment.prepareAutoDeployeds(deplApp)
    taskId = deployment.createDeployTask(deplApp).id
    deployit.startTaskAndWait(taskId)


def upgradeDeployment(appId, envId):
    deplApp = deployment.prepareUpgrade(appId, envId)
    deplApp = deployment.prepareAutoDeployeds(deplApp)
    taskId = deployment.createDeployTask(deplApp).id
    deployit.startTaskAndWait(taskId)


initialDeployment('Applications/Placeholders/PlaceholderNoDeployedsApp/1.0', 'Environments/Placeholders/Netherlands')
upgradeDeployment('Applications/Placeholders/PlaceholderNoDeployedsApp/2.0',
                  'Environments/Placeholders/Netherlands/PlaceholderNoDeployedsApp')

initialDeployment('Applications/Placeholders/PlaceholderNoDeployedsApp/1.0', 'Environments/Placeholders/Croatia')
upgradeDeployment('Applications/Placeholders/PlaceholderNoDeployedsApp/2.0',
                  'Environments/Placeholders/Croatia/PlaceholderNoDeployedsApp')

initialDeployment('Applications/Placeholders/PlaceholderAppWithDeployeds/1.0', 'Environments/Placeholders/Netherlands')
upgradeDeployment('Applications/Placeholders/PlaceholderAppWithDeployeds/2.0',
                  'Environments/Placeholders/Netherlands/PlaceholderAppWithDeployeds')

initialDeployment('Applications/Placeholders/PlaceholderAppWithDeployeds/1.0', 'Environments/Placeholders/Croatia')
upgradeDeployment('Applications/Placeholders/PlaceholderAppWithDeployeds/2.0',
                  'Environments/Placeholders/Croatia/PlaceholderAppWithDeployeds')

initialDeployment('Applications/Placeholders/PlaceholderAppWithEmbeddedDeployeds/1.0',
                  'Environments/Placeholders/Netherlands')
upgradeDeployment('Applications/Placeholders/PlaceholderAppWithEmbeddedDeployeds/2.0',
                  'Environments/Placeholders/Netherlands/PlaceholderAppWithEmbeddedDeployeds')

initialDeployment('Applications/Placeholders/PlaceholderAppWithEmbeddedDeployeds/1.0',
                  'Environments/Placeholders/Croatia')
upgradeDeployment('Applications/Placeholders/PlaceholderAppWithEmbeddedDeployeds/2.0',
                  'Environments/Placeholders/Croatia/PlaceholderAppWithEmbeddedDeployeds')

security.grant('task#takeover', 'test_role1', ['Environments/Placeholders/Dictionaries'])
security.grant('read', 'test_role1', ['Environments/Placeholders'])
security.grant('read', 'test_role1', ['Infrastructure/Placeholders'])
