common

package
v1.39.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 28, 2024 License: Apache-2.0 Imports: 29 Imported by: 0

Documentation

Overview

Package common contains general functions that are used by various packages and unit tests in ibmcloud-terratest-wrapper module

Package common provides utilities for working with Git repositories.

Index

Constants

View Source
const SANITIZE_STRING = "SECURE_VALUE_HIDDEN_HASH:"

SANITIZE_STRING is the string used to replace sensitive values.

Variables

This section is empty.

Functions

func ConditionalAdd

func ConditionalAdd(amap map[string]interface{}, key string, value string, compareValue string)

Adds value to map[key] only if value != compareValue

func ConvertArrayToJsonString

func ConvertArrayToJsonString(arr interface{}) (string, error)

ConvertArrayToJsonString is a helper function that will take an array of Golang data types, and return a string of the array formatted as a JSON array. Helpful to convert Golang arrays into a format that Terraform can consume.

func CopyDirectory added in v1.11.0

func CopyDirectory(src string, dst string, fileFilter ...func(string) bool) error

CopyDirectory copies a directory from source to destination, with optional file filtering. src Source directory to copy from dst Destination directory to copy to fileFilter Optional function to filter files to copy Returns an error if the operation fails.

func CopyFile added in v1.10.10

func CopyFile(source, destination string) error

CopyFile copies a file from source to destination. Returns an error if the operation fails.

func DetermineAuthMethod added in v1.11.0

func DetermineAuthMethod(repoURL string) (transport.AuthMethod, error)

DetermineAuthMethod determines the appropriate authentication method for a given repository URL. The function supports both HTTPS and SSH-based repositories.

For HTTPS repositories: - It first checks if the GIT_TOKEN environment variable is set. If so, it uses this as the Personal Access Token (PAT). - If the GIT_TOKEN environment variable is not set, no authentication is used for HTTPS repositories.

For SSH repositories: - It first checks if the SSH_PRIVATE_KEY environment variable is set. If so, it uses this as the SSH private key. - If the SSH_PRIVATE_KEY environment variable is not set, it attempts to use the default SSH key located at ~/.ssh/id_rsa. - If neither the environment variable nor the default key is available, no authentication is used for SSH repositories.

Parameters: - repoURL: The URL of the Git repository.

Returns: - An appropriate AuthMethod based on the repository URL and available credentials. - An error if there's an issue parsing the SSH private key or if the private key cannot be cast to an ssh.Signer.

func FormatJsonStringPretty added in v1.23.6

func FormatJsonStringPretty(jsonString string) (string, error)

func GenerateSshRsaPublicKey added in v1.10.0

func GenerateSshRsaPublicKey() (string, error)

Generate an SSH RSA Keypair (4096 bits), and return the PublicKey in OpenSSH Authorized Key format. Used for tests to generate unique throw-away (but valid) SSH key to supply to test inputs. SPECIAL NOTE: the newline character at end of key will be trimmed and not included!

func GenerateTempGPGKeyPairBase64 added in v1.33.0

func GenerateTempGPGKeyPairBase64() (privateKeyBase64 string, publicKeyBase64 string, err error)

GenerateTempGPGKeyPairBase64 generates a temporary GPG key pair and returns the private and public keys in base64 format. The function first creates a new pair of keys using the openpgp.NewEntity function with a SHA256 hash configuration. It then serializes the private and public keys into bytes.Buffer variables. If any error occurs during the creation or serialization of the keys, the function returns the error. Finally, the function encodes the serialized private and public keys into base64 format and returns them as strings. The function returns two strings: the first is the base64-encoded private key and the second is the base64-encoded public key.

func GetAllRefsAsString added in v1.39.0

func GetAllRefsAsString(stackRef *StackRef) string

GetAllRefsAsString returns all references in a StackRef structure as a single string

func GetAllUnresolvedRefsAsString added in v1.39.0

func GetAllUnresolvedRefsAsString(stackRef *StackRef) string

GetAllUnresolvedRefsAsString returns all unresolved references in a StackRef structure as a single string

func GetBaseRepoAndBranch added in v1.11.0

func GetBaseRepoAndBranch(repo string, branch string) (string, string)

GetBaseRepoAndBranch determines the base repository URL and branch name based on a hierarchy of sources. The function first checks the provided arguments, then checks environment variables, and finally, if neither source provides the values, it uses Git logic to fetch the details. This function is useful in scenarios where you want to determine the base repository and branch details from multiple potential sources.

Parameters:

  • repo: The initial repository URL to consider. This can be an empty string if you want the function to determine the repository URL from other sources.
  • branch: The initial branch name to consider. This can be an empty string if you want the function to determine the branch name from other sources.

Returns: - A string representing the base repository URL. - A string representing the base branch name. - An error if any of the Git commands fail or if the repository/branch details cannot be determined.

func GetBeforeAfterDiff

func GetBeforeAfterDiff(jsonString string) (string, error)

GetBeforeAfterDiff takes a JSON string as input and returns a string with the differences between the "before" and "after" objects in the JSON.

For example, given the JSON string:

{"before": {"a": 1, "b": 2}, "after": {"a": 2, "b": 3}}

the function would return the string:

"Before: {"b": 2}\nAfter: {"a": 2, "b": 3}"

func GetCurrentPrRepoAndBranch added in v1.11.0

func GetCurrentPrRepoAndBranch() (string, string, error)

GetCurrentPrRepoAndBranch returns the repository URL and branch name of the current PR.

Returns: - A string representing the repository URL of the current PR. - A string representing the branch name of the current PR. - An error if any of the Git commands fail or if the repository/branch details cannot be determined.

func GetRequiredEnvVars

func GetRequiredEnvVars(t *testing.T, variableNames []string) map[string]string

GetRequiredEnvVars returns a map containing required environment variables and their values Fails the test if any are missing

func GetTagsFromTravis

func GetTagsFromTravis() []string

GetTagsFromTravis Generates a list of tags to add to resources if running in Travis. Returns empty list if not in Travis

func GitRootPath

func GitRootPath(fromPath string) (string, error)

GitRootPath returns the root directory of the current Git repository.

Parameters: - fromPath: The directory from which the Git command should be executed.

Returns: - A string representing the path to the root directory of the Git repository. - An error if the command fails or the directory is not part of a Git repository.

func IntArrayContains added in v1.5.1

func IntArrayContains(arr []int, val int) bool

IntArrayContains is a helper function that will check an array and see if an int value is already present

func IsArray

func IsArray(v interface{}) bool

IsArray is a simple helper function that will determine if a given Golang value is a slice or array.

func IsJsonEqual added in v1.23.0

func IsJsonEqual(jsonFile1 string, jsonFile2 string) (bool, error)

IsJsonEqual validates whether the two JSON files are equal or not

func LoadMapFromYaml added in v1.8.0

func LoadMapFromYaml(filePath string) (map[string]interface{}, error)

LoadMapFromYaml loads a YAML file into a map[string]interface{}. It returns the resulting map and any error encountered.

func MergeMaps

func MergeMaps(maps ...map[string]interface{}) map[string]interface{}

overwriting duplicate keys

func PrintAllRefs added in v1.39.0

func PrintAllRefs(stackRef *StackRef)

PrintAllRefs prints all references in a StackRef structure

func PrintStructAsJson added in v1.39.0

func PrintStructAsJson(data interface{})

PrintStructAsJson prints a struct as a formatted JSON string

func PrintUnresolvedRefs added in v1.39.0

func PrintUnresolvedRefs(stackRef *StackRef)

PrintUnresolvedRefs prints the unresolved references in a StackRef structure

func ResolveReferences added in v1.39.0

func ResolveReferences(stackRef *StackRef)

ResolveReferences traverses the StackRef structure and resolves references

func RetrievePrivateKey added in v1.32.0

func RetrievePrivateKey(sshPvtKey string) (interface{}, error)

func SanitizeSensitiveData added in v1.23.6

func SanitizeSensitiveData(inputJSON string, secureList map[string]interface{}) (string, error)

SanitizeSensitiveData takes a JSON string and a list of sensitive keys and replaces the values of the sensitive keys with a predefined string.

func SortMap added in v1.23.0

func SortMap(m map[string]interface{})

SortMap sorts the map and any nested structures inside it. It modifies the original map.

func SortMapKeys added in v1.23.0

func SortMapKeys(m map[string]interface{}) []string

SortMapKeys sorts the keys of a map and returns them as a slice.

func SortSlice added in v1.23.0

func SortSlice(slice []interface{})

SortSlice sorts the slices recursively. It also takes care of any nested slices or maps inside the slice.

func StrArrayContains

func StrArrayContains(arr []string, val string) bool

StrArrayContains is a helper function that will check an array and see if a value is already present

func StringContainsIgnoreCase added in v1.28.1

func StringContainsIgnoreCase(s, substr string) bool

StringContainsIgnoreCase checks if a string contains a substring, ignoring case. Returns true if the string contains the substring, false otherwise.

Types

type ConfigRefs added in v1.39.0

type ConfigRefs struct {
	Name     string
	ID       string
	Inputs   []Ref
	Outputs  []Ref
	Resolved bool
}

ConfigRefs represents the configuration references for a stack member

func ProcessMembers added in v1.39.0

func ProcessMembers(members []*project.ProjectConfig) []ConfigRefs

ProcessMembers processes the stack members and returns a slice of ConfigRefs structs

type Ref added in v1.39.0

type Ref struct {
	Name          string
	Ref           *string
	ResolvedValue *string

	Resolved bool
	// contains filtered or unexported fields
}

Ref represents a reference with its name, resolved value, and flags

func GetAllRefs added in v1.39.0

func GetAllRefs(stackRef *StackRef) []Ref

GetAllRefs returns all references in a StackRef structure

func GetAllUnresolvedRefs added in v1.39.0

func GetAllUnresolvedRefs(stackRef *StackRef) []Ref

GetAllUnresolvedRefs returns all unresolved references in a StackRef structure

func ProcessInputs added in v1.39.0

func ProcessInputs(inputs map[string]interface{}) []Ref

ProcessInputs processes the input values and returns a slice of Ref structs

func ProcessOutputs added in v1.39.0

func ProcessOutputs(outputs []project.OutputValue) []Ref

ProcessOutputs processes the output values and returns a slice of Ref structs

type StackRef added in v1.39.0

type StackRef struct {
	Name     string
	ID       string
	Inputs   []Ref
	Outputs  []Ref
	Members  []ConfigRefs
	Resolved bool
}

StackRef represents the stack reference with inputs, outputs, and members

func CreateStackRefStruct added in v1.39.0

func CreateStackRefStruct(stackConfig *project.StackDefinition, members []*project.ProjectConfig) (*StackRef, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL