Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(62)

Side by Side Diff: impl/cloud/context.go

Issue 2829903002: Add UseFlex(c) for the cloud implementation. (Closed)
Patch Set: UseFlex Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | impl/memory/context.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "fmt"
9
8 "github.com/luci/gae/impl/dummy" 10 "github.com/luci/gae/impl/dummy"
11 "github.com/luci/gae/impl/memory"
9 ds "github.com/luci/gae/service/datastore" 12 ds "github.com/luci/gae/service/datastore"
10 "github.com/luci/gae/service/mail" 13 "github.com/luci/gae/service/mail"
11 mc "github.com/luci/gae/service/memcache" 14 mc "github.com/luci/gae/service/memcache"
12 "github.com/luci/gae/service/module" 15 "github.com/luci/gae/service/module"
13 "github.com/luci/gae/service/taskqueue" 16 "github.com/luci/gae/service/taskqueue"
14 "github.com/luci/gae/service/user" 17 "github.com/luci/gae/service/user"
15 18
19 "cloud.google.com/go/compute/metadata"
16 "cloud.google.com/go/datastore" 20 "cloud.google.com/go/datastore"
17 "github.com/bradfitz/gomemcache/memcache" 21 "github.com/bradfitz/gomemcache/memcache"
18 22
19 "golang.org/x/net/context" 23 "golang.org/x/net/context"
20 ) 24 )
21 25
22 // Config is a full-stack cloud service configuration. A user can selectively 26 // Config is a full-stack cloud service configuration. A user can selectively
23 // populate its fields, and services for the populated fields will be installed 27 // populate its fields, and services for the populated fields will be installed
24 // in the Context and available. 28 // in the Context and available.
25 // 29 //
(...skipping 18 matching lines...) Expand all
44 // Dummy services that we don't support. 48 // Dummy services that we don't support.
45 c = mail.Set(c, dummy.Mail()) 49 c = mail.Set(c, dummy.Mail())
46 c = module.Set(c, dummy.Module()) 50 c = module.Set(c, dummy.Module())
47 c = taskqueue.SetRaw(c, dummy.TaskQueue()) 51 c = taskqueue.SetRaw(c, dummy.TaskQueue())
48 c = user.Set(c, dummy.User()) 52 c = user.Set(c, dummy.User())
49 53
50 c = useInfo(c) 54 c = useInfo(c)
51 55
52 // datastore service 56 // datastore service
53 if cfg.DS != nil { 57 if cfg.DS != nil {
54 » » c = UseDatastore(c, cfg.DS) 58 » » cds := cloudDatastore{
59 » » » client: cfg.DS,
60 » » }
61 » » c = cds.use(c)
55 } else { 62 } else {
56 c = ds.SetRaw(c, dummy.Datastore()) 63 c = ds.SetRaw(c, dummy.Datastore())
57 } 64 }
58 65
59 // memcache service 66 // memcache service
60 if cfg.MC != nil { 67 if cfg.MC != nil {
61 mc := memcacheClient{ 68 mc := memcacheClient{
62 client: cfg.MC, 69 client: cfg.MC,
63 } 70 }
64 c = mc.use(c) 71 c = mc.use(c)
65 } else { 72 } else {
66 c = mc.SetRaw(c, dummy.Memcache()) 73 c = mc.SetRaw(c, dummy.Memcache())
67 } 74 }
68 75
69 return c 76 return c
70 } 77 }
71 78
72 // UseDatastore installs a datastore implementation into the context. 79 // UseFlex installs a set of cloud services into the context with services
73 func UseDatastore(c context.Context, client *datastore.Client) context.Context { 80 // supported by AppEngine Flex, including:
81 // * Info
82 // * Datastore
83 func UseFlex(c context.Context) context.Context {
84 » // Flex is on GCE, so we can get the project ID from the metadata server .
85 » project, err := metadata.Get("project/project-id")
86 » if err != nil {
87 » » panic(fmt.Errorf("could not get project ID, not on GCE? %s", err .Error()))
88 » }
89 » // Use the memory implementation of Info.
90 » c = memory.UseInfo(c, project)
91 » // Create a datastore client.
92 » client, err := datastore.NewClient(c, project)
93 » if err != nil {
94 » » panic(err)
95 » }
74 cds := cloudDatastore{ 96 cds := cloudDatastore{
75 client: client, 97 client: client,
76 } 98 }
77 return cds.use(c) 99 return cds.use(c)
78 } 100 }
OLDNEW
« no previous file with comments | « no previous file | impl/memory/context.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698