|
| 1 | +package datastore |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + |
| 6 | + "connectrpc.com/connect" |
| 7 | + "github.com/google/uuid" |
| 8 | + porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1" |
| 9 | + "github.com/porter-dev/porter/api/server/authz" |
| 10 | + "github.com/porter-dev/porter/api/server/handlers" |
| 11 | + "github.com/porter-dev/porter/api/server/shared" |
| 12 | + "github.com/porter-dev/porter/api/server/shared/apierrors" |
| 13 | + "github.com/porter-dev/porter/api/server/shared/config" |
| 14 | + "github.com/porter-dev/porter/api/server/shared/requestutils" |
| 15 | + "github.com/porter-dev/porter/api/types" |
| 16 | + "github.com/porter-dev/porter/internal/models" |
| 17 | + "github.com/porter-dev/porter/internal/telemetry" |
| 18 | +) |
| 19 | + |
| 20 | +// Credential has all information about connecting to a datastore |
| 21 | +type Credential struct { |
| 22 | + Host string |
| 23 | + Port int |
| 24 | + Username string |
| 25 | + Password string |
| 26 | + DatabaseName string |
| 27 | +} |
| 28 | + |
| 29 | +// CreateDatastoreProxyResponse is the response body for the create datastore proxy endpoint |
| 30 | +type CreateDatastoreProxyResponse struct { |
| 31 | + // PodName is the name of the pod that was created |
| 32 | + PodName string `json:"pod_name"` |
| 33 | + // Credential is the credential used to connect to the datastore |
| 34 | + Credential Credential `json:"credential"` |
| 35 | + // ClusterID is the ID of the cluster that the pod was created in |
| 36 | + ClusterID uint `json:"cluster_id"` |
| 37 | + // Namespace is the namespace that the pod was created in |
| 38 | + Namespace string `json:"namespace"` |
| 39 | + // Type is the type of datastore |
| 40 | + Type string `json:"type"` |
| 41 | +} |
| 42 | + |
| 43 | +// CreateDatastoreProxyHandler is a handler for creating a datastore proxy pod which is used to connect to the datastore |
| 44 | +type CreateDatastoreProxyHandler struct { |
| 45 | + handlers.PorterHandlerReadWriter |
| 46 | + authz.KubernetesAgentGetter |
| 47 | +} |
| 48 | + |
| 49 | +// NewCreateDatastoreProxyHandler returns a CreateDatastoreProxyHandler |
| 50 | +func NewCreateDatastoreProxyHandler( |
| 51 | + config *config.Config, |
| 52 | + decoderValidator shared.RequestDecoderValidator, |
| 53 | + writer shared.ResultWriter, |
| 54 | +) *CreateDatastoreProxyHandler { |
| 55 | + return &CreateDatastoreProxyHandler{ |
| 56 | + PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer), |
| 57 | + KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config), |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// ServeHTTP creates a datastore proxy pod |
| 62 | +func (c *CreateDatastoreProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 63 | + ctx, span := telemetry.NewSpan(r.Context(), "serve-create-datastore-proxy") |
| 64 | + defer span.End() |
| 65 | + |
| 66 | + project, _ := ctx.Value(types.ProjectScope).(*models.Project) |
| 67 | + if project.ID == 0 { |
| 68 | + err := telemetry.Error(ctx, span, nil, "project not found") |
| 69 | + c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest)) |
| 70 | + return |
| 71 | + } |
| 72 | + projectId := int64(project.ID) |
| 73 | + |
| 74 | + var resp CreateDatastoreProxyResponse |
| 75 | + |
| 76 | + datastoreName, reqErr := requestutils.GetURLParamString(r, types.URLParamDatastoreName) |
| 77 | + if reqErr != nil { |
| 78 | + err := telemetry.Error(ctx, span, nil, "error parsing datastore name") |
| 79 | + c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest)) |
| 80 | + return |
| 81 | + } |
| 82 | + telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "datastore-name", Value: datastoreName}) |
| 83 | + |
| 84 | + datastoreRecord, err := c.Repo().Datastore().GetByProjectIDAndName(ctx, project.ID, datastoreName) |
| 85 | + if err != nil { |
| 86 | + err = telemetry.Error(ctx, span, err, "datastore record not found") |
| 87 | + c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError)) |
| 88 | + return |
| 89 | + } |
| 90 | + |
| 91 | + if datastoreRecord == nil || datastoreRecord.ID == uuid.Nil { |
| 92 | + err = telemetry.Error(ctx, span, nil, "datastore record does not exist") |
| 93 | + c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest)) |
| 94 | + return |
| 95 | + } |
| 96 | + |
| 97 | + message := porterv1.CreateDatastoreProxyRequest{ |
| 98 | + ProjectId: projectId, |
| 99 | + DatastoreId: datastoreRecord.ID.String(), |
| 100 | + } |
| 101 | + req := connect.NewRequest(&message) |
| 102 | + ccpResp, err := c.Config().ClusterControlPlaneClient.CreateDatastoreProxy(ctx, req) |
| 103 | + if err != nil { |
| 104 | + err = telemetry.Error(ctx, span, err, "error creating datastore proxy") |
| 105 | + c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError)) |
| 106 | + return |
| 107 | + } |
| 108 | + if ccpResp == nil || ccpResp.Msg == nil { |
| 109 | + err = telemetry.Error(ctx, span, nil, "error creating datastore proxy") |
| 110 | + c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError)) |
| 111 | + return |
| 112 | + } |
| 113 | + |
| 114 | + resp = CreateDatastoreProxyResponse{ |
| 115 | + PodName: ccpResp.Msg.PodName, |
| 116 | + Credential: Credential{ |
| 117 | + Host: ccpResp.Msg.Credential.Host, |
| 118 | + Port: int(ccpResp.Msg.Credential.Port), |
| 119 | + Username: ccpResp.Msg.Credential.Username, |
| 120 | + Password: ccpResp.Msg.Credential.Password, |
| 121 | + DatabaseName: ccpResp.Msg.Credential.DatabaseName, |
| 122 | + }, |
| 123 | + ClusterID: uint(ccpResp.Msg.ClusterId), |
| 124 | + Namespace: ccpResp.Msg.Namespace, |
| 125 | + Type: datastoreRecord.Type, |
| 126 | + } |
| 127 | + |
| 128 | + c.WriteResult(w, r, resp) |
| 129 | +} |
0 commit comments