Skip to content

Commit

Permalink
IAM Policy on subnets is now GA, moving into non-beta provider
Browse files Browse the repository at this point in the history
  • Loading branch information
reechar-goog committed May 2, 2019
1 parent 52b880e commit 24b9c43
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<% autogen_exception -%>
package google
<% unless version == 'ga' -%>

import (
"fmt"
Expand All @@ -25,7 +23,7 @@ func TestAccComputeSubnetworkIamBinding(t *testing.T) {
{
Config: testAccComputeSubnetworkIamBinding_basic(account, region, subnetwork, role),
},
resource.TestStep{
{
ResourceName: "google_compute_subnetwork_iam_binding.foo",
ImportStateId: fmt.Sprintf("%s/%s %s", region, subnetwork, role),
ImportState: true,
Expand All @@ -35,7 +33,7 @@ func TestAccComputeSubnetworkIamBinding(t *testing.T) {
// Test Iam Binding update
Config: testAccComputeSubnetworkIamBinding_update(account, region, subnetwork, role),
},
resource.TestStep{
{
ResourceName: "google_compute_subnetwork_iam_binding.foo",
ImportStateId: fmt.Sprintf("%s/%s %s", region, subnetwork, role),
ImportState: true,
Expand All @@ -62,7 +60,7 @@ func TestAccComputeSubnetworkIamMember(t *testing.T) {
// Test Iam Member creation (no update for member, no need to test)
Config: testAccComputeSubnetworkIamMember_basic(account, region, subnetwork, role),
},
resource.TestStep{
{
ResourceName: "google_compute_subnetwork_iam_member.foo",
ImportStateId: fmt.Sprintf("%s/%s %s serviceAccount:%s@%s.iam.gserviceaccount.com", region, subnetwork, role, account, project),
ImportState: true,
Expand All @@ -89,25 +87,25 @@ func TestAccComputeSubnetworkIamPolicy(t *testing.T) {
Config: testAccComputeSubnetworkIamPolicy_basic(account, region, subnetwork, role),
},
// Test a few import formats
resource.TestStep{
{
ResourceName: "google_compute_subnetwork_iam_policy.foo",
ImportStateId: fmt.Sprintf("projects/%s/regions/%s/subnetworks/%s", project, region, subnetwork),
ImportState: true,
ImportStateVerify: true,
},
resource.TestStep{
{
ResourceName: "google_compute_subnetwork_iam_policy.foo",
ImportStateId: fmt.Sprintf("%s/%s/%s", project, region, subnetwork),
ImportState: true,
ImportStateVerify: true,
},
resource.TestStep{
{
ResourceName: "google_compute_subnetwork_iam_policy.foo",
ImportStateId: fmt.Sprintf("%s/%s", region, subnetwork),
ImportState: true,
ImportStateVerify: true,
},
resource.TestStep{
{
ResourceName: "google_compute_subnetwork_iam_policy.foo",
ImportStateId: fmt.Sprintf("%s", subnetwork),
ImportState: true,
Expand Down Expand Up @@ -247,6 +245,3 @@ resource "google_compute_subnetwork_iam_policy" "foo" {
}
`, account, subnetworkName, subnetworkName, region, roleId)
}
<% else %>
// Magic Modules doesn't let us remove files - blank out beta-only common-compile files for now.
<% end -%>
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<% autogen_exception -%>
package google
<% unless version == 'ga' -%>

import (
"fmt"
Expand All @@ -9,28 +7,28 @@ import (
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/cloudresourcemanager/v1"
computeBeta "google.golang.org/api/compute/v0.beta"
"google.golang.org/api/compute/v1"
)

var IamComputeSubnetworkSchema = map[string]*schema.Schema{
"subnetwork": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"project": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},

"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
}

Expand Down Expand Up @@ -101,13 +99,13 @@ func ComputeSubnetworkIdParseFunc(d *schema.ResourceData, config *Config) error
}

func (u *ComputeSubnetworkIamUpdater) GetResourceIamPolicy() (*cloudresourcemanager.Policy, error) {
p, err := u.Config.clientComputeBeta.Subnetworks.GetIamPolicy(u.project, u.region, u.resourceId).Do()
p, err := u.Config.clientCompute.Subnetworks.GetIamPolicy(u.project, u.region, u.resourceId).Do()

if err != nil {
return nil, errwrap.Wrapf(fmt.Sprintf("Error retrieving IAM policy for %s: {{err}}", u.DescribeResource()), err)
}

cloudResourcePolicy, err := computeBetaToResourceManagerPolicy(p)
cloudResourcePolicy, err := computeToResourceManagerPolicy(p)

if err != nil {
return nil, errwrap.Wrapf(fmt.Sprintf("Invalid IAM policy for %s: {{err}}", u.DescribeResource()), err)
Expand All @@ -117,16 +115,16 @@ func (u *ComputeSubnetworkIamUpdater) GetResourceIamPolicy() (*cloudresourcemana
}

func (u *ComputeSubnetworkIamUpdater) SetResourceIamPolicy(policy *cloudresourcemanager.Policy) error {
computePolicy, err := resourceManagerToComputeBetaPolicy(policy)
computePolicy, err := resourceManagerToComputePolicy(policy)

if err != nil {
return errwrap.Wrapf(fmt.Sprintf("Invalid IAM policy for %s: {{err}}", u.DescribeResource()), err)
}

req := &computeBeta.RegionSetPolicyRequest{
req := &compute.RegionSetPolicyRequest{
Policy: computePolicy,
}
_, err = u.Config.clientComputeBeta.Subnetworks.SetIamPolicy(u.project, u.region, u.resourceId, req).Do()
_, err = u.Config.clientCompute.Subnetworks.SetIamPolicy(u.project, u.region, u.resourceId, req).Do()

if err != nil {
return errwrap.Wrapf(fmt.Sprintf("Error setting IAM policy for %s: {{err}}", u.DescribeResource()), err)
Expand All @@ -147,23 +145,20 @@ func (u *ComputeSubnetworkIamUpdater) DescribeResource() string {
return fmt.Sprintf("Compute Subnetwork %s/%s/%s", u.project, u.region, u.resourceId)
}

func resourceManagerToComputeBetaPolicy(p *cloudresourcemanager.Policy) (*computeBeta.Policy, error) {
out := &computeBeta.Policy{}
func resourceManagerToComputePolicy(p *cloudresourcemanager.Policy) (*compute.Policy, error) {
out := &compute.Policy{}
err := Convert(p, out)
if err != nil {
return nil, errwrap.Wrapf("Cannot convert a resourcemanager policy to a compute policy: {{err}}", err)
}
return out, nil
}

func computeBetaToResourceManagerPolicy(p *computeBeta.Policy) (*cloudresourcemanager.Policy, error) {
func computeToResourceManagerPolicy(p *compute.Policy) (*cloudresourcemanager.Policy, error) {
out := &cloudresourcemanager.Policy{}
err := Convert(p, out)
if err != nil {
return nil, errwrap.Wrapf("Cannot convert a compute policy to a resourcemanager policy: {{err}}", err)
}
return out, nil
}
<% else %>
// Magic Modules doesn't let us remove files - blank out beta-only common-compile files for now.
<% end -%>
2 changes: 0 additions & 2 deletions third_party/terraform/utils/provider.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,9 @@ func ResourceMapWithErrors() (map[string]*schema.Resource, error) {
"google_compute_security_policy": resourceComputeSecurityPolicy(),
"google_compute_shared_vpc_host_project": resourceComputeSharedVpcHostProject(),
"google_compute_shared_vpc_service_project": resourceComputeSharedVpcServiceProject(),
<% unless version == 'ga' -%>
"google_compute_subnetwork_iam_binding": ResourceIamBindingWithImport(IamComputeSubnetworkSchema, NewComputeSubnetworkIamUpdater, ComputeSubnetworkIdParseFunc),
"google_compute_subnetwork_iam_member": ResourceIamMemberWithImport(IamComputeSubnetworkSchema, NewComputeSubnetworkIamUpdater, ComputeSubnetworkIdParseFunc),
"google_compute_subnetwork_iam_policy": ResourceIamPolicyWithImport(IamComputeSubnetworkSchema, NewComputeSubnetworkIamUpdater, ComputeSubnetworkIdParseFunc),
<% end -%>
"google_compute_target_pool": resourceComputeTargetPool(),
"google_container_cluster": resourceContainerCluster(),
"google_container_node_pool": resourceContainerNodePool(),
Expand Down

0 comments on commit 24b9c43

Please sign in to comment.