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

Promote resource policy to GA #4265

Merged
Original file line number Diff line number Diff line change
@@ -1,52 +1,29 @@
<% autogen_exception -%>
package google

<% unless version == 'ga' -%>
import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceGoogleComputeResourcePolicy() *schema.Resource {
return &schema.Resource{
Read: dataSourceGoogleComputeResourcePolicyRead,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},

"project": {
Type: schema.TypeString,
Optional: true,
},
dsSchema := datasourceSchemaFromResourceSchema(resourceComputeResourcePolicy().Schema)

"region": {
Type: schema.TypeString,
Required: true,
},
addRequiredFieldsToSchema(dsSchema, "name")
addOptionalFieldsToSchema(dsSchema, "region")
addOptionalFieldsToSchema(dsSchema, "project")

"description": {
Type: schema.TypeString,
Computed: true,
},

"self_link": {
Type: schema.TypeString,
Computed: true,
},
},
return &schema.Resource{
Read: dataSourceGoogleComputeResourcePolicyRead,
Schema: dsSchema,
}
}

func dataSourceGoogleComputeResourcePolicyRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}

name := d.Get("name").(string)

project, err := getProject(d, config)
if err != nil {
Expand All @@ -56,18 +33,8 @@ func dataSourceGoogleComputeResourcePolicyRead(d *schema.ResourceData, meta inte
if err != nil {
return err
}
name := d.Get("name").(string)
resourcePolicy, err := config.NewComputeClient(userAgent).ResourcePolicies.Get(project, region, name).Do()
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("ResourcePolicy Not Found : %s", name))
}
if err := d.Set("self_link", resourcePolicy.SelfLink); err != nil {
return fmt.Errorf("Error setting self_link: %s", err)
}
if err := d.Set("description", resourcePolicy.Description); err != nil {
return fmt.Errorf("Error setting description: %s", err)
}

d.SetId(fmt.Sprintf("projects/%s/regions/%s/resourcePolicies/%s", project, region, name))
return nil

return resourceComputeResourcePolicyRead(d, meta)
}
<% end -%>
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<% autogen_exception -%>
package google

<% unless version == 'ga' -%>
import (
"fmt"
"testing"
Expand All @@ -27,51 +26,12 @@ func TestAccDataSourceComputeResourcePolicy(t *testing.T) {
Steps: []resource.TestStep{
{
Config: testAccDataSourceComputeResourcePolicyConfig(rsName, dsName, randomSuffix),
Check: resource.ComposeTestCheckFunc(
testAccDataSourceComputeResourcePolicyCheck(t, dsFullName, rsFullName),
),
Check: checkDataSourceStateMatchesResourceState(rsFullName, dsFullName),
},
},
})
}

func testAccDataSourceComputeResourcePolicyCheck(t *testing.T, dataSourceName string, resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
ds, ok := s.RootModule().Resources[dataSourceName]
if !ok {
return fmt.Errorf("root module has no resource called %s", dataSourceName)
}

rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("can't find %s in state", resourceName)
}

dsAttr := ds.Primary.Attributes
rsAttr := rs.Primary.Attributes

policyAttrsToTest := []string{
"name",
}

for _, attrToCheck := range policyAttrsToTest {
if dsAttr[attrToCheck] != rsAttr[attrToCheck] {
return fmt.Errorf(
"%s is %s; want %s",
attrToCheck,
dsAttr[attrToCheck],
rsAttr[attrToCheck],
)
}
}

if !compareSelfLinkOrResourceName("", dsAttr["self_link"], rsAttr["self_link"], nil) && dsAttr["self_link"] != rsAttr["self_link"] {
return fmt.Errorf("self link does not match: %s vs %s", dsAttr["self_link"], rsAttr["self_link"])
}

return nil
}
}

func testAccCheckDataSourceComputeResourcePolicyDestroy(t *testing.T, name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
Expand Down Expand Up @@ -120,4 +80,3 @@ data "google_compute_resource_policy" "%s" {
}
`, rsName, randomSuffix, dsName, rsName, rsName)
}
<% 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 @@ -202,9 +202,7 @@ func Provider() *schema.Provider {
"google_compute_regions": dataSourceGoogleComputeRegions(),
"google_compute_region_instance_group": dataSourceGoogleComputeRegionInstanceGroup(),
"google_compute_region_ssl_certificate": dataSourceGoogleRegionComputeSslCertificate(),
<% unless version == 'ga' -%>
"google_compute_resource_policy": dataSourceGoogleComputeResourcePolicy(),
<% end -%>
"google_compute_router": dataSourceGoogleComputeRouter(),
"google_compute_ssl_certificate": dataSourceGoogleComputeSslCertificate(),
"google_compute_ssl_policy": dataSourceGoogleComputeSslPolicy(),
Expand Down