Răsfoiți Sursa

pushing what i have

Stefan McShane 3 ani în urmă
părinte
comite
bca459d76f

+ 1 - 0
api/server/authz/api_contract.go

@@ -56,6 +56,7 @@ func (n *APIContractRevisionMiddleware) ServeHTTP(w http.ResponseWriter, r *http
 		apierrors.HandleAPIError(n.config.Logger, n.config.Alerter, w, r, apierrors.NewErrInternal(err), true)
 		return
 	}
+	fmt.Println("STEFAN", rev, uid)
 
 	r = r.Clone(NewAPIContractRevisionContext(ctx, rev))
 	n.next.ServeHTTP(w, r)

+ 2 - 1
api/server/handlers/api_contract/delete.go

@@ -32,9 +32,10 @@ func (c *APIContractRevisionDeleteHandler) ServeHTTP(w http.ResponseWriter, r *h
 	revision, _ := r.Context().Value(types.APIContractRevisionScope).(*models.APIContractRevision)
 
 	ctx := r.Context()
+	fmt.Println("STEFAN", revision)
 
 	if revision == nil {
-		e := fmt.Errorf("nil revision")
+		e := fmt.Errorf("nil revision: %s", r.URL.Path)
 		c.HandleAPIError(w, r, apierrors.NewErrInternal(e))
 		return
 	}

+ 28 - 3
api/server/handlers/api_contract/update.go

@@ -46,7 +46,27 @@ func (c *APIContractUpdateHandler) ServeHTTP(w http.ResponseWriter, r *http.Requ
 
 	if c.Config().DisableCAPIProvisioner {
 		// return dummy data if capi provisioner disabled
-		// remove this stub when we can spin up all services locally, easily
+		// TODO: remove this stub when we can spin up all services locally, easily
+		clusterID := apiContract.Cluster.ClusterId
+		if apiContract.Cluster.ClusterId == 0 {
+			dbcli := models.Cluster{
+				ProjectID:                         uint(apiContract.Cluster.ProjectId),
+				Status:                            "UPDATING_UNAVAILABLE",
+				ProvisionedBy:                     "CAPI",
+				CloudProvider:                     "AWS",
+				CloudProviderCredentialIdentifier: apiContract.Cluster.CloudProviderCredentialsId,
+				Name:                              apiContract.Cluster.GetEksKind().ClusterName,
+				VanityName:                        apiContract.Cluster.GetEksKind().ClusterName,
+			}
+			dbcl, err := c.Config().Repo.Cluster().CreateCluster(&dbcli)
+			if err != nil {
+				e := fmt.Errorf("error updating mock contract: %w", err)
+				c.HandleAPIError(w, r, apierrors.NewErrInternal(e))
+				return
+			}
+			clusterID = int32(dbcl.ID)
+		}
+
 		by, err := helpers.MarshalContractObject(ctx, &apiContract)
 		if err != nil {
 			e := fmt.Errorf("error marshalling mock api contract: %w", err)
@@ -57,7 +77,7 @@ func (c *APIContractUpdateHandler) ServeHTTP(w http.ResponseWriter, r *http.Requ
 
 		revisionInput := models.APIContractRevision{
 			ID:             uuid.New(),
-			ClusterID:      int(apiContract.Cluster.ClusterId),
+			ClusterID:      int(clusterID),
 			ProjectID:      int(apiContract.Cluster.ProjectId),
 			Base64Contract: b64Contract,
 		}
@@ -67,8 +87,13 @@ func (c *APIContractUpdateHandler) ServeHTTP(w http.ResponseWriter, r *http.Requ
 			c.HandleAPIError(w, r, apierrors.NewErrInternal(e))
 			return
 		}
+		resp := &porterv1.ContractRevision{
+			ClusterId:  int32(clusterID),
+			ProjectId:  apiContract.Cluster.ProjectId,
+			RevisionId: revision.ID.String(),
+		}
 		w.WriteHeader(http.StatusCreated)
-		c.WriteResult(w, r, revision)
+		c.WriteResult(w, r, resp)
 		return
 	}
 

+ 1 - 1
api/server/router/project.go

@@ -1290,7 +1290,7 @@ func getProjectRoutes(
 		Router:   r,
 	})
 
-	// GET /api/projects/{project_id}/contracts -> apiContract.NewAPIContractUpdateHandler
+	// GET /api/projects/{project_id}/contracts -> apiContract.NewAPIContractRevisionListHandler
 	listAPIContractRevisionsEndpoint := factory.NewAPIEndpoint(
 		&types.APIRequestMetadata{
 			Verb:   types.APIVerbGet,

+ 4 - 0
api/server/router/router.go

@@ -235,6 +235,8 @@ func registerRoutes(config *config.Config, routes []*router.Route) {
 	// preview environment middleware to handle previw environments for a specific project-cluster pair
 	previewEnvFactory := authz.NewPreviewEnvironmentScopedFactory(config)
 
+	apiContractRevisionFactory := authz.NewAPIContractRevisionScopedFactory(config)
+
 	for _, route := range routes {
 		atomicGroup := route.Router.Group(nil)
 
@@ -276,6 +278,8 @@ func registerRoutes(config *config.Config, routes []*router.Route) {
 				atomicGroup.Use(gitlabIntFactory.Middleware)
 			case types.PreviewEnvironmentScope:
 				atomicGroup.Use(previewEnvFactory.Middleware)
+			case types.APIContractRevisionScope:
+				atomicGroup.Use(apiContractRevisionFactory.Middleware)
 			}
 		}