go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/gae/impl/memory/info.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 memory
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  
    21  	"go.chromium.org/luci/gae/impl/dummy"
    22  	"go.chromium.org/luci/gae/service/info"
    23  	"go.chromium.org/luci/gae/service/info/support"
    24  )
    25  
    26  var giContextKey = "holds a *globalInfoData"
    27  
    28  var defaultGlobalInfoData = globalInfoData{
    29  	// versionID returns X.Y where Y is autogenerated by appengine, and X is
    30  	// whatever's in app.yaml.
    31  	versionID: "testVersionID.1",
    32  	requestID: "test-request-id",
    33  }
    34  
    35  type globalInfoData struct {
    36  	appID     string
    37  	fqAppID   string
    38  	namespace string
    39  	versionID string
    40  	requestID string
    41  }
    42  
    43  func curGID(c context.Context) *globalInfoData {
    44  	if gid, ok := c.Value(&giContextKey).(*globalInfoData); ok {
    45  		return gid
    46  	}
    47  	return &defaultGlobalInfoData
    48  }
    49  
    50  func useGID(c context.Context, f func(mod *globalInfoData)) context.Context {
    51  	cur := curGID(c)
    52  	if cur == nil {
    53  		cur = &defaultGlobalInfoData
    54  	}
    55  
    56  	clone := *cur
    57  	f(&clone)
    58  	return context.WithValue(c, &giContextKey, &clone)
    59  }
    60  
    61  // useGI adds a gae.GlobalInfo context, accessible
    62  // by gae.GetGI(c)
    63  func useGI(c context.Context) context.Context {
    64  	return info.SetFactory(c, func(ic context.Context) info.RawInterface {
    65  		return &giImpl{dummy.Info(), curGID(ic), ic}
    66  	})
    67  }
    68  
    69  type giImpl struct {
    70  	info.RawInterface
    71  	*globalInfoData
    72  	c context.Context
    73  }
    74  
    75  var _ = info.RawInterface((*giImpl)(nil))
    76  
    77  func (gi *giImpl) GetNamespace() string { return gi.namespace }
    78  
    79  func (gi *giImpl) Namespace(ns string) (context.Context, error) {
    80  	if err := support.ValidNamespace(ns); err != nil {
    81  		return gi.c, err
    82  	}
    83  
    84  	return useGID(gi.c, func(mod *globalInfoData) {
    85  		mod.namespace = ns
    86  	}), nil
    87  }
    88  
    89  func (gi *giImpl) AppID() string {
    90  	return gi.appID
    91  }
    92  
    93  func (gi *giImpl) FullyQualifiedAppID() string {
    94  	return gi.fqAppID
    95  }
    96  
    97  func (gi *giImpl) DefaultVersionHostname() string {
    98  	return fmt.Sprintf("%s.example.com", gi.appID)
    99  }
   100  
   101  func (gi *giImpl) IsDevAppServer() bool {
   102  	return true
   103  }
   104  
   105  func (gi *giImpl) ServiceAccount() (string, error) {
   106  	return "gae_service_account@example.com", nil
   107  }
   108  
   109  func (gi *giImpl) VersionID() string {
   110  	return curGID(gi.c).versionID
   111  }
   112  
   113  func (gi *giImpl) RequestID() string {
   114  	return curGID(gi.c).requestID
   115  }
   116  
   117  func (gi *giImpl) GetTestable() info.Testable {
   118  	return gi
   119  }
   120  
   121  func (gi *giImpl) SetVersionID(v string) context.Context {
   122  	return useGID(gi.c, func(mod *globalInfoData) {
   123  		mod.versionID = v
   124  	})
   125  }
   126  
   127  func (gi *giImpl) SetRequestID(v string) context.Context {
   128  	return useGID(gi.c, func(mod *globalInfoData) {
   129  		mod.requestID = v
   130  	})
   131  }