Skip to content

Commit

Permalink
DXCDT-644: clean up IDs, address other PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
acwest committed Jul 16, 2024
1 parent 086634e commit 469200f
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 63 deletions.
8 changes: 4 additions & 4 deletions docs/data-sources/connection_scim_configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,20 @@ data "auth0_connection_scim_configuration" "my_conn_scim_configuration_data" {
### Read-Only

- `connection_name` (String) Name of the connection for this SCIM configuration.
- `default_mapping` (Set of Object) Default mapping between Auth0 attributes and SCIM attributes for this connectiion. (see [below for nested schema](#nestedatt--default_mapping))
- `default_mapping` (Block Set) Default mapping between Auth0 attributes and SCIM attributes for this connection type. (see [below for nested schema](#nestedblock--default_mapping))
- `id` (String) The ID of this resource.
- `mapping` (Set of Object) Mapping between Auth0 attributes and SCIM attributes. (see [below for nested schema](#nestedatt--mapping))
- `strategy` (String) Schema of the connection for this SCIM configuration.
- `tenant_name` (String) Name of the tenant for this SCIM configuration.
- `user_id_attribute` (String) User ID attribute for generation unique of user ids.

<a id="nestedatt--default_mapping"></a>
<a id="nestedblock--default_mapping"></a>
### Nested Schema for `default_mapping`

Read-Only:

- `auth0` (String)
- `scim` (String)
- `auth0` (String) The field location in the Auth0 schema.
- `scim` (String) The field location in the SCIM schema.


<a id="nestedatt--mapping"></a>
Expand Down
10 changes: 0 additions & 10 deletions docs/resources/connection_scim_configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ resource "auth0_connection_scim_configuration" "my_conn_scim_configuration" {
### Read-Only

- `connection_name` (String) Name of the connection for this SCIM configuration.
- `default_mapping` (Set of Object) Default mapping between Auth0 attributes and SCIM attributes for this connectiion. (see [below for nested schema](#nestedatt--default_mapping))
- `id` (String) The ID of this resource.
- `strategy` (String) Schema of the connection for this SCIM configuration.
- `tenant_name` (String) Name of the tenant for this SCIM configuration.
Expand All @@ -93,15 +92,6 @@ Required:
- `auth0` (String) The field location in the Auth0 schema.
- `scim` (String) The field location in the SCIM schema.


<a id="nestedatt--default_mapping"></a>
### Nested Schema for `default_mapping`

Read-Only:

- `auth0` (String)
- `scim` (String)

## Import

Import is supported using the following syntax:
Expand Down
54 changes: 53 additions & 1 deletion internal/auth0/connection/data_source_scim.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package connection

import (
"context"

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

"github.com/auth0/terraform-provider-auth0/internal/config"
internalError "github.com/auth0/terraform-provider-auth0/internal/error"
internalSchema "github.com/auth0/terraform-provider-auth0/internal/schema"
)

// NewSCIMConfigurationDataSource will return a new auth0_connection_scim_configuration data source.
func NewSCIMConfigurationDataSource() *schema.Resource {
return &schema.Resource{
ReadContext: readSCIMConfiguration,
ReadContext: readSCIMConfigurationDataSource,
Description: "Data source to retrieve a SCIM configuration for an Auth0 connection by `connection_id`.",
Schema: getSCIMDataSourceSchema(),
}
Expand All @@ -20,6 +25,53 @@ func getSCIMDataSourceSchema() map[string]*schema.Schema {
internalSchema.SetExistingAttributesAsRequired(dataSourceSchema, "connection_id")
dataSourceSchema["user_id_attribute"].Description = "User ID attribute for generation unique of user ids."
dataSourceSchema["mapping"].Description = "Mapping between Auth0 attributes and SCIM attributes."
dataSourceSchema["default_mapping"] = &schema.Schema{
Type: schema.TypeSet,
Optional: true,
RequiredWith: []string{"user_id_attribute", "mapping"},
Computed: true,
Description: "Default mapping between Auth0 attributes and SCIM attributes for this connection type.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"auth0": {
Type: schema.TypeString,
Computed: true,
Description: "The field location in the Auth0 schema.",
},
"scim": {
Type: schema.TypeString,
Computed: true,
Description: "The field location in the SCIM schema.",
},
},
},
}

return dataSourceSchema
}

func readSCIMConfigurationDataSource(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
api := meta.(*config.Config).GetAPI()

connectionID := data.Get("connection_id").(string)
scimConfiguration, err := api.Connection.ReadSCIMConfiguration(ctx, connectionID)
if err != nil {
return diag.FromErr(err)
}

defaultSCIMConfiguration, err := api.Connection.ReadSCIMDefaultConfiguration(ctx, connectionID)
if err != nil {
return diag.FromErr(internalError.HandleAPIError(data, err))
}

diags := flattenSCIMConfiguration(data, scimConfiguration)
if diags.HasError() {
return diags
}
err = data.Set("default_mapping", flattenSCIMMappings(defaultSCIMConfiguration.GetMapping()))
if err == nil {
data.SetId(connectionID)
}

return diag.FromErr(err)
}
1 change: 0 additions & 1 deletion internal/auth0/connection/data_source_scim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ func TestAccDataSCIMConfiguration(t *testing.T) {
resource.TestCheckResourceAttrPair("data.auth0_connection_scim_configuration.my_scim_config", "strategy", "auth0_connection_scim_configuration.my_scim_config", "strategy"),
resource.TestCheckResourceAttrPair("data.auth0_connection_scim_configuration.my_scim_config", "user_id_attribute", "auth0_connection_scim_configuration.my_scim_config", "user_id_attribute"),
resource.TestCheckResourceAttrPair("data.auth0_connection_scim_configuration.my_scim_config", "mapping.#", "auth0_connection_scim_configuration.my_scim_config", "mapping.#"),
resource.TestCheckResourceAttrPair("data.auth0_connection_scim_configuration.my_scim_config", "default_mapping.#", "auth0_connection_scim_configuration.my_scim_config", "default_mapping.#"),
),
},
{
Expand Down
4 changes: 2 additions & 2 deletions internal/auth0/connection/flatten.go
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,7 @@ func flattenSCIMMappings(mappings []management.SCIMConfigurationMapping) *[]map[
return &flattenedMappings
}

func flattenSCIMConfiguration(data *schema.ResourceData, scimConfiguration *management.SCIMConfiguration) error {
func flattenSCIMConfiguration(data *schema.ResourceData, scimConfiguration *management.SCIMConfiguration) diag.Diagnostics {
result := multierror.Append(
data.Set("connection_id", scimConfiguration.GetConnectionID()),
data.Set("connection_name", scimConfiguration.GetConnectionName()),
Expand All @@ -943,5 +943,5 @@ func flattenSCIMConfiguration(data *schema.ResourceData, scimConfiguration *mana
data.Set("tenant_name", scimConfiguration.GetTenantName()),
)

return result.ErrorOrNil()
return diag.FromErr(result.ErrorOrNil())
}
52 changes: 9 additions & 43 deletions internal/auth0/connection/resource_scim.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ package connection
import (
"context"

"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"

"github.com/auth0/terraform-provider-auth0/internal/config"
internalError "github.com/auth0/terraform-provider-auth0/internal/error"
internalSchema "github.com/auth0/terraform-provider-auth0/internal/schema"
)

// NewSCIMConfigurationResource will return a new auth0_connection_scim_configuration (1:1) resource.
Expand All @@ -21,7 +19,7 @@ func NewSCIMConfigurationResource() *schema.Resource {
ReadContext: readSCIMConfiguration,
DeleteContext: deleteSCIMConfiguration,
Importer: &schema.ResourceImporter{
StateContext: internalSchema.ImportResourceGroupID("connection_id"),
StateContext: schema.ImportStatePassthroughContext,
},
Description: "With this resource, you can configure [SCIM(System for Cross-domain Identity Management)](https://simplecloud.info/) support " +
"for `SAML` and `OpenID Connect` Enterprise connections.",
Expand Down Expand Up @@ -84,25 +82,6 @@ func getSCIMResourceSchema() map[string]*schema.Schema {
},
},
},
"default_mapping": {
Type: schema.TypeSet,
Computed: true,
Description: "Default mapping between Auth0 attributes and SCIM attributes for this connectiion.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"auth0": {
Type: schema.TypeString,
Computed: true,
Description: "The field location in the Auth0 schema.",
},
"scim": {
Type: schema.TypeString,
Computed: true,
Description: "The field location in the SCIM schema.",
},
},
},
},
}
}

Expand All @@ -114,51 +93,38 @@ func createSCIMConfiguration(ctx context.Context, data *schema.ResourceData, met
if err := api.Connection.CreateSCIMConfiguration(ctx, connectionID, scimConfiguration); err != nil {
return diag.FromErr(err)
}
internalSchema.SetResourceGroupID(data, connectionID)

data.SetId(connectionID)

return readSCIMConfiguration(ctx, data, meta)
}

func updateSCIMConfiguration(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
api := meta.(*config.Config).GetAPI()
connectionID := data.Get("connection_id").(string)
scimConfiguration := expandSCIMConfiguration(data)

if err := api.Connection.UpdateSCIMConfiguration(ctx, connectionID, scimConfiguration); err != nil {
return diag.FromErr(err)
if err := api.Connection.UpdateSCIMConfiguration(ctx, data.Id(), scimConfiguration); err != nil {
return diag.FromErr(internalError.HandleAPIError(data, err))
}

return readSCIMConfiguration(ctx, data, meta)
}

func readSCIMConfiguration(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
api := meta.(*config.Config).GetAPI()
connectionID := data.Get("connection_id").(string)

scimConfiguration, err := api.Connection.ReadSCIMConfiguration(ctx, connectionID)
if err != nil {
return diag.FromErr(err)
}

defaultSCIMConfiguration, err := api.Connection.ReadSCIMDefaultConfiguration(ctx, connectionID)
scimConfiguration, err := api.Connection.ReadSCIMConfiguration(ctx, data.Id())
if err != nil {
return diag.FromErr(err)
return diag.FromErr(internalError.HandleAPIError(data, err))
}
data.SetId(connectionID)

result := multierror.Append(
flattenSCIMConfiguration(data, scimConfiguration),
data.Set("default_mapping", flattenSCIMMappings(defaultSCIMConfiguration.GetMapping())),
)

return diag.FromErr(result.ErrorOrNil())
return flattenSCIMConfiguration(data, scimConfiguration)
}

func deleteSCIMConfiguration(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
api := meta.(*config.Config).GetAPI()
connectionID := data.Id()

if err := api.Connection.DeleteSCIMConfiguration(ctx, connectionID); err != nil {
if err := api.Connection.DeleteSCIMConfiguration(ctx, data.Id()); err != nil {
return diag.FromErr(internalError.HandleAPIError(data, err))
}

Expand Down
2 changes: 0 additions & 2 deletions internal/auth0/connection/resource_scim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,6 @@ func TestAccSCIMConfiguration(t *testing.T) {
resource.TestCheckResourceAttr("auth0_connection_scim_configuration.my_scim_config", "strategy", "okta"),
resource.TestCheckResourceAttrSet("auth0_connection_scim_configuration.my_scim_config", "user_id_attribute"),
resource.TestCheckResourceAttrSet("auth0_connection_scim_configuration.my_scim_config", "mapping.#"),
resource.TestCheckResourceAttrSet("auth0_connection_scim_configuration.my_scim_config", "default_mapping.#"),
resource.TestCheckResourceAttrPair("auth0_connection_scim_configuration.my_scim_config", "mapping.#", "auth0_connection_scim_configuration.my_scim_config", "default_mapping.#"),
),
},
{
Expand Down

0 comments on commit 469200f

Please sign in to comment.