示例#1
0
func getGceInstanceID() info.InstanceID {
	instanceID, err := metadata.Get("instance/id")
	if err != nil {
		return info.UnknownInstance
	}
	return info.InstanceID(info.InstanceType(instanceID))
}
示例#2
0
func (cs computeSource) Token() (*oauth2.Token, error) {
	if !metadata.OnGCE() {
		return nil, errors.New("oauth2/google: can't get a token from the metadata service; not running on GCE")
	}
	acct := cs.account
	if acct == "" {
		acct = "default"
	}
	tokenJSON, err := metadata.Get("instance/service-accounts/" + acct + "/token")
	if err != nil {
		return nil, err
	}
	var res struct {
		AccessToken  string `json:"access_token"`
		ExpiresInSec int    `json:"expires_in"`
		TokenType    string `json:"token_type"`
	}
	err = json.NewDecoder(strings.NewReader(tokenJSON)).Decode(&res)
	if err != nil {
		return nil, fmt.Errorf("oauth2/google: invalid token JSON from metadata: %v", err)
	}
	if res.ExpiresInSec == 0 || res.AccessToken == "" {
		return nil, fmt.Errorf("oauth2/google: incomplete token received from metadata")
	}
	return &oauth2.Token{
		AccessToken: res.AccessToken,
		TokenType:   res.TokenType,
		Expiry:      time.Now().Add(time.Duration(res.ExpiresInSec) * time.Second),
	}, nil
}
示例#3
0
func getGceInstanceType() info.InstanceType {
	machineType, err := metadata.Get("instance/machine-type")
	if err != nil {
		return info.UnknownInstance
	}

	responseParts := strings.Split(machineType, "/") // Extract the instance name from the machine type.
	return info.InstanceType(responseParts[len(responseParts)-1])
}