Skip to content

Commit

Permalink
DXCDT-360: Move client resources to dedicated pkg (#468)
Browse files Browse the repository at this point in the history
Co-authored-by: Rita Zerrizuela <[email protected]>
  • Loading branch information
sergiught and Widcket authored Feb 7, 2023
1 parent d92a65c commit 4a29da9
Show file tree
Hide file tree
Showing 45 changed files with 461 additions and 415 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ test-sweep: ## Clean up test tenant
${call print_warning, "WARNING: This will destroy infrastructure. Use only in development accounts."}
@read -p "Continue? [y/N] " ans && ans=$${ans:-N} ; \
if [ $${ans} = y ] || [ $${ans} = Y ]; then \
go test ./internal/provider -v -sweep="phony" $(SWEEPARGS) ; \
go test ./internal/provider ./internal/auth0/... -v -sweep="phony" $(SWEEPARGS) ; \
fi

#-----------------------------------------------------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions docs/data-sources/client.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
---
page_title: "Data Source: auth0_client"
description: |-
Data source to retrieve a specific Auth0 Application client by 'client_id' or 'name'.
Data source to retrieve a specific Auth0 application client by client_id or name.
---

# Data Source: auth0_client

Data source to retrieve a specific Auth0 Application client by 'client_id' or 'name'.
Data source to retrieve a specific Auth0 application client by `client_id` or `name`.

## Example Usage

Expand Down
4 changes: 2 additions & 2 deletions docs/data-sources/global_client.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
---
page_title: "Data Source: auth0_global_client"
description: |-
Retrieves a tenant's global Auth0 Application client.
Retrieve a tenant's global Auth0 application client.
---

# Data Source: auth0_global_client

Retrieves a tenant's global Auth0 Application client.
Retrieve a tenant's global Auth0 application client.

## Example Usage

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package provider
package client

import (
"context"
Expand All @@ -10,27 +10,29 @@ import (
internalSchema "github.com/auth0/terraform-provider-auth0/internal/schema"
)

func newDataClient() *schema.Resource {
clientDataSource := &schema.Resource{
ReadContext: readDataClient,
Schema: newClientSchema(),
Description: "Data source to retrieve a specific Auth0 Application client by 'client_id' or 'name'.",
// NewDataSource will return a new auth0_client data source.
func NewDataSource() *schema.Resource {
return &schema.Resource{
ReadContext: readClientForDataSource,
Description: "Data source to retrieve a specific Auth0 application client by `client_id` or `name`.",
Schema: dataSourceSchema(),
}
}

internalSchema.SetExistingAttributesAsOptional(clientDataSource.Schema, "name", "client_id")
clientDataSource.Schema["name"].Description = "The name of the client. If not provided, `client_id` must be set."
clientDataSource.Schema["client_id"].Description = "The ID of the client. If not provided, `name` must be set."
func dataSourceSchema() map[string]*schema.Schema {
dataSourceSchema := internalSchema.TransformResourceToDataSource(NewResource().Schema)

return clientDataSource
}
delete(dataSourceSchema, "client_secret_rotation_trigger")

internalSchema.SetExistingAttributesAsOptional(dataSourceSchema, "name", "client_id")

dataSourceSchema["name"].Description = "The name of the client. If not provided, `client_id` must be set."
dataSourceSchema["client_id"].Description = "The ID of the client. If not provided, `name` must be set."

func newClientSchema() map[string]*schema.Schema {
clientSchema := internalSchema.TransformResourceToDataSource(newClient().Schema)
delete(clientSchema, "client_secret_rotation_trigger")
return clientSchema
return dataSourceSchema
}

func readDataClient(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
func readClientForDataSource(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
clientID := d.Get("client_id").(string)
if clientID != "" {
d.SetId(clientID)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package provider
package client_test

import (
"fmt"
"testing"

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

"github.com/auth0/terraform-provider-auth0/internal/provider"
"github.com/auth0/terraform-provider-auth0/internal/recorder"
"github.com/auth0/terraform-provider-auth0/internal/template"
)
Expand Down Expand Up @@ -35,7 +36,7 @@ func TestAccDataClientByName(t *testing.T) {
httpRecorder := recorder.New(t)

resource.Test(t, resource.TestCase{
ProviderFactories: ProviderTestFactories(httpRecorder),
ProviderFactories: provider.TestFactories(httpRecorder),
PreventPostDestroyRefresh: true,
Steps: []resource.TestStep{
{
Expand All @@ -56,7 +57,7 @@ func TestAccDataClientById(t *testing.T) {
httpRecorder := recorder.New(t)

resource.Test(t, resource.TestCase{
ProviderFactories: ProviderTestFactories(httpRecorder),
ProviderFactories: provider.TestFactories(httpRecorder),
PreventPostDestroyRefresh: true,
Steps: []resource.TestStep{
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package provider
package client

import (
"strconv"
Expand Down Expand Up @@ -393,167 +393,6 @@ func mapFromState(input map[string]interface{}) map[string]interface{} {
return output
}

func flattenCustomSocialConfiguration(customSocial *management.ClientNativeSocialLogin) []interface{} {
if customSocial == nil {
return nil
}

m := map[string]interface{}{
"apple": []interface{}{
map[string]interface{}{
"enabled": customSocial.GetApple().GetEnabled(),
},
},
"facebook": []interface{}{
map[string]interface{}{
"enabled": customSocial.GetFacebook().GetEnabled(),
},
},
}

return []interface{}{m}
}

func flattenClientJwtConfiguration(jwt *management.ClientJWTConfiguration) []interface{} {
if jwt == nil {
return nil
}

return []interface{}{
map[string]interface{}{
"lifetime_in_seconds": jwt.GetLifetimeInSeconds(),
"secret_encoded": jwt.GetSecretEncoded(),
"scopes": jwt.GetScopes(),
"alg": jwt.GetAlgorithm(),
},
}
}

func flattenClientRefreshTokenConfiguration(refreshToken *management.ClientRefreshToken) []interface{} {
if refreshToken == nil {
return nil
}

return []interface{}{
map[string]interface{}{
"rotation_type": refreshToken.GetRotationType(),
"expiration_type": refreshToken.GetExpirationType(),
"leeway": refreshToken.GetLeeway(),
"token_lifetime": refreshToken.GetTokenLifetime(),
"infinite_token_lifetime": refreshToken.GetInfiniteTokenLifetime(),
"infinite_idle_token_lifetime": refreshToken.GetInfiniteIdleTokenLifetime(),
"idle_token_lifetime": refreshToken.GetIdleTokenLifetime(),
},
}
}

func flattenClientAddons(addons map[string]interface{}) []interface{} {
if addons == nil {
return nil
}

m := make(map[string]interface{})

if v, ok := addons["samlp"]; ok {
samlp := v.(map[string]interface{})

samlpMap := map[string]interface{}{
"issuer": samlp["issuer"],
"audience": samlp["audience"],
"recipient": samlp["recipient"],
"mappings": samlp["mappings"],
"create_upn_claim": samlp["createUpnClaim"],
"passthrough_claims_with_no_mapping": samlp["passthroughClaimsWithNoMapping"],
"map_unknown_claims_as_is": samlp["mapUnknownClaimsAsIs"],
"map_identities": samlp["mapIdentities"],
"signature_algorithm": samlp["signatureAlgorithm"],
"digest_algorithm": samlp["digestAlgorithm"],
"destination": samlp["destination"],
"lifetime_in_seconds": samlp["lifetimeInSeconds"],
"sign_response": samlp["signResponse"],
"name_identifier_format": samlp["nameIdentifierFormat"],
"name_identifier_probes": samlp["nameIdentifierProbes"],
"authn_context_class_ref": samlp["authnContextClassRef"],
"typed_attributes": samlp["typedAttributes"],
"include_attribute_name_format": samlp["includeAttributeNameFormat"],
"binding": samlp["binding"],
"signing_cert": samlp["signingCert"],
}

if logout, ok := samlp["logout"].(map[string]interface{}); ok {
samlpMap["logout"] = mapToState(logout)
}

m["samlp"] = []interface{}{samlpMap}
}

for _, name := range []string{
"aws", "azure_blob", "azure_sb", "rms", "mscrm", "slack", "sentry",
"box", "cloudbees", "concur", "dropbox", "echosign", "egnyte",
"firebase", "newrelic", "office365", "salesforce", "salesforce_api",
"salesforce_sandbox_api", "layer", "sap_api", "sharepoint",
"springcm", "wams", "wsfed", "zendesk", "zoom",
} {
if v, ok := addons[name]; ok {
if addonType, ok := v.(map[string]interface{}); ok {
m[name] = mapToState(addonType)
}
}
}

return []interface{}{m}
}

func flattenClientMobile(mobile *management.ClientMobile) []interface{} {
if mobile == nil {
return nil
}

m := map[string]interface{}{
"android": nil,
"ios": nil,
}

if mobile.GetAndroid() != nil {
m["android"] = []interface{}{
map[string]interface{}{
"app_package_name": mobile.GetAndroid().GetAppPackageName(),
"sha256_cert_fingerprints": mobile.GetAndroid().GetKeyHashes(),
},
}
}

if mobile.GetIOS() != nil {
m["ios"] = []interface{}{
map[string]interface{}{
"team_id": mobile.GetIOS().GetTeamID(),
"app_bundle_identifier": mobile.GetIOS().GetAppID(),
},
}
}

return []interface{}{m}
}

func mapToState(input map[string]interface{}) map[string]interface{} {
output := make(map[string]interface{})

for key, v := range input {
switch val := v.(type) {
case bool:
if val {
output[key] = "true"
} else {
output[key] = "false"
}
case float64:
output[key] = strconv.Itoa(int(val))
case int:
output[key] = strconv.Itoa(val)
default:
output[key] = val
}
}

return output
func clientHasChange(c *management.Client) bool {
return c.String() != "{}"
}
Loading

0 comments on commit 4a29da9

Please sign in to comment.