mirror of
https://github.com/letic/terraform-provider-google.git
synced 2024-09-06 17:16:44 +00:00
![Jonathan Pentecost](/assets/img/avatar_default.png)
Commit 8f31fec
introduced a bug for the 'service_account_key' resource
where it required a project be set either in the provider or in the
resource for 'service_account_key', but a project isn't required if the
service account is a service account fully qualified name or a service
account email.
This PR relaxes the requirement that a project needs to be set for the
'service_account_key' resource, 'service_account' datasource and
'service_account_key' datasource, but will error if we try to build a
fully qualified name from a service account id when no project can be
found.
This also cleans up 'serviceAccountFQN' so it is slightly easier to
follow and return an error if there is no project but we need one to
build the service account fully qualified name.
Fixes: #1655
66 lines
1.5 KiB
Go
66 lines
1.5 KiB
Go
package google
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
)
|
|
|
|
func dataSourceGoogleServiceAccount() *schema.Resource {
|
|
return &schema.Resource{
|
|
Read: dataSourceGoogleServiceAccountRead,
|
|
Schema: map[string]*schema.Schema{
|
|
"account_id": &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Required: true,
|
|
ValidateFunc: validateRFC1035Name(6, 30),
|
|
},
|
|
"project": &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Optional: true,
|
|
},
|
|
"email": &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Computed: true,
|
|
},
|
|
"unique_id": &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Computed: true,
|
|
},
|
|
"name": &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Computed: true,
|
|
},
|
|
"display_name": &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Computed: true,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func dataSourceGoogleServiceAccountRead(d *schema.ResourceData, meta interface{}) error {
|
|
config := meta.(*Config)
|
|
|
|
serviceAccountName, err := serviceAccountFQN(d.Get("account_id").(string), d, config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
sa, err := config.clientIAM.Projects.ServiceAccounts.Get(serviceAccountName).Do()
|
|
if err != nil {
|
|
return handleNotFoundError(err, d, fmt.Sprintf("Service Account %q", serviceAccountName))
|
|
}
|
|
|
|
d.SetId(sa.Name)
|
|
d.Set("email", sa.Email)
|
|
d.Set("unique_id", sa.UniqueId)
|
|
d.Set("project", sa.ProjectId)
|
|
d.Set("account_id", strings.Split(sa.Email, "@")[0])
|
|
d.Set("name", sa.Name)
|
|
d.Set("display_name", sa.DisplayName)
|
|
|
|
return nil
|
|
}
|