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

schema_only for attributes #135

Merged
merged 1 commit into from
Jul 5, 2024
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
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
Loading