Skip to content

Commit

Permalink
[4/X] Refactor branding resources to allow for empty fields (#339)
Browse files Browse the repository at this point in the history
* Refactor branding resources to allow for empty fields

* [5/X] Refactor client resources to allow for empty fields (#340)

* Refactor client resources to allow for empty fields

* [6/X] Refactor connection resources to allow for empty fields (#341)

* Refactor client resources to allow for empty fields

* Refactor connection resources to allow for empty fields

* [7/X] Refactor guardian resources to allow for empty fields (#342)

* Refactor guardian resources to allow for empty fields

* Early return for erroneous guardian API requests

* Early return for erroneous guardian API requests

* [8/X] Refactor orgs to allow for empty fields and remove deprecated field (#343)

* Refactor ors to allow for empty fields and remove deprecated field

* [9/X] Refactor email resources to allow for empty fields (#344)

* Refactor email resources to allow for empty fields

* [10/X] Refactor resource server resources to allow for empty fields (#345)

* Refactor resource server resources to allow for empty fields

* [11/X] Refactor tenant resource to allow for empty fields (#346)

* Refactor tenant resource to allow for empty fields

* [12/X] Refactor custom domain resource to allow for empty fields (#347)

* Refactor custom domain resource to allow for empty fields

* [13/X] Refactor user resource to allow for empty fields (#348)

* Refactor user resource to allow for empty fields

* [14/X] Refactor role and rule resources to allow for empty fields (#349)

* Refactor role and rule resources to allow for empty fields

* [15/X] Refactor log stream resource to allow for empty fields (#350)

* Refactor log stream resource to allow for empty fields

* [16/X] Refactor prompts resource to allow for empty fields (#351)

* Refactor prompts resource to allow for empty fields

* [17/X] Refactor hooks resource to allow for empty fields (#352)

Refactor hooks resource to allow for empty fields

* Fix small issue with log stream filters

* Adding user_id back in to tests

Co-authored-by: Will Vedder <[email protected]>

Co-authored-by: Will Vedder <[email protected]>

* Removing default audience string replace

* Removing unncessary check

Co-authored-by: Will Vedder <[email protected]>

Co-authored-by: Will Vedder <[email protected]>

Co-authored-by: Will Vedder <[email protected]>

Co-authored-by: Will Vedder <[email protected]>

Co-authored-by: Will Vedder <[email protected]>

* Adding extra assertion

Co-authored-by: Will Vedder <[email protected]>

Co-authored-by: Will Vedder <[email protected]>

Co-authored-by: Will Vedder <[email protected]>
  • Loading branch information
sergiught and willvedd authored Oct 10, 2022
1 parent f6d358d commit e96f819
Show file tree
Hide file tree
Showing 85 changed files with 13,778 additions and 10,001 deletions.
13 changes: 0 additions & 13 deletions docs/resources/organization.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ resource "auth0_organization" "my_organization" {
### Optional

- `branding` (Block List, Max: 1) Defines how to style the login pages. (see [below for nested schema](#nestedblock--branding))
- `connections` (Block Set, Deprecated) (see [below for nested schema](#nestedblock--connections))
- `display_name` (String) Friendly name of this organization.
- `metadata` (Map of String) Metadata associated with the organization. Maximum of 10 metadata properties allowed.

Expand All @@ -60,18 +59,6 @@ Optional:
- `colors` (Map of String) Color scheme used to customize the login pages.
- `logo_url` (String) URL of logo to display on login page.


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

Required:

- `connection_id` (String) The connection ID of the connection to add to the organization.

Optional:

- `assign_membership_on_login` (Boolean) When `true`, all users that log in with this connection will be automatically granted membership in the organization. When `false`, users must be granted membership in the organization before logging in with this connection.

## Import

Import is supported using the following syntax:
Expand Down
5 changes: 4 additions & 1 deletion docs/resources/resource_server.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,14 @@ resource "auth0_resource_server" "my_resource_server" {
<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `identifier` (String) Unique identifier for the resource server. Used as the audience parameter for authorization calls. Cannot be changed once set.

### Optional

- `allow_offline_access` (Boolean) Indicates whether refresh tokens can be issued for this resource server.
- `enforce_policies` (Boolean) Indicates whether authorization polices are enforced.
- `identifier` (String) Unique identifier for the resource server. Used as the audience parameter for authorization calls. Cannot be changed once set.
- `name` (String) Friendly name for the resource server. Cannot include `<` or `>` characters.
- `options` (Map of String) Used to store additional metadata.
- `scopes` (Block Set) List of permissions (scopes) used by this resource server. (see [below for nested schema](#nestedblock--scopes))
Expand Down
41 changes: 18 additions & 23 deletions internal/provider/data_source_auth0_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,24 @@ import (
"github.com/auth0/terraform-provider-auth0/internal/template"
)

const testAccGivenAClient = `
resource "auth0_client" "my_client" {
name = "Acceptance Test - {{.testName}}"
app_type = "non_interactive"
}
`

const testAccDataClientConfigByName = `
%v
data auth0_client test {
name = "Acceptance Test - {{.testName}}"
data "auth0_client" "test" {
depends_on = [ auth0_client.my_client ]
name = "Acceptance Test - {{.testName}}"
}
`

const testAccDataClientConfigByID = `
%v
data auth0_client test {
client_id = auth0_client.my_client.client_id
data "auth0_client" "test" {
client_id = auth0_client.my_client.client_id
}
`

Expand All @@ -32,18 +39,12 @@ func TestAccDataClientByName(t *testing.T) {
PreventPostDestroyRefresh: true,
Steps: []resource.TestStep{
{
Config: template.ParseTestName(testAccClientConfig, t.Name()),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_client.my_client", "name", fmt.Sprintf("Acceptance Test - %s", t.Name())),
), // check that the client got created correctly before using the data source
},
{
Config: template.ParseTestName(fmt.Sprintf(testAccDataClientConfigByName, testAccClientConfig), t.Name()),
Config: template.ParseTestName(testAccGivenAClient+testAccDataClientConfigByName, t.Name()),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.auth0_client.test", "client_id"),
resource.TestCheckResourceAttr("data.auth0_client.test", "signing_keys.#", "1"), // checks that signing_keys is set, and it includes 1 element
resource.TestCheckResourceAttr("data.auth0_client.test", "signing_keys.#", "1"),
resource.TestCheckResourceAttr("data.auth0_client.test", "name", fmt.Sprintf("Acceptance Test - %v", t.Name())),
resource.TestCheckResourceAttr("data.auth0_client.test", "app_type", "non_interactive"), // Arbitrary property selection
resource.TestCheckResourceAttr("data.auth0_client.test", "app_type", "non_interactive"),
resource.TestCheckNoResourceAttr("data.auth0_client.test", "client_secret_rotation_trigger"),
),
},
Expand All @@ -59,17 +60,11 @@ func TestAccDataClientById(t *testing.T) {
PreventPostDestroyRefresh: true,
Steps: []resource.TestStep{
{
Config: template.ParseTestName(testAccClientConfig, t.Name()),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_client.my_client", "name", fmt.Sprintf("Acceptance Test - %v", t.Name())),
), // check that the client got created correctly before using the data source
},
{
Config: template.ParseTestName(fmt.Sprintf(testAccDataClientConfigByID, testAccClientConfig), t.Name()),
Config: template.ParseTestName(testAccGivenAClient+testAccDataClientConfigByID, t.Name()),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.auth0_client.test", "id"),
resource.TestCheckResourceAttrSet("data.auth0_client.test", "name"),
resource.TestCheckResourceAttr("data.auth0_client.test", "signing_keys.#", "1"), // checks that signing_keys is set, and it includes 1 element
resource.TestCheckResourceAttr("data.auth0_client.test", "signing_keys.#", "1"),
resource.TestCheckNoResourceAttr("data.auth0_client.test", "client_secret_rotation_trigger"),
),
},
Expand Down
119 changes: 65 additions & 54 deletions internal/provider/resource_auth0_branding.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package provider

import (
"context"
"net/http"

"github.com/auth0/go-auth0/management"
"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/auth0/terraform-provider-auth0/internal/value"
)

func newBranding() *schema.Resource {
Expand Down Expand Up @@ -100,36 +102,30 @@ func createBranding(ctx context.Context, d *schema.ResourceData, m interface{})

func readBranding(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := m.(*management.Management)

branding, err := api.Branding.Read()
if err != nil {
if mErr, ok := err.(management.Error); ok {
if mErr.Status() == http.StatusNotFound {
d.SetId("")
return nil
}
}
return diag.FromErr(err)
}

result := multierror.Append(
d.Set("favicon_url", branding.FaviconURL),
d.Set("logo_url", branding.LogoURL),
d.Set("favicon_url", branding.GetFaviconURL()),
d.Set("logo_url", branding.GetLogoURL()),
)
if _, ok := d.GetOk("colors"); ok {
result = multierror.Append(result, d.Set("colors", flattenBrandingColors(branding.Colors)))
result = multierror.Append(result, d.Set("colors", flattenBrandingColors(branding.GetColors())))
}
if _, ok := d.GetOk("font"); ok {
result = multierror.Append(result, d.Set("font", flattenBrandingFont(branding.Font)))
result = multierror.Append(result, d.Set("font", flattenBrandingFont(branding.GetFont())))
}

tenant, err := api.Tenant.Read()
if err != nil {
return diag.FromErr(err)
}

if tenant.Flags.EnableCustomDomainInEmails != nil && *tenant.Flags.EnableCustomDomainInEmails {
if err := setUniversalLogin(d, m); err != nil {
d.SetId("")
if tenant.Flags.GetEnableCustomDomainInEmails() {
if err := setUniversalLogin(d, api); err != nil {
return diag.FromErr(err)
}
}
Expand All @@ -140,13 +136,12 @@ func readBranding(ctx context.Context, d *schema.ResourceData, m interface{}) di
func updateBranding(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := m.(*management.Management)

branding := buildBranding(d)
branding := expandBranding(d.GetRawConfig())
if err := api.Branding.Update(branding); err != nil {
return diag.FromErr(err)
}

universalLogin := buildBrandingUniversalLogin(d)
if universalLogin.GetBody() != "" {
if universalLogin := expandBrandingUniversalLogin(d.GetRawConfig()); universalLogin.GetBody() != "" {
if err := api.Branding.SetUniversalLogin(universalLogin); err != nil {
return diag.FromErr(err)
}
Expand All @@ -157,66 +152,82 @@ func updateBranding(ctx context.Context, d *schema.ResourceData, m interface{})

func deleteBranding(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := m.(*management.Management)

tenant, err := api.Tenant.Read()
if err != nil {
return diag.FromErr(err)
}

if tenant.Flags.EnableCustomDomainInEmails != nil && *tenant.Flags.EnableCustomDomainInEmails {
if tenant.Flags.GetEnableCustomDomainInEmails() {
if err = api.Branding.DeleteUniversalLogin(); err != nil {
if mErr, ok := err.(management.Error); ok {
if mErr.Status() == http.StatusNotFound {
d.SetId("")
return nil
}
}
return diag.FromErr(err)
}
}

return diag.FromErr(err)
d.SetId("")
return nil
}

func buildBranding(d *schema.ResourceData) *management.Branding {
func expandBranding(config cty.Value) *management.Branding {
branding := &management.Branding{
FaviconURL: String(d, "favicon_url"),
LogoURL: String(d, "logo_url"),
FaviconURL: value.String(config.GetAttr("favicon_url")),
LogoURL: value.String(config.GetAttr("logo_url")),
Colors: expandBrandingColors(config.GetAttr("colors")),
Font: expandBrandingFont(config.GetAttr("font")),
}

List(d, "colors").Elem(func(d ResourceData) {
branding.Colors = &management.BrandingColors{
PageBackground: String(d, "page_background"),
Primary: String(d, "primary"),
}
return branding
}

func expandBrandingColors(config cty.Value) *management.BrandingColors {
var brandingColors management.BrandingColors

config.ForEachElement(func(_ cty.Value, colors cty.Value) (stop bool) {
brandingColors.PageBackground = value.String(colors.GetAttr("page_background"))
brandingColors.Primary = value.String(colors.GetAttr("primary"))
return stop
})

List(d, "font").Elem(func(d ResourceData) {
branding.Font = &management.BrandingFont{
URL: String(d, "url"),
}
if brandingColors == (management.BrandingColors{}) {
return nil
}

return &brandingColors
}

func expandBrandingFont(config cty.Value) *management.BrandingFont {
var brandingFont management.BrandingFont

config.ForEachElement(func(_ cty.Value, font cty.Value) (stop bool) {
brandingFont.URL = value.String(font.GetAttr("url"))
return stop
})

return branding
if brandingFont == (management.BrandingFont{}) {
return nil
}

return &brandingFont
}

func buildBrandingUniversalLogin(d *schema.ResourceData) *management.BrandingUniversalLogin {
universalLogin := &management.BrandingUniversalLogin{}
func expandBrandingUniversalLogin(config cty.Value) *management.BrandingUniversalLogin {
var universalLogin management.BrandingUniversalLogin

List(d, "universal_login").Elem(func(d ResourceData) {
universalLogin.Body = String(d, "body")
config.GetAttr("universal_login").ForEachElement(func(_ cty.Value, ul cty.Value) (stop bool) {
universalLogin.Body = value.String(ul.GetAttr("body"))
return stop
})

return universalLogin
if universalLogin == (management.BrandingUniversalLogin{}) {
return nil
}

return &universalLogin
}

func setUniversalLogin(d *schema.ResourceData, m interface{}) error {
api := m.(*management.Management)
func setUniversalLogin(d *schema.ResourceData, api *management.Management) error {
universalLogin, err := api.Branding.UniversalLogin()
if err != nil {
if mErr, ok := err.(management.Error); ok {
if mErr.Status() == http.StatusNotFound {
return nil
}
}
return err
}

Expand All @@ -229,8 +240,8 @@ func flattenBrandingColors(brandingColors *management.BrandingColors) []interfac
}
return []interface{}{
map[string]interface{}{
"page_background": brandingColors.PageBackground,
"primary": brandingColors.Primary,
"page_background": brandingColors.GetPageBackground(),
"primary": brandingColors.GetPrimary(),
},
}
}
Expand All @@ -241,7 +252,7 @@ func flattenBrandingUniversalLogin(brandingUniversalLogin *management.BrandingUn
}
return []interface{}{
map[string]interface{}{
"body": brandingUniversalLogin.Body,
"body": brandingUniversalLogin.GetBody(),
},
}
}
Expand All @@ -252,7 +263,7 @@ func flattenBrandingFont(brandingFont *management.BrandingFont) []interface{} {
}
return []interface{}{
map[string]interface{}{
"url": brandingFont.URL,
"url": brandingFont.GetURL(),
},
}
}
Loading

0 comments on commit e96f819

Please sign in to comment.