Skip to content

Commit

Permalink
apigateway: expose identity/image/k8s usage rpc method
Browse files Browse the repository at this point in the history
  • Loading branch information
zexi committed Jun 9, 2020
1 parent 734d7e9 commit 686cc78
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 5 deletions.
11 changes: 11 additions & 0 deletions cmd/climc/shell/compute/usages.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/onecloud/pkg/mcclient/modules"
"yunion.io/x/onecloud/pkg/mcclient/modules/k8s"
)

type GeneralUsageOptions struct {
Expand Down Expand Up @@ -178,4 +179,14 @@ func init() {
printObject(result)
return nil
})

type K8sUsageOptions struct{}
R(&K8sUsageOptions{}, "k8s-usage", "Show general usage of k8s", func(s *mcclient.ClientSession, args *K8sUsageOptions) error {
result, err := k8s.Usages.GetUsage(s, nil)
if err != nil {
return err
}
printObject(result)
return nil
})
}
7 changes: 7 additions & 0 deletions pkg/apigateway/handler/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,12 @@
package handler

import (
"yunion.io/x/onecloud/pkg/mcclient/modules"
_ "yunion.io/x/onecloud/pkg/mcclient/modules/cloudnet"
"yunion.io/x/onecloud/pkg/mcclient/modules/k8s"
)

func init() {
modules.InitUsages()
modules.Usages.RegisterManager(modules.UsageManagerK8s, k8s.Usages)
}
4 changes: 4 additions & 0 deletions pkg/mcclient/modules/k8s/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func NewResourceManager(keyword, keywordPlural string, columns, adminColumns *Co
return &ResourceManager{man}
}

func (m ResourceManager) GetBaseManager() modulebase.ResourceManager {
return *m.ResourceManager
}

type IClusterResourceManager interface {
modulebase.Manager
GetRaw(s *mcclient.ClientSession, id string, params *jsonutils.JSONDict) (jsonutils.JSONObject, error)
Expand Down
53 changes: 53 additions & 0 deletions pkg/mcclient/modules/k8s/usage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package k8s

import (
"fmt"

"yunion.io/x/jsonutils"

"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/onecloud/pkg/mcclient/modulebase"
)

var (
Usages *SUsageManager
)

func init() {
Usages = NewUsageManager()
}

type SUsageManager struct {
*ResourceManager
}

func NewUsageManager() *SUsageManager {
return &SUsageManager{
ResourceManager: NewResourceManager("usage", "usages", NewColumns(), NewColumns()),
}
}

func (m *SUsageManager) GetUsage(s *mcclient.ClientSession, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
url := "/usages"
if params != nil {
query := params.QueryString()
if len(query) > 0 {
url = fmt.Sprintf("%s?%s", url, query)
}
}
return modulebase.Get(m.GetBaseManager(), s, url, "usage")
}
57 changes: 52 additions & 5 deletions pkg/mcclient/modules/mod_usages.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,28 @@ import (
"yunion.io/x/onecloud/pkg/mcclient/modulebase"
)

type TUsageManager string

const (
UsageManagerImage TUsageManager = "image"
UsageManagerIdentity TUsageManager = "identity"
UsageManagerK8s TUsageManager = "k8s"
)

type IUsageManager interface {
GetUsage(s *mcclient.ClientSession, params jsonutils.JSONObject) (jsonutils.JSONObject, error)
}

type UsageManager struct {
modulebase.ResourceManager
managers map[TUsageManager]IUsageManager
}

func (this *UsageManager) RegisterManager(manType TUsageManager, man IUsageManager) {
if this.managers == nil {
this.managers = make(map[TUsageManager]IUsageManager, 0)
}
this.managers[manType] = man
}

func (this *UsageManager) GetGeneralUsage(session *mcclient.ClientSession, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
Expand All @@ -46,14 +66,41 @@ func (this *UsageManager) GetGeneralUsage(session *mcclient.ClientSession, param
return modulebase.Get(this.ResourceManager, session, url, this.Keyword)
}

func (this *UsageManager) GetManagerByType(t TUsageManager) IUsageManager {
return this.managers[t]
}

func (this *UsageManager) getManagerUsage(manType TUsageManager, s *mcclient.ClientSession, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
return this.GetManagerByType(manType).GetUsage(s, params)
}

func (this *UsageManager) GetK8sUsage(s *mcclient.ClientSession, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
return this.getManagerUsage(UsageManagerK8s, s, params)
}

func (this *UsageManager) GetIdentityUsage(s *mcclient.ClientSession, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
return this.getManagerUsage(UsageManagerIdentity, s, params)
}

func (this *UsageManager) GetImageUsage(s *mcclient.ClientSession, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
return this.getManagerUsage(UsageManagerImage, s, params)
}

var (
Usages UsageManager
Usages *UsageManager
)

func init() {
Usages = UsageManager{NewComputeManager("usage", "usages",
[]string{},
[]string{})}
Usages = &UsageManager{
ResourceManager: NewComputeManager("usage", "usages",
[]string{},
[]string{}),
}

registerCompute(Usages)
}

registerCompute(&Usages)
func InitUsages() {
Usages.RegisterManager(UsageManagerImage, &Images)
Usages.RegisterManager(UsageManagerIdentity, &IdentityUsages)
}

0 comments on commit 686cc78

Please sign in to comment.