Updates the database config via a callback function that can modify a `DbConfig`
(mutator func(*DbConfig) error)
| 37 | |
| 38 | // Updates the database config via a callback function that can modify a `DbConfig` |
| 39 | func (h *handler) mutateDbConfig(mutator func(*DbConfig) error) error { |
| 40 | h.assertAdminOnly() |
| 41 | |
| 42 | if !h.server.persistentConfig { |
| 43 | return base.HTTPErrorf(http.StatusServiceUnavailable, "persistent config is disabled") |
| 44 | } |
| 45 | |
| 46 | dbName := h.db.Name |
| 47 | validateOIDC := !h.getBoolQuery(paramDisableOIDCValidation) |
| 48 | |
| 49 | // Update persistently-stored config: |
| 50 | bucket := h.db.Bucket.GetName() |
| 51 | var updatedDbConfig *DatabaseConfig |
| 52 | cas, err := h.server.BootstrapContext.UpdateConfig(h.ctx(), bucket, h.server.Config.Bootstrap.ConfigGroupID, dbName, func(bucketDbConfig *DatabaseConfig) (updatedConfig *DatabaseConfig, err error) { |
| 53 | |
| 54 | if h.headerDoesNotMatchEtag(bucketDbConfig.Version) { |
| 55 | return nil, base.HTTPErrorf(http.StatusPreconditionFailed, "Provided If-Match header does not match current config version") |
| 56 | } |
| 57 | |
| 58 | // Now call the mutator function: |
| 59 | if err := mutator(&bucketDbConfig.DbConfig); err != nil { |
| 60 | return nil, err |
| 61 | } |
| 62 | |
| 63 | validateReplications := false |
| 64 | if err := bucketDbConfig.validate(h.ctx(), validateOIDC, validateReplications); err != nil { |
| 65 | return nil, base.NewHTTPError(http.StatusBadRequest, err.Error()) |
| 66 | } |
| 67 | |
| 68 | bucketDbConfig.Version, err = GenerateDatabaseConfigVersionID(h.ctx(), bucketDbConfig.Version, &bucketDbConfig.DbConfig) |
| 69 | if err != nil { |
| 70 | return nil, err |
| 71 | } |
| 72 | updatedDbConfig = bucketDbConfig |
| 73 | return bucketDbConfig, nil |
| 74 | }) |
| 75 | if err != nil { |
| 76 | return err |
| 77 | } |
| 78 | updatedDbConfig.cfgCas = cas |
| 79 | |
| 80 | dbCreds := h.server.Config.DatabaseCredentials[dbName] |
| 81 | bucketCreds := h.server.Config.BucketCredentials[bucket] |
| 82 | if err := updatedDbConfig.setup(h.ctx(), dbName, h.server.Config.Bootstrap, dbCreds, bucketCreds, h.server.Config.IsServerless()); err != nil { |
| 83 | return err |
| 84 | } |
| 85 | |
| 86 | h.server._databasesLock.Lock() |
| 87 | defer h.server._databasesLock.Unlock() |
| 88 | |
| 89 | // TODO: Dynamic update instead of reload |
| 90 | if err := h.server._reloadDatabaseWithConfig(h.ctx(), *updatedDbConfig, false, false); err != nil { |
| 91 | return err |
| 92 | } |
| 93 | h.setEtag(updatedDbConfig.Version) |
| 94 | h.setStatus(http.StatusOK, "updated") |
| 95 | return nil |
| 96 | } |
no test coverage detected