Skip to content

Commit a047276

Browse files
committed
update
1 parent 20c6964 commit a047276

3 files changed

Lines changed: 344 additions & 33 deletions

File tree

azureappconfiguration/azureappconfiguration.go

Lines changed: 53 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -428,8 +428,19 @@ func (azappcfg *AzureAppConfiguration) loadKeyValues(ctx context.Context, settin
428428
azappcfg.tracingOptions.UseAIConfiguration = useAIConfiguration
429429
azappcfg.tracingOptions.UseAIChatCompletionConfiguration = useAIChatCompletionConfiguration
430430

431-
if err := azappcfg.loadSettingsFromSnapshotRefs(ctx, settingsClient, snapshotRefs, kvSettings, keyVaultRefs); err != nil {
432-
return err
431+
if len(snapshotRefs) > 0 {
432+
var loadSnapshot snapshotSettingsLoader
433+
if client, ok := settingsClient.(*selectorSettingsClient); ok {
434+
loadSnapshot = func(ctx context.Context, snapshotName string) ([]azappconfig.Setting, error) {
435+
return loadSnapshotSettings(ctx, client.client, snapshotName)
436+
}
437+
}
438+
439+
if loadSnapshot != nil {
440+
if err := azappcfg.loadSettingsFromSnapshotRefs(ctx, loadSnapshot, snapshotRefs, kvSettings, keyVaultRefs); err != nil {
441+
return err
442+
}
443+
}
433444
}
434445

435446
secrets, err := azappcfg.loadKeyVaultSecrets(ctx, keyVaultRefs)
@@ -445,51 +456,60 @@ func (azappcfg *AzureAppConfiguration) loadKeyValues(ctx context.Context, settin
445456
return nil
446457
}
447458

448-
func (azappcfg *AzureAppConfiguration) loadSettingsFromSnapshotRefs(ctx context.Context, settingsClient settingsClient, snapshotRefs map[string]string, kvSettings map[string]any, keyVaultRefs map[string]string) error {
459+
func (azappcfg *AzureAppConfiguration) loadSettingsFromSnapshotRefs(ctx context.Context, loadSnapshot snapshotSettingsLoader, snapshotRefs map[string]string, kvSettings map[string]any, keyVaultRefs map[string]string) error {
449460
for key, snapshotRef := range snapshotRefs {
450461
// Parse the snapshot reference
451462
snapshotName, err := parseSnapshotReference(snapshotRef)
452463
if err != nil {
453464
return fmt.Errorf("invalid format for Snapshot reference setting %s: %w", key, err)
454465
}
455466

456-
if client, ok := settingsClient.(*selectorSettingsClient); ok {
457-
// Load the snapshot settings
458-
settingsFromSnapshot, err := loadSnapshotSettings(ctx, client.client, snapshotName)
459-
if err != nil {
460-
return fmt.Errorf("failed to load snapshot settings: key=%s, error=%s", key, err.Error())
467+
// Load the snapshot settings
468+
settingsFromSnapshot, err := loadSnapshot(ctx, snapshotName)
469+
if err != nil {
470+
return fmt.Errorf("failed to load snapshot settings: key=%s, error=%s", key, err.Error())
471+
}
472+
473+
for _, setting := range settingsFromSnapshot {
474+
if setting.Key == nil {
475+
continue
461476
}
462477

463-
for _, setting := range settingsFromSnapshot {
464-
if setting.ContentType == nil || setting.Value == nil || setting.Key == nil {
465-
continue
466-
}
478+
trimmedKey := azappcfg.trimPrefix(*setting.Key)
479+
if len(trimmedKey) == 0 {
480+
log.Printf("Key of the setting '%s' is trimmed to the empty string, just ignore it", *setting.Key)
481+
continue
482+
}
467483

468-
if *setting.ContentType == featureFlagContentType {
469-
continue
470-
}
484+
if setting.ContentType == nil || setting.Value == nil {
485+
kvSettings[trimmedKey] = setting.Value
486+
continue
487+
}
471488

472-
if *setting.ContentType == secretReferenceContentType {
473-
keyVaultRefs[*setting.Key] = *setting.Value
474-
continue
475-
}
489+
if *setting.ContentType == featureFlagContentType {
490+
continue
491+
}
476492

477-
// Handle JSON content types (similar to regular key-value loading)
478-
if isJsonContentType(setting.ContentType) {
479-
var v any
480-
if err := json.Unmarshal([]byte(*setting.Value), &v); err != nil {
481-
// If the value is not valid JSON, try to remove comments and parse again
482-
if err := json.Unmarshal(jsonc.StripComments([]byte(*setting.Value)), &v); err != nil {
483-
// If still invalid, log the error and treat it as a plain string
484-
log.Printf("Failed to unmarshal JSON value from snapshot: key=%s, error=%s", *setting.Key, err.Error())
485-
kvSettings[*setting.Key] = setting.Value
486-
continue
487-
}
493+
if *setting.ContentType == secretReferenceContentType {
494+
keyVaultRefs[trimmedKey] = *setting.Value
495+
continue
496+
}
497+
498+
// Handle JSON content types (similar to regular key-value loading)
499+
if isJsonContentType(setting.ContentType) {
500+
var v any
501+
if err := json.Unmarshal([]byte(*setting.Value), &v); err != nil {
502+
// If the value is not valid JSON, try to remove comments and parse again
503+
if err := json.Unmarshal(jsonc.StripComments([]byte(*setting.Value)), &v); err != nil {
504+
// If still invalid, log the error and treat it as a plain string
505+
log.Printf("Failed to unmarshal JSON value from snapshot: key=%s, error=%s", *setting.Key, err.Error())
506+
kvSettings[trimmedKey] = setting.Value
507+
continue
488508
}
489-
kvSettings[*setting.Key] = v
490-
} else {
491-
kvSettings[*setting.Key] = setting.Value
492509
}
510+
kvSettings[trimmedKey] = v
511+
} else {
512+
kvSettings[trimmedKey] = setting.Value
493513
}
494514
}
495515
}

azureappconfiguration/settings_client.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ type eTagsClient interface {
5050
checkIfETagChanged(ctx context.Context) (bool, error)
5151
}
5252

53+
// snapshotSettingsLoader is a function type that loads settings from a snapshot by name.
54+
type snapshotSettingsLoader func(ctx context.Context, snapshotName string) ([]azappconfig.Setting, error)
55+
5356
type refreshClient struct {
5457
loader settingsClient
5558
monitor eTagsClient
@@ -203,6 +206,10 @@ func loadSnapshotSettings(ctx context.Context, client *azappconfig.Client, snaps
203206
settings := make([]azappconfig.Setting, 0)
204207
snapshot, err := client.GetSnapshot(ctx, snapshotName, nil)
205208
if err != nil {
209+
var respErr *azcore.ResponseError
210+
if errors.As(err, &respErr) && respErr.StatusCode == 404 {
211+
return settings, nil // treat non-existing snapshot as empty
212+
}
206213
return nil, err
207214
}
208215

0 commit comments

Comments
 (0)