terraform-provider-google/google/data_source_google_client_openid_userinfo.go
The Magician dff7b250c1 Add a datasource for retrieving the client email from OpenID Connect (#3103)
<!-- This change is generated by MagicModules. -->
/cc @rileykarson
2019-02-25 12:39:12 -08:00

37 lines
1.0 KiB
Go

package google
import (
"fmt"
"time"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceGoogleClientOpenIDUserinfo() *schema.Resource {
return &schema.Resource{
Read: dataSourceGoogleClientOpenIDUserinfoRead,
Schema: map[string]*schema.Schema{
"email": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceGoogleClientOpenIDUserinfoRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
// See https://github.com/golang/oauth2/issues/306 for a recommendation to do this from a Go maintainer
// URL retrieved from https://accounts.google.com/.well-known/openid-configuration
res, err := sendRequest(config, "GET", "https://openidconnect.googleapis.com/v1/userinfo", nil)
if err != nil {
return fmt.Errorf("error retrieving userinfo for your provider credentials; have you enabled the 'https://www.googleapis.com/auth/userinfo.email' scope? error: %s", err)
}
d.SetId(time.Now().UTC().String())
d.Set("email", res["email"])
return nil
}