Skip to content

Commit

Permalink
Merge pull request #2004 from okta/OKTA-717455-add-import-okta_profil…
Browse files Browse the repository at this point in the history
…e_mapping

add import okta_profile_mapping
  • Loading branch information
duytiennguyen-okta authored Jun 6, 2024
2 parents 5c667ce + 109ee84 commit 8772b2a
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 4 deletions.
1 change: 1 addition & 0 deletions examples/resources/okta_profile_mapping/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
terraform import okta_profile_mapping.example <id>
13 changes: 10 additions & 3 deletions okta/resource_okta_profile_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ func resourceProfileMapping() *schema.Resource {
Default: false,
},
},
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
}
}

Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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)
Expand Down
78 changes: 78 additions & 0 deletions okta/resource_okta_profile_mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/profile_mapping.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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>

0 comments on commit 8772b2a

Please sign in to comment.