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