Skip to content

Commit

Permalink
Set user attribute resource (#14)
Browse files Browse the repository at this point in the history
* implement set user attribute value resource

* fmt
  • Loading branch information
hirosassa authored Nov 14, 2021
1 parent a75b078 commit cf66667
Show file tree
Hide file tree
Showing 6 changed files with 392 additions and 14 deletions.
36 changes: 36 additions & 0 deletions docs/resources/user_attribute_user_value.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "looker_user_attribute_user_value Resource - terraform-provider-looker"
subcategory: ""
description: |-
---

# looker_user_attribute_user_value (Resource)



## Example Usage

```terraform
resource "looker_user_attribute_user_value" "my_user_attribute_user_value" {
user_id = 1
user_attribute_id = 1
value = "foo"
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

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

### Optional

- **id** (String) The ID of this resource.


Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resource "looker_user_attribute_user_value" "my_user_attribute_user_value" {
user_id = 1
user_attribute_id = 1
value = "foo"
}
23 changes: 12 additions & 11 deletions pkg/looker/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,18 @@ func Provider() *schema.Provider {
},
},
ResourcesMap: map[string]*schema.Resource{
"looker_user": resourceUser(),
"looker_user_roles": resourceUserRoles(),
"looker_permission_set": resourcePermissionSet(),
"looker_model_set": resourceModelSet(),
"looker_group": resourceGroup(),
"looker_group_membership": resourceGroupMembership(),
"looker_role": resourceRole(),
"looker_role_groups": resourceRoleGroups(),
"looker_user_attribute": resourceUserAttribute(),
"looker_connection": resourceConnection(),
"looker_lookml_model": resourceLookMLModel(),
"looker_user": resourceUser(),
"looker_user_roles": resourceUserRoles(),
"looker_permission_set": resourcePermissionSet(),
"looker_model_set": resourceModelSet(),
"looker_group": resourceGroup(),
"looker_group_membership": resourceGroupMembership(),
"looker_role": resourceRole(),
"looker_role_groups": resourceRoleGroups(),
"looker_user_attribute": resourceUserAttribute(),
"looker_user_attribute_user_value": resourceUserAttributeUserValue(),
"looker_connection": resourceConnection(),
"looker_lookml_model": resourceLookMLModel(),
},

ConfigureContextFunc: providerConfigure,
Expand Down
22 changes: 19 additions & 3 deletions pkg/looker/resource_group_membership_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestAcc_GroupMembership(t *testing.T) {
},
// Test: Update
{
Config: groupMembershipConfigUpdate(user5),
Config: groupMembershipConfigUpdate(target1, user1, user2, user5, group1),
Check: resource.ComposeTestCheckFunc(
testAccCheckGroupMembershipExists("looker_group_membership.test"),
resource.TestCheckResourceAttr("looker_group_membership.test", "group_ids.#", "1"),
Expand Down Expand Up @@ -181,17 +181,33 @@ func groupMembershipConfigNoGroup(target, user1, user2 string) string {
`, target, user1, user1, user1, user2, user2, user2)
}

func groupMembershipConfigUpdate(user string) string {
func groupMembershipConfigUpdate(target, user1, user2, user5, group1 string) string {
return fmt.Sprintf(`
resource "looker_group" "target_group" {
name = "%s"
}
resource "looker_user" "membership_user1" {
first_name = "%s"
last_name = "%s"
email = "%[email protected]"
}
resource "looker_user" "membership_user2" {
first_name = "%s"
last_name = "%s"
email = "%[email protected]"
}
resource "looker_user" "membership_user5" {
first_name = "%s"
last_name = "%s"
email = "%[email protected]"
}
resource "looker_group" "membership_group1" {
name = "%s"
}
resource "looker_group_membership" "test" {
target_group_id = looker_group.target_group.id
user_ids = [looker_user.membership_user1.id, looker_user.membership_user2.id, looker_user.membership_user5.id]
group_ids = [looker_group.membership_group1.id]
}
`, user, user, user)
`, target, user1, user1, user1, user2, user2, user2, user5, user5, user5, group1)
}
163 changes: 163 additions & 0 deletions pkg/looker/resource_user_attribute_user_value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package looker

import (
"context"
"strconv"

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

func resourceUserAttributeUserValue() *schema.Resource {
return &schema.Resource{
CreateContext: resourceUserAttributeUserValueCreate,
ReadContext: resourceUserAttributeUserValueRead,
UpdateContext: resourceUserAttributeUserValueUpdate,
DeleteContext: resourceUserAttributeUserValueDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},

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

func resourceUserAttributeUserValueCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*apiclient.LookerSDK)
userID := int64(d.Get("user_id").(int))
userAttributeID := int64(d.Get("user_attribute_id").(int))
userAttributeValue := d.Get("value").(string)

body := apiclient.WriteUserAttributeWithValue{
Value: &userAttributeValue,
}

userAttributeWithValue, err := client.SetUserAttributeUserValue(userID, userAttributeID, body, nil)
if err != nil {
return diag.FromErr(err)
}

userIDString := strconv.Itoa(int(*userAttributeWithValue.UserId))
userAttributeIDString := strconv.Itoa(int(*userAttributeWithValue.UserAttributeId))
id := buildTwoPartID(&userIDString, &userAttributeIDString)

d.SetId(id)

return resourceUserAttributeUserValueRead(ctx, d, m)
}

func resourceUserAttributeUserValueRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*apiclient.LookerSDK)

userIDString, userAttributeIDString, err := parseTwoPartID(d.Id())
if err != nil {
return diag.FromErr(err)
}
userID, err := strconv.ParseInt(userIDString, 10, 64)
if err != nil {
return diag.FromErr(err)
}
userAttributeID, err := strconv.ParseInt(userAttributeIDString, 10, 64)
if err != nil {
return diag.FromErr(err)
}

userAttributeIDs := rtl.DelimInt64{userAttributeID}
request := apiclient.RequestUserAttributeUserValues{
UserId: userID,
UserAttributeIds: &userAttributeIDs,
}

userAttributeUserValues, err := client.UserAttributeUserValues(request, nil)
if err != nil {
return diag.FromErr(err)
}
if len(userAttributeUserValues) != 1 { // the number of the result should be one
return diag.FromErr(err)
}

if err = d.Set("user_id", userAttributeUserValues[0].UserId); err != nil {
return diag.FromErr(err)
}
if err = d.Set("user_attribute_id", userAttributeUserValues[0].UserAttributeId); err != nil {
return diag.FromErr(err)
}
if err = d.Set("value", userAttributeUserValues[0].Value); err != nil {
return diag.FromErr(err)
}

return nil
}

func resourceUserAttributeUserValueUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*apiclient.LookerSDK)

userIDString, userAttributeIDString, err := parseTwoPartID(d.Id())
if err != nil {
return diag.FromErr(err)
}
userID, err := strconv.ParseInt(userIDString, 10, 64)
if err != nil {
return diag.FromErr(err)
}
userAttributeID, err := strconv.ParseInt(userAttributeIDString, 10, 64)
if err != nil {
return diag.FromErr(err)
}

userAttributeValue := d.Get("value").(string)
body := apiclient.WriteUserAttributeWithValue{
Value: &userAttributeValue,
}

_, err = client.SetUserAttributeUserValue(userID, userAttributeID, body, nil)
if err != nil {
return diag.FromErr(err)
}

return resourceUserAttributeUserValueRead(ctx, d, m)
}

func resourceUserAttributeUserValueDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*apiclient.LookerSDK)

userIDString, userAttributeIDString, err := parseTwoPartID(d.Id())
if err != nil {
return diag.FromErr(err)
}
userID, err := strconv.ParseInt(userIDString, 10, 64)
if err != nil {
return diag.FromErr(err)
}
userAttributeID, err := strconv.ParseInt(userAttributeIDString, 10, 64)
if err != nil {
return diag.FromErr(err)
}

body := apiclient.WriteUserAttributeWithValue{
Value: nil,
}

// fill the target value null
_, err = client.SetUserAttributeUserValue(userID, userAttributeID, body, nil)
if err != nil {
return diag.FromErr(err)
}

return nil
}
Loading

0 comments on commit cf66667

Please sign in to comment.