OLD | NEW |
1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 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 cloud | 5 package cloud |
6 | 6 |
7 import ( | 7 import ( |
8 "errors" | |
9 "time" | 8 "time" |
10 | 9 |
11 infoS "github.com/luci/gae/service/info" | 10 infoS "github.com/luci/gae/service/info" |
| 11 "github.com/luci/gae/service/info/support" |
| 12 |
| 13 "github.com/luci/luci-go/common/errors" |
12 | 14 |
13 "golang.org/x/net/context" | 15 "golang.org/x/net/context" |
14 ) | 16 ) |
15 | 17 |
16 var errNotImplemented = errors.New("not implemented") | 18 var errNotImplemented = errors.New("not implemented") |
17 | 19 |
18 // cloudInfo is a reconstruction of the info service for the cloud API. | 20 // cloudInfo is a reconstruction of the info service for the cloud API. |
19 // | 21 // |
20 // It will return information sufficent for datastore operation. | 22 // It will return information sufficent for datastore operation. |
21 type infoState struct { | 23 type infoState struct { |
(...skipping 14 matching lines...) Expand all Loading... |
36 return context.WithValue(c, &infoStateKey, ci) | 38 return context.WithValue(c, &infoStateKey, ci) |
37 } | 39 } |
38 | 40 |
39 func (ci *infoState) derive(mutate func(*infoState)) *infoState { | 41 func (ci *infoState) derive(mutate func(*infoState)) *infoState { |
40 dci := *ci | 42 dci := *ci |
41 mutate(&dci) | 43 mutate(&dci) |
42 return &dci | 44 return &dci |
43 } | 45 } |
44 | 46 |
45 type infoService struct { | 47 type infoService struct { |
| 48 context.Context |
46 *infoState | 49 *infoState |
47 | |
48 ic context.Context | |
49 } | 50 } |
50 | 51 |
51 func useInfo(c context.Context) context.Context { | 52 func useInfo(c context.Context) context.Context { |
52 var baseInfoState infoState | 53 var baseInfoState infoState |
53 c = baseInfoState.use(c) | 54 c = baseInfoState.use(c) |
54 | 55 |
55 return infoS.SetFactory(c, func(ic context.Context) infoS.RawInterface { | 56 return infoS.SetFactory(c, func(ic context.Context) infoS.RawInterface { |
56 return &infoService{ | 57 return &infoService{ |
| 58 Context: ic, |
57 infoState: getInfoState(ic), | 59 infoState: getInfoState(ic), |
58 ic: ic, | |
59 } | 60 } |
60 }) | 61 }) |
61 } | 62 } |
62 | 63 |
63 func (i *infoService) AppID() string { panic(errNotImplemented) } | 64 func (*infoService) AppID() string { panic(errNotImplemented) } |
64 func (i *infoService) FullyQualifiedAppID() string { return "" } | 65 func (*infoService) FullyQualifiedAppID() string { return "" } |
65 func (i *infoService) GetNamespace() (string, bool) { return i.namespace, (i.nam
espace != "") } | 66 func (i *infoService) GetNamespace() string { return i.namespace } |
66 | 67 |
67 func (i *infoService) Datacenter() string { panic(errNotImplemented)
} | 68 func (*infoService) Datacenter() string { panic(errNotImplemented) } |
68 func (i *infoService) DefaultVersionHostname() string { panic(errNotImplemented)
} | 69 func (*infoService) DefaultVersionHostname() string { panic(errNotImplemented) } |
69 func (i *infoService) InstanceID() string { panic(errNotImplemented)
} | 70 func (*infoService) InstanceID() string { panic(errNotImplemented) } |
70 func (i *infoService) IsDevAppServer() bool { panic(errNotImplemented)
} | 71 func (*infoService) IsDevAppServer() bool { panic(errNotImplemented) } |
71 func (i *infoService) IsOverQuota(err error) bool { panic(errNotImplemented)
} | 72 func (*infoService) IsOverQuota(err error) bool { panic(errNotImplemented) } |
72 func (i *infoService) IsTimeoutError(err error) bool { panic(errNotImplemented)
} | 73 func (*infoService) IsTimeoutError(err error) bool { panic(errNotImplemented) } |
73 func (i *infoService) ModuleHostname(module, version, instance string) (string,
error) { | 74 func (*infoService) ModuleHostname(module, version, instance string) (string, er
ror) { |
74 return "", errNotImplemented | 75 return "", errNotImplemented |
75 } | 76 } |
76 func (i *infoService) ModuleName() string { panic(errNotImplemented
) } | 77 func (*infoService) ModuleName() string { panic(errNotImplemented)
} |
77 func (i *infoService) RequestID() string { panic(errNotImplemented
) } | 78 func (*infoService) RequestID() string { panic(errNotImplemented)
} |
78 func (i *infoService) ServerSoftware() string { panic(errNotImplemented
) } | 79 func (*infoService) ServerSoftware() string { panic(errNotImplemented)
} |
79 func (i *infoService) ServiceAccount() (string, error) { return "", errNotImplem
ented } | 80 func (*infoService) ServiceAccount() (string, error) { return "", errNotImplemen
ted } |
80 func (i *infoService) VersionID() string { panic(errNotImplemented
) } | 81 func (*infoService) VersionID() string { panic(errNotImplemented)
} |
81 | 82 |
82 func (i *infoService) Namespace(namespace string) (context.Context, error) { | 83 func (i *infoService) Namespace(namespace string) (context.Context, error) { |
83 » return i.MustNamespace(namespace), nil | 84 » if err := support.ValidNamespace(namespace); err != nil { |
| 85 » » return i, err |
| 86 » } |
| 87 |
| 88 » return i.derive(func(ci *infoState) { |
| 89 » » ci.namespace = namespace |
| 90 » }).use(i), nil |
84 } | 91 } |
85 | 92 |
86 func (i *infoService) MustNamespace(namespace string) context.Context { | 93 func (*infoService) AccessToken(scopes ...string) (token string, expiry time.Tim
e, err error) { |
87 » return i.derive(func(ci *infoState) { | |
88 » » ci.namespace = namespace | |
89 » }).use(i.ic) | |
90 } | |
91 | |
92 func (i *infoService) AccessToken(scopes ...string) (token string, expiry time.T
ime, err error) { | |
93 return "", time.Time{}, errNotImplemented | 94 return "", time.Time{}, errNotImplemented |
94 } | 95 } |
95 | 96 |
96 func (i *infoService) PublicCertificates() ([]infoS.Certificate, error) { | 97 func (*infoService) PublicCertificates() ([]infoS.Certificate, error) { |
97 return nil, errNotImplemented | 98 return nil, errNotImplemented |
98 } | 99 } |
99 | 100 |
100 func (i *infoService) SignBytes(bytes []byte) (keyName string, signature []byte,
err error) { | 101 func (*infoService) SignBytes(bytes []byte) (keyName string, signature []byte, e
rr error) { |
101 return "", nil, errNotImplemented | 102 return "", nil, errNotImplemented |
102 } | 103 } |
103 | 104 |
104 func (i *infoService) Testable() infoS.Testable { return nil } | 105 func (*infoService) GetTestable() infoS.Testable { return nil } |
OLD | NEW |