From 109ee844285d0e21e5ef12e0fc92f1b889c36ad4 Mon Sep 17 00:00:00 2001 From: Tien Nguyen Date: Wed, 5 Jun 2024 15:10:16 -0400 Subject: [PATCH] add import okta_profile_mapping Signed-off-by: Tien Nguyen --- .../resources/okta_profile_mapping/import.sh | 1 + okta/resource_okta_profile_mapping.go | 13 +++- okta/resource_okta_profile_mapping_test.go | 78 +++++++++++++++++++ website/docs/r/profile_mapping.html.markdown | 2 +- 4 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 examples/resources/okta_profile_mapping/import.sh diff --git a/examples/resources/okta_profile_mapping/import.sh b/examples/resources/okta_profile_mapping/import.sh new file mode 100644 index 000000000..2a1a638a3 --- /dev/null +++ b/examples/resources/okta_profile_mapping/import.sh @@ -0,0 +1 @@ +terraform import okta_profile_mapping.example <id> \ No newline at end of file diff --git a/okta/resource_okta_profile_mapping.go b/okta/resource_okta_profile_mapping.go index 04043b92c..dd2658a18 100644 --- a/okta/resource_okta_profile_mapping.go +++ b/okta/resource_okta_profile_mapping.go @@ -74,6 +74,9 @@ func resourceProfileMapping() *schema.Resource { Default: false, }, }, + Importer: &schema.ResourceImporter{ + StateContext: schema.ImportStatePassthroughContext, + }, } } @@ -108,7 +111,8 @@ func resourceProfileMappingCreate(ctx context.Context, d *schema.ResourceData, m } d.SetId(mapping.Id) newMapping := buildMapping(d) - if d.Get("delete_when_absent").(bool) { + deleteWhenAbsent, ok := d.GetOk("delete_when_absent") + if ok && deleteWhenAbsent.(bool) { newMapping.Properties = mergeProperties(newMapping.Properties, getDeleteProperties(d, mapping.Properties)) } _, _, err = getOktaClientFromMetadata(m).ProfileMapping.UpdateProfileMapping(ctx, mapping.Id, newMapping) @@ -131,12 +135,14 @@ func resourceProfileMappingRead(ctx context.Context, d *schema.ResourceData, m i d.SetId("") return nil } + _ = d.Set("source_id", mapping.Source.Id) _ = d.Set("source_type", mapping.Source.Type) _ = d.Set("source_name", mapping.Source.Name) _ = d.Set("target_type", mapping.Target.Type) _ = d.Set("target_id", mapping.Target.Id) _ = d.Set("target_name", mapping.Target.Name) - if !d.Get("delete_when_absent").(bool) { + deleteWhenAbsent, ok := d.GetOk("delete_when_absent") + if ok && !deleteWhenAbsent.(bool) { current := buildMappingProperties(d.Get("mappings").(*schema.Set)) for k := range mapping.Properties { if _, ok := current[k]; !ok { @@ -159,7 +165,8 @@ func resourceProfileMappingUpdate(ctx context.Context, d *schema.ResourceData, m return diag.Errorf("no profile mappings found for source ID '%s' and target ID '%s'", sourceID, targetID) } newMapping := buildMapping(d) - if d.Get("delete_when_absent").(bool) { + deleteWhenAbsent, ok := d.GetOk("delete_when_absent") + if ok && deleteWhenAbsent.(bool) { newMapping.Properties = mergeProperties(newMapping.Properties, getDeleteProperties(d, mapping.Properties)) } _, _, err = getOktaClientFromMetadata(m).ProfileMapping.UpdateProfileMapping(ctx, mapping.Id, newMapping) diff --git a/okta/resource_okta_profile_mapping_test.go b/okta/resource_okta_profile_mapping_test.go index 604d70f77..335d53480 100644 --- a/okta/resource_okta_profile_mapping_test.go +++ b/okta/resource_okta_profile_mapping_test.go @@ -2,10 +2,12 @@ package okta import ( "context" + "errors" "fmt" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" ) func TestAccResourceOktaProfileMapping_crud(t *testing.T) { @@ -43,6 +45,82 @@ func TestAccResourceOktaProfileMapping_crud(t *testing.T) { }) } +func TestAccResourceOktaProfileMapping_import(t *testing.T) { + resourceName := fmt.Sprintf("%s.test", profileMapping) + config := ` + resource "okta_profile_mapping" "test" { + source_id = okta_idp_social.google.id + target_id = data.okta_user_profile_mapping_source.user.id + delete_when_absent = true + + mappings { + id = "firstName" + expression = "appuser.firstName" + } + + mappings { + id = "lastName" + expression = "appuser.lastName" + } + + mappings { + id = "email" + expression = "appuser.email" + } + + mappings { + id = "login" + expression = "appuser.email" + } + } + + resource "okta_idp_social" "google" { + type = "GOOGLE" + protocol_type = "OIDC" + name = "testAcc_google_replace_with_uuid" + + scopes = [ + "profile", + "email", + "openid", + ] + + client_id = "abcd123" + client_secret = "abcd123" + username_template = "idpuser.email" + } + + data "okta_user_profile_mapping_source" "user" { + depends_on = [okta_idp_social.google] + } + + ` + oktaResourceTest(t, resource.TestCase{ + ProtoV5ProviderFactories: testAccMergeProvidersFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttrSet(resourceName, "id"), + resource.TestCheckResourceAttrSet(resourceName, "source_id"), + resource.TestCheckResourceAttrSet(resourceName, "target_id"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateCheck: func(s []*terraform.InstanceState) error { + if len(s) != 1 { + return errors.New("failed to import schema into state") + } + return nil + }, + }, + }, + }) +} + // TODO deprecated endpoint func doesOktaProfileExist(profileID string) (bool, error) { client := sdkSupplementClientForTest() diff --git a/website/docs/r/profile_mapping.html.markdown b/website/docs/r/profile_mapping.html.markdown index 4e2ac5aba..b4fbfa363 100644 --- a/website/docs/r/profile_mapping.html.markdown +++ b/website/docs/r/profile_mapping.html.markdown @@ -81,4 +81,4 @@ The following arguments are supported: ## Import -There is no reason to import this resource. You can simply create the resource config and point it to a source ID. Mind here, once the source is deleted this resources will no longer exist. +terraform import okta_profile_mapping.example <id> \ No newline at end of file