forked from DevotedHealth/terraform-provider-looker
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* implement set user attribute value resource * fmt
- Loading branch information
Showing
6 changed files
with
392 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
|
5 changes: 5 additions & 0 deletions
5
examples/resources/looker_user_attribute_user_value/resource.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"), | ||
|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.