| OLD | NEW |
| 1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package coordinator | 5 package coordinator |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "strings" | 8 "strings" |
| 9 | 9 |
| 10 "github.com/luci/gae/service/info" | 10 "github.com/luci/gae/service/info" |
| 11 » luciConfig "github.com/luci/luci-go/common/config" | 11 » commonConfig "github.com/luci/luci-go/common/config" |
| 12 » log "github.com/luci/luci-go/common/logging" | |
| 13 "github.com/luci/luci-go/logdog/api/config/svcconfig" | 12 "github.com/luci/luci-go/logdog/api/config/svcconfig" |
| 14 » "github.com/luci/luci-go/logdog/appengine/coordinator/config" | 13 |
| 15 "golang.org/x/net/context" | 14 "golang.org/x/net/context" |
| 16 ) | 15 ) |
| 17 | 16 |
| 18 const ( | 17 const ( |
| 19 // projectNamespacePrefix is the datastore namespace prefix for project | 18 // projectNamespacePrefix is the datastore namespace prefix for project |
| 20 // namespaces. | 19 // namespaces. |
| 21 projectNamespacePrefix = "luci." | 20 projectNamespacePrefix = "luci." |
| 22 ) | 21 ) |
| 23 | 22 |
| 24 // ProjectNamespace returns the AppEngine namespace for a given luci-config | 23 // ProjectNamespace returns the AppEngine namespace for a given luci-config |
| 25 // project name. | 24 // project name. |
| 26 func ProjectNamespace(project luciConfig.ProjectName) string { | 25 func ProjectNamespace(project commonConfig.ProjectName) string { |
| 27 return projectNamespacePrefix + string(project) | 26 return projectNamespacePrefix + string(project) |
| 28 } | 27 } |
| 29 | 28 |
| 30 // ProjectFromNamespace returns the current project installed in the supplied | 29 // ProjectFromNamespace returns the current project installed in the supplied |
| 31 // Context's namespace. | 30 // Context's namespace. |
| 32 // | 31 // |
| 33 // If the namespace does not have a project namespace prefix, this function | 32 // If the namespace does not have a project namespace prefix, this function |
| 34 // will return an empty string. | 33 // will return an empty string. |
| 35 func ProjectFromNamespace(ns string) luciConfig.ProjectName { | 34 func ProjectFromNamespace(ns string) commonConfig.ProjectName { |
| 36 if !strings.HasPrefix(ns, projectNamespacePrefix) { | 35 if !strings.HasPrefix(ns, projectNamespacePrefix) { |
| 37 return "" | 36 return "" |
| 38 } | 37 } |
| 39 » return luciConfig.ProjectName(ns[len(projectNamespacePrefix):]) | 38 » return commonConfig.ProjectName(ns[len(projectNamespacePrefix):]) |
| 40 } | 39 } |
| 41 | 40 |
| 42 // CurrentProject returns the current project based on the currently-loaded | 41 // CurrentProject returns the current project based on the currently-loaded |
| 43 // namespace. | 42 // namespace. |
| 44 // | 43 // |
| 45 // If there is no current namespace, or if the current namespace is not a valid | 44 // If there is no current namespace, or if the current namespace is not a valid |
| 46 // project namespace, an empty string will be returned. | 45 // project namespace, an empty string will be returned. |
| 47 func CurrentProject(c context.Context) luciConfig.ProjectName { | 46 func CurrentProject(c context.Context) commonConfig.ProjectName { |
| 48 if ns := info.GetNamespace(c); ns != "" { | 47 if ns := info.GetNamespace(c); ns != "" { |
| 49 return ProjectFromNamespace(ns) | 48 return ProjectFromNamespace(ns) |
| 50 } | 49 } |
| 51 return "" | 50 return "" |
| 52 } | 51 } |
| 53 | 52 |
| 54 // CurrentProjectConfig returns the project-specific configuration for the | 53 // CurrentProjectConfig returns the project-specific configuration for the |
| 55 // current project. | 54 // current project. |
| 56 // | 55 // |
| 57 // If there is no current project namespace, or if the current project has no | 56 // If there is no current project namespace, or if the current project has no |
| 58 // configuration, config.ErrInvalidConfig will be returned. | 57 // configuration, config.ErrInvalidConfig will be returned. |
| 59 func CurrentProjectConfig(c context.Context) (*svcconfig.ProjectConfig, error) { | 58 func CurrentProjectConfig(c context.Context) (*svcconfig.ProjectConfig, error) { |
| 60 return GetServices(c).ProjectConfig(c, CurrentProject(c)) | 59 return GetServices(c).ProjectConfig(c, CurrentProject(c)) |
| 61 } | 60 } |
| 62 | |
| 63 // ActiveUserProjects returns a full list of all config service projects with | |
| 64 // LogDog project configurations that the current user has READ access to. | |
| 65 // | |
| 66 // TODO: Load project configs and all project configs lists from datastore. Add | |
| 67 // a background cron job to periodically update these lists from luci-config. | |
| 68 // This should be a generic config service capability. | |
| 69 func ActiveUserProjects(c context.Context) (map[luciConfig.ProjectName]*svcconfi
g.ProjectConfig, error) { | |
| 70 allPcfgs, err := config.AllProjectConfigs(c) | |
| 71 if err != nil { | |
| 72 return nil, err | |
| 73 } | |
| 74 | |
| 75 for project, pcfg := range allPcfgs { | |
| 76 // Verify user READ access. | |
| 77 if err := IsProjectReader(c, pcfg); err != nil { | |
| 78 delete(allPcfgs, project) | |
| 79 | |
| 80 // If it is a membership error, prune this project and c
ontinue. | |
| 81 // Otherwise, forward the error. | |
| 82 if !IsMembershipError(err) { | |
| 83 // No configuration for this project, the config
uration is invalid, or | |
| 84 // the user didn't have access. Remove it from t
he list. | |
| 85 log.Fields{ | |
| 86 log.ErrorKey: err, | |
| 87 "project": project, | |
| 88 }.Errorf(c, "Failed to check project.") | |
| 89 return nil, err | |
| 90 } | |
| 91 } | |
| 92 } | |
| 93 return allPcfgs, nil | |
| 94 } | |
| OLD | NEW |