// CreatePod creates a new pod and service using passed in values
// kubeURL - the URL to the kube
// podInfo - the params used to configure the pod
// return an error if anything goes wrong
func CreatePod(kubeURL string, podInfo template.KubePodParams) error {
	//client, err := getHttpClient()

	logit.Info.Println("creating pod " + podInfo.ID)

	//use a pod template to build the pod definition
	data, err := template.KubeNodePod(podInfo)
	if err != nil {
		logit.Error.Println("CreatePod:" + err.Error())
		return err
	}
	logit.Info.Println(string(data[:]))

	//var bodyType = "application/json"
	//var url = kubeURL + "/api/v1/pods"
	//logit.Info.Println("url is " + url)

	// POST POD
	/*
		resp, err := client.Post(url, bodyType, bytes.NewReader(data))
		if err != nil {
			logit.Error.Println(err.Error())
			return err
		}
		defer resp.Body.Close()

		// Dump response
		data, err = ioutil.ReadAll(resp.Body)
		if err != nil {
			logit.Error.Println(err.Error())
			return err
		}
		logit.Info.Println(string(data))
	*/

	return nil

}
func provisionImpl(dbConn *sql.DB, params *cpmserverapi.DockerRunRequest, PROFILE string, standby bool) error {
	logit.Info.Println("PROFILE: provisionImpl starts 1")

	var errorStr string
	//make sure the container name is not already taken
	_, err := admindb.GetContainerByName(dbConn, params.ContainerName)
	if err != nil {
		if err != sql.ErrNoRows {
			return err
		}
	} else {
		errorStr = "container name" + params.ContainerName + " already used can't provision"
		logit.Error.Println("Provision error" + errorStr)
		return errors.New(errorStr)
	}

	//create the container by constructing a template and calling openshift

	params.CPU, params.MEM, err = getDockerResourceSettings(dbConn, PROFILE)
	if err != nil {
		logit.Error.Println("Provision: problem in getting profiles call" + err.Error())
		return err
	}

	//remove any existing pods and services with this name
	var username = "******"
	var password = "******"
	var objectName = params.ContainerName
	var objectType = "pod"

	err = OpenshiftDelete(username, password, objectName, objectType)
	if err != nil {
		logit.Info.Println("Provision:" + err.Error())
	}

	objectName = params.ContainerName
	err = OpenshiftDelete(username, password, objectName, objectType)
	if err != nil {
		logit.Info.Println("Provision:" + err.Error())
	}

	podInfo := template.KubePodParams{
		NAME:                 params.ContainerName,
		ID:                   params.ContainerName,
		PODID:                params.ContainerName,
		CPU:                  params.CPU,
		MEM:                  params.MEM,
		IMAGE:                params.Image,
		VOLUME:               params.PGDataPath,
		PORT:                 "13000",
		BACKUP_NAME:          "",
		BACKUP_SERVERNAME:    "",
		BACKUP_SERVERIP:      "",
		BACKUP_SCHEDULEID:    "",
		BACKUP_PROFILENAME:   "",
		BACKUP_CONTAINERNAME: "",
		BACKUP_PATH:          "",
		BACKUP_HOST:          "",
		BACKUP_PORT:          "",
		BACKUP_USER:          "",
		BACKUP_SERVER_URL:    "",
	}

	//generate the pod template
	var podTemplateData []byte
	podTemplateData, err = template.KubeNodePod(podInfo)
	if err != nil {
		logit.Error.Println("Provision:" + err.Error())
		return err
	}
	logit.Info.Println("pod template=" + string(podTemplateData[:]))

	//create the pod
	file, err := ioutil.TempFile("/tmp", "openshift-template")
	if err != nil {
		logit.Error.Println("Provision:" + err.Error())
		return err
	}
	defer os.Remove(file.Name())
	err = ioutil.WriteFile(file.Name(), podTemplateData, 0644)
	if err != nil {
		logit.Error.Println("Provision:" + err.Error())
		return err
	}

	err = OpenshiftCreate(username, password, file.Name())
	if err != nil {
		logit.Info.Println("Provision:" + err.Error())
	}

	var pgport admindb.Setting
	pgport, err = admindb.GetSetting(dbConn, "PG-PORT")
	if err != nil {
		logit.Error.Println("Provision:PG-PORT setting error " + err.Error())
		return err
	}

	//generate the admin service template
	serviceInfo := template.KubeServiceParams{
		SERVICENAME: params.ContainerName,
		NAME:        params.ContainerName,
		PORT:        "10001",
		DBPORT:      pgport.Value,
	}

	//create the admin service template
	var serviceTemplateData []byte
	serviceTemplateData, err = template.KubeNodeService(serviceInfo)
	if err != nil {
		logit.Error.Println("Provision:" + err.Error())
		return err
	}
	logit.Info.Println("service template=" + string(serviceTemplateData[:]))

	file, err = ioutil.TempFile("/tmp", "openshift-template")
	if err != nil {
		logit.Error.Println("Provision:" + err.Error())
		return err
	}
	defer os.Remove(file.Name())
	err = ioutil.WriteFile(file.Name(), serviceTemplateData, 0644)
	if err != nil {
		logit.Error.Println("Provision:" + err.Error())
		return err
	}

	//create the service
	err = OpenshiftCreate(username, password, file.Name())
	if err != nil {
		logit.Info.Println("Provision:" + err.Error())
	}

	dbnode := admindb.Container{}
	dbnode.ID = ""
	dbnode.Name = params.ContainerName
	dbnode.Image = params.Image
	dbnode.ClusterID = "-1"
	dbnode.ProjectID = params.ProjectID
	dbnode.ServerID = params.ServerID

	if params.Standalone == "true" {
		dbnode.Role = "standalone"
	} else {
		dbnode.Role = "unassigned"
	}

	var strid int
	strid, err = admindb.InsertContainer(dbConn, dbnode)
	newid := strconv.Itoa(strid)
	if err != nil {
		logit.Error.Println("Provision:" + err.Error())
		return err
	}
	dbnode.ID = newid

	//register default db users on the new node
	err = createDBUsers(dbConn, dbnode)

	return err

}
// GetPods gets all the pods
// kubeURL - the URL to the kube
// podInfo - the params used to configure the pod
// return an error if anything goes wrong
func GetPods(kubeURL string, podInfo template.KubePodParams) error {
	logit.Info.Println("creating pod " + podInfo.ID)

	/**
	client, err := getHttpClient()
	if err != nil {
		logit.Error.Println(err.Error())
		return err
	}
	*/

	//use a pod template to build the pod definition
	data, err := template.KubeNodePod(podInfo)
	if err != nil {
		logit.Error.Println("CreatePod:" + err.Error())
		return err
	}

	logit.Info.Println(string(data[:]))

	// Do GET something
	/**
		resp, err2 := client.Get(kubeURL + "/api/v1/pods")
		if err2 != nil {
			logit.Error.Println(err2.Error())
			return err2
		}
		defer resp.Body.Close()

		// Dump response
		data, err3 := ioutil.ReadAll(resp.Body)
		if err3 != nil {
			logit.Error.Println(err3.Error())
			return err3
		}
		logit.Info.Println(string(data))

		return nil
	}

	// GetPod gets information about a single pod from kube
	// kubeURL - the URL to the kube
	// podName - the pod name
	// return an error if anything goes wrong
	func GetPod(kubeURL string, podName string) (MyPod, error) {
		var podInfo MyPod

		logit.Info.Println("getting pod info " + podName)

		/**
		client, err := getHttpClient()
		if err != nil {
			logit.Error.Println(err.Error())
			return podInfo, err
		}

	// Do GET something
	resp, err2 := client.Get(kubeURL + "/api/v1/pods/" + podName)
	if err2 != nil {
		logit.Error.Println(err2.Error())
		return podInfo, err2
	}
	defer resp.Body.Close()

	// Dump response
	data, err3 := ioutil.ReadAll(resp.Body)
	if err3 != nil {
		logit.Error.Println(err3.Error())
		return podInfo, err3
	}
	logit.Info.Println(string(data))
	err2 = json.Unmarshal(data, &podInfo)
	if err2 != nil {
		logit.Error.Println("error in unmarshalling pod " + err2.Error())
		return podInfo, err2
	}

	*/
	return nil
}