forked from lei.chen/myxce
Merge Go frontend (apiserver + xcecli) into meson build
This commit is contained in:
parent
43dc12a36c
commit
c9dc8c4676
205 changed files with 30793 additions and 328 deletions
359
src/frontend/xcecli/cmd/resources/list.go
Normal file
359
src/frontend/xcecli/cmd/resources/list.go
Normal file
|
|
@ -0,0 +1,359 @@
|
|||
package resources
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"xcecli/common/output"
|
||||
"xcecli/common/request"
|
||||
"xcecli/config"
|
||||
"xcecli/models"
|
||||
|
||||
logger "xcecli/config"
|
||||
resource_filter "xcecli/filters"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type ResourcesDistQuerier struct {
|
||||
configManager *config.ConfigManager
|
||||
filter *resource_filter.ResourceDistFilter
|
||||
fields string
|
||||
distType string
|
||||
jsonOutput bool
|
||||
noHeader bool
|
||||
verbose bool
|
||||
wideFormat bool // wide format
|
||||
}
|
||||
|
||||
func NewResourcesDistQuerier(configManager *config.ConfigManager) *ResourcesDistQuerier {
|
||||
return &ResourcesDistQuerier{
|
||||
configManager: configManager,
|
||||
filter: &resource_filter.ResourceDistFilter{},
|
||||
}
|
||||
}
|
||||
|
||||
func (rq *ResourcesDistQuerier) ParseCmdArgs(cmd *cobra.Command, args []string) error {
|
||||
// No additional processing needed, all parameters are bound in flags
|
||||
if len(args) > 0 {
|
||||
rq.filter.ResName = strings.Join(args, ",")
|
||||
}
|
||||
|
||||
// Define mutual exclusions for verbose flag
|
||||
if rq.verbose {
|
||||
conflicts := make([]string, 0)
|
||||
|
||||
if rq.wideFormat {
|
||||
conflicts = append(conflicts, "--wided (-w)")
|
||||
}
|
||||
|
||||
if len(conflicts) > 0 {
|
||||
return fmt.Errorf("flag --verbose (-l) cannot be used with: %s\n\n%s",
|
||||
strings.Join(conflicts, ", "), cmd.UsageString())
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ResourcesDefQuerier handles querying resource definition information
|
||||
type ResourcesDefQuerier struct {
|
||||
configManager *config.ConfigManager
|
||||
filter *resource_filter.ResourceDefFilter
|
||||
fields string
|
||||
jsonOutput bool
|
||||
noHeader bool
|
||||
verbose bool
|
||||
wideFormat bool // wide format
|
||||
}
|
||||
|
||||
func NewResourcesDefQuerier(configManager *config.ConfigManager) *ResourcesDefQuerier {
|
||||
return &ResourcesDefQuerier{
|
||||
configManager: configManager,
|
||||
filter: &resource_filter.ResourceDefFilter{},
|
||||
}
|
||||
}
|
||||
|
||||
func (rq *ResourcesDefQuerier) ParseCmdArgs(cmd *cobra.Command, args []string) error {
|
||||
// No additional processing needed, all parameters are bound in flags
|
||||
if len(args) > 0 {
|
||||
rq.filter.ResName = strings.Join(args, ",")
|
||||
}
|
||||
|
||||
// Define mutual exclusions for verbose flag
|
||||
if rq.verbose {
|
||||
conflicts := make([]string, 0)
|
||||
|
||||
if rq.wideFormat {
|
||||
conflicts = append(conflicts, "--wided (-w)")
|
||||
}
|
||||
|
||||
if len(conflicts) > 0 {
|
||||
return fmt.Errorf("flag --verbose (-l) cannot be used with: %s\n\n%s",
|
||||
strings.Join(conflicts, ", "), cmd.UsageString())
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// NewListResourcesDefCmd creates a command for listing resource definition information
|
||||
func NewListResourcesDefCmd(configManager *config.ConfigManager) *cobra.Command {
|
||||
resourcesDefQuerier := NewResourcesDefQuerier(configManager)
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "Query the resources definition info",
|
||||
Long: `Query the resources definition info.
|
||||
Example:
|
||||
xce resources definition list # query the definition info`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if err := resourcesDefQuerier.ParseCmdArgs(cmd, args); err != nil {
|
||||
fmt.Println(strings.ToLower(err.Error()))
|
||||
return nil
|
||||
}
|
||||
if err := resourcesDefQuerier.RunResourcesDefList(); err != nil {
|
||||
fmt.Println(strings.ToLower(err.Error()))
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
// Add command line parameters
|
||||
flags := cmd.Flags()
|
||||
|
||||
flags.BoolVarP(&resourcesDefQuerier.verbose, "verbose", "l", false, "show resource detail")
|
||||
flags.BoolVarP(&resourcesDefQuerier.wideFormat, "wided", "w", false, "output in wide format")
|
||||
flags.BoolVarP(&resourcesDefQuerier.jsonOutput, "json", "", false, "output in JSON format")
|
||||
flags.BoolVarP(&resourcesDefQuerier.noHeader, "noheader", "n", false, "output no header")
|
||||
return cmd
|
||||
}
|
||||
|
||||
func (rq *ResourcesDefQuerier) RunResourcesDefList() error {
|
||||
// Get configuration
|
||||
|
||||
// Get server information
|
||||
serverInfo, _ := rq.configManager.GetServerInfo()
|
||||
// create API client
|
||||
apiClient := request.NewAPIClient(serverInfo.URL)
|
||||
apiClient.SetRootCAs(serverInfo.CACert)
|
||||
|
||||
// Create query parameters
|
||||
params := make(map[string]string)
|
||||
|
||||
// Get filter string
|
||||
filterStr, err := rq.filter.HandleResourceDefFilter()
|
||||
|
||||
if err != nil {
|
||||
errMsg := request.ExtractErrorMessage(err)
|
||||
return fmt.Errorf("failed to get filter: [%s]", errMsg)
|
||||
} else if filterStr != "" {
|
||||
params["filter"] = filterStr
|
||||
}
|
||||
|
||||
// Field mapping
|
||||
fieldsMap := models.ResourceDefFieldsMap
|
||||
|
||||
if rq.verbose && rq.fields == "" {
|
||||
for header := range fieldsMap {
|
||||
rq.fields += header + ","
|
||||
}
|
||||
rq.fields = strings.TrimSuffix(rq.fields, ",")
|
||||
}
|
||||
// Process field selection
|
||||
if rq.fields != "" {
|
||||
rq.fields = request.CleanFields(rq.fields)
|
||||
mappedFields, err := request.CheckAndMapFields(rq.fields, fieldsMap)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
params["fields"] = strings.Join(mappedFields, ",")
|
||||
}
|
||||
|
||||
// create page printer
|
||||
pagePrinter := output.NewPagePrinter(output.DataTypeResourcesDef, output.WithFields(rq.fields),
|
||||
output.WithFormatJson(rq.jsonOutput), output.WithNoHeader(rq.noHeader),
|
||||
output.WithVerbose(rq.verbose), output.WithWideFormat(rq.wideFormat))
|
||||
|
||||
// Record if the first page has been processed
|
||||
isFirstPage := true
|
||||
|
||||
var (
|
||||
resp models.ResourceDefResponse
|
||||
loopCount int
|
||||
pageToken string
|
||||
)
|
||||
|
||||
// Loop to get all pages of data
|
||||
for {
|
||||
if loopCount > 0 && pageToken == "" {
|
||||
break
|
||||
}
|
||||
err = apiClient.GetRequestFromAPIServer(serverInfo.Token, pageToken, serverInfo.CACert, "/xce/v1/resources/definition", &resp, params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(resp.Data.Rows) == 0 {
|
||||
if isFirstPage {
|
||||
fmt.Println("no resource definition found")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// Process the first page
|
||||
if isFirstPage {
|
||||
// Start printing process
|
||||
pagePrinter.StartPrinting()
|
||||
// Prepare for printing, calculate column width and table header, etc.
|
||||
pagePrinter.PrepareForPrinting(&resp)
|
||||
isFirstPage = false
|
||||
}
|
||||
|
||||
// Print the current page
|
||||
pagePrinter.PrintPage(&resp)
|
||||
|
||||
// Update page token and loop counter
|
||||
pageToken = resp.Data.Page_token
|
||||
loopCount++
|
||||
}
|
||||
|
||||
// Finish printing
|
||||
pagePrinter.Finish()
|
||||
|
||||
logger.Debug("resource definition response completed", zap.Int("pages", loopCount))
|
||||
return nil
|
||||
}
|
||||
|
||||
// NewListResourcesDistCmd creates a command for listing resource distribution information
|
||||
func NewListResourcesDistCmd(configManager *config.ConfigManager) *cobra.Command {
|
||||
resourcesDistQuerier := NewResourcesDistQuerier(configManager)
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "Query the resources distribution info",
|
||||
Long: `Query the resources info, support static and dynamic type.
|
||||
Example:
|
||||
xce resources distribution list # query the distribution info`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if err := resourcesDistQuerier.ParseCmdArgs(cmd, args); err != nil {
|
||||
fmt.Println(strings.ToLower(err.Error()))
|
||||
return nil
|
||||
}
|
||||
if err := resourcesDistQuerier.RunResourcesDistList(); err != nil {
|
||||
fmt.Println(strings.ToLower(err.Error()))
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
// Add command line parameters
|
||||
flags := cmd.Flags()
|
||||
flags.StringVarP(&resourcesDistQuerier.distType, "type", "t", "", "static or dynamic")
|
||||
flags.BoolVarP(&resourcesDistQuerier.wideFormat, "wided", "w", false, "output in wide format")
|
||||
flags.BoolVarP(&resourcesDistQuerier.jsonOutput, "json", "", false, "output in JSON format")
|
||||
flags.BoolVarP(&resourcesDistQuerier.noHeader, "noheader", "n", false, "output no header")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func (rq *ResourcesDistQuerier) RunResourcesDistList() error {
|
||||
// Get configuration
|
||||
|
||||
// Get server information
|
||||
serverInfo, _ := rq.configManager.GetServerInfo()
|
||||
// create API client
|
||||
apiClient := request.NewAPIClient(serverInfo.URL)
|
||||
apiClient.SetRootCAs(serverInfo.CACert)
|
||||
|
||||
// Create query parameters
|
||||
params := make(map[string]string)
|
||||
|
||||
// Get filter string
|
||||
filterStr, err := rq.filter.HandleResourceDistFilter()
|
||||
|
||||
if rq.distType != "" {
|
||||
if rq.distType != "static" && rq.distType != "dynamic" {
|
||||
return fmt.Errorf("invalid type, must be 'static' or 'dynamic'")
|
||||
} else {
|
||||
params["type"] = rq.distType
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
errMsg := request.ExtractErrorMessage(err)
|
||||
return fmt.Errorf("failed to get filter: [%s]", errMsg)
|
||||
} else if filterStr != "" {
|
||||
params["filter"] = filterStr
|
||||
}
|
||||
|
||||
// Field mapping
|
||||
fieldsMap := models.ResourceDistFieldsMap
|
||||
|
||||
if rq.verbose && rq.fields == "" {
|
||||
for header := range fieldsMap {
|
||||
rq.fields += header + ","
|
||||
}
|
||||
rq.fields = strings.TrimSuffix(rq.fields, ",")
|
||||
}
|
||||
// Process field selection
|
||||
if rq.fields != "" {
|
||||
rq.fields = request.CleanFields(rq.fields)
|
||||
mappedFields, err := request.CheckAndMapFields(rq.fields, fieldsMap)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
params["fields"] = strings.Join(mappedFields, ",")
|
||||
}
|
||||
|
||||
// create page printer
|
||||
pagePrinter := output.NewPagePrinter(output.DataTypeResourcesDist, output.WithFields(rq.fields),
|
||||
output.WithFormatJson(rq.jsonOutput), output.WithNoHeader(rq.noHeader),
|
||||
output.WithVerbose(rq.verbose), output.WithWideFormat(rq.wideFormat))
|
||||
|
||||
// Record if the first page has been processed
|
||||
isFirstPage := true
|
||||
|
||||
var (
|
||||
resp models.ResourceDistResponse
|
||||
loopCount int
|
||||
pageToken string
|
||||
)
|
||||
|
||||
// Loop to get all pages of data
|
||||
for {
|
||||
if loopCount > 0 && pageToken == "" {
|
||||
break
|
||||
}
|
||||
err = apiClient.GetRequestFromAPIServer(serverInfo.Token, pageToken, serverInfo.CACert, "/xce/v1/resources/distribution", &resp, params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(resp.Data.Rows) == 0 {
|
||||
if isFirstPage {
|
||||
fmt.Println("no resource found")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// Process the first page
|
||||
if isFirstPage {
|
||||
// Start printing process
|
||||
pagePrinter.StartPrinting()
|
||||
// Prepare for printing, calculate column width and table header, etc.
|
||||
pagePrinter.PrepareForPrinting(&resp)
|
||||
isFirstPage = false
|
||||
}
|
||||
|
||||
// Print the current page
|
||||
pagePrinter.PrintPage(&resp)
|
||||
|
||||
// Update page token and loop counter
|
||||
pageToken = resp.Data.Page_token
|
||||
loopCount++
|
||||
}
|
||||
|
||||
// Finish printing
|
||||
pagePrinter.Finish()
|
||||
|
||||
logger.Debug("resource dirstribution response completed", zap.Int("pages", loopCount))
|
||||
return nil
|
||||
}
|
||||
56
src/frontend/xcecli/cmd/resources/resources.go
Normal file
56
src/frontend/xcecli/cmd/resources/resources.go
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
package resources
|
||||
|
||||
import (
|
||||
"xcecli/config"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// NewResourcesCmd creates a command for querying resource information
|
||||
func NewResourcesCmd(configManager *config.ConfigManager) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "resources",
|
||||
GroupID: "query",
|
||||
Short: "Manage resources",
|
||||
Long: `Manage resources in the system.
|
||||
Use 'xce resources distribution list' to query resources distribution information.
|
||||
Use 'xce resources definition list' to query resources definition information.`,
|
||||
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// Display help when no subcommand is specified
|
||||
cmd.Help()
|
||||
},
|
||||
}
|
||||
|
||||
// Add distribution subcommand
|
||||
distributionCmd := &cobra.Command{
|
||||
Use: "distribution",
|
||||
Short: "Manage resource distribution",
|
||||
Long: `Manage resource distribution information.
|
||||
Use 'xce resources distribution list' to query resources distribution information.`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// Display help when no subcommand is specified
|
||||
cmd.Help()
|
||||
},
|
||||
}
|
||||
distributionCmd.AddCommand(NewListResourcesDistCmd(configManager))
|
||||
|
||||
// Add definition subcommand
|
||||
definitionCmd := &cobra.Command{
|
||||
Use: "definition",
|
||||
Short: "Manage resource definition",
|
||||
Long: `Manage resource definition information.
|
||||
Use 'xce resources definition list' to query resources definition information.`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// Display help when no subcommand is specified
|
||||
cmd.Help()
|
||||
},
|
||||
}
|
||||
definitionCmd.AddCommand(NewListResourcesDefCmd(configManager))
|
||||
|
||||
// Add subcommands to main command
|
||||
cmd.AddCommand(distributionCmd)
|
||||
cmd.AddCommand(definitionCmd)
|
||||
|
||||
return cmd
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue