Skip to content

Commit

Permalink
refactor!: Namegenerator functions refactor (#12)
Browse files Browse the repository at this point in the history
Co-authored-by: Sebastian Czech <sczech@paloaltonetworks.com>
  • Loading branch information
horiagunica and sebastianczech authored Sep 25, 2023
1 parent b02e993 commit 8accb51
Showing 1 changed file with 45 additions and 43 deletions.
88 changes: 45 additions & 43 deletions pkg/testskeleton/testskeleton.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,47 +58,19 @@ type AdditionalChangesAfterDeployment struct {
ChangedResources []ChangedResource
}

// Structure used for AWS deployments - contains randomly generated resource names.
type AwsRandomNames struct {
NamePrefix string
// Structure used for terraform tfvars input information,
// not all options are used in all public cloud environments
type TerraformVarsInfo struct {
NamePrefix string
AzureResourceGroupName string
AzureStorageAccountName string
GoogleProjectId string
}

// Function that generates and returns a set of random AWS resource names.
// Randomization is based on UUID.
func GenerateAwRandomNames() AwsRandomNames {
// Function that generates and returns information used by Terraform TFVARS.
func GenerateTerraformVarsInfo(cloud string) (TerraformVarsInfo, error) {
prid := os.Getenv("PRID")
if prid != "" {
prid = fmt.Sprintf("p%s", prid)
} else {
prid = "tt"
}

id := uuid.New().String()
idSliced := strings.Split(id, "-")

prefixId := idSliced[2]

names := AwsRandomNames{
NamePrefix: fmt.Sprintf("%s-%s-", prid, prefixId),
}

return names
}

// Structure used for Azure deployments - contains randomly generated resource names.
type AzureRandomNames struct {
NamePrefix string
ResourceGroupName string
StorageAccountName string
}

// Function that generates and returns a set of random Azure resource names.
// Randomization is based on UUID.
func GenerateAzureRandomNames() AzureRandomNames {
prid := os.Getenv("PRID")
if prid != "" {
prid = fmt.Sprintf("-pr%s-", prid)
}
var names TerraformVarsInfo

id := uuid.New().String()
idSliced := strings.Split(id, "-")
Expand All @@ -107,13 +79,43 @@ func GenerateAzureRandomNames() AzureRandomNames {
gid := idSliced[0:2]
storageId := idSliced[3:5]

names := AzureRandomNames{
NamePrefix: fmt.Sprintf("ghci%s%s-", prid, prefixId),
ResourceGroupName: strings.Join(gid, ""),
StorageAccountName: fmt.Sprintf("ghci%s", strings.Join(storageId, "")),
if cloud == "aws" {
if prid != "" {
prid = fmt.Sprintf("p%s", prid)
} else {
prid = "tt"
}
names = TerraformVarsInfo{
NamePrefix: fmt.Sprintf("%s-%s-", prid, prefixId),
}
} else if cloud == "azure" {
if prid != "" {
prid = fmt.Sprintf("-pr%s-", prid)
}

names = TerraformVarsInfo{
NamePrefix: fmt.Sprintf("ghci%s%s-", prid, prefixId),
AzureResourceGroupName: strings.Join(gid, ""),
AzureStorageAccountName: fmt.Sprintf("ghci%s", strings.Join(storageId, "")),
}
} else if cloud == "gcp" {
projectId := os.Getenv("PROJECT_ID")
if prid != "" {
prid = fmt.Sprintf("p%s", prid)
} else {
prid = "tt"
}

names = TerraformVarsInfo{
NamePrefix: fmt.Sprintf("ghci%s%s-", prid, prefixId),
GoogleProjectId: projectId,
}
} else {
// If no cloud is provided - throw an error
return TerraformVarsInfo{}, fmt.Errorf("Wrong cloud input provided %s", cloud)
}

return names
return names, nil
}

// Function running only only code validation.
Expand Down

0 comments on commit 8accb51

Please sign in to comment.