beego 笔记

1.开发文档

https://beego.me/docs/intro/

2.bee run projectname

demo controller

package autoscaler


import (
	"api-server/controllers"

	"github.com/golang/glog"
	"api-server/modules/helper"
	"encoding/json"
	"api-server/models/cluster"
	"github.com/astaxie/beego/validation"
	clusterautoscaler "api-server/modules/clusterautoscaler"
)

// Autoscaler Controller
type AutoscalerController struct {
	controllers.BaseController
}
const (
	CLOUD_PROVIDER_NAME      = "cloud-provider"
	CLOUD_PROVIDER_VSPHERE_NAME = CLOUD_PROVIDER_NAME + ".properties"
	CLOUD_PROVIDER_ALI_NAME = CLOUD_PROVIDER_NAME + ".ali"
	CLOUD_PROVIDER_AWS_NAME = CLOUD_PROVIDER_NAME + ".aws"
	CLOUD_PROVIDER_AZURE_NAME = CLOUD_PROVIDER_NAME + ".azure"
	AUTOSCALER_CONFIG_NAME         = "autoscaler-config"
	AUTOSCALER_CONFIG_FILE_NAME    = AUTOSCALER_CONFIG_NAME + ".properties"

)

type Deplotment struct {
	Name             string `json:"name" valid:"Required"`
	ClusterId        string `json:"cluster" valid:"Required"`
	Issa             string `json:"issa" valid:"Required"`
	TemplatePath     string `json:"templatePath" valid:"Required"`
	DatastorePath    string `json:"datastorePath" valid:"Required"`
	ResourcePoolPath string `json:"resourcePoolPath" valid:"Required"`
	TargetPath       string `json:"targetPath" valid:"Required"`
	RemoveAndDelete  string `json:"removeAndDelete" valid:"Required"`
	Max              string `json:"max" valid:"Required"`
	Min              string `json:"min" valid:"Required"`
	Email            string `json:"email" valid:"Required"`
	Duration         string `json:"duration" valid:"Required"`
}



// Create server ConfigMap
// @Title CreateServerConfigMap
// @Description Create server ConfigMap
// @Success 200 {string} success
// @Failure 500 internal server error
// @Failure 400 bad request
// @Failure 401 authorization failure
// @router /server [post]
func (c *AutoscalerController) CreateServerConfigMap() {

	method := "controller/autoscaler CreateServerConfigMap"
	// parse params
	param := &struct {
		ClusterId       string `json:"cluster" valid:"Required"`
		Issa       		string `json:"issa" valid:"Required"`
		Name            string `json:"name" valid:"Required"`
		Password        string `json:"password" valid:"Required"`
		Server          string `json:"server" valid:"Required"`
		Date            string `json:"date" valid:"Required"`
	}{}
	josnErro := json.Unmarshal(c.Ctx.Input.RequestBody, param)
	if josnErro != nil {
		glog.Errorf("%s json unmarshal failed, err: %v", method, josnErro)
		c.ErrorInternalServerError(josnErro)
		return
	}
	var valid validation.Validation
	pass, err := valid.Valid(param)
	if err != nil {
		glog.Errorln(method, "error occured when validating parameters", err)
		c.ErrorInternalServerError(err)
		return
	}
	if !pass {
		for _, detail := range valid.Errors {
			glog.Errorln(method, "parameter", detail.Key, detail)
		}
		c.ErrorBadRequest("Invalid Parameter", nil)
		return
	}

	clientset, err := helper.GetKubernetesClientset(param.ClusterId)
	if err != nil {
		glog.Errorf("%s Create clientset failed, err: %v", method, err)
		c.ErrorInternalServerError(err)
		return
	}
	
	dataMap := make(map[string]string)
	dataMap[CLOUD_PROVIDER_VSPHERE_NAME] = (string)(c.Ctx.Input.RequestBody)
	err = clusterautoscaler.CreateConfigMap(dataMap, clientset, CLOUD_PROVIDER_NAME)
	if err != nil {
		glog.Errorf("%s createConfigMap failed, err: %v", method, err)
		c.ErrorInternalServerError(err)
		return
	}

	c.ResponseSuccess("")
}

// Update server ConfigMap
// @Title UpdateServerConfigMap
// @Description Update server ConfigMap
// @Success 200 {string} success
// @Failure 500 internal server error
// @Failure 400 bad request
// @Failure 401 authorization failure
// @router /server [put]
func (c *AutoscalerController) UpdateServerConfigMap() {

	method := "controller/autoscaler UpdateServerConfigMap"
	// parse params
	param := &struct {
		ClusterId       string `json:"cluster" valid:"Required"`
		Issa       		string `json:"issa" valid:"Required"`
		Name            string `json:"name" valid:"Required"`
		Password        string `json:"password" valid:"Required"`
		Server          string `json:"server" valid:"Required"`
		Date            string `json:"date" valid:"Required"`
	}{}
	josnErro := json.Unmarshal(c.Ctx.Input.RequestBody, param)
	if josnErro != nil {
		glog.Errorf("%s json unmarshal failed, err: %v", method, josnErro)
		c.ErrorInternalServerError(josnErro)
		return
	}
	var valid validation.Validation
	pass, err := valid.Valid(param)
	if err != nil {
		glog.Errorln(method, "error occured when validating parameters", err)
		c.ErrorInternalServerError(err)
		return
	}
	if !pass {
		for _, detail := range valid.Errors {
			glog.Errorln(method, "parameter", detail.Key, detail)
		}
		c.ErrorBadRequest("Invalid Parameter", nil)
		return
	}

	clientset, err := helper.GetKubernetesClientset(param.ClusterId)
	if err != nil {
		glog.Errorf("%s Create clientset failed, err: %v", method, err)
		c.ErrorInternalServerError(err)
		return
	}
	dataMap := make(map[string]string)
	dataMap[CLOUD_PROVIDER_VSPHERE_NAME] = (string)(c.Ctx.Input.RequestBody)
	err = clusterautoscaler.UpdateConfigMap(dataMap, clientset, CLOUD_PROVIDER_NAME)
	if err != nil {
		glog.Errorf("%s updateConfigMap failed, err: %v", method, err)
		c.ErrorInternalServerError(err)
		return
	}
	c.ResponseSuccess("")
}

// Delete server ConfigMap
// @Title DeleteServerConfigMap
// @Description delete server configMap
// @Success 200 {string} success
// @Failure 500 internal server error
// @Failure 400 bad request
// @Failure 401 authorization failure
// @router /server [delete]
func (c *AutoscalerController) DeleteServerConfigMap() {

	method := "controller/autoscaler DeleteServerConfigMap"
	// parse params
	clusterId := c.GetString("cluster")
	configmapName := c.GetString("configname")
	clientset, err := helper.GetKubernetesClientset(clusterId)
	if err != nil {
		glog.Errorf("%s Create clientset failed, err: %v", method, err)
		c.ErrorInternalServerError(err)
		return
		return
	}
	err = clusterautoscaler.DeleteConfigMap(clientset, configmapName)
	if err != nil {
		glog.Errorf("%s deleteConfigMap failed, err: %v", method, err)
		c.ErrorInternalServerError(err)
		return
	}
	c.ResponseSuccess("")
}

// get all server configmap in all cluster
// @Title ListAllConfigMap
// @Description get all server configmap
// @Success 200 {string} success
// @Failure 500 internal server error
// @Failure 400 bad request
// @Failure 401 authorization failure
// @router /server [get]
func (c *AutoscalerController) ListAllConfigMap() {

	method := "controller/autoscaler ListAllConfigMap"
	dataselect := helper.DataSelectFromContext(c.Ctx)
	_, clusterModels, _, _ := cluster.GetClusterModelList(dataselect)
	relsult, err := clusterautoscaler.ListAllConfigMap(clusterModels,CLOUD_PROVIDER_NAME)
	if err != nil {
		glog.Errorf("%s listAllConfigMap failed, err: %v", method, err)
		c.ErrorInternalServerError(err)
		return
	}
	c.ResponseSuccess(relsult)
}

// filter cluster issa
// @Title FilterCluster
// @Description get cluster issa status
// @Success 200 {string} success
// @Failure 500 internal server error
// @Failure 400 bad request
// @Failure 401 authorization failure
// @router /cluster [get]
func (c *AutoscalerController) FilterCluster() {

	method := "controller/autoscaler FilterCluster"
	dataselect := helper.DataSelectFromContext(c.Ctx)
	_, clusterModels, _, _ := cluster.GetClusterModelList(dataselect)
	relsult, err := clusterautoscaler.FilterCLuster(clusterModels)
	if err != nil {
		glog.Errorf("%s get cluster issa status failed, err: %v", method, err)
		c.ErrorInternalServerError(err)
		return
	}
	c.ResponseSuccess(relsult)
}

// Create app ConfigMap
// @Title CreateAppConfig
// @Description Create app ConfigMap
// @Success 200 {string} success
// @Failure 500 internal server error
// @Failure 400 bad request
// @Failure 401 authorization failure
// @router /app [post]
func (c *AutoscalerController) CreateAppConfig() {

	method := "controller/autoscaler CreateAppConfig"
	// parse params
	/*param := &struct {
		Name                	 string `json:"name" valid:"Required"`
		ClusterId                string `json:"cluster" valid:"Required"`
		Issa               	     string `json:"issa" valid:"Required"`
		TemplatePath             string `json:"templatePath" valid:"Required"`
		DatastorePath            string `json:"datastorePath" valid:"Required"`
		RecourcePoolPath         string `json:"recourcePoolPath" valid:"Required"`
		TargetPath               string `json:"targetPath" valid:"Required"`
		RemoveAndDelete          string `json:"removeAndDelete" valid:"Required"`
		Max                      string `json:"max" valid:"Required"`
		Min                      string `json:"min" valid:"Required"`
		Email                    string `json:"email" valid:"Required"`
		Duration                 string `json:"duration" valid:"Required"`
	}{}*/
	param := Deplotment{}
	err := json.Unmarshal(c.Ctx.Input.RequestBody, &param)
	if err != nil {
		glog.Errorf("%s json unmarshal failed, err: %v", method, err)
		c.ErrorInternalServerError(err)
		return
	}
	var valid validation.Validation
	pass, err := valid.Valid(param)
	if err != nil {
		glog.Errorln(method, "error occured when validating parameters", err)
		c.ErrorInternalServerError(err)
		return
	}
	if !pass {
		for _, detail := range valid.Errors {
			glog.Errorln(method, "parameter", detail.Key, detail)
		}
		c.ErrorBadRequest("Invalid Parameter", nil)
		return
	}
	clientset, err := helper.GetKubernetesClientset(param.ClusterId)
	if err != nil {
		glog.Error("create clientset erro")
		c.ErrorInternalServerError(err)
		return
	}
	dataMap := make(map[string]string)
	dataMap[AUTOSCALER_CONFIG_FILE_NAME] = (string)(c.Ctx.Input.RequestBody)
	err = clusterautoscaler.CreateConfigMap(dataMap,clientset,AUTOSCALER_CONFIG_NAME)
	if err != nil {
		glog.Errorf("%s createConfigMap failed, err: %v", method, err)
		c.ErrorInternalServerError(err)
		return
	}
	clusterautoscaler.CreateApp(clientset,param.Duration)
	c.ResponseSuccess("")
}

// get autoscaler configmap in all cluster
// @Title ListAppConfigMap
// @Description get all autoscaler configmap
// @Success 200 {string} success
// @Failure 500 internal server error
// @Failure 400 bad request
// @Failure 401 authorization failure
// @router /app [get]
func (c *AutoscalerController) ListAppConfigMap() {

	method := "controller/autoscaler ListAppConfigMap"
	dataselect := helper.DataSelectFromContext(c.Ctx)
	_, clusterModels, _, _ := cluster.GetClusterModelList(dataselect)
	relsult, err := clusterautoscaler.ListAllConfigMap(clusterModels,AUTOSCALER_CONFIG_NAME)
	if err != nil {
		glog.Errorf("%s listAllConfigMap failed, err: %v", method, err)
		c.ErrorInternalServerError(err)
		return
	}
	c.ResponseSuccess(relsult)
}

// Delete app ConfigMap
// @Title DeleteAppConfigMap
// @Description delete server configMap
// @Success 200 {string} success
// @Failure 500 internal server error
// @Failure 400 bad request
// @Failure 401 authorization failure
// @router /app [delete]
func (c *AutoscalerController) DeleteAppConfigMap() {

	method := "controller/autoscaler DeleteAppConfigMap"
	// parse params
	clusterId := c.GetString("cluster")
	clientset, err := helper.GetKubernetesClientset(clusterId)
	if err != nil {
		glog.Errorf("%s Create clientset failed, err: %v", method, err)
		c.ErrorInternalServerError(err)
		return
	}
	err = clusterautoscaler.DeleteConfigMap(clientset, AUTOSCALER_CONFIG_NAME)
	if err != nil {
		glog.Errorf("%s deleteConfigMap failed, err: %v", method, err)
		c.ErrorInternalServerError(err)
		return
	}
	err = clusterautoscaler.DeleteApp(clientset)
	if err != nil {
		glog.Errorf("%s deleteApp failed, err: %v", method, err)
		c.ErrorInternalServerError(err)
		return
	}
	c.ResponseSuccess("")
}

// Update app ConfigMap
// @Title UpdataAppConfig
// @Description Update app ConfigMap
// @Success 200 {string} success
// @Failure 500 internal server error
// @Failure 400 bad request
// @Failure 401 authorization failure
// @router /app [put]
func (c *AutoscalerController) UpdateAppConfig() {

	method := "controller/autoscaler UpdateAppConfig"
	// parse params
	param := Deplotment{}
	err := json.Unmarshal(c.Ctx.Input.RequestBody, &param)
	if err != nil {
		glog.Errorf("%s json unmarshal failed, err: %v", method, err)
		c.ErrorInternalServerError(err)
		return
	}
	var valid validation.Validation
	pass, err := valid.Valid(param)
	if err != nil {
		glog.Errorln(method, "error occured when validating parameters", err)
		c.ErrorInternalServerError(err)
		return
	}
	if !pass {
		for _, detail := range valid.Errors {
			glog.Errorln(method, "parameter", detail.Key, detail)
		}
		c.ErrorBadRequest("Invalid Parameter", nil)
		return
	}
	clientset, err := helper.GetKubernetesClientset(param.ClusterId)
	if err != nil {
		glog.Error("create clientset erro")
		c.ErrorInternalServerError(err)
		return
	}
	dataMap := make(map[string]string)
	dataMap[AUTOSCALER_CONFIG_FILE_NAME] = (string)(c.Ctx.Input.RequestBody)
	err = clusterautoscaler.UpdateConfigMap(dataMap,clientset,AUTOSCALER_CONFIG_NAME)
	if err != nil {
		glog.Errorf("%s updateConfigMap failed, err: %v", method, err)
		c.ErrorInternalServerError(err)
		return
	}
	err = clusterautoscaler.RestartApp(clientset,param.Duration)
	if err != nil {
		glog.Errorf("%s restartApp failed, err: %v", method, err)
		c.ErrorInternalServerError(err)
		return
	}
	c.ResponseSuccess("")
}

 

posted @ 2018-04-09 20:19 丨Mars 阅读(...) 评论(...) 编辑 收藏