Skip to content

Commit

Permalink
WIP: bug fix (#27)
Browse files Browse the repository at this point in the history
* update to latest SDK

* fix tests

* fix attribute

* update golangci-lint

* tidy

* fix doc
  • Loading branch information
hirosassa authored Jun 4, 2022
1 parent c283fd4 commit 70060c3
Show file tree
Hide file tree
Showing 9 changed files with 444 additions and 22 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ jobs:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
uses: golangci/golangci-lint-action@v3
with:
version: v1.45.2
version: v1.46.2
check:
runs-on: ubuntu-latest
steps:
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.8.3
0.8.6
4 changes: 2 additions & 2 deletions docs/resources/user_attribute_user_value.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ resource "looker_user_attribute_user_value" "my_user_attribute_user_value" {

### Required

- `user_attribute_id` (Number)
- `user_id` (Number)
- `user_attribute_id` (String)
- `user_id` (String)
- `value` (String)

### Optional
Expand Down
11 changes: 6 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ go 1.17
require (
github.com/hashicorp/terraform-plugin-docs v0.7.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.13.0
github.com/looker-open-source/sdk-codegen/go v0.0.2-0.20220324185106-8f79913c70dc
github.com/looker-open-source/sdk-codegen/go v0.0.2-0.20220425180701-d51a6750f7d5
github.com/stretchr/testify v1.7.0
)

replace github.com/looker-open-source/sdk-codegen/go => github.com/hirosassa/sdk-codegen/go v0.0.2-0.20220405051148-f906184facec
replace github.com/looker-open-source/sdk-codegen/go => github.com/hirosassa/sdk-codegen/go v0.0.2-0.20220604105615-6ef4a4149ee4

require (
github.com/Masterminds/goutils v1.1.0 // indirect
Expand Down Expand Up @@ -67,9 +67,10 @@ require (
github.com/vmihailenco/tagparser v0.1.1 // indirect
github.com/zclconf/go-cty v1.10.0 // indirect
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e // indirect
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d // indirect
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 // indirect
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/appengine v1.6.6 // indirect
google.golang.org/genproto v0.0.0-20200904004341-0bd0a958aa1d // indirect
google.golang.org/grpc v1.45.0 // indirect
Expand Down
264 changes: 255 additions & 9 deletions go.sum

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions pkg/looker/resource_user_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package looker

import (
"context"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -71,6 +72,8 @@ func resourceUserAttributeCreate(ctx context.Context, d *schema.ResourceData, m
UserCanEdit: &userAttributeUserCanEdit,
}

log.Printf("[DEBUG] Create user attribute %s", userAttributeName)

userAttribute, err := client.CreateUserAttribute(writeUserAttribute, "", nil)
if err != nil {
return diag.FromErr(err)
Expand Down Expand Up @@ -140,6 +143,8 @@ func resourceUserAttributeUpdate(ctx context.Context, d *schema.ResourceData, m
UserCanEdit: &userAttributeUserCanEdit,
}

log.Printf("[DEBUG] Update user attribute %s", userAttributeID)

_, err := client.UpdateUserAttribute(userAttributeID, writeUserAttribute, "", nil)
if err != nil {
return diag.FromErr(err)
Expand All @@ -153,6 +158,8 @@ func resourceUserAttributeDelete(ctx context.Context, d *schema.ResourceData, m

userAttributeID := d.Id()

log.Printf("[DEBUG] Delete user attribute %s", userAttributeID)

_, err := client.DeleteUserAttribute(userAttributeID, nil)
if err != nil {
return diag.FromErr(err)
Expand Down
10 changes: 10 additions & 0 deletions pkg/looker/resource_user_attribute_group_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package looker

import (
"context"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -42,6 +43,8 @@ func resourceUserAttributeGroupValueCreate(ctx context.Context, d *schema.Resour
userAttributeID := d.Get("user_attribute_id").(string)
value := d.Get("value").(string)

log.Printf("[DEBUG] Create user attribute group value %s for %s by %s", userAttributeID, groupID, value)

body := apiclient.UserAttributeGroupValue{
GroupId: &groupID,
UserAttributeId: &userAttributeID,
Expand Down Expand Up @@ -69,6 +72,8 @@ func resourceUserAttributeGroupValueRead(ctx context.Context, d *schema.Resource
return diag.FromErr(err)
}

log.Printf("[DEBUG] Read user attribute group value %s for %s", userAttributeID, groupID)

userAttributeGroupValues, err := client.AllUserAttributeGroupValues(userAttributeID, "", nil)
if err != nil {
return diag.FromErr(err)
Expand Down Expand Up @@ -105,6 +110,8 @@ func resourceUserAttributeGroupValueUpdate(ctx context.Context, d *schema.Resour

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

log.Printf("[DEBUG] Update user attribute group value %s for %s by %s", userAttributeID, groupID, value)

body := apiclient.UserAttributeGroupValue{
GroupId: &groupID,
UserAttributeId: &userAttributeID,
Expand All @@ -126,8 +133,11 @@ func resourceUserAttributeGroupValueDelete(ctx context.Context, d *schema.Resour
return diag.FromErr(err)
}

log.Printf("[DEBUG] Delete user attribute group value %s for %s", userAttributeID, groupID)

err = client.DeleteUserAttributeGroupValue(groupID, userAttributeID, nil)
if err != nil {
log.Printf("[DEBUG] %+v", err)
return diag.FromErr(err)
}

Expand Down
158 changes: 158 additions & 0 deletions pkg/looker/resource_user_attribute_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package looker

import (
"fmt"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
apiclient "github.com/looker-open-source/sdk-codegen/go/sdk/v4"
)

func TestAcc_UserAttribute(t *testing.T) {
name1 := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
name2 := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
// Test: Create
{
Config: userAttributeConfig(name1),
Check: resource.ComposeTestCheckFunc(
testAccCheckUserAttributeExists("looker_user_attribute.test"),
resource.TestCheckResourceAttr("looker_user_attribute.test", "name", name1),
),
},
// Test: Update
{
Config: userAttributeGroupValueConfig(name2),
Check: resource.ComposeTestCheckFunc(
testAccCheckUserAttributeGroupValueExists("looker_user_attribute.test"),
resource.TestCheckResourceAttr("looker_user_attribute.test", "name", name2),
),
},
// Test: Import
{
ResourceName: "looker_user_attribute.test",
ImportState: true,
ImportStateVerify: true,
},
},
CheckDestroy: testAccCheckUserAttributeDestroy,
})
}

func TestAcc_UserAttributeWithDefaultValue(t *testing.T) {
name := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
type1 := "advanced_filter_string"
type2 := "advanced_filter_number"
defaultValue1 := "%, NULL"
defaultValue2 := "<0, >=0, NULL"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
// Test: Create
{
Config: userAttributeConfigWithDefaultValue(name, type1, defaultValue1),
Check: resource.ComposeTestCheckFunc(
testAccCheckUserAttributeExists("looker_user_attribute.test_with_default"),
resource.TestCheckResourceAttr("looker_user_attribute.test_with_default", "name", name),
resource.TestCheckResourceAttr("looker_user_attribute.test_with_default", "type", type1),
resource.TestCheckResourceAttr("looker_user_attribute.test_with_default", "default_value", defaultValue1),
),
},
// Test: Update
{
Config: userAttributeConfigWithDefaultValue(name, type2, defaultValue2),
Check: resource.ComposeTestCheckFunc(
testAccCheckUserAttributeExists("looker_user_attribute.test_with_default"),
resource.TestCheckResourceAttr("looker_user_attribute.test_with_default", "name", name),
resource.TestCheckResourceAttr("looker_user_attribute.test_with_default", "type", type2),
resource.TestCheckResourceAttr("looker_user_attribute.test_with_default", "default_value", defaultValue2),
),
},
// Test: Import
{
ResourceName: "looker_user_attribute.test_with_default",
ImportState: true,
ImportStateVerify: true,
},
},
CheckDestroy: testAccCheckUserAttributeDestroy,
})
}

func testAccCheckUserAttributeExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("user attribute setting not found from resources: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("no user attribute setting ID is set")
}

client := testAccProvider.Meta().(*apiclient.LookerSDK)
userAttribute, err := client.UserAttribute(rs.Primary.ID, "", nil)
if err != nil {
return err
}

if userAttribute.Name != rs.Primary.Attributes["name"] {
return fmt.Errorf("looker_user_attribute '%s' does not exist", rs.Primary.ID)
}

return nil
}
}

func testAccCheckUserAttributeDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*apiclient.LookerSDK)

for _, rs := range s.RootModule().Resources {
if rs.Type != "looker_user_attribute" {
continue
}

userAttribute, err := client.UserAttribute(rs.Primary.ID, "", nil)
if err != nil {
if strings.Contains(err.Error(), "404") {
return nil // successfully destroyed
}
return err
}

if userAttribute.Name == rs.Primary.Attributes["name"] {
return fmt.Errorf("looker_user_attribute '%s' still exists", rs.Primary.ID)
}
}

return nil
}

func userAttributeConfig(name string) string {
return fmt.Sprintf(`
resource "looker_user_attribute" "test" {
name = "%s"
type = "string"
label = "testing"
}
`, name)
}

func userAttributeConfigWithDefaultValue(name, dataType, defaultValue string) string {
return fmt.Sprintf(`
resource "looker_user_attribute" "test_with_default" {
name = "%s"
type = "%s"
label = "testing"
default_value = "%s"
}
`, name, dataType, defaultValue)
}
4 changes: 2 additions & 2 deletions pkg/looker/resource_user_attribute_user_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ func resourceUserAttributeUserValue() *schema.Resource {

Schema: map[string]*schema.Schema{
"user_id": {
Type: schema.TypeInt,
Type: schema.TypeString,
Required: true,
},
"user_attribute_id": {
Type: schema.TypeInt,
Type: schema.TypeString,
Required: true,
},
"value": {
Expand Down

0 comments on commit 70060c3

Please sign in to comment.