diff --git a/data_source_google_iam_policy_document.go b/data_source_google_iam_policy.go similarity index 84% rename from data_source_google_iam_policy_document.go rename to data_source_google_iam_policy.go index 79cdabd5..e47b0f00 100644 --- a/data_source_google_iam_policy_document.go +++ b/data_source_google_iam_policy.go @@ -9,6 +9,25 @@ import ( "google.golang.org/api/cloudresourcemanager/v1" ) +var iamBinding *schema.Schema = &schema.Schema{ + Type: schema.TypeSet, + Required: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "role": { + Type: schema.TypeString, + Required: true, + }, + "members": { + Type: schema.TypeSet, + Required: true, + Elem: &schema.Schema{Type: schema.TypeString}, + Set: schema.HashString, + }, + }, + }, +} + // dataSourceGoogleIamPolicy returns a *schema.Resource that allows a customer // to express a Google Cloud IAM policy in a data resource. This is an example // of how the schema would be used in a config: @@ -25,25 +44,8 @@ func dataSourceGoogleIamPolicy() *schema.Resource { return &schema.Resource{ Read: dataSourceGoogleIamPolicyRead, Schema: map[string]*schema.Schema{ - "binding": { - Type: schema.TypeSet, - Required: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "role": { - Type: schema.TypeString, - Required: true, - }, - "members": { - Type: schema.TypeSet, - Required: true, - Elem: &schema.Schema{Type: schema.TypeString}, - Set: schema.HashString, - }, - }, - }, - }, - "policy": { + "binding": iamBinding, + "policy_data": { Type: schema.TypeString, Computed: true, }, @@ -81,7 +83,7 @@ func dataSourceGoogleIamPolicyRead(d *schema.ResourceData, meta interface{}) err } pstring := string(pjson) - d.Set("policy", pstring) + d.Set("policy_data", pstring) d.SetId(strconv.Itoa(hashcode.String(pstring))) return nil diff --git a/resource_google_project.go b/resource_google_project.go index b922951b..b46d6614 100644 --- a/resource_google_project.go +++ b/resource_google_project.go @@ -31,31 +31,23 @@ func resourceGoogleProject() *schema.Resource { Delete: resourceGoogleProjectDelete, Schema: map[string]*schema.Schema{ - "project": &schema.Schema{ + "id": &schema.Schema{ Type: schema.TypeString, Required: true, ForceNew: true, }, - - "policy": &schema.Schema{ + "policy_data": &schema.Schema{ Type: schema.TypeString, Optional: true, }, - "name": &schema.Schema{ Type: schema.TypeString, Computed: true, }, - "number": &schema.Schema{ Type: schema.TypeString, Computed: true, }, - - "id": &schema.Schema{ - Type: schema.TypeString, - Computed: true, - }, }, } } @@ -77,7 +69,7 @@ func resourceGoogleProjectCreate(d *schema.ResourceData, meta interface{}) error } // Apply the IAM policy if it is set - if pString, ok := d.GetOk("policy"); ok { + if pString, ok := d.GetOk("policy_data"); ok { // The policy string is just a marshaled cloudresourcemanager.Policy. // Unmarshal it to a struct. var policy cloudresourcemanager.Policy @@ -116,6 +108,7 @@ func resourceGoogleProjectRead(d *schema.ResourceData, meta interface{}) error { if err != nil { return err } + d.SetId(project) // Confirm the project exists. // TODO(evanbrown): Support project creation @@ -141,10 +134,10 @@ func resourceGoogleProjectUpdate(d *schema.ResourceData, meta interface{}) error } // Policy has changed - if ok := d.HasChange("policy"); ok { + if ok := d.HasChange("policy_data"); ok { // The policy string is just a marshaled cloudresourcemanager.Policy. // Unmarshal it to a struct that contains the old and new policies - oldP, newP := d.GetChange("policy") + oldP, newP := d.GetChange("policy_data") oldPString := oldP.(string) newPString := newP.(string) diff --git a/resource_google_project_test.go b/resource_google_project_test.go index c5b4ad7c..f9208e11 100644 --- a/resource_google_project_test.go +++ b/resource_google_project_test.go @@ -133,9 +133,9 @@ func testAccCheckGoogleProjectIamPolicyIsMerged(projectRes, policyRes string, or var projectP, policyP cloudresourcemanager.Policy // The project should have a policy - ps, ok := project.Primary.Attributes["policy"] + ps, ok := project.Primary.Attributes["policy_data"] if !ok { - return fmt.Errorf("Project resource %q did not have a 'policy' attribute", project.Primary.ID) + return fmt.Errorf("Project resource %q did not have a 'policy_data' attribute. Attributes were %#v", project.Primary.Attributes["id"], project.Primary.Attributes) } if err := json.Unmarshal([]byte(ps), &projectP); err != nil { return err @@ -146,9 +146,9 @@ func testAccCheckGoogleProjectIamPolicyIsMerged(projectRes, policyRes string, or if !ok { return fmt.Errorf("Not found: %s", policyRes) } - ps, ok = policy.Primary.Attributes["policy"] + ps, ok = policy.Primary.Attributes["policy_data"] if !ok { - return fmt.Errorf("Policy resource %q did not have a 'policy' attribute", policy.Primary.ID) + return fmt.Errorf("Data policy resource %q did not have a 'policy_data' attribute. Attributes were %#v", policy.Primary.Attributes["id"], project.Primary.Attributes) } if err := json.Unmarshal([]byte(ps), &policyP); err != nil { return err @@ -158,7 +158,6 @@ func testAccCheckGoogleProjectIamPolicyIsMerged(projectRes, policyRes string, or if !reflect.DeepEqual(derefBindings(projectP.Bindings), derefBindings(policyP.Bindings)) { return fmt.Errorf("Project and data source policies do not match: project policy is %+v, data resource policy is %+v", derefBindings(projectP.Bindings), derefBindings(policyP.Bindings)) } - return nil // Merge the project policy in Terrafomr state with the policy the project had before the config was applied expected := make([]*cloudresourcemanager.Binding, 0) @@ -446,13 +445,13 @@ func (b Binding) Less(i, j int) bool { var testAccGoogleProject_basic = ` resource "google_project" "acceptance" { - project = "%v" + id = "%v" }` var testAccGoogleProject_policy1 = ` resource "google_project" "acceptance" { - project = "%v" - policy = "${data.google_iam_policy.admin.policy}" + id = "%v" + policy_data = "${data.google_iam_policy.admin.policy_data}" } data "google_iam_policy" "admin" {