UpdateVolumeType

This commit is contained in:
panda-zxs 2019-12-18 10:18:26 +08:00
parent 6e989d95be
commit 7491be0df3
8 changed files with 140 additions and 9 deletions

View File

@ -55,6 +55,8 @@ func (v2 *V2) Routes() chi.Router {
r.Get("/gateway/ips", controller.GetGatewayIPs) r.Get("/gateway/ips", controller.GetGatewayIPs)
r.Get("/gateway/ports", controller.GetManager().GetAvailablePort) r.Get("/gateway/ports", controller.GetManager().GetAvailablePort)
r.Get("/volume-options", controller.VolumeOptions) r.Get("/volume-options", controller.VolumeOptions)
r.Post("/volume-options", controller.VolumeSetVar)
r.Delete("/volume-options/{volume_type}", controller.DeleteVolumeType)
return r return r
} }

View File

@ -21,7 +21,10 @@ package controller
import ( import (
"net/http" "net/http"
"github.com/jinzhu/gorm"
"github.com/go-chi/chi"
"github.com/goodrain/rainbond/api/handler" "github.com/goodrain/rainbond/api/handler"
api_model "github.com/goodrain/rainbond/api/model"
httputil "github.com/goodrain/rainbond/util/http" httputil "github.com/goodrain/rainbond/util/http"
) )
@ -54,3 +57,70 @@ func VolumeOptions(w http.ResponseWriter, r *http.Request) {
} }
httputil.ReturnSuccess(r, w, volumetypeOptions) httputil.ReturnSuccess(r, w, volumetypeOptions)
} }
// VolumeSetVar set volume option
func VolumeSetVar(w http.ResponseWriter, r *http.Request) {
// swagger:operation POST /v2/volume-options v2 volumeOptions
//
// 创建可用存储驱动模型列表
//
// get volume-options
//
// ---
// consumes:
// - application/json
// - application/x-protobuf
//
// produces:
// - application/json
// - application/xml
//
// responses:
// default:
// schema:
// description: 统一返回格式
volumeType := api_model.VolumeTypeOptionsStruct{}
if ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &volumeType, nil); !ok {
return
}
err := handler.GetVolumeTypeHandler().SetVolumeType(&volumeType)
if err != nil {
httputil.ReturnError(r, w, 500, err.Error())
return
}
httputil.ReturnSuccess(r, w, nil)
}
// DeleteVolumeType delete volume option
func DeleteVolumeType(w http.ResponseWriter, r *http.Request) {
// swagger:operation POST /v2/volume-options v2 volumeOptions
//
// 删除可用存储驱动模型
//
// get volume-options
//
// ---
// consumes:
// - application/json
// - application/x-protobuf
//
// produces:
// - application/json
// - application/xml
//
// responses:
// default:
// schema:
// description: 统一返回格式
volumeType := chi.URLParam(r, "volume_type")
err := handler.GetVolumeTypeHandler().DeleteVolumeType(volumeType)
if err != nil {
if err == gorm.ErrRecordNotFound {
httputil.ReturnError(r, w, 404, "not found")
return
}
httputil.ReturnError(r, w, 500, err.Error())
return
}
httputil.ReturnSuccess(r, w, nil)
}

View File

@ -19,6 +19,9 @@
package handler package handler
import ( import (
"strings"
"encoding/json"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
api_model "github.com/goodrain/rainbond/api/model" api_model "github.com/goodrain/rainbond/api/model"
"github.com/goodrain/rainbond/api/util" "github.com/goodrain/rainbond/api/util"
@ -37,6 +40,7 @@ type VolumeTypeHandler interface {
GetAllStorageClasses() ([]*pb.StorageClassDetail, error) GetAllStorageClasses() ([]*pb.StorageClassDetail, error)
VolumeTypeAction(action, volumeTypeID string) error VolumeTypeAction(action, volumeTypeID string) error
DeleteVolumeType(volumeTypeID string) error DeleteVolumeType(volumeTypeID string) error
SetVolumeType(vtm *api_model.VolumeTypeOptionsStruct) error
} }
var defaultVolumeTypeHandler VolumeTypeHandler var defaultVolumeTypeHandler VolumeTypeHandler
@ -125,6 +129,49 @@ func (vta *VolumeTypeAction) VolumeTypeAction(action, volumeTypeID string) error
} }
// DeleteVolumeType delte volume type // DeleteVolumeType delte volume type
func (vta *VolumeTypeAction) DeleteVolumeType(volumeTypeID string) error { func (vta *VolumeTypeAction) DeleteVolumeType(volumeType string) error {
db.GetManager().VolumeTypeDao().DeleteModelByVolumeTypes(volumeType)
return nil return nil
} }
// SetVolumeType set volume type
func (vta *VolumeTypeAction) SetVolumeType(vol *api_model.VolumeTypeOptionsStruct) error {
var accessMode []string
var sharePolicy []string
var backupPolicy []string
jsonStr, _ := json.Marshal(vol.CapacityValidation)
if vol.AccessMode == nil{
accessMode[1] = "RWO"
}else {
accessMode = vol.AccessMode
}
if vol.SharePolicy == nil{
sharePolicy[1] = "exclusive"
}else {
sharePolicy = vol.SharePolicy
}
if vol.BackupPolicy == nil{
backupPolicy[1] = "exclusive"
}else {
backupPolicy = vol.BackupPolicy
}
dbVolume := dbmodel.TenantServiceVolumeType{}
dbVolume.VolumeType = vol.VolumeType
dbVolume.NameShow = vol.NameShow
dbVolume.VolumeProviderName = vol.VolumeProviderName
dbVolume.CapacityValidation = string(jsonStr)
dbVolume.Description = vol.Description
dbVolume.AccessMode = strings.Join(accessMode, ",")
dbVolume.SharePolicy = strings.Join(sharePolicy, ",")
dbVolume.BackupPolicy = strings.Join(backupPolicy, ",")
dbVolume.ReclaimPolicy = vol.ReclaimPolicy
dbVolume.VolumeBindingMode = vol.VolumeBindingMode
dbVolume.AllowVolumeExpansion = *vol.AllowVolumeExpansion
dbVolume.Sort = vol.Sort
dbVolume.Enable = vol.Enable
err := db.GetManager().VolumeTypeDao().AddModel(&dbVolume)
return err
}

View File

@ -34,18 +34,19 @@ type VolumeBestRespStruct struct {
// VolumeTypeOptionsStruct volume option struct // VolumeTypeOptionsStruct volume option struct
type VolumeTypeOptionsStruct struct { type VolumeTypeOptionsStruct struct {
VolumeType string `json:"volume_type"` VolumeType string `json:"volume_type" validate:"volume_type|required"`
NameShow string `json:"name_show"` NameShow string `json:"name_show" validate:"string,max=64"`
VolumeProviderName string `json:"volume_provider_name"` VolumeProviderName string `json:"volume_provider_name" validate:"string,max=64"`
CapacityValidation map[string]interface{} `json:"capacity_validation"` CapacityValidation map[string]interface{} `json:"capacity_validation"`
Description string `json:"description"` Description string `json:"description" validate:"string,max=1024"`
AccessMode []string `json:"access_mode"` AccessMode []string `json:"access_mode"`
SharePolicy []string `json:"share_policy"` //共享模式 SharePolicy []string `json:"share_policy"` //共享模式
BackupPolicy []string `json:"backup_policy"` // 备份策略 BackupPolicy []string `json:"backup_policy"` // 备份策略
ReclaimPolicy string `json:"reclaim_policy"` // 回收策略,delete, retain, recyle ReclaimPolicy string `json:"reclaim_policy" validate:"string,max=20"` // 回收策略,delete, retain, recyle
VolumeBindingMode string `json:"volume_binding_mode"` // 绑定模式,Immediate,WaitForFirstConsumer VolumeBindingMode string `json:"volume_binding_mode" validate:"string,max=20"` // 绑定模式,Immediate,WaitForFirstConsumer
AllowVolumeExpansion *bool `json:"allow_volume_expansion"` // 是否支持扩展 AllowVolumeExpansion *bool `json:"allow_volume_expansion"` // 是否支持扩展
Sort int `json:"sort"` // 排序 Sort int `json:"sort"` // 排序
Enable bool `json:"enable"` // 是否生效
} }
// VolumeProviderDetail volume provider detail // VolumeProviderDetail volume provider detail

View File

@ -65,6 +65,7 @@ type AppDao interface {
// VolumeTypeDao volume type dao // VolumeTypeDao volume type dao
type VolumeTypeDao interface { type VolumeTypeDao interface {
Dao Dao
DeleteModelByVolumeTypes(volumeType string) error
GetAllVolumeTypes() ([]*model.TenantServiceVolumeType, error) GetAllVolumeTypes() ([]*model.TenantServiceVolumeType, error)
GetVolumeTypeByType(vt string) (*model.TenantServiceVolumeType, error) GetVolumeTypeByType(vt string) (*model.TenantServiceVolumeType, error)
} }

View File

@ -36,7 +36,7 @@ type TenantServiceVolumeType struct {
// 绑定模式 // 绑定模式
VolumeBindingMode string `gorm:"column:volume_binding_mode; size:20" json:"volume_binding_mode"` VolumeBindingMode string `gorm:"column:volume_binding_mode; size:20" json:"volume_binding_mode"`
// 是否可扩容 // 是否可扩容
AllowVolumeExpansion bool `gorm:"column:allow_volume_expansion" json:"allow_volume_expansion"` AllowVolumeExpansion bool `gorm:"column:allow_volume_expansion; default 0" json:"allow_volume_expansion"`
// 备份策略 // 备份策略
BackupPolicy string `gorm:"column:backup_policy; size:128" json:"backup_policy"` BackupPolicy string `gorm:"column:backup_policy; size:128" json:"backup_policy"`
//读写模式 //读写模式
@ -45,6 +45,8 @@ type TenantServiceVolumeType struct {
SharePolicy string `gorm:"share_policy; size:128" json:"share_policy"` SharePolicy string `gorm:"share_policy; size:128" json:"share_policy"`
// 排序 // 排序
Sort int `gorm:"sort; default:9999" json:"sort"` Sort int `gorm:"sort; default:9999" json:"sort"`
// 是否生效
Enable bool `gorm:"enable; default:1" json:"enable"`
} }
// TableName 表名 // TableName 表名

View File

@ -67,3 +67,11 @@ func (vtd *VolumeTypeDaoImpl) GetVolumeTypeByType(vt string) (*model.TenantServi
} }
return volumeType, nil return volumeType, nil
} }
// DeleteModelByVolumeTypes delete volume by type
func (vtd *VolumeTypeDaoImpl) DeleteModelByVolumeTypes(volumeType string) error {
if err := vtd.DB.Where("volume_type=?", volumeType).Delete(&model.TenantServiceVolumeType{}).Error; err != nil {
return err
}
return nil
}