Skip to content

Commit 09d6f12

Browse files
authored
Merge pull request #455 from piyumaldk/gw-apis
Revert Deployment Changes
2 parents 815ee2f + debabb6 commit 09d6f12

File tree

2 files changed

+124
-10
lines changed

2 files changed

+124
-10
lines changed

gateway/gateway-controller/pkg/utils/api_deployment.go

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,13 @@ func (s *APIDeploymentService) saveOrUpdateConfig(storedCfg *models.StoredConfig
340340
if err := s.db.SaveConfig(storedCfg); err != nil {
341341
// Check if it's a conflict (API already exists)
342342
if storage.IsConflictError(err) {
343-
return false, fmt.Errorf("%w: configuration with name '%s' and version '%s' already exists", storage.ErrConflict, storedCfg.GetName(), storedCfg.GetVersion())
343+
logger.Info("API configuration already exists in database, updating instead",
344+
zap.String("api_id", storedCfg.ID),
345+
zap.String("name", storedCfg.GetName()),
346+
zap.String("version", storedCfg.GetVersion()))
347+
348+
// Try to update instead
349+
return s.updateExistingConfig(storedCfg, logger)
344350
} else {
345351
return false, fmt.Errorf("failed to save config to database: %w", err)
346352
}
@@ -351,10 +357,13 @@ func (s *APIDeploymentService) saveOrUpdateConfig(storedCfg *models.StoredConfig
351357
if err := s.store.Add(storedCfg); err != nil {
352358
// Check if it's a conflict (API already exists)
353359
if storage.IsConflictError(err) {
354-
if s.db != nil {
355-
_ = s.db.DeleteConfig(storedCfg.ID)
356-
}
357-
return false, fmt.Errorf("%w: configuration with name '%s' and version '%s' already exists", storage.ErrConflict, storedCfg.GetName(), storedCfg.GetVersion())
360+
logger.Info("API configuration already exists in memory, updating instead",
361+
zap.String("api_id", storedCfg.ID),
362+
zap.String("name", storedCfg.GetName()),
363+
zap.String("version", storedCfg.GetVersion()))
364+
365+
// Try to update instead
366+
return s.updateExistingConfig(storedCfg, logger)
358367
} else {
359368
// Rollback database write (only if persistent mode)
360369
if s.db != nil {
@@ -367,6 +376,53 @@ func (s *APIDeploymentService) saveOrUpdateConfig(storedCfg *models.StoredConfig
367376
return false, nil // Successfully created new config
368377
}
369378

379+
// updateExistingConfig updates an existing API configuration
380+
func (s *APIDeploymentService) updateExistingConfig(newConfig *models.StoredConfig, logger *zap.Logger) (bool, error) {
381+
// Get existing config
382+
existing, err := s.store.GetByNameVersion(newConfig.GetName(), newConfig.GetVersion())
383+
if err != nil {
384+
return false, fmt.Errorf("failed to get existing config: %w", err)
385+
}
386+
387+
// Backup original state for potential rollback
388+
original := *existing
389+
390+
// Update the existing configuration
391+
now := time.Now()
392+
existing.Configuration = newConfig.Configuration
393+
existing.Status = models.StatusPending
394+
existing.UpdatedAt = now
395+
existing.DeployedAt = nil
396+
existing.DeployedVersion = 0
397+
398+
// Update database first (only if persistent mode)
399+
if s.db != nil {
400+
if err := s.db.UpdateConfig(existing); err != nil {
401+
return false, fmt.Errorf("failed to update config in database: %w", err)
402+
}
403+
}
404+
405+
// Update in-memory store
406+
if err := s.store.Update(existing); err != nil {
407+
// Rollback DB to original state since memory update failed
408+
if s.db != nil {
409+
if rbErr := s.db.UpdateConfig(&original); rbErr != nil {
410+
logger.Error("Failed to rollback DB after memory update failure",
411+
zap.Error(rbErr),
412+
zap.String("id", original.ID),
413+
zap.String("name", original.GetName()),
414+
zap.String("version", original.GetVersion()))
415+
}
416+
}
417+
return false, fmt.Errorf("failed to update config in memory store: %w", err)
418+
}
419+
420+
// Update the newConfig to reflect the changes
421+
*newConfig = *existing
422+
423+
return true, nil // Successfully updated existing config
424+
}
425+
370426
// RegisterTopicWithHub registers a topic with the WebSubHub
371427
func (s *APIDeploymentService) RegisterTopicWithHub(httpClient *http.Client, topic, webSubHubHost string, webSubPort int, logger *zap.Logger) error {
372428
return s.sendTopicRequestToHub(httpClient, topic, "register", webSubHubHost, webSubPort, logger)

gateway/gateway-controller/pkg/utils/mcp_deployment.go

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,13 @@ func (s *MCPDeploymentService) saveOrUpdateConfig(storedCfg *models.StoredConfig
200200
if err := s.db.SaveConfig(storedCfg); err != nil {
201201
// Check if it's a conflict (Configuration already exists)
202202
if storage.IsConflictError(err) {
203-
return false, fmt.Errorf("%w: configuration with name '%s' and version '%s' already exists", storage.ErrConflict, storedCfg.GetName(), storedCfg.GetVersion())
203+
logger.Info("MCP configuration already exists in database, updating instead",
204+
zap.String("id", storedCfg.ID),
205+
zap.String("name", storedCfg.GetName()),
206+
zap.String("version", storedCfg.GetVersion()))
207+
208+
// Try to update instead
209+
return s.updateExistingConfig(storedCfg, logger)
204210
} else {
205211
return false, fmt.Errorf("failed to save config to database: %w", err)
206212
}
@@ -211,10 +217,13 @@ func (s *MCPDeploymentService) saveOrUpdateConfig(storedCfg *models.StoredConfig
211217
if err := s.store.Add(storedCfg); err != nil {
212218
// Check if it's a conflict (API already exists)
213219
if storage.IsConflictError(err) {
214-
if s.db != nil {
215-
_ = s.db.DeleteConfig(storedCfg.ID)
216-
}
217-
return false, fmt.Errorf("%w: configuration with name '%s' and version '%s' already exists", storage.ErrConflict, storedCfg.GetName(), storedCfg.GetVersion())
220+
logger.Info("MCP configuration already exists in memory, updating instead",
221+
zap.String("id", storedCfg.ID),
222+
zap.String("name", storedCfg.GetName()),
223+
zap.String("version", storedCfg.GetVersion()))
224+
225+
// Try to update instead
226+
return s.updateExistingConfig(storedCfg, logger)
218227
} else {
219228
// Rollback database write (only if persistent mode)
220229
if s.db != nil {
@@ -226,3 +235,52 @@ func (s *MCPDeploymentService) saveOrUpdateConfig(storedCfg *models.StoredConfig
226235

227236
return false, nil // Successfully created new config
228237
}
238+
239+
// updateExistingConfig updates an existing API configuration
240+
func (s *MCPDeploymentService) updateExistingConfig(newConfig *models.StoredConfig,
241+
logger *zap.Logger) (bool, error) {
242+
// Get existing config
243+
existing, err := s.store.GetByNameVersion(newConfig.GetName(), newConfig.GetVersion())
244+
if err != nil {
245+
return false, fmt.Errorf("failed to get existing config: %w", err)
246+
}
247+
248+
// Backup original state for potential rollback
249+
original := *existing
250+
251+
// Update the existing configuration
252+
now := time.Now()
253+
existing.Configuration = newConfig.Configuration
254+
existing.SourceConfiguration = newConfig.SourceConfiguration
255+
existing.Status = models.StatusPending
256+
existing.UpdatedAt = now
257+
existing.DeployedAt = nil
258+
existing.DeployedVersion = 0
259+
260+
// Update database first (only if persistent mode)
261+
if s.db != nil {
262+
if err := s.db.UpdateConfig(existing); err != nil {
263+
return false, fmt.Errorf("failed to update config in database: %w", err)
264+
}
265+
}
266+
267+
// Update in-memory store
268+
if err := s.store.Update(existing); err != nil {
269+
// Rollback DB to original state since memory update failed
270+
if s.db != nil {
271+
if rbErr := s.db.UpdateConfig(&original); rbErr != nil {
272+
logger.Error("Failed to rollback DB after memory update failure",
273+
zap.Error(rbErr),
274+
zap.String("id", original.ID),
275+
zap.String("name", original.GetName()),
276+
zap.String("version", original.GetVersion()))
277+
}
278+
}
279+
return false, fmt.Errorf("failed to update config in memory store: %w", err)
280+
}
281+
282+
// Update the newConfig to reflect the changes
283+
*newConfig = *existing
284+
285+
return true, nil // Successfully updated existing config
286+
}

0 commit comments

Comments
 (0)