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

[4/X] Refactor branding resources to allow for empty fields #339

Merged
merged 2 commits into from
Oct 10, 2022
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
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