Skip to content

Commit

Permalink
Remove unnecessary MapData struct from resource data helpers (#310)
Browse files Browse the repository at this point in the history
* Prevent erasing connection.options.configuration by mistake

* Upgrade go-vcr to v3

* Remove unnecessary MapData struct from resource data helpers

* Add allowedHeaders in http recorder
  • Loading branch information
sergiught authored Aug 30, 2022
1 parent eee61df commit 5495a19
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 212 deletions.
114 changes: 48 additions & 66 deletions internal/provider/resource_auth0_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,16 +714,15 @@ func newClient() *schema.Resource {
}

func createClient(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client, err := expandClient(d)
if err != nil {
return diag.FromErr(err)
}

api := m.(*management.Management)

client := expandClient(d)
if err := api.Client.Create(client); err != nil {
return diag.FromErr(err)
}

d.SetId(auth0.StringValue(client.ClientID))

return readClient(ctx, d, m)
}

Expand Down Expand Up @@ -781,12 +780,9 @@ func readClient(ctx context.Context, d *schema.ResourceData, m interface{}) diag
}

func updateClient(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client, err := expandClient(d)
if err != nil {
return diag.FromErr(err)
}

api := m.(*management.Management)

client := expandClient(d)
if clientHasChange(client) {
if err := api.Client.Update(d.Id(), client); err != nil {
return diag.FromErr(err)
Expand Down Expand Up @@ -816,7 +812,7 @@ func deleteClient(ctx context.Context, d *schema.ResourceData, m interface{}) di
return nil
}

func expandClient(d *schema.ResourceData) (*management.Client, error) {
func expandClient(d *schema.ResourceData) *management.Client {
client := &management.Client{
Name: String(d, "name"),
Description: String(d, "description"),
Expand Down Expand Up @@ -872,49 +868,46 @@ func expandClient(d *schema.ResourceData) (*management.Client, error) {
}
}

var result *multierror.Error
List(d, "addons").Elem(func(d ResourceData) {
client.Addons = make(map[string]interface{})

for _, name := range []string{
var allowedAddons = []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",
} {
}

for _, name := range allowedAddons {
if _, ok := d.GetOk(name); ok {
client.Addons[name] = mapFromState(Map(d, name))
}
}

List(d, "samlp").Elem(func(d ResourceData) {
m := make(MapData)

result = multierror.Append(
m.Set("audience", String(d, "audience")),
m.Set("authnContextClassRef", String(d, "authn_context_class_ref")),
m.Set("binding", String(d, "binding")),
m.Set("signingCert", String(d, "signing_cert")),
m.Set("createUpnClaim", Bool(d, "create_upn_claim")),
m.Set("destination", String(d, "destination")),
m.Set("digestAlgorithm", String(d, "digest_algorithm")),
m.Set("includeAttributeNameFormat", Bool(d, "include_attribute_name_format")),
m.Set("lifetimeInSeconds", Int(d, "lifetime_in_seconds")),
m.Set("mapIdentities", Bool(d, "map_identities")),
m.Set("mappings", Map(d, "mappings")),
m.Set("mapUnknownClaimsAsIs", Bool(d, "map_unknown_claims_as_is")),
m.Set("nameIdentifierFormat", String(d, "name_identifier_format")),
m.Set("nameIdentifierProbes", Slice(d, "name_identifier_probes")),
m.Set("passthroughClaimsWithNoMapping", Bool(d, "passthrough_claims_with_no_mapping")),
m.Set("recipient", String(d, "recipient")),
m.Set("signatureAlgorithm", String(d, "signature_algorithm")),
m.Set("signResponse", Bool(d, "sign_response")),
m.Set("typedAttributes", Bool(d, "typed_attributes")),
m.Set("logout", mapFromState(Map(d, "logout"))),
)

client.Addons["samlp"] = m
client.Addons["samlp"] = map[string]interface{}{
"audience": String(d, "audience"),
"authnContextClassRef": String(d, "authn_context_class_ref"),
"binding": String(d, "binding"),
"signingCert": String(d, "signing_cert"),
"createUpnClaim": Bool(d, "create_upn_claim"),
"destination": String(d, "destination"),
"digestAlgorithm": String(d, "digest_algorithm"),
"includeAttributeNameFormat": Bool(d, "include_attribute_name_format"),
"lifetimeInSeconds": Int(d, "lifetime_in_seconds"),
"mapIdentities": Bool(d, "map_identities"),
"mappings": Map(d, "mappings"),
"mapUnknownClaimsAsIs": Bool(d, "map_unknown_claims_as_is"),
"nameIdentifierFormat": String(d, "name_identifier_format"),
"nameIdentifierProbes": Slice(d, "name_identifier_probes"),
"passthroughClaimsWithNoMapping": Bool(d, "passthrough_claims_with_no_mapping"),
"recipient": String(d, "recipient"),
"signatureAlgorithm": String(d, "signature_algorithm"),
"signResponse": Bool(d, "sign_response"),
"typedAttributes": Bool(d, "typed_attributes"),
"logout": mapFromState(Map(d, "logout")),
}
})
})

Expand All @@ -929,48 +922,37 @@ func expandClient(d *schema.ResourceData) (*management.Client, error) {
client.NativeSocialLogin = &management.ClientNativeSocialLogin{}

List(d, "apple").Elem(func(d ResourceData) {
m := make(MapData)
result = multierror.Append(result, m.Set("enabled", Bool(d, "enabled")))

client.NativeSocialLogin.Apple = m
client.NativeSocialLogin.Apple = map[string]interface{}{
"enabled": Bool(d, "enabled"),
}
})

List(d, "facebook").Elem(func(d ResourceData) {
m := make(MapData)
result = multierror.Append(result, m.Set("enabled", Bool(d, "enabled")))

client.NativeSocialLogin.Facebook = m
client.NativeSocialLogin.Facebook = map[string]interface{}{
"enabled": Bool(d, "enabled"),
}
})
})

List(d, "mobile").Elem(func(d ResourceData) {
client.Mobile = make(map[string]interface{})

List(d, "android").Elem(func(d ResourceData) {
m := make(MapData)
result = multierror.Append(
result,
m.Set("app_package_name", String(d, "app_package_name")),
m.Set("sha256_cert_fingerprints", Slice(d, "sha256_cert_fingerprints")),
)

client.Mobile["android"] = m
client.Mobile["android"] = map[string]interface{}{
"app_package_name": String(d, "app_package_name"),
"sha256_cert_fingerprints": Slice(d, "sha256_cert_fingerprints"),
}
})

List(d, "ios").Elem(func(d ResourceData) {
m := make(MapData)

result = multierror.Append(
result,
m.Set("team_id", String(d, "team_id")),
m.Set("app_bundle_identifier", String(d, "app_bundle_identifier")),
)

client.Mobile["ios"] = m
client.Mobile["ios"] = map[string]interface{}{
"team_id": String(d, "team_id"),
"app_bundle_identifier": String(d, "app_bundle_identifier"),
}
})
})

return client, result.ErrorOrNil()
return client
}

func mapFromState(input map[string]interface{}) map[string]interface{} {
Expand Down
75 changes: 0 additions & 75 deletions internal/provider/resource_data_helpers.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package provider

import (
"reflect"
"strconv"

"github.com/auth0/go-auth0"
Expand Down Expand Up @@ -99,67 +98,6 @@ func (d *resourceData) Set(key string, value interface{}) error {
return d.ResourceData.Set(d.prefix+"."+key, value)
}

// MapData wraps a map satisfying the Data interface, so it can be used in the
// accessor methods defined below.
type MapData map[string]interface{}

// IsNewResource reports whether the resource is seen for the first time.
func (md MapData) IsNewResource() bool {
return false
}

// HasChange reports whether the given key has been changed.
func (md MapData) HasChange(key string) bool {
_, ok := md[key]
return ok
}

// GetChange returns the old and new value for a given key.
func (md MapData) GetChange(key string) (interface{}, interface{}) {
return md[key], md[key]
}

// Get returns the data for the given key, or nil
// if the key doesn't exist in the schema.
func (md MapData) Get(key string) interface{} {
return md[key]
}

// GetOk returns the data for the given key and whether the
// key has been set to a non-zero value at some point.
func (md MapData) GetOk(key string) (interface{}, bool) {
v, ok := md[key]
return v, ok && !isNil(v) && !isZero(v)
}

// GetOkExists can check if TypeBool attributes that are
// Optional with no Default value have been set.
func (md MapData) GetOkExists(key string) (interface{}, bool) {
v, ok := md[key]
return v, ok && !isNil(v)
}

// Set sets the value for the given key.
func (md MapData) Set(key string, value interface{}) error {
if !isNil(value) {
md[key] = value
}
return nil
}

func isNil(v interface{}) bool {
rv := reflect.ValueOf(v)
switch rv.Kind() {
case reflect.Ptr, reflect.Slice, reflect.Map:
return rv.IsNil()
}
return v == nil
}

func isZero(v interface{}) bool {
return reflect.DeepEqual(v, reflect.Zero(reflect.TypeOf(v)).Interface())
}

var _ ResourceData = (*schema.ResourceData)(nil)

// Condition is a function that checks whether a condition holds true for a
Expand Down Expand Up @@ -204,19 +142,6 @@ func Any(conditions ...Condition) Condition {
}
}

// All is a condition that evaluates to true if all of its child conditions
// evaluate to true.
func All(conditions ...Condition) Condition {
return func(d ResourceData, key string) bool {
for _, condition := range conditions {
if !condition.Eval(d, key) {
return false
}
}
return true
}
}

// Not is a condition that evaluates to true if its child condition evaluates to
// false. False otherwise.
func Not(condition Condition) Condition {
Expand Down
71 changes: 0 additions & 71 deletions internal/provider/resource_data_helpers_test.go

This file was deleted.

0 comments on commit 5495a19

Please sign in to comment.