Skip to content

Commit

Permalink
Merge branch 'develop' into dependabot/npm_and_yarn/dashboard/prettie…
Browse files Browse the repository at this point in the history
…r-plugin-tailwindcss-0.6.8
  • Loading branch information
Azanul authored Nov 21, 2024
2 parents b0dc5d9 + f69ac91 commit 48ff4a5
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 2 deletions.
5 changes: 5 additions & 0 deletions controller/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ func (ctrl *Controller) RescanAccount(c context.Context, account *models.Account
return
}

func (ctrl *Controller) GetAccountById(c context.Context, accountId string) (account models.Account, err error) {
_, err = ctrl.repo.HandleQuery(c, repository.ListKey, &account, [][3]string{{"id", "=", accountId}}, "")
return
}

func (ctrl *Controller) DeleteAccount(c context.Context, accountId string) (err error) {
_, err = ctrl.repo.HandleQuery(c, repository.DeleteKey, new(models.Account), [][3]string{{"id", "=", accountId}}, "")
return
Expand Down
20 changes: 19 additions & 1 deletion handlers/accounts_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,30 @@ func (handler *ApiHandler) ReScanAccount(c *gin.Context) {
func (handler *ApiHandler) DeleteCloudAccountHandler(c *gin.Context) {
accountId := c.Param("id")

err := handler.ctrl.DeleteAccount(c, accountId)
res, err := handler.ctrl.GetAccountById(c, accountId)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}

err = handler.ctrl.DeleteAccount(c, accountId)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}

err = deleteConfigAccounts(res, &handler.cfg)
if err != nil {
fmt.Println(err)
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}

err = updateConfig(handler.configPath, &handler.cfg)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"message": "account has been deleted"})
}

Expand Down
120 changes: 119 additions & 1 deletion handlers/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,11 +486,129 @@ func populateConfigFromAccount(account models.Account, config *models.Config) er
return nil
}

func deleteConfigAccounts(account models.Account, config *models.Config) error {
switch strings.ToLower(account.Provider) {
case "aws":

updatedConfig := make([]models.AWSConfig, 0)
for _, acc := range config.AWS {
if acc.Name != account.Name {
updatedConfig = append(updatedConfig, acc)
}
}
config.AWS = updatedConfig

case "digitalocean":
updatedConfig := make([]models.DigitalOceanConfig, 0)
for _, acc := range config.DigitalOcean {
if acc.Name != account.Name {
updatedConfig = append(updatedConfig, acc)
}
}
config.DigitalOcean = updatedConfig

case "oci":
updatedConfig := make([]models.OciConfig, 0)
for _, acc := range config.Oci {
if acc.Name != account.Name {
updatedConfig = append(updatedConfig, acc)
}
}
config.Oci = updatedConfig

case "civo":
updatedConfig := make([]models.CivoConfig, 0)
for _, acc := range config.Civo {
if acc.Name != account.Name {
updatedConfig = append(updatedConfig, acc)
}
}
config.Civo = updatedConfig

case "kubernetes":
updatedConfig := make([]models.KubernetesConfig, 0)
for _, acc := range config.Kubernetes {
if acc.Name != account.Name {
updatedConfig = append(updatedConfig, acc)
}
}
config.Kubernetes = updatedConfig

case "linode":
updatedConfig := make([]models.LinodeConfig, 0)
for _, acc := range config.Linode {
if acc.Name != account.Name {
updatedConfig = append(updatedConfig, acc)
}
}
config.Linode = updatedConfig

case "tencent":
updatedConfig := make([]models.TencentConfig, 0)
for _, acc := range config.Tencent {
if acc.Name != account.Name {
updatedConfig = append(updatedConfig, acc)
}
}
config.Tencent = updatedConfig

case "azure":
updatedConfig := make([]models.AzureConfig, 0)
for _, acc := range config.Azure {
if acc.Name != account.Name {
updatedConfig = append(updatedConfig, acc)
}
}
config.Azure = updatedConfig

case "scaleway":
updatedConfig := make([]models.ScalewayConfig, 0)
for _, acc := range config.Scaleway {
if acc.Name != account.Name {
updatedConfig = append(updatedConfig, acc)
}
}
config.Scaleway = updatedConfig

case "mongodb":
updatedConfig := make([]models.MongoDBAtlasConfig, 0)
for _, acc := range config.MongoDBAtlas {
if acc.Name != account.Name {
updatedConfig = append(updatedConfig, acc)
}
}
config.MongoDBAtlas = updatedConfig

case "gcp":
updatedConfig := make([]models.GCPConfig, 0)
for _, acc := range config.GCP {
if acc.Name != account.Name {
updatedConfig = append(updatedConfig, acc)
}
}
config.GCP = updatedConfig

case "ovh":
updatedConfig := make([]models.OVHConfig, 0)
for _, acc := range config.OVH {
if acc.Name != account.Name {
updatedConfig = append(updatedConfig, acc)
}
}
config.OVH = updatedConfig

default:
return fmt.Errorf("illegle provider")
}

return nil
}

func updateConfig(path string, cfg *models.Config) error {
mu.Lock()
defer mu.Unlock()

f, err := os.OpenFile(path, os.O_WRONLY, 0644)
f, err := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return err
}
Expand Down

0 comments on commit 48ff4a5

Please sign in to comment.