Skip to content

Commit 69d3575

Browse files
committed
add api that will generate a new token
- checks that record exists - generate a new token
1 parent e7fa63a commit 69d3575

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

pkg/api/api.go

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ func (h APIHandler) RegisterRoutes() {
110110
cluster.PUT("/:cluster_name/event", h.ResourceEvent)
111111
cluster.DELETE("/:cluster_name/event/:tfo_resource_uuid", h.ResourceEvent)
112112
cluster.GET("/:cluster_name/resource/:namespace/:name/poll", h.ResourcePoll) // Poll for resource objects in the cluster
113+
cluster.PATCH("/:cluster_name/resource/:namespace/:name/token", h.manualTokenPatch)
113114
cluster.GET("/:cluster_name/resource/:namespace/:name/debug", h.Debugger)
114115
cluster.GET("/:cluster_name/debug/:namespace/:name", h.Debugger) // Alias
115116
cluster.GET("/:cluster_name/resource/:namespace/:name/unlock", h.UnlockTerraform)

pkg/api/resource.go

+45
Original file line numberDiff line numberDiff line change
@@ -1540,6 +1540,51 @@ func (h APIHandler) updateResource(c *gin.Context) error {
15401540
return nil
15411541
}
15421542

1543+
// manualTokenPatch is used to re-submit a new token secret to the vcluster. Resources can generally
1544+
// use a refresh token, but this can be useful if the refresh token has been invalidated.
1545+
func (h APIHandler) manualTokenPatch(c *gin.Context) {
1546+
name := c.Param("name")
1547+
namespace := c.Param("namespace")
1548+
clusterName := c.Param("cluster_name")
1549+
1550+
var tfoResourceSpec models.TFOResourceSpec
1551+
1552+
result := h.DB.Raw(`
1553+
SELECT
1554+
tfo_resource_specs.*
1555+
FROM
1556+
tfo_resource_specs
1557+
JOIN
1558+
tfo_resources
1559+
ON tfo_resources.uuid = tfo_resource_specs.tfo_resource_uuid
1560+
JOIN
1561+
clusters
1562+
ON clusters.id = tfo_resources.cluster_id
1563+
WHERE clusters.name = ?
1564+
AND tfo_resources.namespace = ?
1565+
AND tfo_resources.name = ?
1566+
AND tfo_resource_specs.generation = tfo_resources.current_generation
1567+
`, clusterName, namespace, name).Scan(&tfoResourceSpec)
1568+
if result.Error != nil {
1569+
c.JSON(http.StatusUnprocessableEntity, response(http.StatusUnprocessableEntity, fmt.Sprintf("error getting TFOResourceSpec: %v", result.Error), nil))
1570+
return
1571+
}
1572+
if tfoResourceSpec.ID == 0 {
1573+
c.JSON(http.StatusNotFound, response(http.StatusNotFound, "TFOResourceSpec not found", nil))
1574+
return
1575+
}
1576+
1577+
apiURL := GetApiURL(c, h.serviceIP)
1578+
_, err := NewTaskToken(h.DB, tfoResourceSpec, h.tenant, clusterName, apiURL, h.clientset)
1579+
if err != nil {
1580+
1581+
c.JSON(http.StatusUnprocessableEntity, response(http.StatusUnprocessableEntity, err.Error(), nil))
1582+
return
1583+
}
1584+
1585+
c.JSON(http.StatusNoContent, response(http.StatusNoContent, "", nil))
1586+
}
1587+
15431588
// Soft deletes tfo_resources from the database except the latest one via created_at timestamp
15441589
func deleteTFOResourcesExceptNewest(db *gorm.DB, tfoResource *models.TFOResource) error {
15451590
// A tfo resource has a namespace and a name, which are used to identify it uniquely within the cluster.

0 commit comments

Comments
 (0)