Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions cloudaccounts/handler/handler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handler

import (
"strconv"
"strings"

"gofr.dev/pkg/gofr"
Expand Down Expand Up @@ -56,6 +57,64 @@ func (h *Handler) ListCloudAccounts(ctx *gofr.Context) (interface{}, error) {
return resp, nil
}

func (h *Handler) ListDeploymentSpace(ctx *gofr.Context) (interface{}, error) {
id := ctx.PathParam("id")
id = strings.TrimSpace(id)

cloudAccountID, err := strconv.Atoi(id)
if err != nil {
return nil, http.ErrorInvalidParam{Params: []string{"id"}}
}

res, err := h.service.FetchDeploymentSpace(ctx, cloudAccountID)
if err != nil {
return nil, err
}

return res, nil
}

func (h *Handler) ListNamespaces(ctx *gofr.Context) (interface{}, error) {
id := ctx.PathParam("id")
id = strings.TrimSpace(id)

cloudAccountID, err := strconv.Atoi(id)
if err != nil {
return nil, http.ErrorInvalidParam{Params: []string{"id"}}
}

clusterName := strings.TrimSpace(ctx.Param("name"))
clusterRegion := strings.TrimSpace(ctx.Param("region"))

if clusterName == "" || clusterRegion == "" {
return nil, http.ErrorInvalidParam{Params: []string{"cluster"}}
}

res, err := h.service.ListNamespaces(ctx, cloudAccountID, clusterName, clusterRegion)
if err != nil {
return nil, err
}

return res, nil
}

func (h *Handler) ListDeploymentSpaceOptions(ctx *gofr.Context) (interface{}, error) {
id := ctx.PathParam("id")
id = strings.TrimSpace(id)

cloudAccountID, err := strconv.Atoi(id)
if err != nil {
return nil, http.ErrorInvalidParam{Params: []string{"id"}}
}

res, err := h.service.FetchDeploymentSpaceOptions(ctx, cloudAccountID)
if err != nil {
return nil, err
}

return res, nil
}

// validateCloudAccount checks the required fields and values in a CloudAccount.
func validateCloudAccount(cloudAccount *store.CloudAccount) error {
params := []string{}
Expand Down
3 changes: 1 addition & 2 deletions cloudaccounts/handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func TestHandler_AddCloudAccount(t *testing.T) {
defer ctrl.Finish()

mockService := service.NewMockCloudAccountService(ctrl)

handler := New(mockService)

testCases := []struct {
Expand Down Expand Up @@ -82,7 +81,7 @@ func TestHandler_AddCloudAccount(t *testing.T) {
tc.mockBehavior()

// Prepare HTTP request
req := httptest.NewRequest(netHTTP.MethodPost, "/add", strings.NewReader(tc.requestBody))
req := httptest.NewRequest(netHTTP.MethodPost, "/add/{id}", strings.NewReader(tc.requestBody))
req.Header.Set("Content-Type", "application/json")

ctx := &gofr.Context{Context: context.Background(), Request: http.NewRequest(req)}
Expand Down
3 changes: 3 additions & 0 deletions cloudaccounts/service/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ import (
type CloudAccountService interface {
AddCloudAccount(ctx *gofr.Context, accounts *store.CloudAccount) (*store.CloudAccount, error)
FetchAllCloudAccounts(ctx *gofr.Context) ([]store.CloudAccount, error)
FetchDeploymentSpace(ctx *gofr.Context, cloudAccountID int) (interface{}, error)
ListNamespaces(ctx *gofr.Context, id int, clusterName, clusterRegion string) (interface{}, error)
FetchDeploymentSpaceOptions(ctx *gofr.Context, id int) ([]DeploymentSpaceOptions, error)
}
46 changes: 46 additions & 0 deletions cloudaccounts/service/mock_interface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions cloudaccounts/service/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,10 @@ type gcpCredentials struct {
ClientX509CertURL string `json:"client_x509_cert_url"`
UniverseDomain string `json:"universe_domain"`
}

// DeploymentSpaceOptions contains options to setup deployment space.
type DeploymentSpaceOptions struct {
Name string `json:"name"`
Path string `json:"path"`
Type string `json:"type"`
}
78 changes: 75 additions & 3 deletions cloudaccounts/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,25 @@ import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"strings"
"time"

"gofr.dev/pkg/gofr"
"gofr.dev/pkg/gofr/http"

"github.com/zopdev/zop-api/cloudaccounts/store"
"github.com/zopdev/zop-api/provider"
)

type Service struct {
store store.CloudAccountStore
store store.CloudAccountStore
deploymentSpace provider.Provider
}

// New creates a new CloudAccountService with the provided CloudAccountStore.
func New(clStore store.CloudAccountStore) CloudAccountService {
return &Service{store: clStore}
func New(clStore store.CloudAccountStore, deploySpace provider.Provider) CloudAccountService {
return &Service{store: clStore, deploymentSpace: deploySpace}
}

// AddCloudAccount adds a new cloud account to the store if it doesn't already exist.
Expand Down Expand Up @@ -77,3 +80,72 @@ func fetchGCPProviderDetails(ctx *gofr.Context, cloudAccount *store.CloudAccount

return nil
}

func (s *Service) FetchDeploymentSpace(ctx *gofr.Context, cloudAccountID int) (interface{}, error) {
cloudAccount, err := s.store.GetCloudAccountByID(ctx, cloudAccountID)
if err != nil {
return nil, err
}

credentials, err := s.store.GetCredentials(ctx, cloudAccount.ID)
if err != nil {
return nil, err
}

deploymentSpaceAccount := provider.CloudAccount{
ID: cloudAccount.ID,
Name: cloudAccount.Name,
Provider: cloudAccount.Provider,
ProviderID: cloudAccount.ProviderID,
ProviderDetails: cloudAccount.ProviderDetails,
}

clusters, err := s.deploymentSpace.ListAllClusters(ctx, &deploymentSpaceAccount, credentials)
if err != nil {
return nil, err
}

return clusters, nil
}

func (s *Service) ListNamespaces(ctx *gofr.Context, id int, clusterName, clusterRegion string) (interface{}, error) {
cloudAccount, err := s.store.GetCloudAccountByID(ctx, id)
if err != nil {
return nil, err
}

credentials, err := s.store.GetCredentials(ctx, cloudAccount.ID)
if err != nil {
return nil, err
}

deploymentSpaceAccount := provider.CloudAccount{
ID: cloudAccount.ID,
Name: cloudAccount.Name,
Provider: cloudAccount.Provider,
ProviderID: cloudAccount.ProviderID,
ProviderDetails: cloudAccount.ProviderDetails,
}

cluster := provider.Cluster{
Name: clusterName,
Region: clusterRegion,
}

res, err := s.deploymentSpace.ListNamespace(ctx, &cluster, &deploymentSpaceAccount, credentials)
if err != nil {
return nil, err
}

return res, nil
}

func (*Service) FetchDeploymentSpaceOptions(_ *gofr.Context, id int) ([]DeploymentSpaceOptions, error) {
options := []DeploymentSpaceOptions{
{Name: "gke",
Path: fmt.Sprintf("/cloud-accounts/%v/deployment-space/clusters", id),
Type: "type"},
}

return options, nil
}
8 changes: 6 additions & 2 deletions cloudaccounts/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import (
"github.com/stretchr/testify/require"

"go.uber.org/mock/gomock"

"gofr.dev/pkg/gofr"
"gofr.dev/pkg/gofr/http"

"github.com/zopdev/zop-api/cloudaccounts/store"
"github.com/zopdev/zop-api/provider"
)

var (
Expand All @@ -22,6 +24,7 @@ func TestService_AddCloudAccount(t *testing.T) {
defer ctrl.Finish()

mockStore := store.NewMockCloudAccountStore(ctrl)
mockProvider := provider.NewMockProvider(ctrl)

ctx := &gofr.Context{}

Expand Down Expand Up @@ -100,7 +103,7 @@ func TestService_AddCloudAccount(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
tc.mockBehavior()

service := New(mockStore)
service := New(mockStore, mockProvider)
_, err := service.AddCloudAccount(ctx, tc.input)

if tc.expectedError != nil {
Expand All @@ -118,6 +121,7 @@ func TestService_FetchAllCloudAccounts(t *testing.T) {
defer ctrl.Finish()

mockStore := store.NewMockCloudAccountStore(ctrl)
mockProvider := provider.NewMockProvider(ctrl)

ctx := &gofr.Context{}

Expand Down Expand Up @@ -160,7 +164,7 @@ func TestService_FetchAllCloudAccounts(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
tc.mockBehavior()

service := New(mockStore)
service := New(mockStore, mockProvider)
_, err := service.FetchAllCloudAccounts(ctx)

if tc.expectedError != nil {
Expand Down
2 changes: 2 additions & 0 deletions cloudaccounts/store/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ type CloudAccountStore interface {
InsertCloudAccount(ctx *gofr.Context, config *CloudAccount) (*CloudAccount, error)
GetALLCloudAccounts(ctx *gofr.Context) ([]CloudAccount, error)
GetCloudAccountByProvider(ctx *gofr.Context, providerType, providerID string) (*CloudAccount, error)
GetCloudAccountByID(ctx *gofr.Context, cloudAccountID int) (*CloudAccount, error)
GetCredentials(ctx *gofr.Context, cloudAccountID int64) (interface{}, error)
}
30 changes: 30 additions & 0 deletions cloudaccounts/store/mock_interface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading