<commit_before>package handlers

import (
	"github.com/go-openapi/runtime/middleware"
	coreclusters "github.com/radanalyticsio/oshinko-cli/core/clusters"
	osa "github.com/radanalyticsio/oshinko-cli/rest/helpers/authentication"
	oe "github.com/radanalyticsio/oshinko-cli/rest/helpers/errors"
	"github.com/radanalyticsio/oshinko-cli/rest/helpers/info"
	"github.com/radanalyticsio/oshinko-cli/rest/models"
	apiclusters "github.com/radanalyticsio/oshinko-cli/rest/restapi/operations/clusters"
)

const nameSpaceMsg = "cannot determine target openshift namespace"
const clientMsg = "unable to create an openshift client"

var codes map[int]int32 = map[int]int32{
	coreclusters.NoCodeAvailable: 500,
	coreclusters.ClusterConfigCode: 409,
	coreclusters.ClientOperationCode: 500,
	coreclusters.ClusterIncompleteCode: 409,
	coreclusters.NoSuchClusterCode: 404,
	coreclusters.ComponentExistsCode: 409,
}

func generalErr(err error, title, msg string, code int32) *models.ErrorResponse {
	if err != nil {
		if msg != "" {
			msg += ", reason: "
		}
		msg += err.Error()
	}
	return oe.NewSingleErrorResponse(code, title, msg)
}

func tostrptr(val string) *string {
	v := val
	return &v
}

func getErrorCode(err error) int32 {

	code := coreclusters.ErrorCode(err)
	if httpcode, ok := codes[code]; ok {
		return httpcode
	}
	return 500

}

func int64ptr(val int) *int64 {
	if val <= coreclusters.SentinelCountValue {
		return nil
	}
	ret := int64(val)
	return &ret
}

func singleClusterResponse(sc coreclusters.SparkCluster) *models.SingleCluster {

	addpod := func(p coreclusters.SparkPod) *models.ClusterModelPodsItems0 {
		pod := new(models.ClusterModelPodsItems0)
		pod.IP = tostrptr(p.IP)
		pod.Status = tostrptr(p.Status)
		pod.Type = tostrptr(p.Type)
		return pod
	}

	// Build the response
	cluster := &models.SingleCluster{&models.ClusterModel{}}
	cluster.Cluster.Name = tostrptr(sc.Name)
	cluster.Cluster.MasterURL = tostrptr(sc.MasterURL)
	cluster.Cluster.MasterWebURL = tostrptr(sc.MasterWebURL)

	cluster.Cluster.Status = tostrptr(sc.Status)

	cluster.Cluster.Pods = []*models.ClusterModelPodsItems0{}
	for i := range sc.Pods {
		cluster.Cluster.Pods = append(cluster.Cluster.Pods, addpod(sc.Pods[i]))
	}

	cluster.Cluster.Config = &models.NewClusterConfig{
		SparkMasterConfig: sc.Config.SparkMasterConfig,
		SparkWorkerConfig: sc.Config.SparkWorkerConfig,
		MasterCount: int64ptr(sc.Config.MasterCount),
		WorkerCount: int64ptr(sc.Config.WorkerCount),
		Name: sc.Config.Name,
	}
	return cluster
}

func getModelCount(val *int64) int {
	if val == nil {
		return coreclusters.SentinelCountValue
	}
	return int(*val)
}

func assignConfig(config *models.NewClusterConfig) *coreclusters.ClusterConfig {
	if config == nil {
		return nil
	}
	result := &coreclusters.ClusterConfig{
		Name: config.Name,
		MasterCount: getModelCount(config.MasterCount),
		WorkerCount: getModelCount(config.WorkerCount),
		SparkMasterConfig: config.SparkMasterConfig,
		SparkWorkerConfig: config.SparkWorkerConfig,
		SparkImage: config.SparkImage,
	}
	return result
}

// CreateClusterResponse create a cluster and return the representation
func CreateClusterResponse(params apiclusters.CreateClusterParams) middleware.Responder {

	// Do this so that we only have to specify the error code when we build ErrorResponse
	reterr := func(err *models.ErrorResponse) *apiclusters.CreateClusterDefault {
		return apiclusters.NewCreateClusterDefault(int(*err.Errors[0].Status)).WithPayload(err)
	}

	// Convenience wrapper for create failure
	fail := func(err error, msg string, code int32) *models.ErrorResponse {
		return generalErr(err, "cannot create cluster", msg, code)
	}

	const imageMsg = "cannot determine name of spark image"

	clustername := *params.Cluster.Name
<commit_msg>Finishing-up REST support.<commit_after>package handlers

import (
	"github.com/go-openapi/runtime/middleware"
	coreclusters "github.com/radanalyticsio/oshinko-cli/core/clusters"
	osa "github.com/radanalyticsio/oshinko-cli/rest/helpers/authentication"
	oe "github.com/radanalyticsio/oshinko-cli/rest/helpers/errors"
	"github.com/radanalyticsio/oshinko-cli/rest/helpers/info"
	"github.com/radanalyticsio/oshinko-cli/rest/models"
	apiclusters "github.com/radanalyticsio/oshinko-cli/rest/restapi/operations/clusters"
)

const nameSpaceMsg = "cannot determine target openshift namespace"
const clientMsg = "unable to create an openshift client"

var codes map[int]int32 = map[int]int32{
	coreclusters.NoCodeAvailable: 500,
	coreclusters.ClusterConfigCode: 409,
	coreclusters.ClientOperationCode: 500,
	coreclusters.ClusterIncompleteCode: 409,
	coreclusters.NoSuchClusterCode: 404,
	coreclusters.ComponentExistsCode: 409,
}

func generalErr(err error, title, msg string, code int32) *models.ErrorResponse {
	if err != nil {
		if msg != "" {
			msg += ", reason: "
		}
		msg += err.Error()
	}
	return oe.NewSingleErrorResponse(code, title, msg)
}

func tostrptr(val string) *string {
	v := val
	return &v
}

func getErrorCode(err error) int32 {

	code := coreclusters.ErrorCode(err)
	if httpcode, ok := codes[code]; ok {
		return httpcode
	}
	return 500

}

func int64ptr(val int) *int64 {
	if val <= coreclusters.SentinelCountValue {
		return nil
	}
	ret := int64(val)
	return &ret
}

func boolptr(val bool) *bool {
	return &val
}

func singleClusterResponse(sc coreclusters.SparkCluster) *models.SingleCluster {

	addpod := func(p coreclusters.SparkPod) *models.ClusterModelPodsItems0 {
		pod := new(models.ClusterModelPodsItems0)
		pod.IP = tostrptr(p.IP)
		pod.Status = tostrptr(p.Status)
		pod.Type = tostrptr(p.Type)
		return pod
	}

	// Build the response
	cluster := &models.SingleCluster{&models.ClusterModel{}}
	cluster.Cluster.Name = tostrptr(sc.Name)
	cluster.Cluster.MasterURL = tostrptr(sc.MasterURL)
	cluster.Cluster.MasterWebURL = tostrptr(sc.MasterWebURL)

	cluster.Cluster.Status = tostrptr(sc.Status)

	cluster.Cluster.Pods = []*models.ClusterModelPodsItems0{}
	for i := range sc.Pods {
		cluster.Cluster.Pods = append(cluster.Cluster.Pods, addpod(sc.Pods[i]))
	}

	cluster.Cluster.Config = &models.NewClusterConfig{
		SparkMasterConfig: sc.Config.SparkMasterConfig,
		SparkWorkerConfig: sc.Config.SparkWorkerConfig,
		MasterCount: int64ptr(sc.Config.MasterCount),
		WorkerCount: int64ptr(sc.Config.WorkerCount),
		Name: sc.Config.Name,
		ExposeWebUI: boolptr(sc.Config.ExposeWebUI),
	}
	return cluster
}

func getModelCount(val *int64) int {
	if val == nil {
		return coreclusters.SentinelCountValue
	}
	return int(*val)
}

func getBoolVal(val *bool) bool {
	if val == nil {
		return true
	}
	return bool(*val)
}

func assignConfig(config *models.NewClusterConfig) *coreclusters.ClusterConfig {
	if config == nil {
		return nil
	}
	result := &coreclusters.ClusterConfig{
		Name: config.Name,
		MasterCount: getModelCount(config.MasterCount),
		WorkerCount: getModelCount(config.WorkerCount),
		SparkMasterConfig: config.SparkMasterConfig,
		SparkWorkerConfig: config.SparkWorkerConfig,
		SparkImage: config.SparkImage,
		ExposeWebUI: getBoolVal(config.ExposeWebUI),
	}
	return result
}

// CreateClusterResponse create a cluster and return the representation
func CreateClusterResponse(params apiclusters.CreateClusterParams) middleware.Responder {

	// Do this so that we only have to specify the error code when we build ErrorResponse
	reterr := func(err *models.ErrorResponse) *apiclusters.CreateClusterDefault {
		return apiclusters.NewCreateClusterDefault(int(*err.Errors[0].Status)).WithPayload(err)
	}

	// Convenience wrapper for create failure
	fail := func(err error, msg string, code int32) *models.ErrorResponse {
		return generalErr(err, "cannot create cluster", msg, code)
	}

	const imageMsg = "cannot determine name of spark image"

	clustername := *params.Cluster.Name

{'fraction_non_alphanumeric': 0.11049510056730273, 'fraction_numerical': 0.012119649303764827, 'mean_word_length': 5.185805422647528, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5184927', 'n_tokens_mistral': 2754, 'n_tokens_neox': 2609, 'n_words': 543}