(ctx context.Context, rc requestContext)
| 92 | } |
| 93 | |
| 94 | func handleRepoCreate(ctx context.Context, rc requestContext) (any, *apiError) { |
| 95 | if rc.rep != nil { |
| 96 | return nil, requestError(serverapi.ErrorAlreadyConnected, "already connected") |
| 97 | } |
| 98 | |
| 99 | var req serverapi.CreateRepositoryRequest |
| 100 | |
| 101 | if err := json.Unmarshal(rc.body, &req); err != nil { |
| 102 | return nil, unableToDecodeRequest(err) |
| 103 | } |
| 104 | |
| 105 | if err := maybeDecodeToken(&req.ConnectRepositoryRequest); err != nil { |
| 106 | return nil, err |
| 107 | } |
| 108 | |
| 109 | st, err := blob.NewStorage(ctx, req.Storage, true) |
| 110 | if err != nil { |
| 111 | return nil, requestError(serverapi.ErrorStorageConnection, "unable to connect to storage: "+err.Error()) |
| 112 | } |
| 113 | defer st.Close(ctx) //nolint:errcheck |
| 114 | |
| 115 | if err = repo.Initialize(ctx, st, &req.NewRepositoryOptions, req.Password); err != nil { |
| 116 | return nil, repoErrorToAPIError(err) |
| 117 | } |
| 118 | |
| 119 | newRepo, err := connectAndOpen(ctx, req.Storage, req.Password, rc.srv.getConnectOptions(req.ClientOptions), rc.srv.getOptions()) |
| 120 | if err != nil { |
| 121 | return nil, repoErrorToAPIError(err) |
| 122 | } |
| 123 | |
| 124 | err = rc.srv.SetRepository(ctx, newRepo) |
| 125 | if err != nil { |
| 126 | return nil, repoErrorToAPIError(err) |
| 127 | } |
| 128 | |
| 129 | if err := repo.WriteSession(ctx, newRepo, repo.WriteSessionOptions{ |
| 130 | Purpose: "handleRepoCreate", |
| 131 | }, func(ctx context.Context, w repo.RepositoryWriter) error { |
| 132 | if err := policy.SetPolicy(ctx, w, policy.GlobalPolicySourceInfo, policy.DefaultPolicy); err != nil { |
| 133 | return errors.Wrap(err, "set global policy") |
| 134 | } |
| 135 | |
| 136 | p := maintenance.DefaultParams() |
| 137 | p.Owner = w.ClientOptions().UsernameAtHost() |
| 138 | |
| 139 | if err := maintenance.SetParams(ctx, w, &p); err != nil { |
| 140 | return errors.Wrap(err, "unable to set maintenance params") |
| 141 | } |
| 142 | |
| 143 | return nil |
| 144 | }); err != nil { |
| 145 | return nil, internalServerError(err) |
| 146 | } |
| 147 | |
| 148 | return handleRepoStatus(ctx, rc) |
| 149 | } |
| 150 | |
| 151 | func handleRepoExists(ctx context.Context, rc requestContext) (any, *apiError) { |
nothing calls this directly
no test coverage detected