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