/**
 * Copyright (C) 2016 Adenops Consultants Informatique Inc.
 *
 * This file is part of the Moustack project, see http://www.moustack.org for
 * more information.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.adenops.moustack.agent.module.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.adenops.moustack.agent.DeploymentEnvironment;
import com.adenops.moustack.agent.DeploymentException;
import com.adenops.moustack.agent.config.StackProperty;
import com.adenops.moustack.agent.model.deployment.DeploymentFile;
import com.adenops.moustack.agent.model.docker.Volume;
import com.adenops.moustack.agent.module.ContainerModule;

public class Keystone extends ContainerModule {
	private static final Logger log = LoggerFactory.getLogger(Keystone.class);

	public Keystone(String name, String image, String imageTag, String imageRegistry, List<DeploymentFile> files,
			List<String> environments, List<Volume> volumes, List<String> capabilities, boolean privileged,
			List<String> devices, boolean syslog) {
		super(name, image, imageTag, imageRegistry, files, environments, volumes, capabilities, privileged, devices,
				syslog);
	}

	@Override
	public boolean deploy(DeploymentEnvironment env) throws DeploymentException {
		boolean changed = false;
		changed |= env.getMySQLClient().createDatabaseUser("keystone", "keystone",
				env.getStack().get(StackProperty.DB_KEYSTONE_PASSWORD));
		changed |= deployConfig(env);

		if (changed) {
			env.getDockerClient().discardContainer(this);
			log.info("running keystone bootstrap");
			env.getDockerClient().startEphemeralContainer(this, "root", "bootstrap-image");
		}

		env.getDockerClient().startContainer(changed, this);

		env.getKeystoneClient().createProject(env.getStack(), "admin", "Admin project");
		env.getKeystoneClient().createProject(env.getStack(), "services", "Services project");
		env.getKeystoneClient().createRole(env.getStack(), "admin");
		env.getKeystoneClient().createRole(env.getStack(), "_member_");
		env.getKeystoneClient().createProjectUser(env.getStack(), StackProperty.KEYSTONE_ADMIN_USER, "Admin user",
				"admin@localhost", StackProperty.KEYSTONE_ADMIN_PASSWORD, StackProperty.KEYSTONE_ADMIN_PROJECT);
		env.getKeystoneClient().grantProjectRole(env.getStack(), StackProperty.KEYSTONE_ADMIN_USER,
				StackProperty.KEYSTONE_ADMIN_PROJECT, StackProperty.KEYSTONE_ADMIN_ROLE);
		env.getKeystoneClient().createService(env.getStack(), "keystone", "OpenStack Identity", "identity",
				"http://%s:5000/v2.0", "http://%s:5000/v2.0", "http://%s:35357/v2.0");

		return changed;
	}

	@Override
	public void validate(DeploymentEnvironment env) throws DeploymentException {
		super.validate(env);
		env.getValidationClient().validateEndpoint(env.getStack(), "keystone", "http://%s:5000/", 300);
	}
}

@@ -3054,35 +3054,33 @@
 http://%25s:5000/v
-2.0
+3
 %22, %22http://%25s:50
@@ -3083,19 +3083,17 @@
 s:5000/v
-2.0
+3
 %22, %22http
@@ -3105,19 +3105,17 @@
 :35357/v
-2.0
+3
 %22);%0A%0A%09%09r

switch keystone endpoints to v3