-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathhandler_config_database.go
96 lines (81 loc) · 3.23 KB
/
handler_config_database.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright 2022-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
// in that file, in accordance with the Business Source License, use of this
// software will be governed by the Apache License, Version 2.0, included in
// the file licenses/APL2.txt.
package rest
import (
"net/http"
"github.com/couchbase/sync_gateway/base"
)
// Returns the current database config object for this request
func (h *handler) getDBConfig() (config *DbConfig, etagVersion string, err error) {
h.assertAdminOnly()
if h.server.BootstrapContext.Connection == nil {
if dbConfig := h.server.GetDatabaseConfig(h.db.Name); dbConfig != nil {
etagVersion = dbConfig.Version
if etagVersion == "" {
etagVersion = "0-"
}
return &dbConfig.DbConfig, etagVersion, nil
}
return nil, "", nil
} else if found, databaseConfig, err := h.server.fetchDatabase(h.ctx(), h.db.Name); err != nil {
return nil, "", err
} else if !found {
return nil, "", base.HTTPErrorf(http.StatusNotFound, "database config not found")
} else {
return &databaseConfig.DbConfig, databaseConfig.Version, nil
}
}
// Updates the database config via a callback function that can modify a `DbConfig`
func (h *handler) mutateDbConfig(mutator func(*DbConfig) error) error {
h.assertAdminOnly()
if !h.server.persistentConfig {
return base.HTTPErrorf(http.StatusServiceUnavailable, "persistent config is disabled")
}
dbName := h.db.Name
validateOIDC := !h.getBoolQuery(paramDisableOIDCValidation)
// Update persistently-stored config:
bucket := h.db.Bucket.GetName()
var updatedDbConfig *DatabaseConfig
cas, err := h.server.BootstrapContext.UpdateConfig(h.ctx(), bucket, h.server.Config.Bootstrap.ConfigGroupID, dbName, func(bucketDbConfig *DatabaseConfig) (updatedConfig *DatabaseConfig, err error) {
if h.headerDoesNotMatchEtag(bucketDbConfig.Version) {
return nil, base.HTTPErrorf(http.StatusPreconditionFailed, "Provided If-Match header does not match current config version")
}
// Now call the mutator function:
if err := mutator(&bucketDbConfig.DbConfig); err != nil {
return nil, err
}
validateReplications := false
if err := bucketDbConfig.validate(h.ctx(), validateOIDC, validateReplications); err != nil {
return nil, base.NewHTTPError(http.StatusBadRequest, err.Error())
}
bucketDbConfig.Version, err = GenerateDatabaseConfigVersionID(h.ctx(), bucketDbConfig.Version, &bucketDbConfig.DbConfig)
if err != nil {
return nil, err
}
updatedDbConfig = bucketDbConfig
return bucketDbConfig, nil
})
if err != nil {
return err
}
updatedDbConfig.cfgCas = cas
dbCreds := h.server.Config.DatabaseCredentials[dbName]
bucketCreds := h.server.Config.BucketCredentials[bucket]
if err := updatedDbConfig.setup(h.ctx(), dbName, h.server.Config.Bootstrap, dbCreds, bucketCreds, h.server.Config.IsServerless()); err != nil {
return err
}
h.server.lock.Lock()
defer h.server.lock.Unlock()
// TODO: Dynamic update instead of reload
if err := h.server._reloadDatabaseWithConfig(h.ctx(), *updatedDbConfig, false, false); err != nil {
return err
}
h.setEtag(updatedDbConfig.Version)
h.setStatus(http.StatusOK, "updated")
return nil
}