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

Added new okta_link_definition and okta_link_value resources #794

Merged
merged 1 commit into from
Nov 12, 2021
Merged
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
7 changes: 7 additions & 0 deletions examples/okta_link_definition/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# okta_link_definition

This resource represents an Okta Linked Object Definition. For more information see
the [API docs](https://developer.okta.com/docs/reference/api/linked-objects/#link-definition-operations).
Note that this resource is immutable, thus can not be modified.

- Simple example [can be found here](./basic.tf).
8 changes: 8 additions & 0 deletions examples/okta_link_definition/basic.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
resource "okta_link_definition" "test" {
primary_name = "testAcc_replace_with_uuid"
primary_title = "Manager"
primary_description = "Manager link property"
associated_name = "testAcc_subordinate"
associated_title = "Subordinate"
associated_description = "Subordinate link property"
}
6 changes: 6 additions & 0 deletions examples/okta_link_value/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# okta_link_value

This resource represents a relationship between users. For more information see
the [API docs](https://developer.okta.com/docs/reference/api/linked-objects/#link-value-operations).

- Simple example [can be found here](./basic.tf).
27 changes: 27 additions & 0 deletions examples/okta_link_value/basic.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
resource "okta_link_definition" "test" {
primary_name = "testAcc_replace_with_uuid"
primary_title = "Manager"
primary_description = "Manager link property"
associated_name = "testAcc_subordinate"
associated_title = "Subordinate"
associated_description = "Subordinate link property"
}

resource "okta_user" "test" {
count = 5
first_name = "TestAcc"
last_name = "Smith"
login = "testAcc_${count.index}@example.com"
email = "testAcc_${count.index}@example.com"
}

resource "okta_link_value" "test" {
primary_name = okta_link_definition.test.primary_name
primary_user_id = okta_user.test[0].id
associated_user_ids = [
okta_user.test[1].id,
okta_user.test[2].id,
okta_user.test[3].id,
okta_user.test[4].id,
]
}
24 changes: 24 additions & 0 deletions examples/okta_link_value/updated.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
resource "okta_link_definition" "test" {
primary_name = "testAcc_replace_with_uuid"
primary_title = "Manager"
primary_description = "Manager link property"
associated_name = "testAcc_subordinate"
associated_title = "Subordinate"
associated_description = "Subordinate link property"
}

resource "okta_user" "test" {
count = 5
first_name = "TestAcc"
last_name = "Smith"
login = "testAcc_${count.index}@example.com"
email = "testAcc_${count.index}@example.com"
}

resource "okta_link_value" "test" {
primary_name = okta_link_definition.test.primary_name
primary_user_id = okta_user.test[0].id
associated_user_ids = [
okta_user.test[4].id,
]
}
8 changes: 6 additions & 2 deletions okta/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (

// Resource names, defined in place, used throughout the provider and tests
const (
adminRoleTargets = "okta_admin_role_targets"
adminRoleCustom = "okta_admin_role_custom"
adminRoleCustomAssignments = "okta_admin_role_custom_assignments"
adminRoleTargets = "okta_admin_role_targets"
app = "okta_app"
appAutoLogin = "okta_app_auto_login"
appBasicAuth = "okta_app_basic_auth"
Expand Down Expand Up @@ -78,6 +78,8 @@ const (
idpSamlKey = "okta_idp_saml_key"
idpSocial = "okta_idp_social"
inlineHook = "okta_inline_hook"
linkDefinition = "okta_link_definition"
linkValue = "okta_link_value"
networkZone = "okta_network_zone"
orgConfiguration = "okta_org_configuration"
orgSupport = "okta_org_support"
Expand Down Expand Up @@ -218,9 +220,9 @@ func Provider() *schema.Provider {
},
},
ResourcesMap: map[string]*schema.Resource{
adminRoleTargets: resourceAdminRoleTargets(),
adminRoleCustom: resourceAdminRoleCustom(),
adminRoleCustomAssignments: resourceAdminRoleCustomAssignments(),
adminRoleTargets: resourceAdminRoleTargets(),
appAutoLogin: resourceAppAutoLogin(),
appBasicAuth: resourceAppBasicAuth(),
appBookmark: resourceAppBookmark(),
Expand Down Expand Up @@ -269,6 +271,8 @@ func Provider() *schema.Provider {
idpSamlKey: resourceIdpSigningKey(),
idpSocial: resourceIdpSocial(),
inlineHook: resourceInlineHook(),
linkDefinition: resourceLinkDefinition(),
linkValue: resourceLinkValue(),
networkZone: resourceNetworkZone(),
orgConfiguration: resourceOrgConfiguration(),
orgSupport: resourceOrgSupport(),
Expand Down
1 change: 1 addition & 0 deletions okta/provider_sweeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func TestMain(m *testing.M) {
setupSweeper(behavior, sweepBehaviors)
setupSweeper(resourceSet, sweepResourceSets)
setupSweeper(adminRoleCustom, sweepCustomRoles)
setupSweeper(linkDefinition, sweepLinkDefinitions)

resource.TestMain(m)
}
Expand Down
32 changes: 15 additions & 17 deletions okta/resource_okta_app_signon_policy_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestAccOktaAppSignOnPolicyRule(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProvidersFactories,
CheckDestroy: appSignOnPolicyRuleExists(),
CheckDestroy: appSignOnPolicyRuleExists,
Steps: []resource.TestStep{
{
Config: config,
Expand Down Expand Up @@ -70,24 +70,22 @@ func TestAccOktaAppSignOnPolicyRule(t *testing.T) {
})
}

func appSignOnPolicyRuleExists() func(*terraform.State) error {
return func(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != appSignOnPolicyRule {
continue
}
rule, resp, err := getSupplementFromMetadata(testAccProvider.Meta()).
GetAppSignOnPolicyRule(context.Background(), rs.Primary.Attributes["policy_id"], rs.Primary.ID)
if resp != nil && resp.StatusCode == http.StatusNotFound {
return nil
} else if err != nil {
return err
}
if rule != nil {
return fmt.Errorf("app sign-on policy rule still exists, ID: %s, PolicyID: %s", rs.Primary.ID, rs.Primary.Attributes["policy_id"])
}
func appSignOnPolicyRuleExists(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != appSignOnPolicyRule {
continue
}
rule, resp, err := getSupplementFromMetadata(testAccProvider.Meta()).
GetAppSignOnPolicyRule(context.Background(), rs.Primary.Attributes["policy_id"], rs.Primary.ID)
if resp != nil && resp.StatusCode == http.StatusNotFound {
return nil
} else if err != nil {
return err
}
if rule != nil {
return fmt.Errorf("app sign-on policy rule still exists, ID: %s, PolicyID: %s", rs.Primary.ID, rs.Primary.Attributes["policy_id"])
}
return nil
}
return nil
}
108 changes: 108 additions & 0 deletions okta/resource_okta_link_definition.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package okta

import (
"context"

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

func resourceLinkDefinition() *schema.Resource {
return &schema.Resource{
CreateContext: resourceLinkDefinitionCreate,
ReadContext: resourceLinkDefinitionRead,
DeleteContext: resourceLinkDefinitionDelete,
Importer: &schema.ResourceImporter{StateContext: schema.ImportStatePassthroughContext},
Schema: map[string]*schema.Schema{
"primary_name": {
Type: schema.TypeString,
Required: true,
Description: "API name of the primary link.",
ForceNew: true,
},
"primary_title": {
Type: schema.TypeString,
Required: true,
Description: "Display name of the primary link.",
ForceNew: true,
},
"primary_description": {
Type: schema.TypeString,
Required: true,
Description: "Description of the primary relationship.",
ForceNew: true,
},
"associated_name": {
Type: schema.TypeString,
Required: true,
Description: "API name of the associated link.",
ForceNew: true,
},
"associated_title": {
Type: schema.TypeString,
Required: true,
Description: "Display name of the associated link.",
ForceNew: true,
},
"associated_description": {
Type: schema.TypeString,
Required: true,
Description: "Description of the associated relationship.",
ForceNew: true,
},
},
}
}

func resourceLinkDefinitionCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
linkedObject := sdk.LinkedObject{
Primary: &sdk.LinkedObjectPart{
Name: d.Get("primary_name").(string),
Title: d.Get("primary_title").(string),
Description: d.Get("primary_description").(string),
Type: "USER",
},
Associated: &sdk.LinkedObjectPart{
Name: d.Get("associated_name").(string),
Title: d.Get("associated_title").(string),
Description: d.Get("associated_description").(string),
Type: "USER",
},
}
_, _, err := getSupplementFromMetadata(m).CreateLinkedObject(ctx, linkedObject)
if err != nil {
return diag.Errorf("failed to create linked object: %v", err)
}
d.SetId(d.Get("primary_name").(string))
return nil
}

func resourceLinkDefinitionRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
linkedObject, resp, err := getSupplementFromMetadata(m).GetLinkedObject(ctx, d.Id())
if err := suppressErrorOn404(resp, err); err != nil {
return diag.Errorf("failed to get linked object: %v", err)
}
if linkedObject == nil {
d.SetId("")
return nil
}
if d.Id() != linkedObject.Primary.Name {
d.SetId(linkedObject.Primary.Name)
}
_ = d.Set("primary_name", linkedObject.Primary.Name)
_ = d.Set("primary_title", linkedObject.Primary.Title)
_ = d.Set("primary_description", linkedObject.Primary.Description)
_ = d.Set("associated_name", linkedObject.Associated.Name)
_ = d.Set("associated_title", linkedObject.Associated.Title)
_ = d.Set("associated_description", linkedObject.Associated.Description)
return nil
}

func resourceLinkDefinitionDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
resp, err := getSupplementFromMetadata(m).DeleteLinkedObject(ctx, d.Id())
if err := suppressErrorOn404(resp, err); err != nil {
return diag.Errorf("failed to remove linked object: %v", err)
}
return nil
}
57 changes: 57 additions & 0 deletions okta/resource_okta_link_definition_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package okta

import (
"context"
"fmt"
"strings"
"testing"

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

func sweepLinkDefinitions(client *testClient) error {
var errorList []error
linkedObjects, _, err := client.apiSupplement.ListLinkedObjects(context.Background())
if err != nil {
return err
}
for _, object := range linkedObjects {
if strings.HasPrefix(object.Primary.Name, testResourcePrefix) {
if _, err := client.apiSupplement.DeleteLinkedObject(context.Background(), object.Primary.Name); err != nil {
errorList = append(errorList, err)
}
}
}
return condenseError(errorList)
}

func TestAccOktaLinkDefinition(t *testing.T) {
ri := acctest.RandInt()
mgr := newFixtureManager(linkDefinition)
config := mgr.GetFixtures("basic.tf", ri, t)
resourceName := fmt.Sprintf("%s.test", linkDefinition)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProvidersFactories,
CheckDestroy: createCheckResourceDestroy(linkDefinition, doesLinkDefinitionExist),
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "primary_name", buildResourceName(ri)),
resource.TestCheckResourceAttr(resourceName, "primary_title", "Manager"),
resource.TestCheckResourceAttr(resourceName, "primary_description", "Manager link property"),
resource.TestCheckResourceAttr(resourceName, "associated_name", "testAcc_subordinate"),
resource.TestCheckResourceAttr(resourceName, "associated_title", "Subordinate"),
resource.TestCheckResourceAttr(resourceName, "associated_description", "Subordinate link property"),
),
},
},
})
}

func doesLinkDefinitionExist(id string) (bool, error) {
_, response, err := getSupplementFromMetadata(testAccProvider.Meta()).GetLinkedObject(context.Background(), id)
return doesResourceExist(response, err)
}
Loading