climc: support k8s service v3.2

This commit is contained in:
Zexi Li
2020-04-14 17:26:29 +08:00
parent 524ccc5c5f
commit 0b62292cd4
7 changed files with 200 additions and 9 deletions
+22 -5
View File
@@ -606,28 +606,45 @@ func (f *ResourceHandlers) patchJointHandler(ctx context.Context, w http.Respons
}
}
// batch update Joint
// * batch update Joint
// * put specific
// /<resname>/<resid>/<resname2>
// /<resname>/<resid>/<spec>
func (f *ResourceHandlers) batchUpdateJointHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
req := newRequest(ctx, w, r).WithMod1().WithMod2()
req := newRequest(ctx, w, r).WithMod1()
if err := req.Error(); err != nil {
httperrors.GeneralServerError(w, err)
return
}
session := req.Session()
module := req.Mod1()
module2 := req.Mod2()
body := req.Body()
query := req.Query()
idlist := fetchIdList(query, w)
if idlist == nil {
if idlist, _ := query.GetArray("id"); len(idlist) == 0 {
// do put specific
spec := req.ResName2()
obj, e := module.PutSpecific(session, req.ResID(), spec, query, body)
if e != nil {
httperrors.GeneralServerError(w, e)
} else {
appsrv.SendJSON(w, obj)
}
return
}
req = req.WithMod2()
if err := req.Error(); err != nil {
httperrors.GeneralServerError(w, err)
return
}
module2 := req.Mod2()
jmod, e := modulebase.GetJointModule2(session, module, module2)
if e != nil { // update joint
httperrors.GeneralServerError(w, e)
return
}
idlist := fetchIdList(query, w)
ret := jmod.BatchUpdate(session, req.ResID(), idlist, query, body)
w.WriteHeader(207)
appsrv.SendJSON(w, modulebase.SubmitResults2JSON(ret))
+1
View File
@@ -109,6 +109,7 @@ type Manager interface {
BatchCreateInContexts(session *mcclient.ClientSession, params jsonutils.JSONObject, count int, ctxs []ManagerContext) []SubmitResult
Update(session *mcclient.ClientSession, id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error)
Put(session *mcclient.ClientSession, id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error)
PutSpecific(session *mcclient.ClientSession, id string, spec string, query jsonutils.JSONObject, params jsonutils.JSONObject) (jsonutils.JSONObject, error)
PutInContext(session *mcclient.ClientSession, id string, params jsonutils.JSONObject, ctx Manager, ctxid string) (jsonutils.JSONObject, error)
PutInContexts(session *mcclient.ClientSession, id string, params jsonutils.JSONObject, ctxs []ManagerContext) (jsonutils.JSONObject, error)
BatchUpdate(session *mcclient.ClientSession, idlist []string, params jsonutils.JSONObject) []SubmitResult
+26
View File
@@ -409,6 +409,32 @@ func (this *ResourceManager) PutInContexts(session *mcclient.ClientSession, id s
return this.filterSingleResult(session, result, nil)
}
func (this *ResourceManager) PutSpecific(session *mcclient.ClientSession, id string, spec string, query, body jsonutils.JSONObject) (jsonutils.JSONObject, error) {
return this.PutSpecificInContexts(session, id, spec, query, body, nil)
}
func (this *ResourceManager) PutSpecificInContext(session *mcclient.ClientSession, id string, spec string, query, body jsonutils.JSONObject, ctx Manager, ctxid string) (jsonutils.JSONObject, error) {
return this.PutSpecificInContexts(session, id, spec, query, body, []ManagerContext{{ctx, ctxid}})
}
func (this *ResourceManager) PutSpecificInContexts(session *mcclient.ClientSession, id string, spec string, query, body jsonutils.JSONObject, ctxs []ManagerContext) (jsonutils.JSONObject, error) {
path := fmt.Sprintf("/%s/%s/%s", this.ContextPath(ctxs), url.PathEscape(id), url.PathEscape(spec))
if query != nil {
qs := query.QueryString()
if len(qs) > 0 {
path = fmt.Sprintf("%s?%s", path, qs)
}
}
if body != nil {
body = this.params2Body(session, body, this.Keyword)
}
result, err := this._put(session, path, body, this.Keyword)
if err != nil {
return nil, err
}
return this.filterSingleResult(session, result, nil)
}
func (this *ResourceManager) BatchUpdate(session *mcclient.ClientSession, idlist []string, params jsonutils.JSONObject) []SubmitResult {
return this.BatchPutInContexts(session, idlist, params, nil)
}
+14
View File
@@ -35,6 +35,12 @@ func NewResourceManager(keyword, keywordPlural string, columns, adminColumns *Co
return &ResourceManager{man}
}
type IClusterResourceManager interface {
modulebase.Manager
GetRaw(s *mcclient.ClientSession, id string, params *jsonutils.JSONDict) (jsonutils.JSONObject, error)
UpdateRaw(s *mcclient.ClientSession, id string, query, body *jsonutils.JSONDict) (jsonutils.JSONObject, error)
}
type ClusterResourceManager struct {
*ResourceManager
}
@@ -50,6 +56,14 @@ func (man ClusterResourceManager) GetCluster(obj jsonutils.JSONObject) interface
return cluster
}
func (man ClusterResourceManager) GetRaw(s *mcclient.ClientSession, id string, params *jsonutils.JSONDict) (jsonutils.JSONObject, error) {
return man.GetSpecific(s, id, "rawdata", params)
}
func (man ClusterResourceManager) UpdateRaw(s *mcclient.ClientSession, id string, query, rawdata *jsonutils.JSONDict) (jsonutils.JSONObject, error) {
return man.PutSpecific(s, id, "rawdata", query, rawdata)
}
type MetaResourceManager struct {
*ClusterResourceManager
nameGetter
+54 -2
View File
@@ -14,12 +14,64 @@
package k8s
var (
Logs *ResourceManager
import (
"fmt"
"yunion.io/x/jsonutils"
"yunion.io/x/onecloud/pkg/mcclient/modules"
)
var (
Logs *ResourceManager
Events *EventManager
)
type EventManager struct {
*NamespaceResourceManager
}
func init() {
Logs = NewResourceManager("event", "events",
NewColumns("id", "ops_time", "obj_id", "obj_type", "obj_name", "user", "user_id", "tenant", "tenant_id", "owner_tenant_id", "action", "notes"),
NewColumns())
Events = &EventManager{
NamespaceResourceManager: NewNamespaceResourceManager("k8s_event", "k8s_events",
NewNamespaceCols("Source", "Type", "Action", "Object", "Message"),
NewColumns()),
}
modules.Register(Events)
}
func (m EventManager) GetSource(obj jsonutils.JSONObject) interface{} {
src, _ := obj.GetString("source")
return src
}
func (m EventManager) GetType(obj jsonutils.JSONObject) interface{} {
typ, _ := obj.GetString("type")
return typ
}
func (m EventManager) GetAction(obj jsonutils.JSONObject) interface{} {
act, _ := obj.Get("action")
return act
}
func (m EventManager) GetObject(obj jsonutils.JSONObject) interface{} {
refObj, _ := obj.Get("involvedObject")
if refObj == nil {
return ""
}
kind, _ := refObj.GetString("kind")
name, _ := refObj.GetString("name")
return fmt.Sprintf("%s/%s", kind, name)
}
func (m EventManager) GetMessage(obj jsonutils.JSONObject) interface{} {
msg, _ := obj.GetString("message")
return msg
}