本文整理汇总了Golang中google/golang.org/cloud/compute/metadata.ProjectID函数的典型用法代码示例。如果您正苦于以下问题:Golang ProjectID函数的具体用法?Golang ProjectID怎么用?Golang ProjectID使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ProjectID函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: authenticatedClient
// If we're not running on GCE (e.g. dev mode on localhost) and have
// no other way to get the info, the error value is is errNoRefresh.
func (h *DeployHandler) authenticatedClient() (project string, hc *http.Client, err error) {
project = os.Getenv("CAMLI_GCE_PROJECT")
accountFile := os.Getenv("CAMLI_GCE_SERVICE_ACCOUNT")
if project != "" && accountFile != "" {
data, errr := ioutil.ReadFile(accountFile)
err = errr
if err != nil {
return
}
jwtConf, errr := google.JWTConfigFromJSON(data, "https://www.googleapis.com/auth/compute.readonly")
err = errr
if err != nil {
return
}
hc = jwtConf.Client(context.Background())
return
}
if !metadata.OnGCE() {
err = errNoRefresh
return
}
project, _ = metadata.ProjectID()
hc, err = google.DefaultClient(oauth2.NoContext)
return project, hc, err
}
示例2: NewCore
// Returns a thread-compatible implementation of GCM interactions.
func NewCore() (*GcmCore, error) {
token, err := gce.NewAuthTokenProvider(GCMAuthScope)
if err != nil {
return nil, err
}
// Detect project.
project, err := metadata.ProjectID()
if err != nil {
return nil, err
}
core := &GcmCore{
token: token,
project: project,
exportedMetrics: make(map[string]metricDescriptor),
lastValue: gcstore.New(time.Hour),
}
// Wait for an initial token.
_, err = core.token.WaitForToken()
if err != nil {
return nil, err
}
if err := core.listMetrics(); err != nil {
return nil, err
}
return core, nil
}
示例3: newClient
// newClient creates http.Client with a jwt service account when
// jsonFile flag is specified, otherwise by obtaining the GCE service
// account's access token.
func newClient(jsonFile string) (*http.Client, error) {
if jsonFile != "" {
jsonKey, err := ioutil.ReadFile(jsonFile)
if err != nil {
return nil, err
}
conf, err := google.JWTConfigFromJSON(jsonKey, pubsub.ScopePubSub)
if err != nil {
return nil, err
}
return conf.Client(oauth2.NoContext), nil
}
if metadata.OnGCE() {
c := &http.Client{
Transport: &oauth2.Transport{
Source: google.ComputeTokenSource(""),
},
}
if *projID == "" {
projectID, err := metadata.ProjectID()
if err != nil {
return nil, fmt.Errorf("ProjectID failed, %v", err)
}
*projID = projectID
}
return c, nil
}
return nil, errors.New("Could not create an authenticated client.")
}
示例4: logsHandler
func logsHandler(w http.ResponseWriter, r *http.Request) {
suffix := strings.TrimPrefix(r.URL.Path, "/debug/logs/")
switch suffix {
case "camlistored":
projID, err := metadata.ProjectID()
if err != nil {
httputil.ServeError(w, r, fmt.Errorf("Error getting project ID: %v", err))
return
}
http.Redirect(w, r,
"https://console.developers.google.com/logs?project="+projID+"&service=custom.googleapis.com&logName=camlistored-stderr",
http.StatusFound)
case "system":
c := &http.Client{
Transport: &http.Transport{
Dial: func(network, addr string) (net.Conn, error) {
return net.Dial("unix", "/run/camjournald.sock")
},
},
}
res, err := c.Get("http://journal/entries")
if err != nil {
http.Error(w, err.Error(), 500)
return
}
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
io.Copy(w, res.Body)
default:
http.Error(w, "no such logs", 404)
}
}
示例5: new
// Returns an implementation of a Google Cloud Logging (GCL) sink.
func new() (sink_api.ExternalSink, error) {
// TODO: Retry OnGCE call for ~15 seconds before declaring failure.
time.Sleep(3 * time.Second)
// Only support GCE for now.
if !metadata.OnGCE() {
return nil, fmt.Errorf("The Google Cloud Logging (GCL) sink failed to start: this process must be running on Google Compute Engine (GCE)")
}
// Detect project ID
projectId, err := metadata.ProjectID()
if err != nil {
return nil, err
}
glog.Infof("Project ID for GCL sink is: %q\r\n", projectId)
// Check for required auth scopes
err = gce.VerifyAuthScope(GCLAuthScope)
if err != nil {
return nil, err
}
impl := &gclSink{
projectId: projectId,
httpClient: &http.Client{},
}
// Get an initial token.
err = impl.refreshToken()
if err != nil {
return nil, err
}
return impl, nil
}
示例6: maybeRemapCloudSQL
func maybeRemapCloudSQL(host string) (out string, err error) {
if !strings.HasSuffix(host, cloudSQLSuffix) {
return host, nil
}
inst := strings.TrimSuffix(host, cloudSQLSuffix)
if !metadata.OnGCE() {
return "", errors.New("CloudSQL support only available when running on Google Compute Engine.")
}
proj, err := metadata.ProjectID()
if err != nil {
return "", fmt.Errorf("Failed to lookup GCE project ID: %v", err)
}
admin, _ := sqladmin.New(oauth2.NewClient(context.Background(), google.ComputeTokenSource("")))
listRes, err := admin.Instances.List(proj).Do()
if err != nil {
return "", fmt.Errorf("error enumerating Cloud SQL instances: %v", err)
}
for _, it := range listRes.Items {
if !strings.EqualFold(it.Instance, inst) {
continue
}
js, _ := json.Marshal(it)
log.Printf("Found Cloud SQL instance %s: %s", inst, js)
for _, ipm := range it.IpAddresses {
return ipm.IpAddress, nil
}
return "", fmt.Errorf("No external IP address for Cloud SQL instances %s", inst)
}
var found []string
for _, it := range listRes.Items {
found = append(found, it.Instance)
}
return "", fmt.Errorf("Cloud SQL instance %q not found. Found: %q", inst, found)
}
示例7: init
func init() {
if !metadata.OnGCE() {
return
}
hc, err := google.DefaultClient(oauth2.NoContext)
if err != nil {
registerBrokenFS(fmt.Errorf("could not get http client for context: %v", err))
return
}
projID, err := metadata.ProjectID()
if projID == "" || err != nil {
registerBrokenFS(fmt.Errorf("could not get GCE project ID: %v", err))
return
}
ctx := cloud.NewContext(projID, hc)
sc, err := storage.NewClient(ctx)
if err != nil {
registerBrokenFS(fmt.Errorf("could not get cloud storage client: %v", err))
return
}
wkfs.RegisterFS("/gcs/", &gcsFS{
ctx: ctx,
sc: sc,
})
}
示例8: LogWriter
// LogWriter returns an environment-specific io.Writer suitable for passing
// to log.SetOutput. It will also include writing to os.Stderr as well.
func LogWriter() (w io.Writer) {
w = os.Stderr
if !env.OnGCE() {
return
}
projID, err := metadata.ProjectID()
if projID == "" {
log.Printf("Error getting project ID: %v", err)
return
}
scopes, _ := metadata.Scopes("default")
haveScope := func(scope string) bool {
for _, x := range scopes {
if x == scope {
return true
}
}
return false
}
if !haveScope(logging.Scope) {
log.Printf("when this Google Compute Engine VM instance was created, it wasn't granted enough access to use Google Cloud Logging (Scope URL: %v).", logging.Scope)
return
}
logc, err := logging.NewClient(context.Background(), projID, "camlistored-stderr")
if err != nil {
log.Printf("Error creating Google logging client: %v", err)
return
}
return io.MultiWriter(w, logc.Writer(logging.Debug))
}
示例9: checkInProduction
func checkInProduction() bool {
if !metadata.OnGCE() {
return false
}
proj, _ := metadata.ProjectID()
inst, _ := metadata.InstanceName()
log.Printf("Running on GCE: %v / %v", proj, inst)
return proj == "camlistore-website" && inst == "camweb"
}
示例10: googleCloudConsole
func (sh *StatusHandler) googleCloudConsole() (string, error) {
if !env.OnGCE() {
return "", errors.New("not on GCE")
}
projID, err := metadata.ProjectID()
if err != nil {
return "", fmt.Errorf("Error getting project ID: %v", err)
}
return "https://console.cloud.google.com/compute/instances?project=" + projID, nil
}
示例11: projectID
// projectID returns the GCE project ID used for running this camweb on GCE
// and/or for logging on Google Cloud Logging, if any.
func projectID() string {
if *gceProjectID != "" {
return *gceProjectID
}
projID, err := metadata.ProjectID()
if projID == "" || err != nil {
log.Fatalf("GCE project ID needed but --gce_project_id not specified (and not running on GCE); metadata error: %v", err)
}
return projID
}
示例12: CreateGCMSink
func CreateGCMSink(uri *url.URL) (core.DataSink, error) {
if len(uri.Scheme) > 0 {
return nil, fmt.Errorf("scheme should not be set for GCM sink")
}
if len(uri.Host) > 0 {
return nil, fmt.Errorf("host should not be set for GCM sink")
}
opts, err := url.ParseQuery(uri.RawQuery)
metrics := "all"
if len(opts["metrics"]) > 0 {
metrics = opts["metrics"][0]
}
var metricFilter MetricFilter = metricsAll
switch metrics {
case "all":
metricFilter = metricsAll
case "autoscaling":
metricFilter = metricsOnlyAutoscaling
default:
return nil, fmt.Errorf("invalid metrics parameter: %s", metrics)
}
if err := gce_util.EnsureOnGCE(); err != nil {
return nil, err
}
// Detect project ID
projectId, err := gce.ProjectID()
if err != nil {
return nil, err
}
// Create Google Cloud Monitoring service.
client := oauth2.NewClient(oauth2.NoContext, google.ComputeTokenSource(""))
gcmService, err := gcm.New(client)
if err != nil {
return nil, err
}
sink := &gcmSink{
registered: false,
project: projectId,
gcmService: gcmService,
metricFilter: metricFilter,
}
glog.Infof("created GCM sink")
if err := sink.registerAllMetrics(); err != nil {
glog.Warningf("Error during metrics registration: %v", err)
}
return sink, nil
}
示例13: initClient
func initClient() *http.Client {
metaproject, _ := metadata.ProjectID()
if Gceproject == "" && metaproject == "" {
log.Errorf("No project specified")
os.Exit(1)
} else if Gceproject == "" && metaproject != "" {
Gceproject = metaproject
}
gcs := GCS(Gceproject)
gc := gcs.Client()
log.Debugf("Google Auth: %#v", gc)
return gc
}
示例14: initGCE
func initGCE() error {
initGCECalled = true
// Use the staging project if not on GCE. This assumes the DefaultTokenSource
// credential used below has access to that project.
if !metadata.OnGCE() {
projectID = stagingProjectID
}
var err error
projectID, err = metadata.ProjectID()
if err != nil {
return fmt.Errorf("failed to get current GCE ProjectID: %v", err)
}
inStaging = projectID == stagingProjectID
if inStaging {
log.Printf("Running in staging cluster (%q)", projectID)
}
tokenSource, _ = google.DefaultTokenSource(oauth2.NoContext)
httpClient := oauth2.NewClient(oauth2.NoContext, tokenSource)
serviceCtx = cloud.NewContext(projectID, httpClient)
projectZone, err = metadata.Get("instance/zone")
if err != nil || projectZone == "" {
return fmt.Errorf("failed to get current GCE zone: %v", err)
}
// Convert the zone from "projects/1234/zones/us-central1-a" to "us-central1-a".
projectZone = path.Base(projectZone)
if !hasComputeScope() {
return errors.New("The coordinator is not running with access to read and write Compute resources. VM support disabled.")
}
projectRegion = projectZone[:strings.LastIndex(projectZone, "-")] // "us-central1"
externalIP, err = metadata.ExternalIP()
if err != nil {
return fmt.Errorf("ExternalIP: %v", err)
}
computeService, _ = compute.New(httpClient)
errTryDeps = checkTryBuildDeps()
if errTryDeps != nil {
log.Printf("TryBot builders disabled due to error: %v", errTryDeps)
} else {
log.Printf("TryBot builders enabled.")
}
go gcePool.pollQuotaLoop()
return nil
}
示例15: initGCP
func initGCP() {
initGCPOnce.Do(func() {
onGCE = metadata.OnGCE()
if onGCE {
// These will fail on instances if the metadata service is
// down or the client is compiled with an API version that
// has been removed. Since these are not vital, let's ignore
// them and make their fields in the dockeLogEntry ,omitempty
projectID, _ = metadata.ProjectID()
zone, _ = metadata.Zone()
instanceName, _ = metadata.InstanceName()
instanceID, _ = metadata.InstanceID()
}
})
}