Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ResourceIamPolicy for google_project_iam_policy #3410

Merged
merged 5 commits into from
Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

16 changes: 16 additions & 0 deletions third_party/terraform/resources/resource_google_project_migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"

"github.com/hashicorp/terraform-plugin-sdk/terraform"
"google.golang.org/api/cloudresourcemanager/v1"
)

func resourceGoogleProjectMigrateState(v int, s *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) {
Expand Down Expand Up @@ -45,3 +46,18 @@ func migrateGoogleProjectStateV0toV1(s *terraform.InstanceState, config *Config)
log.Printf("[DEBUG] Attributes after migration: %#v", s.Attributes)
return s, nil
}

// Retrieve the existing IAM Policy for a Project
func getProjectIamPolicy(project string, config *Config) (*cloudresourcemanager.Policy, error) {
p, err := config.clientResourceManager.Projects.GetIamPolicy(project,
&cloudresourcemanager.GetIamPolicyRequest{
Options: &cloudresourcemanager.GetPolicyOptions{
RequestedPolicyVersion: iamPolicyVersion,
},
}).Do()

if err != nil {
return nil, fmt.Errorf("Error retrieving IAM policy for project %q: %s", project, err)
}
return p, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func getStatePrimaryResource(s *terraform.State, res, expectedID string) (*terra
if !ok {
return nil, fmt.Errorf("Not found: %s", res)
}
if resource.Primary.Attributes["id"] != expectedID && expectedID != "" {
if expectedID != "" && !compareProjectName("", resource.Primary.Attributes["id"], expectedID, nil) {
return nil, fmt.Errorf("Expected project %q to match ID %q in state", resource.Primary.ID, expectedID)
}
return resource.Primary, nil
Expand Down
29 changes: 29 additions & 0 deletions third_party/terraform/utils/iam_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ var IamProjectSchema = map[string]*schema.Schema{
},
}

// In google_project_iam_policy, project is required and not inferred by
// getProject.
var IamPolicyProjectSchema = map[string]*schema.Schema{
"project": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: compareProjectName,
},
}

type ProjectIamUpdater struct {
resourceId string
Config *Config
Expand All @@ -37,6 +48,15 @@ func NewProjectIamUpdater(d *schema.ResourceData, config *Config) (ResourceIamUp
}, nil
}

// NewProjectIamPolicyUpdater is similar to NewProjectIamUpdater, except that it
// doesn't call getProject and only uses an explicitly set project.
func NewProjectIamPolicyUpdater(d *schema.ResourceData, config *Config) (ResourceIamUpdater, error) {
return &ProjectIamUpdater{
resourceId: d.Get("project").(string),
Config: config,
}, nil
}

func ProjectIdParseFunc(d *schema.ResourceData, _ *Config) error {
d.Set("project", d.Id())
return nil
Expand Down Expand Up @@ -84,3 +104,12 @@ func (u *ProjectIamUpdater) GetMutexKey() string {
func (u *ProjectIamUpdater) DescribeResource() string {
return fmt.Sprintf("project %q", u.resourceId)
}

func compareProjectName(_, old, new string, _ *schema.ResourceData) bool {
// We can either get "projects/project-id" or "project-id", so strip any prefixes
return GetResourceNameFromSelfLink(old) == GetResourceNameFromSelfLink(new)
}

func getProjectIamPolicyMutexKey(pid string) string {
return fmt.Sprintf("iam-project-%s", pid)
}
6 changes: 3 additions & 3 deletions third_party/terraform/utils/provider.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -372,11 +372,11 @@ end # products.each do
"google_organization_iam_binding": ResourceIamBinding(IamOrganizationSchema, NewOrganizationIamUpdater, OrgIdParseFunc),
"google_organization_iam_custom_role": resourceGoogleOrganizationIamCustomRole(),
"google_organization_iam_member": ResourceIamMember(IamOrganizationSchema, NewOrganizationIamUpdater, OrgIdParseFunc),
"google_organization_iam_policy": ResourceIamPolicy(IamOrganizationSchema, NewOrganizationIamUpdater, OrgIdParseFunc),
"google_organization_iam_audit_config": ResourceIamAuditConfig(IamOrganizationSchema, NewOrganizationIamUpdater, OrgIdParseFunc),
"google_organization_iam_policy": ResourceIamPolicy(IamOrganizationSchema, NewOrganizationIamUpdater, OrgIdParseFunc),
"google_organization_iam_audit_config": ResourceIamAuditConfig(IamOrganizationSchema, NewOrganizationIamUpdater, OrgIdParseFunc),
"google_organization_policy": resourceGoogleOrganizationPolicy(),
"google_project": resourceGoogleProject(),
"google_project_iam_policy": resourceGoogleProjectIamPolicy(),
"google_project_iam_policy": ResourceIamPolicy(IamPolicyProjectSchema, NewProjectIamPolicyUpdater, ProjectIdParseFunc),
"google_project_iam_binding": ResourceIamBindingWithBatching(IamProjectSchema, NewProjectIamUpdater, ProjectIdParseFunc, IamBatchingEnabled),
"google_project_iam_member": ResourceIamMemberWithBatching(IamProjectSchema, NewProjectIamUpdater, ProjectIdParseFunc, IamBatchingEnabled),
"google_project_iam_audit_config": ResourceIamAuditConfigWithBatching(IamProjectSchema, NewProjectIamUpdater, ProjectIdParseFunc, IamBatchingEnabled),
Expand Down