go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/gae/service/info/interface.go (about) 1 // Copyright 2015 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 info 16 17 import ( 18 "context" 19 "strings" 20 "time" 21 ) 22 23 // RawInterface is the interface for all of the package methods which normally 24 // would be in the 'appengine' package. 25 type RawInterface interface { 26 AppID() string 27 FullyQualifiedAppID() string 28 GetNamespace() string 29 30 Datacenter() string 31 DefaultVersionHostname() string 32 InstanceID() string 33 IsDevAppServer() bool 34 IsOverQuota(err error) bool 35 IsTimeoutError(err error) bool 36 ModuleHostname(module, version, instance string) (string, error) 37 ModuleName() string 38 RequestID() string 39 ServerSoftware() string 40 ServiceAccount() (string, error) 41 VersionID() string 42 43 Namespace(namespace string) (context.Context, error) 44 45 AccessToken(scopes ...string) (token string, expiry time.Time, err error) 46 PublicCertificates() ([]Certificate, error) 47 SignBytes(bytes []byte) (keyName string, signature []byte, err error) 48 49 // Testable returns this RawInterface's Testing interface. Testing will 50 // return nil if testing is not supported in this implementation. 51 GetTestable() Testable 52 } 53 54 // Testable is an additional set of functions for testing instrumentation. 55 type Testable interface { 56 SetVersionID(string) context.Context 57 SetRequestID(string) context.Context 58 } 59 60 // AppID returns the current App ID. 61 func AppID(c context.Context) string { 62 return Raw(c).AppID() 63 } 64 65 // TrimmedAppID gets the 'appid' portion of "foo.com:appid". This form can 66 // occur if you use 67 func TrimmedAppID(c context.Context) string { 68 toks := strings.Split(AppID(c), ":") 69 return toks[len(toks)-1] 70 } 71 72 // FullyQualifiedAppID returns the fully-qualified App ID. 73 func FullyQualifiedAppID(c context.Context) string { 74 return Raw(c).FullyQualifiedAppID() 75 } 76 77 // GetNamespace returns the current namespace. If the current namespace is the 78 // default namespace, GetNamespace will return an empty string. 79 func GetNamespace(c context.Context) string { 80 return Raw(c).GetNamespace() 81 } 82 83 // Datacenter returns the current datacenter. 84 func Datacenter(c context.Context) string { 85 return Raw(c).Datacenter() 86 } 87 88 // DefaultVersionHostname returns the default version hostname. 89 func DefaultVersionHostname(c context.Context) string { 90 return Raw(c).DefaultVersionHostname() 91 } 92 93 // InstanceID returns the current instance ID. 94 func InstanceID(c context.Context) string { 95 return Raw(c).InstanceID() 96 } 97 98 // IsDevAppServer returns true if running on a development server. 99 func IsDevAppServer(c context.Context) bool { 100 return Raw(c).IsDevAppServer() 101 } 102 103 // IsOverQuota returns true if the supplied error is an over quota error. 104 func IsOverQuota(c context.Context, err error) bool { 105 return Raw(c).IsOverQuota(err) 106 } 107 108 // IsTimeoutError returns true if the supplied error indicates a timeout. 109 func IsTimeoutError(c context.Context, err error) bool { 110 return Raw(c).IsTimeoutError(err) 111 } 112 113 // ModuleHostname returns the hostname of a module instance. 114 func ModuleHostname(c context.Context, module, version, instance string) (string, error) { 115 return Raw(c).ModuleHostname(module, version, instance) 116 } 117 118 // ModuleName returns the current module name. 119 func ModuleName(c context.Context) string { 120 return Raw(c).ModuleName() 121 } 122 123 // RequestID returns the current request ID. 124 func RequestID(c context.Context) string { 125 return Raw(c).RequestID() 126 } 127 128 // ServerSoftware returns the AppEngine release version. 129 func ServerSoftware(c context.Context) string { 130 return Raw(c).ServerSoftware() 131 } 132 133 // ServiceAccount returns the current service account name, in the form of an 134 // e-mail address. 135 func ServiceAccount(c context.Context) (string, error) { 136 return Raw(c).ServiceAccount() 137 } 138 139 // VersionID returns the version ID for the current application, in the form 140 // "X.Y". 141 func VersionID(c context.Context) string { 142 return Raw(c).VersionID() 143 } 144 145 // Namespace sets the current namespace. If the namespace is invalid or could 146 // not be set, an error will be returned. 147 func Namespace(c context.Context, namespace string) (context.Context, error) { 148 return Raw(c).Namespace(namespace) 149 } 150 151 // MustNamespace is the same as Namespace, but will panic if there's an error. 152 // Since an error can only occur if namespace doesn't match the a regex this 153 // is valid to use if the namespace you're using is statically known, or known 154 // to conform to the regex. The regex in question is: 155 // 156 // ^[0-9A-Za-z._-]{0,100}$ 157 func MustNamespace(c context.Context, namespace string) context.Context { 158 ret, err := Namespace(c, namespace) 159 if err != nil { 160 panic(err) 161 } 162 return ret 163 } 164 165 // AccessToken generates an OAuth2 access token for the specified scopes 166 // on behalf of the current ServiceAccount. 167 func AccessToken(c context.Context, scopes ...string) (token string, expiry time.Time, err error) { 168 return Raw(c).AccessToken(scopes...) 169 } 170 171 // PublicCertificates retrieves the public certificates of the app. 172 func PublicCertificates(c context.Context) ([]Certificate, error) { 173 return Raw(c).PublicCertificates() 174 } 175 176 // SignBytes signs bytes using the application's unique private key. 177 func SignBytes(c context.Context, bytes []byte) (keyName string, signature []byte, err error) { 178 return Raw(c).SignBytes(bytes) 179 } 180 181 // GetTestable returns this Interface's Testing interface. Testing will return 182 // nil if testing is not supported in this implementation. 183 func GetTestable(c context.Context) Testable { 184 return Raw(c).GetTestable() 185 }