diff --git a/docs/resources/user_attribute_group_value.md b/docs/resources/user_attribute_group_value.md new file mode 100644 index 0000000..0bcfe40 --- /dev/null +++ b/docs/resources/user_attribute_group_value.md @@ -0,0 +1,36 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "looker_user_attribute_group_value Resource - terraform-provider-looker" +subcategory: "" +description: |- + +--- + +# looker_user_attribute_group_value (Resource) + + + +## Example Usage + +```terraform +resource "looker_user_attribute_group_value" "my_user_attribute_group_value" { + group_id = 1 + user_attribute_id = 1 + value = "foo" +} +``` + + +## Schema + +### Required + +- **group_id** (Number) +- **user_attribute_id** (Number) +- **value** (String) + +### Optional + +- **id** (String) The ID of this resource. + + diff --git a/examples/resources/looker_user_attribute_group_value/resource.tf b/examples/resources/looker_user_attribute_group_value/resource.tf new file mode 100644 index 0000000..4aa8acc --- /dev/null +++ b/examples/resources/looker_user_attribute_group_value/resource.tf @@ -0,0 +1,5 @@ +resource "looker_user_attribute_group_value" "my_user_attribute_group_value" { + group_id = 1 + user_attribute_id = 1 + value = "foo" +} diff --git a/pkg/looker/provider.go b/pkg/looker/provider.go index 1044fd6..063944d 100644 --- a/pkg/looker/provider.go +++ b/pkg/looker/provider.go @@ -51,18 +51,19 @@ 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_user_attribute_user_value": resourceUserAttributeUserValue(), - "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_user_attribute_group_value": resourceUserAttributeGroupValue(), + "looker_connection": resourceConnection(), + "looker_lookml_model": resourceLookMLModel(), }, ConfigureContextFunc: providerConfigure, diff --git a/pkg/looker/resource_user_attribute_group_value.go b/pkg/looker/resource_user_attribute_group_value.go new file mode 100644 index 0000000..55ef6ea --- /dev/null +++ b/pkg/looker/resource_user_attribute_group_value.go @@ -0,0 +1,160 @@ +package looker + +import ( + "context" + "strconv" + + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + apiclient "github.com/looker-open-source/sdk-codegen/go/sdk/v4" +) + +func resourceUserAttributeGroupValue() *schema.Resource { + return &schema.Resource{ + CreateContext: resourceUserAttributeGroupValueCreate, + ReadContext: resourceUserAttributeGroupValueRead, + UpdateContext: resourceUserAttributeGroupValueUpdate, + DeleteContext: resourceUserAttributeGroupValueDelete, + Importer: &schema.ResourceImporter{ + StateContext: schema.ImportStatePassthroughContext, + }, + + Schema: map[string]*schema.Schema{ + "user_attribute_id": { + Type: schema.TypeInt, + Required: true, + }, + "group_id": { + Type: schema.TypeInt, + Required: true, + }, + "value": { + Type: schema.TypeString, + Required: true, + }, + }, + } +} + +func resourceUserAttributeGroupValueCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + client := m.(*apiclient.LookerSDK) + + groupID := int64(d.Get("group_id").(int)) + userAttributeID := int64(d.Get("user_attribute_id").(int)) + value := d.Get("value").(string) + + body := apiclient.UserAttributeGroupValue{ + GroupId: &groupID, + UserAttributeId: &userAttributeID, + Value: &value, + } + userAttributeGroupValue, err := client.UpdateUserAttributeGroupValue(groupID, userAttributeID, body, nil) + if err != nil { + return diag.FromErr(err) + } + + groupIDString := strconv.Itoa(int(*userAttributeGroupValue.GroupId)) + userAttributeIDString := strconv.Itoa(int(*userAttributeGroupValue.UserAttributeId)) + id := buildTwoPartID(&groupIDString, &userAttributeIDString) + + d.SetId(id) + + return resourceUserAttributeGroupValueRead(ctx, d, m) +} + +func resourceUserAttributeGroupValueRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + client := m.(*apiclient.LookerSDK) + + groupIDString, userAttributeIDString, err := parseTwoPartID(d.Id()) + if err != nil { + return diag.FromErr(err) + } + groupID, err := strconv.ParseInt(groupIDString, 10, 64) + if err != nil { + return diag.FromErr(err) + } + userAttributeID, err := strconv.ParseInt(userAttributeIDString, 10, 64) + if err != nil { + return diag.FromErr(err) + } + + userAttributeGroupValues, err := client.AllUserAttributeGroupValues(userAttributeID, "", nil) + if err != nil { + return diag.FromErr(err) + } + + var userAttributeGroupValue apiclient.UserAttributeGroupValue + for _, groupValue := range userAttributeGroupValues { + if *groupValue.GroupId == groupID { + userAttributeGroupValue = groupValue + break + } + } + + if err = d.Set("group_id", userAttributeGroupValue.GroupId); err != nil { + return diag.FromErr(err) + } + if err = d.Set("user_attribute_id", userAttributeGroupValue.UserAttributeId); err != nil { + return diag.FromErr(err) + } + if err = d.Set("value", userAttributeGroupValue.Value); err != nil { + return diag.FromErr(err) + } + + return nil +} + +func resourceUserAttributeGroupValueUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + client := m.(*apiclient.LookerSDK) + + groupIDString, userAttributeIDString, err := parseTwoPartID(d.Id()) + if err != nil { + return diag.FromErr(err) + } + groupID, err := strconv.ParseInt(groupIDString, 10, 64) + if err != nil { + return diag.FromErr(err) + } + userAttributeID, err := strconv.ParseInt(userAttributeIDString, 10, 64) + if err != nil { + return diag.FromErr(err) + } + + value := d.Get("value").(string) + + body := apiclient.UserAttributeGroupValue{ + GroupId: &groupID, + UserAttributeId: &userAttributeID, + Value: &value, + } + _, err = client.UpdateUserAttributeGroupValue(groupID, userAttributeID, body, nil) + if err != nil { + return diag.FromErr(err) + } + + return resourceUserAttributeGroupValueRead(ctx, d, m) +} + +func resourceUserAttributeGroupValueDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + client := m.(*apiclient.LookerSDK) + + groupIDString, userAttributeIDString, err := parseTwoPartID(d.Id()) + if err != nil { + return diag.FromErr(err) + } + groupID, err := strconv.ParseInt(groupIDString, 10, 64) + if err != nil { + return diag.FromErr(err) + } + userAttributeID, err := strconv.ParseInt(userAttributeIDString, 10, 64) + if err != nil { + return diag.FromErr(err) + } + + err = client.DeleteUserAttributeGroupValue(groupID, userAttributeID, nil) + if err != nil { + return diag.FromErr(err) + } + + return nil +} diff --git a/pkg/looker/resource_user_attribute_group_value_test.go b/pkg/looker/resource_user_attribute_group_value_test.go new file mode 100644 index 0000000..8a7a48b --- /dev/null +++ b/pkg/looker/resource_user_attribute_group_value_test.go @@ -0,0 +1,134 @@ +package looker + +import ( + "fmt" + "strconv" + "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_UserAttributeGroupValue(t *testing.T) { + groupValue1 := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) + groupValue2 := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + // Test: Create + { + Config: userAttributeGroupValueConfig(groupValue1), + Check: resource.ComposeTestCheckFunc( + testAccCheckUserAttributeGroupValueExists("looker_user_attribute_group_value.test"), + resource.TestCheckResourceAttr("looker_user_attribute_group_value.test", "value", groupValue1), + ), + }, + // Test: Update + { + Config: userAttributeGroupValueConfig(groupValue2), + Check: resource.ComposeTestCheckFunc( + testAccCheckUserAttributeGroupValueExists("looker_user_attribute_group_value.test"), + resource.TestCheckResourceAttr("looker_user_attribute_group_value.test", "value", groupValue2), + ), + }, + // Test: Import + { + ResourceName: "looker_user_attribute_group_value.test", + ImportState: true, + ImportStateVerify: true, + }, + }, + CheckDestroy: testAccCheckUserAttributeGroupValueDestroy, + }) +} + +func testAccCheckUserAttributeGroupValueExists(n string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("user attribute group value setting not found from resources: %s", n) + } + if rs.Primary.ID == "" { + return fmt.Errorf("no user attribute group value setting ID is set") + } + + _, userAttributeIDString, err := parseTwoPartID(rs.Primary.ID) + if err != nil { + return err + } + + userAttributeID, err := strconv.ParseInt(userAttributeIDString, 10, 64) + if err != nil { + return err + } + + client := testAccProvider.Meta().(*apiclient.LookerSDK) + userAttributeGroupValues, err := client.AllUserAttributeGroupValues(userAttributeID, "", nil) + if err != nil { + return err + } + + if len(userAttributeGroupValues) != 1 { + return fmt.Errorf("looker_user_attribute_group_value '%s' doesn't exist", rs.Primary.ID) + } + + return nil + } +} + +func testAccCheckUserAttributeGroupValueDestroy(s *terraform.State) error { + client := testAccProvider.Meta().(*apiclient.LookerSDK) + + for _, rs := range s.RootModule().Resources { + if rs.Type != "looker_user_attribute_group_value" { + continue + } + + _, userAttributeIDString, err := parseTwoPartID(rs.Primary.ID) + if err != nil { + return err + } + + userAttributeID, err := strconv.ParseInt(userAttributeIDString, 10, 64) + if err != nil { + return err + } + + userAttributeGroupValues, err := client.AllUserAttributeGroupValues(userAttributeID, "", nil) + if err != nil { + if strings.Contains(err.Error(), "404") { + return nil // successfully destroyed + } + return err + } + + if len(userAttributeGroupValues) != 0 { + return fmt.Errorf("looker_user_attribute_group_value '%s' still exists", rs.Primary.ID) + } + } + + return nil +} + +func userAttributeGroupValueConfig(groupValue string) string { + return fmt.Sprintf(` + resource "looker_group" "test" { + name = "testing" + } + resource "looker_user_attribute" "test" { + name = "testing" + type = "string" + label = "testing" + } + resource "looker_user_attribute_group_value" "test" { + group_id = looker_group.test.id + user_attribute_id = looker_user_attribute.test.id + value = "%s" + } + `, groupValue) +}