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("")
}

beego 笔记的更多相关文章

  1. beego笔记

    beego学习笔记一:创建第一个beego Web项目 Go语言beego框架快速搭建体验五分钟讲解01 beego框架图文简介五分钟讲解02 beego框架图文简介五分钟讲解03-go语言简单方式操 ...

  2. Beego学习笔记——Logs

    日志处理 这是一个用来处理日志的库,它的设计思路来自于database/sql,目前支持的引擎有file.console.net.smtp,可以通过如下方式进行安装: go get github.co ...

  3. Beego学习笔记——Config

    配置文件解析 这是一个用来解析文件的库,它的设计思路来自于database/sql,目前支持解析的文件格式有ini.json.xml.yaml,可以通过如下方式进行安装: go get github. ...

  4. Beego学习笔记——开始

    beego简介 beego是一个快速开发Go应用的http框架,他可以用来快速开发API.Web.后端服务等各种应用,是一个RESTFul的框架,主要设计灵感来源于tornado.sinatra.fl ...

  5. Beego学习笔记

    Beego学习笔记 Go 路由(Controller) 路由就是根据用户的请求找到需要执行的函数或者controller. Get /v1/shop/nike ShopController Get D ...

  6. golang学习笔记16 beego orm 数据库操作

    golang学习笔记16 beego orm 数据库操作 beego ORM 是一个强大的 Go 语言 ORM 框架.她的灵感主要来自 Django ORM 和 SQLAlchemy. 目前该框架仍处 ...

  7. golang学习笔记12 beego table name `xxx` repeat register, must be unique 错误问题

    golang学习笔记12 beego table name `xxx` repeat register, must be unique 错误问题 今天测试了重新建一个项目生成新的表,然后复制到旧的项目 ...

  8. golang学习笔记10 beego api 用jwt验证auth2 token 获取解码信息

    golang学习笔记10 beego api 用jwt验证auth2 token 获取解码信息 Json web token (JWT), 是为了在网络应用环境间传递声明而执行的一种基于JSON的开放 ...

  9. golang学习笔记9 beego nginx 部署 nginx 反向代理 golang web

    golang学习笔记9 beego nginx 部署 nginx 反向代理 golang web Nginx 部署 - beego: 简约 & 强大并存的 Go 应用框架https://bee ...

随机推荐

  1. 如何编写及运行JS

    JS也是一种脚本语言,他可以有两种方式在HTML页面进行引入,一种是外联,一种是内部.       外联JS的写法为: <script src="相对路径"></ ...

  2. CentOS中的 yum upgrade 和 yum update 的区别

    通过 man yum 的帮助信息了解 yum update 和 yum upgrade: update If run without any packages, update will update ...

  3. Python-爬虫小计

    # -*-coding:utf8-*-import requestsfrom bs4 import BeautifulSoupimport timeimport osimport urllibimpo ...

  4. php_Trait

    * Trait Answer* Trait : 关键字 Trait* 使用 use* 我的理解 是为了解决php不能多继承的一个处理方式* 在使用的时候 可以让两个不相关的类 产生联系* Trait ...

  5. Old ST-LINK firmware detected.do you want to upgrade it?已解决,stlink升级

    错误提示: 原因: stlink固件太旧了, 解决办法: 升级stlink固件,升级方法: 1. 下载st link utility工具. 官网下载:https://www.st.com/conten ...

  6. Qt——模态、非模态

    模态: 只能操作对话框非模态:要使用 QDialog *_d = new QDialog();_d->setattribute(Qt::WA_DeleteOnClose);_d->show ...

  7. Uber优步北京第四组奖励政策

    优步北京第四组: 定义为2015年7月20日至今激活的司机(以优步后台数据显示为准) 滴滴快车单单2.5倍,注册地址:http://www.udache.com/如何注册Uber司机(全国版最新最详细 ...

  8. Java:String、StringBuffer、StringBuilder

    一.String 1. String类是final类,意味着String类不能被继承,它的成员方法都默认为final方法.在早期的JVM版本中,被final修饰的方法会转为内嵌调用来提升执行效率.从J ...

  9. 基于套接字通信的简单练习(FTP)

    本项目基于c/s架构开发(采用套接字通信,使用TCP协议) FTP-Socket"""__author:rianley cheng""" 功 ...

  10. Android ObjectOutputStream Serializable引发的血案

    遇到一个问题 安装后第二次进app,闪退 重现步骤 [前置条件] 打包分支:dev_7.13 手机:vivo NEX 8.1.0 [步骤] 安装三星app----同意用户协议进入书城---连续点击ba ...