MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / handleCreateDB

Method handleCreateDB

rest/admin_api.go:44–181  ·  view source on GitHub ↗

DATABASE MAINTENANCE: "Create" a database (actually just register an existing bucket)

()

Source from the content-addressed store, hash-verified

42
43// "Create" a database (actually just register an existing bucket)
44func (h *handler) handleCreateDB() error {
45 contextNoCancel := base.NewNonCancelCtx()
46 h.assertAdminOnly()
47 dbName := h.PathVar("newdb")
48 rawBytes, config, err := h.readSanitizeDbConfigJSON()
49 if err != nil {
50 return err
51 }
52
53 validateOIDC := !h.getBoolQuery(paramDisableOIDCValidation)
54
55 if config.Name != "" && dbName != config.Name {
56 return base.HTTPErrorf(http.StatusBadRequest, "When providing a name in the JSON body (%s), ensure it matches the name in the path (%s).", config.Name, dbName)
57 }
58
59 config.Name = dbName
60 if h.server.persistentConfig {
61 if err := config.validatePersistentDbConfig(); err != nil {
62 return base.NewHTTPError(http.StatusBadRequest, err.Error())
63 }
64
65 validateReplications := true
66 if err := config.validate(h.ctx(), validateOIDC, validateReplications); err != nil {
67 return base.NewHTTPError(http.StatusBadRequest, err.Error())
68 }
69
70 version, err := GenerateDatabaseConfigVersionID(h.ctx(), "", config)
71 if err != nil {
72 return err
73 }
74
75 bucket := config.GetBucketName()
76
77 // Before computing metadata ID or taking a copy of the "persistedDbConfig", stamp bucket if missing...
78 // Ensures all newly persisted configs have a bucket field set to detect bucket moves via backup/restore or XDCR
79 if config.Bucket == nil {
80 config.Bucket = &bucket
81 }
82
83 metadataID, metadataIDError := h.server.BootstrapContext.ComputeMetadataIDForDbConfig(h.ctx(), config)
84 if metadataIDError != nil {
85 base.WarnfCtx(h.ctx(), "Unable to compute metadata ID - using standard metadataID. Error: %v", metadataIDError)
86 metadataID = h.server.BootstrapContext.standardMetadataID(config.Name)
87 }
88
89 // copy config before setup to persist the raw config the user supplied
90 var persistedDbConfig DbConfig
91 if err := base.DeepCopyInefficient(&persistedDbConfig, config); err != nil {
92 return base.HTTPErrorf(http.StatusInternalServerError, "couldn't create copy of db config: %v", err)
93 }
94
95 dbCreds, _ := h.server.Config.DatabaseCredentials[dbName]
96 bucketCreds, _ := h.server.Config.BucketCredentials[bucket]
97 if err := config.setup(h.ctx(), dbName, h.server.Config.Bootstrap, dbCreds, bucketCreds, h.server.Config.IsServerless()); err != nil {
98 return err
99 }
100
101 loadedConfig := DatabaseConfig{

Callers

nothing calls this directly

Calls 15

assertAdminOnlyMethod · 0.95
PathVarMethod · 0.95
getBoolQueryMethod · 0.95
ctxMethod · 0.95
NewNonCancelCtxFunction · 0.92
HTTPErrorfFunction · 0.92
NewHTTPErrorFunction · 0.92
WarnfCtxFunction · 0.92
DeepCopyInefficientFunction · 0.92
AuditFunction · 0.92

Tested by

no test coverage detected