Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(697)

Unified Diff: logdog/appengine/coordinator/config/projects.go

Issue 2575383002: Add server/cache support to gaeconfig. (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: logdog/appengine/coordinator/config/projects.go
diff --git a/logdog/appengine/coordinator/config/projects.go b/logdog/appengine/coordinator/config/projects.go
index 7a67a9d759ae12abbf77630cecd68225db10430d..53d89e6f1d685fc6091dc7ae7dc8d13a5fcfa71a 100644
--- a/logdog/appengine/coordinator/config/projects.go
+++ b/logdog/appengine/coordinator/config/projects.go
@@ -6,18 +6,18 @@ package config
import (
"fmt"
- "strings"
+ "sort"
- "github.com/golang/protobuf/proto"
"github.com/luci/gae/service/info"
- "github.com/luci/luci-go/common/config"
+ ccfg "github.com/luci/luci-go/common/config"
log "github.com/luci/luci-go/common/logging"
"github.com/luci/luci-go/logdog/api/config/svcconfig"
+ "github.com/luci/luci-go/server/config"
+ "github.com/luci/luci-go/server/config/textproto"
+
"golang.org/x/net/context"
)
-const maxProjectWorkers = 32
-
// ProjectConfigPath returns the path of the project-specific configuration.
// This path should be used with a project config set.
//
@@ -35,7 +35,7 @@ func ProjectConfigPath(c context.Context) string {
// be loaded.
// - Some other error if an error occurred that does not fit one of the
// previous categories.
-func ProjectConfig(c context.Context, project config.ProjectName) (*svcconfig.ProjectConfig, error) {
+func ProjectConfig(c context.Context, project ccfg.ProjectName) (*svcconfig.ProjectConfig, error) {
if project == "" {
return nil, config.ErrNoConfig
}
@@ -43,8 +43,9 @@ func ProjectConfig(c context.Context, project config.ProjectName) (*svcconfig.Pr
// Get the config from the config service. If the configuration doesn't exist,
// this will return config.ErrNoConfig.
configSet, configPath := config.ProjectConfigSet(project), ProjectConfigPath(c)
- cfg, err := config.GetConfig(c, configSet, configPath, false)
- if err != nil {
+
+ var pcfg svcconfig.ProjectConfig
+ if err := config.Get(c, config.AsService, configSet, configPath, textproto.Message(&pcfg), nil); err != nil {
log.Fields{
log.ErrorKey: err,
"project": project,
@@ -53,69 +54,50 @@ func ProjectConfig(c context.Context, project config.ProjectName) (*svcconfig.Pr
}.Errorf(c, "Failed to load project configuration content.")
return nil, err
}
-
- pcfg, err := unmarshalProjectConfig(cfg)
- if err != nil {
- log.Fields{
- log.ErrorKey: err,
- "project": project,
- "configSet": cfg.ConfigSet,
- "path": cfg.Path,
- "contentHash": cfg.ContentHash,
- }.Errorf(c, "Failed to unmarshal project configuration.")
- return nil, ErrInvalidConfig
- }
- return pcfg, nil
+ return &pcfg, nil
}
-// AllProjectConfigs returns the project configurations for all projects that
-// have a configuration.
-//
-// If a project's configuration fails to load, an error will be logged and the
-// project will be omitted from the output map.
-func AllProjectConfigs(c context.Context) (map[config.ProjectName]*svcconfig.ProjectConfig, error) {
- // TODO: This endpoint is generally slow. Even though there is memcache-based
- // config cache, this really should be loaded from a more failsafe cache like
- // datastore to protect against config service outages.
- configs, err := config.GetProjectConfigs(c, ProjectConfigPath(c), false)
- if err != nil {
+// ProjectNames returns a sorted list of the names of all of the projects
+// that the supplied authority can view.
+func ProjectNames(c context.Context, a config.Authority) ([]ccfg.ProjectName, error) {
+ configPath := ProjectConfigPath(c)
+
+ var metas []*config.Meta
+ if err := config.GetAll(c, a, config.Project, configPath, nil, &metas); err != nil {
log.WithError(err).Errorf(c, "Failed to load project configs.")
return nil, err
}
- result := make(map[config.ProjectName]*svcconfig.ProjectConfig, len(configs))
- for _, cfg := range configs {
- // Identify the project by removng the "projects/" prefix.
- project := config.ProjectName(strings.TrimPrefix(cfg.ConfigSet, "projects/"))
- if err := project.Validate(); err != nil {
- log.Fields{
- log.ErrorKey: err,
- "configSet": cfg.ConfigSet,
- }.Errorf(c, "Invalid project name returned.")
- continue
- }
-
- // Unmarshal the project's configuration.
- pcfg, err := unmarshalProjectConfig(&cfg)
- if err != nil {
- log.Fields{
- log.ErrorKey: err,
- "configSet": cfg.ConfigSet,
- "path": cfg.Path,
- "contentHash": cfg.ContentHash,
- }.Errorf(c, "Failed to unmarshal project configuration.")
- continue
+ // Iterate through our Metas and extract the project names.
+ projects := make([]ccfg.ProjectName, 0, len(metas))
+ for _, meta := range metas {
+ projectName, _, _ := config.ParseProjectConfigSet(meta.ConfigSet)
+ if projectName != "" {
+ projects = append(projects, projectName)
}
-
- result[project] = pcfg
}
- return result, nil
+ sort.Sort(projectNameSlice(projects))
+ return projects, nil
}
-func unmarshalProjectConfig(cfg *config.Config) (*svcconfig.ProjectConfig, error) {
- var pcfg svcconfig.ProjectConfig
- if err := proto.UnmarshalText(cfg.Content, &pcfg); err != nil {
- return nil, err
- }
- return &pcfg, nil
+// ActiveProjects returns a full list of all config service projects with
+// LogDog project configurations.
+//
+// The list will be alphabetically sorted.
+func ActiveProjects(c context.Context) ([]ccfg.ProjectName, error) {
+ return ProjectNames(c, config.AsService)
}
+
+// ActiveUserProjects returns a full list of all config service projects with
+// LogDog project configurations that the current user can see.
+//
+// The list will be alphabetically sorted.
+func ActiveUserProjects(c context.Context) ([]ccfg.ProjectName, error) {
+ return ProjectNames(c, config.AsUser)
+}
+
+type projectNameSlice []ccfg.ProjectName
+
+func (s projectNameSlice) Len() int { return len(s) }
+func (s projectNameSlice) Less(i, j int) bool { return s[i] < s[j] }
+func (s projectNameSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }

Powered by Google App Engine
This is Rietveld 408576698