mirror of
https://github.com/letic/terraform-provider-google.git
synced 2024-09-07 01:26:43 +00:00
![Sébastien GLON](/assets/img/avatar_default.png)
* Initial support for google service account keys * Add vendor for vault and encryption * Add change for PR comment * Add doc and improvement fo public key management * adding waiter for compatibility with issue google/google-api-go-client#234 * improvement * Add test with pgp_key * Perform doc anf format * remove test if public_key exists * Add link on doc * correct pr
62 lines
1.4 KiB
Go
62 lines
1.4 KiB
Go
package google
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
"google.golang.org/api/googleapi"
|
|
"google.golang.org/api/iam/v1"
|
|
)
|
|
|
|
type ServiceAccountKeyWaiter struct {
|
|
Service *iam.ProjectsServiceAccountsKeysService
|
|
PublicKeyType string
|
|
KeyName string
|
|
}
|
|
|
|
func (w *ServiceAccountKeyWaiter) RefreshFunc() resource.StateRefreshFunc {
|
|
return func() (interface{}, string, error) {
|
|
var err error
|
|
var sak *iam.ServiceAccountKey
|
|
sak, err = w.Service.Get(w.KeyName).PublicKeyType(w.PublicKeyType).Do()
|
|
|
|
if err != nil {
|
|
if err.(*googleapi.Error).Code == 404 {
|
|
return nil, "PENDING", nil
|
|
} else {
|
|
return nil, "", err
|
|
}
|
|
} else {
|
|
return sak, "DONE", nil
|
|
}
|
|
}
|
|
}
|
|
|
|
func (w *ServiceAccountKeyWaiter) Conf() *resource.StateChangeConf {
|
|
return &resource.StateChangeConf{
|
|
Pending: []string{"PENDING"},
|
|
Target: []string{"DONE"},
|
|
Refresh: w.RefreshFunc(),
|
|
}
|
|
}
|
|
|
|
func serviceAccountKeyWaitTime(client *iam.ProjectsServiceAccountsKeysService, keyName, publicKeyType, activity string, timeoutMin int) error {
|
|
w := &ServiceAccountKeyWaiter{
|
|
Service: client,
|
|
PublicKeyType: publicKeyType,
|
|
KeyName: keyName,
|
|
}
|
|
|
|
state := w.Conf()
|
|
state.Delay = 10 * time.Second
|
|
state.Timeout = time.Duration(timeoutMin) * time.Minute
|
|
state.MinTimeout = 2 * time.Second
|
|
_, err := state.WaitForState()
|
|
if err != nil {
|
|
return fmt.Errorf("Error waiting for %s: %s", activity, err)
|
|
}
|
|
|
|
return nil
|
|
}
|