Skip to content

Commit

Permalink
Promote resource policy to GA (#4265) (#2755)
Browse files Browse the repository at this point in the history
Co-authored-by: upodroid <cy@borg.dev>
Signed-off-by: Modular Magician <magic-modules@google.com>

Co-authored-by: upodroid <cy@borg.dev>
  • Loading branch information
modular-magician and upodroid authored Dec 2, 2020
1 parent d31549e commit adb4c26
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 84 deletions.
3 changes: 3 additions & 0 deletions .changelog/4265.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
compute: promoted `google_compute_resource_policy` to GA
```
55 changes: 12 additions & 43 deletions google-beta/data_source_google_compute_resource_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,22 @@ import (
)

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 @@ -54,17 +32,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)
}
42 changes: 1 addition & 41 deletions google-beta/data_source_google_compute_resource_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,52 +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 {
for _, rs := range s.RootModule().Resources {
Expand Down

0 comments on commit adb4c26

Please sign in to comment.