Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New data source: okta_app_user_profile #1488

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions examples/okta_app_user_profile/datasource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
resource "okta_app_oauth" "test" {
label = "testAcc_replace_with_uuid"
type = "web"
grant_types = ["implicit", "authorization_code"]
redirect_uris = ["http://d.com/"]
response_types = ["code", "token", "id_token"]
issuer_mode = "ORG_URL"

lifecycle {
ignore_changes = [users, groups]
}
}

resource "okta_user" "test" {
first_name = "TestAcc"
last_name = "Smith"
login = "[email protected]"
email = "[email protected]"
}

resource "okta_app_user_schema_property" "test" {
app_id = okta_app_oauth.test.id
index = "testCustom"
title = "terraform acceptance test"
type = "string"
description = "terraform acceptance test updated"
required = true
master = "OKTA"
scope = "SELF"
}

resource "okta_app_user" "test" {
app_id = okta_app_oauth.test.id
user_id = okta_user.test.id
username = okta_user.test.email

profile = <<JSON
{"testCustom":"testing"}
JSON

depends_on = [okta_app_user_schema_property.test]
}

data "okta_app_user_profile" "test" {
app_id = okta_app_oauth.test.id
user_id = okta_user.test.id
}
60 changes: 60 additions & 0 deletions okta/data_source_okta_app_user_profile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package okta

import (
"context"
"encoding/json"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/okta/okta-sdk-golang/v2/okta"
"github.com/okta/okta-sdk-golang/v2/okta/query"
)

func dataSourceAppUserProfile() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceAppUserProfileRead,
Schema: map[string]*schema.Schema{
"app_id": {
Type: schema.TypeString,
Required: true,
Description: "ID of the Okta App being queried for",
ForceNew: true,
},
"user_id": {
Type: schema.TypeString,
Required: true,
Description: "ID of the User associated with the application",
ForceNew: true,
},
"profile": {
Type: schema.TypeString,
Computed: true,
Description: "The application profile for the user",
ForceNew: true,
},
},
}
}

func dataSourceAppUserProfileRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := getOktaClientFromMetadata(m)
appId := d.Get("app_id").(string)
userId := d.Get("user_id").(string)

var appUser *okta.AppUser

appUser, _, err := client.Application.GetApplicationUser(ctx, appId, userId, &query.Params{})
if err != nil {
return diag.Errorf("unable to get profile for user (%s) assisgned to app (%s): %s", userId, appId, err)
}

jsonProfile, err := json.Marshal(appUser.Profile)
if err != nil {
return diag.Errorf("failed to marshal app user profile to JSON: %v", err)
}
_ = d.Set("profile", string(jsonProfile))

_ = d.Set("user_id", userId)
d.SetId(appId)
return nil
}
28 changes: 28 additions & 0 deletions okta/data_source_okta_app_user_profile_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package okta

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccOktaDataSourceAppUserProfile_read(t *testing.T) {
ri := acctest.RandInt()
mgr := newFixtureManager("okta_app_user_profile")
config := mgr.GetFixtures("datasource.tf", ri, t)

resource.Test(t, resource.TestCase{
PreCheck: testAccPreCheck(t),
ErrorCheck: testAccErrorChecks(t),
ProviderFactories: testAccProvidersFactories,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.okta_app_user_profile.test", "profile"),
),
},
},
})
}
1 change: 1 addition & 0 deletions okta/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ func Provider() *schema.Provider {
appSaml: dataSourceAppSaml(),
appSignOnPolicy: dataSourceAppSignOnPolicy(),
appUserAssignments: dataSourceAppUserAssignments(),
appUserProfile: dataSourceAppUserProfile(),
authenticator: dataSourceAuthenticator(),
authServer: dataSourceAuthServer(),
authServerClaim: dataSourceAuthServerClaim(),
Expand Down
48 changes: 48 additions & 0 deletions website/docs/d/app_user_profile.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
layout: 'okta'
page_title: 'Okta: okta_app_user_profile'
sidebar_current: 'docs-okta-datasource-app-user-profile'
description: |-
Get the application profile for a user assigned to an Okta application.
---


# okta_app_user_profile

Use this data source to get the app profile for a user assigned to the given Okta application (by ID). This allows you to retrieve
custom app profile attributes for use within Terraform.

## Example Usage

```hcl
data "okta_app" "example" {
label = "Example App"

skip_groups = true
skip_users = true
}

data "okta_user" "example" {
user_id = "00u22mtxlrJ8YkzXQ357"
}

data "okta_app_user_profile" "test" {
app_id = data.okta_app.example.id
user_id = data.okta_user.example.id
}
```

## Argument Reference

- `app_id` - (Required) The ID of the Okta application you want to retrieve the user profile for.

- `user_id` - (Required) The ID of the user you want to retrieve the user profile for.

## Attribute Reference

- `app_id` - ID of the application.

- `user_id` - ID of the user.

- `profile` - The profile for the user.