Skip to content

Commit

Permalink
add post route to validate digital ocean domain. re-creates checks us… (
Browse files Browse the repository at this point in the history
#279)

* add post route to validate digital ocean domain. re-creates checks used in cluster controller. run go fmt on project

* replace civo response with digital ocean response
  • Loading branch information
D-B-Hawk authored Jan 19, 2024
1 parent b2f642f commit efa57c0
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 13 deletions.
52 changes: 52 additions & 0 deletions internal/router/api/v1/digitalocean.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Copyright (C) 2021-2024, Kubefirst
This program is licensed under MIT.
See the LICENSE file for more details.
*/
package api

import (
"context"
"net/http"

"github.com/gin-gonic/gin"
"github.com/kubefirst/kubefirst-api/internal/types"
"github.com/kubefirst/runtime/pkg/digitalocean"
)

func PostValidateDigitalOceanDomain(c *gin.Context) {
domainName, exists := c.Params.Get("domain")
if !exists {
c.JSON(http.StatusBadRequest, types.JSONFailureResponse{
Message: ":domain parameter not provided in request",
})
return
}

var request types.DigitalOceanDomainValidationRequest
err := c.Bind(&request)
if err != nil {
c.JSON(http.StatusBadRequest, types.JSONFailureResponse{
Message: err.Error(),
})
return
}

digitaloceanConf := digitalocean.DigitaloceanConfiguration{
Client: digitalocean.NewDigitalocean(request.Token),
Context: context.Background(),
}

validated := digitaloceanConf.TestDomainLiveness(domainName)
if !validated {
c.JSON(http.StatusBadRequest, types.JSONFailureResponse{
Message: "domain validation failed",
})
return
}

c.JSON(http.StatusOK, types.DigitalOceanDomainValidationResponse{
Validated: validated,
})
}
6 changes: 2 additions & 4 deletions internal/router/api/v1/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/kubefirst/kubefirst-api/pkg/k8s"
)


func GetClusterSecret(c *gin.Context) {
clusterName, param := c.Params.Get("cluster_name")
if !param {
Expand Down Expand Up @@ -46,7 +45,7 @@ func GetClusterSecret(c *gin.Context) {

kcfg := k8s.CreateKubeConfig(false, fmt.Sprintf("%s/kubeconfig", clusterDir))

secrets, err := k8s.ReadSecretV2(kcfg.Clientset,"kubefirst",secret)
secrets, err := k8s.ReadSecretV2(kcfg.Clientset, "kubefirst", secret)
if err != nil {
c.JSON(http.StatusBadRequest, types.JSONFailureResponse{
Message: err.Error(),
Expand Down Expand Up @@ -95,7 +94,7 @@ func UpdateClusterSecret(c *gin.Context) {

kcfg := k8s.CreateKubeConfig(false, fmt.Sprintf("%s/kubeconfig", clusterDir))

err = k8s.UpdateSecretV2(kcfg.Clientset,"kubefirst",secret, clusterSecretUpdates)
err = k8s.UpdateSecretV2(kcfg.Clientset, "kubefirst", secret, clusterSecretUpdates)
if err != nil {
c.JSON(http.StatusBadRequest, types.JSONFailureResponse{
Message: err.Error(),
Expand All @@ -107,4 +106,3 @@ func UpdateClusterSecret(c *gin.Context) {
Message: "cluster secret updated",
})
}

6 changes: 2 additions & 4 deletions internal/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func SetupRouter() *gin.Engine {
v1.GET("/cluster/:cluster_name/export", middleware.ValidateAPIKey(), router.GetExportCluster)
v1.POST("/cluster/:cluster_name/reset_progress", middleware.ValidateAPIKey(), router.PostResetClusterProgress)
v1.POST("/cluster/:cluster_name/:cloud_provider/kubeconfig", middleware.ValidateAPIKey(), router.GetClusterKubeconfig)

// Cluster Secret
v1.GET("/secret/:cluster_name/:secret", router.GetClusterSecret)
v1.POST("/secret/:cluster_name/:secret", router.UpdateClusterSecret)
Expand All @@ -72,7 +72,7 @@ func SetupRouter() *gin.Engine {
v1.POST("/domain/:dns_provider", middleware.ValidateAPIKey(), router.PostDomains)
v1.GET("/domain/validate/aws/:domain", middleware.ValidateAPIKey(), router.GetValidateAWSDomain)
v1.GET("/domain/validate/civo/:domain", middleware.ValidateAPIKey(), router.GetValidateCivoDomain)
// v1.GET("/domain/validate/digitalocean/:domain", middleware.ValidateAPIKey(), router.GetValidateDigitalOceanDomain)
v1.POST("/domain/validate/digitalocean/:domain", middleware.ValidateAPIKey(), router.PostValidateDigitalOceanDomain)
// v1.GET("/domain/validate/vultr/:domain", middleware.ValidateAPIKey(), router.GetValidateVultrDomain)
// v1.GET("/domain/validate/google/:domain", middleware.ValidateAPIKey(), router.GetValidateGoogleDomain)
// Regions
Expand All @@ -99,8 +99,6 @@ func SetupRouter() *gin.Engine {
// Event streaming
v1.GET("/stream", router.GetLogs)



// Telemetry
v1.POST("/telemetry/:cluster_name", middleware.ValidateAPIKey(), router.PostTelemetry)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/types/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type CivoDomainValidationResponse struct {

// DigitalOceanDomainValidationRequest /digitalocean/domain/validate required parameters
type DigitalOceanDomainValidationRequest struct {
CloudRegion string `json:"cloud_region"`
Token string `json:"token"`
}

// DigitalOceanDomainValidationResponse is the response for the /digitalocean/domain/validate route
Expand Down
2 changes: 1 addition & 1 deletion pkg/constants/cloudProviderDefaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ var cloudProviderDefaults = types.CloudProviderDefaults{

func GetCloudDefaults() types.CloudProviderDefaults {
return cloudProviderDefaults
}
}
4 changes: 2 additions & 2 deletions pkg/k8s/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ func UpdateSecretV2(clientset *kubernetes.Clientset, namespace string, secretNam

// create map to iterate over values to change
secretsToUpdateMap := make(map[string]string)
err = json.Unmarshal(secretsToUpdate,&secretsToUpdateMap)
err = json.Unmarshal(secretsToUpdate, &secretsToUpdateMap)
if err != nil {
return err
}

currentSecret, err := clientset.CoreV1().Secrets(namespace).Get(context.Background(), secretName, metav1.GetOptions{})
if err != nil{
if err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/k8s/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ type KubernetesClient struct {
Clientset *kubernetes.Clientset
RestConfig *rest.Config
KubeConfigPath string
}
}

0 comments on commit efa57c0

Please sign in to comment.