-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: resource groups and domain endpoints #469
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ import ( | |
cloudflare_api "github.com/cloudflare/cloudflare-go" | ||
"github.com/gin-gonic/gin" | ||
awsinternal "github.com/konstructio/kubefirst-api/internal/aws" | ||
"github.com/konstructio/kubefirst-api/internal/azure" | ||
"github.com/konstructio/kubefirst-api/internal/civo" | ||
cloudflare "github.com/konstructio/kubefirst-api/internal/cloudflare" | ||
"github.com/konstructio/kubefirst-api/internal/digitalocean" | ||
|
@@ -119,6 +120,38 @@ func PostDomains(c *gin.Context) { | |
return | ||
} | ||
domainListResponse.Domains = domains | ||
|
||
case "azure": | ||
err = domainListRequest.AzureAuth.ValidateAuthCredentials() | ||
if err != nil { | ||
c.JSON(http.StatusBadRequest, types.JSONFailureResponse{ | ||
Message: err.Error(), | ||
}) | ||
return | ||
} | ||
|
||
azureClient, err := azure.NewClient( | ||
domainListRequest.AzureAuth.ClientID, | ||
domainListRequest.AzureAuth.ClientSecret, | ||
domainListRequest.AzureAuth.SubscriptionID, | ||
domainListRequest.AzureAuth.TenantID, | ||
) | ||
if err != nil { | ||
c.JSON(http.StatusBadRequest, types.JSONFailureResponse{ | ||
Message: err.Error(), | ||
}) | ||
return | ||
} | ||
|
||
domains, err := azureClient.GetDNSDomains(context.Background(), domainListRequest.ResourceGroup) | ||
if err != nil { | ||
c.JSON(http.StatusBadRequest, types.JSONFailureResponse{ | ||
Message: err.Error(), | ||
}) | ||
return | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, if you think about it, the data the client sent you was already "validated" at the top. Reaching here post validation to be told that you couldn't load DNS domains seems wrong to, at the same time, return The whole idea of having a REST API is that the API should have a "representational state transfer" (or REST xD) and it's a bad representation that every single error is attributed to the user in the sense that they made a bad request. Talk about gaslighting! 😆 |
||
|
||
domainListResponse.Domains = domains | ||
case "cloudflare": | ||
// check for token, make sure it aint blank | ||
if domainListRequest.CloudflareAuth.APIToken == "" { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/konstructio/kubefirst-api/internal/azure" | ||
"github.com/konstructio/kubefirst-api/internal/types" | ||
) | ||
|
||
// Currently only needs to support google | ||
func ListResourceGroups(c *gin.Context) { | ||
var resourceGroupsListRequest types.ResourceGroupsListRequest | ||
err := c.Bind(&resourceGroupsListRequest) | ||
if err != nil { | ||
c.JSON(http.StatusBadRequest, types.JSONFailureResponse{ | ||
Message: err.Error(), | ||
}) | ||
return | ||
} | ||
|
||
err = resourceGroupsListRequest.AzureAuth.ValidateAuthCredentials() | ||
if err != nil { | ||
c.JSON(http.StatusBadRequest, types.JSONFailureResponse{ | ||
Message: err.Error(), | ||
}) | ||
return | ||
} | ||
|
||
azureClient, err := azure.NewClient( | ||
resourceGroupsListRequest.AzureAuth.ClientID, | ||
resourceGroupsListRequest.AzureAuth.ClientSecret, | ||
resourceGroupsListRequest.AzureAuth.SubscriptionID, | ||
resourceGroupsListRequest.AzureAuth.TenantID, | ||
) | ||
if err != nil { | ||
c.JSON(http.StatusBadRequest, types.JSONFailureResponse{ | ||
Message: err.Error(), | ||
}) | ||
return | ||
} | ||
|
||
var resourceGroupsListResponse types.ResourceGroupsListResponse | ||
|
||
resourceGroups, err := azureClient.GetResourceGroups(context.Background()) | ||
if err != nil { | ||
c.JSON(http.StatusBadRequest, types.JSONFailureResponse{ | ||
Message: err.Error(), | ||
}) | ||
return | ||
} | ||
|
||
resourceGroupsListResponse.ResourceGroups = resourceGroups | ||
|
||
c.JSON(http.StatusOK, resourceGroupsListResponse) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package types | ||
|
||
import ( | ||
pkgtypes "github.com/konstructio/kubefirst-api/pkg/types" | ||
) | ||
|
||
type ResourceGroupsListRequest struct { | ||
AzureAuth pkgtypes.AzureAuth `bson:"azure_auth,omitempty" json:"azure_auth,omitempty"` | ||
} | ||
|
||
type ResourceGroupsListResponse struct { | ||
ResourceGroups []string `json:"resource_groups"` | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package types | ||
|
||
import "fmt" | ||
|
||
// AzureAuth holds necessary auth credentials for interacting with azure | ||
type AzureAuth struct { | ||
ClientID string `bson:"client_id" json:"client_id"` | ||
ClientSecret string `bson:"client_secret" json:"client_secret"` | ||
TenantID string `bson:"tenant_id" json:"tenant_id"` | ||
SubscriptionID string `bson:"subscription_id" json:"subscription_id"` | ||
} | ||
|
||
func (auth *AzureAuth) ValidateAuthCredentials() error { | ||
if auth.ClientID == "" || | ||
auth.ClientSecret == "" || | ||
auth.SubscriptionID == "" || | ||
auth.TenantID == "" { | ||
return fmt.Errorf("missing authentication credentials in request, please check and try again") | ||
} | ||
|
||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to fix it right now but food for thought for the future...
The code that validates parameters makes sense it would return a
HTTP Bad Request
response whenever an error there occurs.This code though makes no sense returning
http.StatusBadRequest
. This is an error that after you validated the parameters were there, that you simply couldn't create an Azure client. The error code would probably be some sort of authentication failure or, to avoid disclosing credential details, anInternal Server Error
would probably be best!