go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/gae/impl/cloud/context_lite.go (about)

     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  	"context"
    19  
    20  	"cloud.google.com/go/datastore"
    21  
    22  	"go.chromium.org/luci/gae/impl/dummy"
    23  	ds "go.chromium.org/luci/gae/service/datastore"
    24  	"go.chromium.org/luci/gae/service/mail"
    25  	mc "go.chromium.org/luci/gae/service/memcache"
    26  	"go.chromium.org/luci/gae/service/module"
    27  	"go.chromium.org/luci/gae/service/taskqueue"
    28  	"go.chromium.org/luci/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  
    89  // Use configures the context with implementation of Cloud Services.
    90  //
    91  // Any services that are missing will have "impl/dummy" stubs installed. These
    92  // stubs will panic if called.
    93  func (cfg *ConfigLite) Use(c context.Context) context.Context {
    94  	// Dummy services that we don't support.
    95  	c = mail.Set(c, dummy.Mail())
    96  	c = module.Set(c, dummy.Module())
    97  	c = taskqueue.SetRaw(c, dummy.TaskQueue())
    98  	c = user.Set(c, dummy.User())
    99  	c = mc.SetRaw(c, dummy.Memcache())
   100  
   101  	c = useInfo(c, &serviceInstanceGlobalInfo{
   102  		IsDev:              cfg.IsDev,
   103  		ProjectID:          cfg.ProjectID,
   104  		ServiceName:        cfg.ServiceName,
   105  		VersionName:        cfg.VersionName,
   106  		InstanceID:         cfg.InstanceID,
   107  		RequestID:          cfg.RequestID,
   108  		ServiceAccountName: cfg.ServiceAccountName,
   109  	})
   110  
   111  	if cfg.DS != nil {
   112  		cds := cloudDatastore{client: cfg.DS}
   113  		c = cds.use(c)
   114  	} else {
   115  		c = ds.SetRaw(c, dummy.Datastore())
   116  	}
   117  
   118  	return c
   119  }