mirror of
https://github.com/letic/terraform-provider-google.git
synced 2024-09-06 17:16:44 +00:00
![Vincent Roseberry](/assets/img/avatar_default.png)
* Refactor project iam binding and member resources to improve reusability * Use default mask when updating project iam policy * Add a doc comment for the ResourceIamUpdater interface
68 lines
1.6 KiB
Go
68 lines
1.6 KiB
Go
package google
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
"google.golang.org/api/cloudresourcemanager/v1"
|
|
)
|
|
|
|
var IamProjectSchema = map[string]*schema.Schema{
|
|
"project": {
|
|
Type: schema.TypeString,
|
|
Optional: true,
|
|
ForceNew: true,
|
|
},
|
|
}
|
|
|
|
type ProjectIamUpdater struct {
|
|
resourceId string
|
|
Config *Config
|
|
}
|
|
|
|
func NewProjectIamUpdater(d *schema.ResourceData, config *Config) (ResourceIamUpdater, error) {
|
|
pid, err := getProject(d, config)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &ProjectIamUpdater{
|
|
resourceId: pid,
|
|
Config: config,
|
|
}, nil
|
|
}
|
|
|
|
func (u *ProjectIamUpdater) GetResourceIamPolicy() (*cloudresourcemanager.Policy, error) {
|
|
p, err := u.Config.clientResourceManager.Projects.GetIamPolicy(u.resourceId,
|
|
&cloudresourcemanager.GetIamPolicyRequest{}).Do()
|
|
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Error retrieving IAM policy for %s: %s", u.DescribeResource(), err)
|
|
}
|
|
|
|
return p, nil
|
|
}
|
|
|
|
func (u *ProjectIamUpdater) SetResourceIamPolicy(policy *cloudresourcemanager.Policy) error {
|
|
_, err := u.Config.clientResourceManager.Projects.SetIamPolicy(u.resourceId, &cloudresourcemanager.SetIamPolicyRequest{
|
|
Policy: policy,
|
|
}).Do()
|
|
|
|
if err != nil {
|
|
return fmt.Errorf("Error setting IAM policy for %s: %s", u.DescribeResource(), err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (u *ProjectIamUpdater) GetResourceId() string {
|
|
return u.resourceId
|
|
}
|
|
|
|
func (u *ProjectIamUpdater) GetMutexKey() string {
|
|
return fmt.Sprintf("iam-project-%s", u.resourceId)
|
|
}
|
|
|
|
func (u *ProjectIamUpdater) DescribeResource() string {
|
|
return fmt.Sprintf("project %q", u.resourceId)
|
|
}
|