Skip to content

Commit

Permalink
feat: [PLG-2612] : Add flags to support service creation for GCP flow (
Browse files Browse the repository at this point in the history
  • Loading branch information
neelam-harness authored Aug 7, 2023
1 parent 73a78e4 commit d52afe0
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 20 deletions.
1 change: 1 addition & 0 deletions constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const CONNECTOR_ENDPOINT = "connectors"
const CONTENT_TYPE_JSON = "application/json"
const CONTENT_TYPE_YAML = "application/yaml"
const ENVIRONMENT_ENDPOINT = "environmentsV2"
const GCP_BUCKET_NAME_PLACEHOLDER = "GCP_BUCKET_NAME"
const GCP_PROJECT_NAME_PLACEHOLDER = "GCP_PROJECT_NAME"
const GCP_REGION_NAME_PLACEHOLDER = "GCP_REGION_NAME"
const GITHUB_SECRET_IDENTIFIER = "harness_gitpat"
Expand Down
23 changes: 3 additions & 20 deletions infrastructure.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
"regexp"

log "github.com/sirupsen/logrus"

Expand Down Expand Up @@ -30,7 +29,7 @@ func applyInfraDefinition(c *cli.Context) error {
"accountIdentifier": cliCdRequestData.Account,
})
var content = readFromFile(c.String("file"))
content = updateYamlContent(content)
content = updateInfraYamlContent(content)

requestBody := getJsonFromYaml(content)
if requestBody == nil {
Expand Down Expand Up @@ -69,8 +68,8 @@ func applyInfraDefinition(c *cli.Context) error {
return nil
}

func updateYamlContent(content string) string {
var infraType = fetchInfraType(content)
func updateInfraYamlContent(content string) string {
var infraType = fetchCloudType(content)
switch {
case infraType == GCP:
log.Info("Looks like you are creating an infrastructure definition for GCP," +
Expand Down Expand Up @@ -105,19 +104,3 @@ func listInfraDefinition(*cli.Context) error {
fmt.Println(NOT_IMPLEMENTED)
return nil
}

func fetchInfraType(str string) string {
fmt.Println("Checking infra type in the yaml..")
gcpRegexPattern := `type:\s+GoogleCloudFunctions`
isGcpMatch, _ := regexp.MatchString(gcpRegexPattern, str)
awsRegexPattern := `type:\s+AwsLambda`
isAwsMatch, _ := regexp.MatchString(awsRegexPattern, str)

if isGcpMatch {
return GCP
} else if isAwsMatch {
return AWS
}

return ""
}
10 changes: 10 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,16 @@ func main() {
{
Name: "apply",
Usage: "Create a new service or Update an existing one.",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "gcp-project",
Usage: "provide the Google Cloud Platform project name.",
},
&cli.StringFlag{
Name: "gcp-bucket",
Usage: "provide the Google Cloud Platform bucket name.",
},
},
Action: func(context *cli.Context) error {
return cliWrapper(applyService, context)
},
Expand Down
34 changes: 34 additions & 0 deletions services.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,31 @@ package main
import (
"fmt"

log "github.com/sirupsen/logrus"

"github.com/fatih/color"
"github.com/urfave/cli/v2"
)

var gcpBucketName = ""

func applyService(c *cli.Context) error {
filePath := c.String("file")
baseURL := getNGBaseURL(c)
if filePath == "" {
fmt.Println("Please enter valid filename")
return nil
}
gcpProjectName = c.String("gcp-project")
gcpBucketName = c.String("gcp-bucket")
fmt.Println("Trying to create or update service using the yaml=",
getColoredText(filePath, color.FgCyan))
createOrUpdateSvcURL := GetUrlWithQueryParams("", baseURL, SERVICES_ENDPOINT, map[string]string{
"accountIdentifier": cliCdRequestData.Account,
})
var content = readFromFile(c.String("file"))
content = updateServiceYamlContent(content)

requestBody := getJsonFromYaml(content)
if requestBody == nil {
println(getColoredText("Please enter valid yaml", color.FgRed))
Expand Down Expand Up @@ -54,6 +62,32 @@ func applyService(c *cli.Context) error {
return nil
}

func updateServiceYamlContent(content string) string {
var serviceType = fetchCloudType(content)
switch {
case serviceType == GCP:
log.Info("Looks like you are creating a service for GCP," +
" validating GCP project and bucket now...")
if gcpProjectName == "" || gcpProjectName == GCP_PROJECT_NAME_PLACEHOLDER {
gcpProjectName = TextInput("Enter a valid GCP project name:")
}

if gcpBucketName == "" || gcpBucketName == GCP_BUCKET_NAME_PLACEHOLDER {
gcpBucketName = TextInput("Enter a valid GCP bucket name:")
}
log.Info("Got your gcp project and bucket info, let's create the service now...")
content = replacePlaceholderValues(content, GCP_PROJECT_NAME_PLACEHOLDER, gcpProjectName)
content = replacePlaceholderValues(content, GCP_BUCKET_NAME_PLACEHOLDER, gcpBucketName)
case serviceType == AWS:
log.Info("Looks like you are creating a service for AWS, validating yaml now...")
default:
fmt.Println("Nothing to update in the yaml.")
panic("The supported Cloud types are GCP and AWS. Input should be one of these.")
}

return content
}

func deleteService(*cli.Context) error {
fmt.Println(NOT_IMPLEMENTED)
return nil
Expand Down
15 changes: 15 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"os"
"regexp"
"strings"

"github.com/AlecAivazis/survey/v2"
Expand Down Expand Up @@ -294,3 +295,17 @@ func mergeMaps(map1 map[string]string, map2 map[string]string) map[string]string
func replacePlaceholderValues(haystack string, needle string, value string) string {
return strings.ReplaceAll(haystack, needle, value)
}

func fetchCloudType(str string) string {
fmt.Println("Checking cloud type in the yaml..")
gcpRegexPattern := `type:\s+GoogleCloudFunctions`
awsRegexPattern := `type:\s+AwsLambda`

if isGcpMatch, err1 := regexp.MatchString(gcpRegexPattern, str); err1 == nil && isGcpMatch {
return GCP
} else if isAwsMatch, err2 := regexp.MatchString(awsRegexPattern, str); err2 == nil && isAwsMatch {
return AWS
}

return ""
}

0 comments on commit d52afe0

Please sign in to comment.