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. Spring入门第二课:Spring配置Bean的细节

    1.配置bean的作用域: 通过配置scope属性可以bean的作用域,参数有 prototype.request.session.singleton. 1)singleton为单例,IoC容器只会创 ...

  2. indexPathForCell的事

    UITableView *tableview = (UITableView *)self.superview; NSIndexPath *indexPath = [tableview indexPat ...

  3. Git单人本地仓库操作

    本地仓库是个.git隐藏文件 以下为演示Git单人本地仓库操作 1.安装git sudo apt-get install git 密码:skylark 2.查看git安装结果 git 3.创建项目 在 ...

  4. Windows远程常见问题

    1.window2003远程桌面“已达最大连接数”解决:1)mstsc /v:(此处为服务器IP) /console   任务管理器注销已断开用户 mstsc /v:192.168.4.3 /cons ...

  5. Eclipse切换字体颜色

    打开window-preferences

  6. jQuery获取Select option 选择的Text和 Value

    获取一组radio被选中项的值:var item = $('input[name=items][checked]').val();获取select被选中项的文本var item = $("s ...

  7. scala 获取当前时间的两种方式

    在编写程序时,有时需要获取当前时间,这在记录异常信息.获取程序运行耗时很有用处 方式一: val time1=System.currentTimeMillis() 这种方式获取的是程序运行到此的毫秒数 ...

  8. python学习笔记:第4天 列表和元组

    目录 基本数据类型:列表 基本数据类型:元组 补充知识 基本数据类型:列表 1. 列表的介绍 列表也是python的基础的数据类型之一,类似于Java中的数组一样,可以存放很多元素.列表是用括号括起来 ...

  9. 如何配置 SpaceVim

    本文将系统地介绍如何配置 SpaceVim,配置 SpaceVim 主要包括以下几个内容: 设置 SpaceVim 选项 启动/禁用模块 添加自定义插件 添加自定义按键映射以及插件配置 设置Space ...

  10. 20145207 ms11_050漏洞攻击

    实验过程 查看两台主机ip,并ping通 启动msf,进入该漏洞模块,查看漏洞的信息 exploit生成网站地址,开启服务