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. CSS权重的比较方法

    CSS的权重如下: !important  Infinity正无穷 行间样式  1000 id     100 class|属性|唯类 10 标签|伪元素  1 通配符   0 256进制 当出现多个 ...

  2. Oracle 左连接(+)加号用法及常用语法之间的关系

    本文目的: 通过分析左连接(+)加号的写法和一些常用语法之间的联系,了解到Oracle 加号(+)的用法 分析步骤: 1.首先创建测试表的结构: create table test_left_a (a ...

  3. iOS:SQL

    iOS虽然也有SQL,不过用得少(至少我目前是这样).大数据直接丢给后台,小的用Plist足矣. 再退一步,有FMDB,原生的也用得少了. 下面是之前学SQL时候的笔记. 1.创建 1-1).打开:  ...

  4. ios reloadsection 位置偏移

    这个问题再iOS11之前不会发生,目前仅在iOS11机型上会出现. 解决这个问题很简单,只需要你在初始化tableview的时候,把estimate的高度都设为0即可. self.tableView. ...

  5. 创建<Bean>sessionFactory错误, init方法调用失败;嵌套异常是org.hibernate.exception。

    未知原因:在Maven中hibernate映射开启了自动更新表,出现此异常 org.springframework.beans.factory.BeanCreationException: Error ...

  6. LogViewer超大文本浏览工具

    官方下载 LogViewer 是一款简单好用的log日志文件查看工具.您想要查看log日志吗?那么不妨来看看这款LogViewer .该款工具可以在短短数秒内打开上G的LOG文件,支持高亮某行文字(例 ...

  7. axios api封装

    import axios from 'axios'; import qs from 'qs'; const host = 'url' axios.defaults.baseURL = host; // ...

  8. Spring : JPA的单独使用

    title: 如何单独使用spring data jpa 引用pom文件: <dependency> <groupId>org.springframework.data< ...

  9. STM32学习日志

    今天是开学第二周周末 写这篇博客纯属是为了记住一些学到的知识,大佬勿喷.. 首先学32要知道一些选型知识,32中常用的芯片类型(某宝常卖的开发板芯片大多是这几种):C8T6,RBT6,RCT6,VET ...

  10. rails应用无法读取kafka数据报错Kafka::Error: Failed to find group coordinator

    如果确保kafka中有数据,rails应用中却无法读取到,或报如下错误: Kafka::Error: Failed to find group coordinator   一般有两种情况,解决:   ...