Skip to content

Commit

Permalink
Remove deprecated fields from plugin config to avoid false drift
Browse files Browse the repository at this point in the history
  • Loading branch information
scastrianni committed Oct 19, 2024
1 parent 72a0ea3 commit 7c67172
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
7 changes: 4 additions & 3 deletions konnect/client/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ type PluginSchema struct {
Fields []map[string]PluginField `json:"fields"`
}
type PluginField struct {
Type string `json:"type"`
Default interface{} `json:"default"`
Fields []map[string]PluginField `json:"fields"`
Type string `json:"type"`
Default interface{} `json:"default"`
Fields []map[string]PluginField `json:"fields"`
ShorthandFields []map[string]PluginField `json:"shorthand_fields"`
}

func (s *Plugin) PluginEncodeId() string {
Expand Down
23 changes: 15 additions & 8 deletions konnect/resource_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,15 @@ func fillPlugin(c *client.Plugin, d *schema.ResourceData) {

func fillResourceDataFromPlugin(ctx context.Context, c *client.Plugin, d *schema.ResourceData, pluginSchema *client.PluginSchema) {
// Grab just the schema for config parameter from plugin schema
var configSchema []map[string]client.PluginField
configSchema = nil
var fieldSchema []map[string]client.PluginField
fieldSchema = nil
var shorthandFieldSchema []map[string]client.PluginField
shorthandFieldSchema = nil
for _, value := range pluginSchema.Fields {
valueMap, ok := value["config"]
if ok {
configSchema = valueMap.Fields
fieldSchema = valueMap.Fields
shorthandFieldSchema = valueMap.ShorthandFields
break
}
}
Expand All @@ -159,29 +162,33 @@ func fillResourceDataFromPlugin(ctx context.Context, c *client.Plugin, d *schema
d.Set("consumer_id", consumerId)
d.Set("plugin_id", c.Id)
// Remove all default values from config based on plugin schema
if configSchema != nil {
pruneConfigValues(ctx, c.Config, configSchema)
if fieldSchema != nil {
pruneConfigValues(ctx, c.Config, fieldSchema, false)
}
if shorthandFieldSchema != nil {
pruneConfigValues(ctx, c.Config, shorthandFieldSchema, true)
}
bytes, _ := json.Marshal(c.Config)
d.Set("config_json", string(bytes[:]))
bytes, _ = json.Marshal(c.ConfigAll)
d.Set("config_all_json", string(bytes[:]))
}

func pruneConfigValues(ctx context.Context, config map[string]interface{}, configSchema []map[string]client.PluginField) {
func pruneConfigValues(ctx context.Context, config map[string]interface{}, configSchema []map[string]client.PluginField, isShorthand bool) {
for _, fieldConfig := range configSchema {
for fieldKey, field := range fieldConfig {
configValue, ok := config[fieldKey]
if ok {
// If field type is record, then recurse
if field.Type == "record" {
childConfigValue := configValue.(map[string]interface{})
pruneConfigValues(ctx, childConfigValue, field.Fields)
pruneConfigValues(ctx, childConfigValue, field.Fields, false)
pruneConfigValues(ctx, childConfigValue, field.ShorthandFields, true)
// If all fields of child config are defaults, then remove entire child config from parent
if len(childConfigValue) == 0 {
delete(config, fieldKey)
}
} else if areConfigValuesEqual(ctx, fieldKey, configValue, field.Default) {
} else if areConfigValuesEqual(ctx, fieldKey, configValue, field.Default) || isShorthand {
delete(config, fieldKey)
}
}
Expand Down
6 changes: 3 additions & 3 deletions test/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ provider "konnect" {
# role_display_name = data.konnect_role.R.display_name
#}

data "konnect_team" "T" {
name = "api-product-developer"
}
# data "konnect_team" "T" {
# name = "api-product-developer"
# }

#resource "konnect_user" "U" {
# email = "jblow@example.com"
Expand Down

0 comments on commit 7c67172

Please sign in to comment.