Skip to content

Commit

Permalink
feature. add cloudaccount magic keyword for creating stack
Browse files Browse the repository at this point in the history
  • Loading branch information
ktkfree committed May 31, 2023
1 parent e6f7a17 commit f4bbf9f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
10 changes: 9 additions & 1 deletion internal/usecase/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package usecase
import (
"context"
"fmt"
"strings"

"github.com/openinfradev/tks-api/internal/middleware/auth/request"

Expand Down Expand Up @@ -125,9 +126,16 @@ func (u *ClusterUsecase) Create(ctx context.Context, dto domain.Cluster) (cluste
if err != nil {
return "", httpErrors.NewBadRequestError(fmt.Errorf("Failed to get cloudAccounts"), "", "")
}

tksCloudAccountId := dto.CloudAccountId.String()
isExist := false
for _, ca := range cloudAccounts {
if ca.ID == dto.CloudAccountId {

// FOR TEST. ADD MAGIC KEYWORD
if strings.Contains(ca.Name, "INCLUSTER") {
tksCloudAccountId = "NULL"
}
isExist = true
break
}
Expand Down Expand Up @@ -175,7 +183,7 @@ func (u *ClusterUsecase) Create(ctx context.Context, dto domain.Cluster) (cluste
"template_name=" + stackTemplate.Template,
"git_account=" + viper.GetString("git-account"),
"creator=" + user.GetUserId().String(),
"cloud_account_id=" + dto.CloudAccountId.String(),
"cloud_account_id=" + tksCloudAccountId,
//"manifest_repo_url=" + viper.GetString("git-base-url") + "/" + viper.GetString("git-account") + "/" + clusterId + "-manifests",
},
})
Expand Down
12 changes: 2 additions & 10 deletions internal/usecase/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
"gorm.io/gorm"
)

const CLOUD_ACCOUNT_INCLUSTER = "INCLUSTER"

type IStackUsecase interface {
Get(ctx context.Context, stackId domain.StackId) (domain.Stack, error)
GetByName(ctx context.Context, organizationId string, name string) (domain.Stack, error)
Expand Down Expand Up @@ -69,17 +67,11 @@ func (u *StackUsecase) Create(ctx context.Context, dto domain.Stack) (stackId do
return "", httpErrors.NewInternalServerError(errors.Wrap(err, "Invalid stackTemplateId"), "S_INVALID_STACK_TEMPLATE", "")
}

cloudAccount, err := u.cloudAccountRepo.Get(dto.CloudAccountId)
_, err = u.cloudAccountRepo.Get(dto.CloudAccountId)
if err != nil {
return "", httpErrors.NewInternalServerError(errors.Wrap(err, "Invalid cloudAccountId"), "S_INVALID_CLOUD_ACCOUNT", "")
}

// MAGIC KEYWORD
cloudAccountId := cloudAccount.ID.String()
if strings.Contains(cloudAccount.Name, CLOUD_ACCOUNT_INCLUSTER) {
cloudAccountId = "NULL"
}

clusters, err := u.clusterRepo.FetchByOrganizationId(dto.OrganizationId)
if err != nil {
return "", httpErrors.NewInternalServerError(errors.Wrap(err, "Failed to get clusters"), "S_FAILED_GET_CLUSTERS", "")
Expand Down Expand Up @@ -111,7 +103,7 @@ func (u *StackUsecase) Create(ctx context.Context, dto domain.Stack) (stackId do
"cluster_name=" + dto.Name,
"description=" + dto.Description,
"organization_id=" + dto.OrganizationId,
"cloud_account_id=" + cloudAccountId,
"cloud_account_id=" + dto.CloudAccountId.String(),
"stack_template_id=" + dto.StackTemplateId.String(),
"creator=" + user.GetUserId().String(),
"infra_conf=" + strings.Replace(helper.ModelToJson(stackConf), "\"", "\\\"", -1),
Expand Down

0 comments on commit f4bbf9f

Please sign in to comment.