|
| 1 | +// Copyright 2019 The LUCI Authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package cloud |
| 16 | + |
| 17 | +import ( |
| 18 | + "cloud.google.com/go/datastore" |
| 19 | + "github.com/bradfitz/gomemcache/memcache" |
| 20 | + "golang.org/x/net/context" |
| 21 | + |
| 22 | + "go.chromium.org/gae/impl/dummy" |
| 23 | + ds "go.chromium.org/gae/service/datastore" |
| 24 | + "go.chromium.org/gae/service/mail" |
| 25 | + mc "go.chromium.org/gae/service/memcache" |
| 26 | + "go.chromium.org/gae/service/module" |
| 27 | + "go.chromium.org/gae/service/taskqueue" |
| 28 | + "go.chromium.org/gae/service/user" |
| 29 | +) |
| 30 | + |
| 31 | +// ConfigLite can be used to configure a context to have supported Cloud |
| 32 | +// Services clients and ONLY them. |
| 33 | +// |
| 34 | +// Currently supports only datastore and its required dependencies. |
| 35 | +// |
| 36 | +// Unlike Config, it doesn't try to setup logging, intercept HTTP requests, |
| 37 | +// provide auth, etc. |
| 38 | +type ConfigLite struct { |
| 39 | + // IsDev is true if this is a development execution. |
| 40 | + IsDev bool |
| 41 | + |
| 42 | + // ProjectID, if not empty, is the project ID returned by the "info" service. |
| 43 | + // |
| 44 | + // If empty, the service will treat requests for this field as not |
| 45 | + // implemented. |
| 46 | + ProjectID string |
| 47 | + |
| 48 | + // ServiceName, if not empty, is the service (module) name returned by the |
| 49 | + // "info" service. |
| 50 | + // |
| 51 | + // If empty, the service will treat requests for this field as not |
| 52 | + // implemented. |
| 53 | + ServiceName string |
| 54 | + |
| 55 | + // VersionName, if not empty, is the version name returned by the "info" |
| 56 | + // service. |
| 57 | + // |
| 58 | + // If empty, the service will treat requests for this field as not |
| 59 | + // implemented. |
| 60 | + VersionName string |
| 61 | + |
| 62 | + // InstanceID, if not empty, is the instance ID returned by the "info" |
| 63 | + // service. |
| 64 | + // |
| 65 | + // If empty, the service will treat requests for this field as not |
| 66 | + // implemented. |
| 67 | + InstanceID string |
| 68 | + |
| 69 | + // RequestID, if not empty, is the request ID returned by the "info" |
| 70 | + // service. |
| 71 | + // |
| 72 | + // If empty, the service will treat requests for this field as not |
| 73 | + // implemented. |
| 74 | + RequestID string |
| 75 | + |
| 76 | + // ServiceAccountName, if not empty, is the service account name returned by |
| 77 | + // the "info" service. |
| 78 | + // |
| 79 | + // If empty, the service will treat requests for this field as not |
| 80 | + // implemented. |
| 81 | + ServiceAccountName string |
| 82 | + |
| 83 | + // DS is the Cloud Datastore client. |
| 84 | + // |
| 85 | + // If populated, the datastore service will be installed. |
| 86 | + DS *datastore.Client |
| 87 | + |
| 88 | + // MC is the memcache service client. |
| 89 | + // |
| 90 | + // If populated, the memcache service will be installed. |
| 91 | + MC *memcache.Client |
| 92 | +} |
| 93 | + |
| 94 | +// Use configures the context with implementation of Cloud Services. |
| 95 | +// |
| 96 | +// Any services that are missing will have "impl/dummy" stubs installed. These |
| 97 | +// stubs will panic if called. |
| 98 | +func (cfg *ConfigLite) Use(c context.Context) context.Context { |
| 99 | + // Dummy services that we don't support. |
| 100 | + c = mail.Set(c, dummy.Mail()) |
| 101 | + c = module.Set(c, dummy.Module()) |
| 102 | + c = taskqueue.SetRaw(c, dummy.TaskQueue()) |
| 103 | + c = user.Set(c, dummy.User()) |
| 104 | + |
| 105 | + c = useInfo(c, &serviceInstanceGlobalInfo{ |
| 106 | + IsDev: cfg.IsDev, |
| 107 | + ProjectID: cfg.ProjectID, |
| 108 | + ServiceName: cfg.ServiceName, |
| 109 | + VersionName: cfg.VersionName, |
| 110 | + InstanceID: cfg.InstanceID, |
| 111 | + RequestID: cfg.RequestID, |
| 112 | + ServiceAccountName: cfg.ServiceAccountName, |
| 113 | + }) |
| 114 | + |
| 115 | + if cfg.DS != nil { |
| 116 | + cds := cloudDatastore{client: cfg.DS} |
| 117 | + c = cds.use(c) |
| 118 | + } else { |
| 119 | + c = ds.SetRaw(c, dummy.Datastore()) |
| 120 | + } |
| 121 | + |
| 122 | + if cfg.MC != nil { |
| 123 | + mc := memcacheClient{client: cfg.MC} |
| 124 | + c = mc.use(c) |
| 125 | + } else { |
| 126 | + c = mc.SetRaw(c, dummy.Memcache()) |
| 127 | + } |
| 128 | + |
| 129 | + return c |
| 130 | +} |
0 commit comments