Skip to content

Commit

Permalink
🚀 Org & Project CheckandCreate
Browse files Browse the repository at this point in the history
  • Loading branch information
ritek01 committed May 28, 2024
1 parent e474cbb commit 68b1014
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 6 deletions.
15 changes: 9 additions & 6 deletions deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/fatih/color"
"github.com/urfave/cli/v2"
"harness/defaults"
. "harness/utils"
"os"
"strings"
)
Expand All @@ -20,15 +21,19 @@ func DeployProject(c *cli.Context) error {
return err
}

fmt.Printf("Loaded framework: %s\n", framework)
fmt.Printf("Loaded language: %s\n", language)
fmt.Printf("Loaded framework: %s\n", GetColoredText(framework, color.FgCyan))
fmt.Printf("Loaded language: %s\n", GetColoredText(language, color.FgCyan))

hasDockerfile, err := checkDockerfile()
if err != nil {
return err
}

if !hasDockerfile {
if hasDockerfile {
color.Set(color.FgGreen)
fmt.Println("Awesome! 🐋 Dockerfile found.")
color.Unset()
} else {
fmt.Print("No Dockerfile found. Would you like to create one? (y/n) : ")
var response string
fmt.Scanln(&response)
Expand All @@ -39,7 +44,7 @@ func DeployProject(c *cli.Context) error {
return err
}
color.Set(color.FgGreen)
fmt.Println("Dockerfile created.")
fmt.Println("🐋 Dockerfile created.")
color.Unset()
hasDockerfile = true
}
Expand Down Expand Up @@ -75,8 +80,6 @@ func DeployProject(c *cli.Context) error {

fmt.Println("Deployment process initialized.")

// Further deployment steps would go here

return nil
}

Expand Down
85 changes: 85 additions & 0 deletions utils/org.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package utils

import (
"encoding/json"
"fmt"
"github.com/urfave/cli/v2"
"harness/globals"
"harness/netclient"
. "harness/types"
"log"
)

func getOrganisations(c *cli.Context) []OrgDetails {
url := fmt.Sprintf("%s/aggregate/organizations?accountIdentifier=%s&pageSize=1000", GetNGBaseURL(c), globals.AccountId)
resp, err := netclient.GetNew(url, globals.ApiKey)
if err != nil || resp.Status != "SUCCESS" {
log.Fatal("Failed to fetch organisations", err)
}
byteData, err := json.Marshal(resp.Data)
if err != nil {
log.Fatal("Failed to fetch organisations", err)
}
var orgListBody OrgListBody
err = json.Unmarshal(byteData, &orgListBody)
if err != nil {
log.Fatal("Failed to fetch organisations", err)
}
var details []OrgDetails

for _, o := range orgListBody.Organisations {
details = append(details, o.Org.Org)
}
return details
}
func findOrgIdByName(org []OrgDetails, orgName string) string {
for _, p := range org {
if p.Name == orgName {
return p.Identifier
}
}
return ""
}
func createHarnessOrg(c *cli.Context, orgName string) error {
url := fmt.Sprintf("%s/organizations?accountIdentifier=%s", GetNGBaseURL(c), globals.AccountId)
//payload := fmt.Sprintf(`{"Identifier": "%s", "Name": "%s"}`, orgName, orgName)
resp, err := netclient.PostNew(url, globals.ApiKey, OrgBody{
Org: OrgDetails{
Identifier: orgName,
Name: orgName,
Description: "",
}})
if err != nil || resp.Status != "SUCCESS" {
log.Fatal("Failed to create organization", err)
}
return nil
}

func CheckOrgExistsAndCreate(c *cli.Context, orgName string) (bool, error) {
org := getOrganisations(c)
orgId := findOrgIdByName(org, orgName)
if len(orgId) == 0 {
fmt.Printf("Organization '%s' does not exist. Do you want to create it? (y/n): ", orgName)
var createOrg string
fmt.Scanln(&createOrg)

if createOrg != "y" {
fmt.Println("Deployment aborted.")
return false, nil
}
err := createHarnessOrg(c, orgName)
if err != nil {
return false, err
}
} else {
fmt.Printf("Organization '%s' already exists. Do you want to use it? (y/n): ", orgName)
var useOrg string
fmt.Scanln(&useOrg)

if useOrg != "y" {
fmt.Println("Deployment aborted.")
return false, nil
}
}
return true, nil
}
76 changes: 76 additions & 0 deletions utils/project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package utils

import (
"encoding/json"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"harness/globals"
"harness/netclient"
. "harness/types"
)

func getProjectIdByName(project []ProjectDetails, projectName string) string {
for _, p := range project {
if p.Identifier == projectName {
return p.Identifier
}
}
return ""
}

func getProjects(c *cli.Context, orgName string) []ProjectDetails {
url := fmt.Sprintf("%s/projects?accountIdentifier=%s&orgIdentifier=%s&pageSize=1000", GetNGBaseURL(c), globals.AccountId, orgName)
resp, err := netclient.GetNew(url, globals.ApiKey)
if err != nil || resp.Status != "SUCCESS" {
log.Fatal("Failed to fetch projects", err)
}
byteData, err := json.Marshal(resp.Data)
if err != nil {
log.Fatal("Failed to fetch projects", err)
}
var projects ProjectListBody
err = json.Unmarshal(byteData, &projects)
if err != nil {
log.Fatal("Failed to fetch projects", err)
}
var projectDetails []ProjectDetails

for _, p := range projects.Projects {
projectDetails = append(projectDetails, p.Project)
}
return projectDetails
}

func CheckProjectExistsAndCreate(c *cli.Context, orgName string, projectName string) (bool, error) {
projects := getProjects(c, orgName)
projectId := getProjectIdByName(projects, projectName)

if len(projectId) == 0 {
fmt.Println("Project not found. Creating project...")
url := fmt.Sprintf("%s/projects?accountIdentifier=%s&orgIdentifier=%s", GetNGBaseURL(c), globals.AccountId, orgName)
resp, err := netclient.PostNew(url, globals.ApiKey, ProjectBody{
Project: ProjectDetails{
OrgIdentifier: orgName,
Identifier: projectName,
Name: projectName,
Color: "#5dc22f",
Modules: []string{"CD"},
Description: "",
}})
if err != nil || resp.Status != "SUCCESS" {
log.Fatal("Failed to create project", err)
}
log.Info("Project created successfully")
} else {
fmt.Printf("Project '%s' already exists. Do you want to use it? (y/n): ", projectName)
var useProject string
fmt.Scanln(&useProject)

if useProject != "y" {
fmt.Println("Deployment aborted.")
return false, nil
}
}
return true, nil
}

0 comments on commit 68b1014

Please sign in to comment.