Skip to content

Commit

Permalink
schema_only for attributes (#135)
Browse files Browse the repository at this point in the history
Allow specifying that an attribute is 'schema_only' so that we sync the
schema but preserve any values modified in the dashboard, rather than
syncing the values we get from our source.
  • Loading branch information
lawrencejones authored Jul 5, 2024
1 parent 348d929 commit 0f7aa00
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
4 changes: 2 additions & 2 deletions cmd/catalog-importer/cmd/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ func (opt *SyncOptions) Run(ctx context.Context, logger kitlog.Logger, cfg *conf
logger.Log("msg", "reconciling catalog entries", "output", outputType.TypeName)
catalogType := catalogTypesByOutput[outputType.TypeName]

err = reconcile.Entries(ctx, logger, entriesClient, catalogType, entryModels, newEntriesProgress(!opt.DryRun))
err = reconcile.Entries(ctx, logger, entriesClient, outputType, catalogType, entryModels, newEntriesProgress(!opt.DryRun))
if err != nil {
return errors.Wrap(err, fmt.Sprintf("outputs (type_name = '%s'): reconciling catalog entries", outputType.TypeName))
}
Expand Down Expand Up @@ -509,7 +509,7 @@ func (opt *SyncOptions) Run(ctx context.Context, logger kitlog.Logger, cfg *conf

OUT("\n ↻ %s (enum)", enumModel.TypeName)
catalogType := catalogTypesByOutput[enumModel.TypeName]
err := reconcile.Entries(ctx, logger, entriesClient, catalogType, enumModels, newEntriesProgress(!opt.DryRun))
err := reconcile.Entries(ctx, logger, entriesClient, outputType, catalogType, enumModels, newEntriesProgress(!opt.DryRun))
if err != nil {
return errors.Wrap(err,
fmt.Sprintf("outputs (type_name = '%s'): enum for attribute (id = '%s'): %s: reconciling catalog entries",
Expand Down
6 changes: 6 additions & 0 deletions config/reference.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,12 @@
//
// Will default to the id of this attribute.
source: '$.metadata.description',

// If true we will only create the attribute in the schema and won't sync
// the value of the attribute. This is useful when you want to specify the
// schema but leave this field available to be controlled from the dashboard
// manually, separately from the importer.
schema_only: false,
},

// Most of the time you can be much less verbose, as source will
Expand Down
1 change: 1 addition & 0 deletions output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type Attribute struct {
Enum *AttributeEnum `json:"enum"`
BacklinkAttribute null.String `json:"backlink_attribute"`
Path []string `json:"path"`
SchemaOnly bool `json:"schema_only"`
}

func (a Attribute) Validate() error {
Expand Down
17 changes: 16 additions & 1 deletion reconcile/entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type EntriesProgress struct {
OnUpdateProgress func()
}

func Entries(ctx context.Context, logger kitlog.Logger, cl EntriesClient, catalogType *client.CatalogTypeV2, entryModels []*output.CatalogEntryModel, progress *EntriesProgress) error {
func Entries(ctx context.Context, logger kitlog.Logger, cl EntriesClient, outputType *output.Output, catalogType *client.CatalogTypeV2, entryModels []*output.CatalogEntryModel, progress *EntriesProgress) error {
logger = kitlog.With(logger,
"catalog_type_id", catalogType.Id,
"catalog_type_name", catalogType.TypeName,
Expand Down Expand Up @@ -197,6 +197,15 @@ func Entries(ctx context.Context, logger kitlog.Logger, cl EntriesClient, catalo
}
}

// Identify the attributes that are schema-only, as we want to preserve the existing
// value instead of setting it outselves.
schemaOnlyAttributes := []*output.Attribute{}
for _, attr := range outputType.Attributes {
if attr.SchemaOnly {
schemaOnlyAttributes = append(schemaOnlyAttributes, attr)
}
}

{
toUpdate := []*output.CatalogEntryModel{}
eachPayload:
Expand Down Expand Up @@ -238,6 +247,12 @@ func Entries(ctx context.Context, logger kitlog.Logger, cl EntriesClient, catalo
currentBindings[attributeID] = current
}

// For any of the schema only attributes, preserve the existing value when
// performing an update.
for _, attr := range schemaOnlyAttributes {
model.AttributeValues[attr.ID] = currentBindings[attr.ID]
}

if isSame && reflect.DeepEqual(model.AttributeValues, currentBindings) {
logger.Log("msg", "catalog entry has not changed, not updating", "entry_id", entry.Id)
continue eachPayload
Expand Down

0 comments on commit 0f7aa00

Please sign in to comment.