Skip to content

Commit

Permalink
change logging
Browse files Browse the repository at this point in the history
  • Loading branch information
katherinelc321 committed Jun 4, 2024
1 parent 0bc7022 commit f9e90fa
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions frontend/pkg/frontend/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,10 +502,12 @@ func (f *Frontend) ArmSubscriptionPut(writer http.ResponseWriter, request *http.
} else {
f.logger.Info(fmt.Sprintf("existing document found for subscription - will update document for subscription %s", subscriptionID))
doc.Subscription = &subscription
}

// Log differences
logSubscriptionDifferences(f.logger, doc.Subscription, &subscription)
messages := logSubscriptionDifferences(doc.Subscription, &subscription)
for _, message := range messages {
f.logger.Info(message)
}
}

err = f.dbClient.SetSubscriptionDoc(ctx, doc)
if err != nil {
Expand Down Expand Up @@ -645,39 +647,41 @@ func (f *Frontend) ArmDeploymentPreflight(writer http.ResponseWriter, request *h
arm.WriteDeploymentPreflightResponse(writer, preflightErrors)
}

func logSubscriptionDifferences(logger *slog.Logger, oldSub, newSub *arm.Subscription) {
func logSubscriptionDifferences(oldSub, newSub *arm.Subscription) []string {
var messages []string

// Compare states
if oldSub.State != newSub.State {
logger.Info(fmt.Sprintf("Subscription state changed from %s to %s", oldSub.State, newSub.State))
messages = append(messages, fmt.Sprintf("Subscription state changed from %s to %s", oldSub.State, newSub.State))
}

if oldSub.Properties != nil && newSub.Properties != nil {
// Compare tenantId
if oldSub.Properties.TenantId != nil && newSub.Properties.TenantId != nil &&
*oldSub.Properties.TenantId != *newSub.Properties.TenantId {
logger.Info(fmt.Sprintf("Subscription tenantId changed from %s to %s", *oldSub.Properties.TenantId, *newSub.Properties.TenantId))
messages = append(messages, fmt.Sprintf("Subscription tenantId changed from %s to %s", *oldSub.Properties.TenantId, *newSub.Properties.TenantId))
}

// Compare registeredFeatures
if oldSub.Properties.RegisteredFeatures != nil && newSub.Properties.RegisteredFeatures != nil {
oldFeatures := featuresMap(oldSub.Properties.RegisteredFeatures)
newFeatures := featuresMap(newSub.Properties.RegisteredFeatures)

for featureName, oldState := range oldFeatures {
newState, exists := newFeatures[featureName]
if !exists {
logger.Info(fmt.Sprintf("Feature %s removed", featureName))
messages = append(messages, fmt.Sprintf("Feature %s removed", featureName))
} else if oldState != newState {
logger.Info(fmt.Sprintf("Feature %s state changed from %s to %s", featureName, oldState, newState))
messages = append(messages, fmt.Sprintf("Feature %s state changed from %s to %s", featureName, oldState, newState))
}
}
for featureName, newState := range newFeatures {
if _, exists := oldFeatures[featureName]; !exists {
logger.Info(fmt.Sprintf("Feature %s added with state %s", featureName, newState))
messages = append(messages, fmt.Sprintf("Feature %s added with state %s", featureName, newState))
}
}
}
}

return messages
}

func featuresMap(features *[]arm.Feature) map[string]string {
Expand Down

0 comments on commit f9e90fa

Please sign in to comment.