-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
170 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |