|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/julienschmidt/httprouter" |
| 9 | + "github.com/kubeflow/model-registry/ui/bff/internal/constants" |
| 10 | + helper "github.com/kubeflow/model-registry/ui/bff/internal/helpers" |
| 11 | + "github.com/kubeflow/model-registry/ui/bff/internal/models" |
| 12 | +) |
| 13 | + |
| 14 | +type ModelRegistrySettingsListEnvelope Envelope[[]models.ModelRegistryKind, None] |
| 15 | +type ModelRegistrySettingsEnvelope Envelope[models.ModelRegistryKind, None] |
| 16 | + |
| 17 | +func (app *App) GetAllModelRegistriesSettingsHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { |
| 18 | + ctxLogger := helper.GetContextLoggerFromReq(r) |
| 19 | + ctxLogger.Info("This functionality is not implement yet. This is a STUB API to unblock frontend development") |
| 20 | + |
| 21 | + namespace, ok := r.Context().Value(constants.NamespaceHeaderParameterKey).(string) |
| 22 | + if !ok || namespace == "" { |
| 23 | + app.badRequestResponse(w, r, fmt.Errorf("missing namespace in the context")) |
| 24 | + } |
| 25 | + |
| 26 | + registries := []models.ModelRegistryKind{createSampleModelRegistry("model-registry", namespace), |
| 27 | + createSampleModelRegistry("model-registry-dora", namespace), |
| 28 | + createSampleModelRegistry("model-registry-bella", namespace)} |
| 29 | + |
| 30 | + modelRegistryRes := ModelRegistrySettingsListEnvelope{ |
| 31 | + Data: registries, |
| 32 | + } |
| 33 | + |
| 34 | + err := app.WriteJSON(w, http.StatusOK, modelRegistryRes, nil) |
| 35 | + |
| 36 | + if err != nil { |
| 37 | + app.serverErrorResponse(w, r, err) |
| 38 | + } |
| 39 | + |
| 40 | +} |
| 41 | + |
| 42 | +func (app *App) GetModelRegistrySettingsHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { |
| 43 | + ctxLogger := helper.GetContextLoggerFromReq(r) |
| 44 | + ctxLogger.Info("This functionality is not implement yet. This is a STUB API to unblock frontend development") |
| 45 | + |
| 46 | + namespace, ok := r.Context().Value(constants.NamespaceHeaderParameterKey).(string) |
| 47 | + if !ok || namespace == "" { |
| 48 | + app.badRequestResponse(w, r, fmt.Errorf("missing namespace in the context")) |
| 49 | + } |
| 50 | + |
| 51 | + modelId := ps.ByName(ModelRegistryId) |
| 52 | + registry := createSampleModelRegistry(modelId, namespace) |
| 53 | + |
| 54 | + modelRegistryRes := ModelRegistrySettingsEnvelope{ |
| 55 | + Data: registry, |
| 56 | + } |
| 57 | + |
| 58 | + err := app.WriteJSON(w, http.StatusOK, modelRegistryRes, nil) |
| 59 | + |
| 60 | + if err != nil { |
| 61 | + app.serverErrorResponse(w, r, err) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func (app *App) CreateModelRegistrySettingsHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { |
| 66 | + ctxLogger := helper.GetContextLoggerFromReq(r) |
| 67 | + ctxLogger.Info("This functionality is not implement yet. This is a STUB API to unblock frontend development") |
| 68 | + |
| 69 | + namespace, ok := r.Context().Value(constants.NamespaceHeaderParameterKey).(string) |
| 70 | + if !ok || namespace == "" { |
| 71 | + app.badRequestResponse(w, r, fmt.Errorf("missing namespace in the context")) |
| 72 | + } |
| 73 | + |
| 74 | + registry := createSampleModelRegistry("new-model-registry", namespace) |
| 75 | + |
| 76 | + modelRegistryRes := ModelRegistrySettingsEnvelope{ |
| 77 | + Data: registry, |
| 78 | + } |
| 79 | + |
| 80 | + w.Header().Set("Location", r.URL.JoinPath(modelRegistryRes.Data.Metadata.Name).String()) |
| 81 | + err := app.WriteJSON(w, http.StatusCreated, modelRegistryRes, nil) |
| 82 | + if err != nil { |
| 83 | + app.serverErrorResponse(w, r, fmt.Errorf("error writing JSON")) |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | +} |
| 88 | + |
| 89 | +func (app *App) UpdateModelRegistrySettingsHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { |
| 90 | + ctxLogger := helper.GetContextLoggerFromReq(r) |
| 91 | + ctxLogger.Info("This functionality is not implement yet. This is a STUB API to unblock frontend development") |
| 92 | + |
| 93 | + namespace, ok := r.Context().Value(constants.NamespaceHeaderParameterKey).(string) |
| 94 | + if !ok || namespace == "" { |
| 95 | + app.badRequestResponse(w, r, fmt.Errorf("missing namespace in the context")) |
| 96 | + } |
| 97 | + |
| 98 | + modelId := ps.ByName(ModelRegistryId) |
| 99 | + registry := createSampleModelRegistry(modelId, namespace) |
| 100 | + |
| 101 | + modelRegistryRes := ModelRegistrySettingsEnvelope{ |
| 102 | + Data: registry, |
| 103 | + } |
| 104 | + |
| 105 | + err := app.WriteJSON(w, http.StatusOK, modelRegistryRes, nil) |
| 106 | + if err != nil { |
| 107 | + app.serverErrorResponse(w, r, fmt.Errorf("error writing JSON")) |
| 108 | + return |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func (app *App) DeleteModelRegistrySettingsHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { |
| 113 | + ctxLogger := helper.GetContextLoggerFromReq(r) |
| 114 | + ctxLogger.Info("This functionality is not implement yet. This is a STUB API to unblock frontend development") |
| 115 | + |
| 116 | + namespace, ok := r.Context().Value(constants.NamespaceHeaderParameterKey).(string) |
| 117 | + if !ok || namespace == "" { |
| 118 | + app.badRequestResponse(w, r, fmt.Errorf("missing namespace in the context")) |
| 119 | + } |
| 120 | + |
| 121 | + w.WriteHeader(200) |
| 122 | +} |
| 123 | + |
| 124 | +// This function is a temporary function to create a sample model registry kind until we have a real implementation |
| 125 | +func createSampleModelRegistry(name string, namespace string) models.ModelRegistryKind { |
| 126 | + |
| 127 | + creationTime, _ := time.Parse(time.RFC3339, "2024-03-14T08:01:42Z") |
| 128 | + lastTransitionTime, _ := time.Parse(time.RFC3339, "2024-03-22T09:30:02Z") |
| 129 | + |
| 130 | + return models.ModelRegistryKind{ |
| 131 | + APIVersion: "modelregistry.io/v1alpha1", |
| 132 | + Kind: "ModelRegistry", |
| 133 | + Metadata: models.Metadata{ |
| 134 | + Name: name, |
| 135 | + Namespace: namespace, |
| 136 | + CreationTimestamp: creationTime, |
| 137 | + Annotations: map[string]string{}, |
| 138 | + }, |
| 139 | + Spec: models.ModelRegistrySpec{ |
| 140 | + GRPC: models.EmptyObject{}, |
| 141 | + REST: models.EmptyObject{}, |
| 142 | + Istio: models.IstioConfig{ |
| 143 | + Gateway: models.GatewayConfig{ |
| 144 | + GRPC: models.GRPCConfig{ |
| 145 | + TLS: models.EmptyObject{}, |
| 146 | + }, |
| 147 | + REST: models.RESTConfig{ |
| 148 | + TLS: models.EmptyObject{}, |
| 149 | + }, |
| 150 | + }, |
| 151 | + }, |
| 152 | + DatabaseConfig: models.DatabaseConfig{ |
| 153 | + DatabaseType: models.MySQL, |
| 154 | + Database: "model-registry", |
| 155 | + Host: "model-registry-db", |
| 156 | + //intentionally not set |
| 157 | + // PasswordSecret: models.PasswordSecret{ |
| 158 | + // Key: "database-password", |
| 159 | + // Name: "model-registry-db", |
| 160 | + // }, |
| 161 | + Port: 5432, |
| 162 | + SkipDBCreation: false, |
| 163 | + Username: "mlmduser", |
| 164 | + SSLRootCertificateConfigMap: "ssl-config-map", |
| 165 | + SSLRootCertificateSecret: "ssl-secret", |
| 166 | + }, |
| 167 | + }, |
| 168 | + Status: models.Status{ |
| 169 | + Conditions: []models.Condition{ |
| 170 | + { |
| 171 | + LastTransitionTime: lastTransitionTime, |
| 172 | + Message: "Deployment for custom resource " + name + " was successfully created", |
| 173 | + Reason: "CreatedDeployment", |
| 174 | + Status: "True", |
| 175 | + Type: "Progressing", |
| 176 | + }, |
| 177 | + }, |
| 178 | + }, |
| 179 | + } |
| 180 | +} |
0 commit comments