Skip to content

Commit

Permalink
adding logic to use base64 encode
Browse files Browse the repository at this point in the history
Signed-off-by: Vivek Reddy <vivekrsplunk@github.com>
  • Loading branch information
Vivek Reddy committed Oct 15, 2024
1 parent 294db0f commit 9e31a48
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 9 deletions.
3 changes: 3 additions & 0 deletions test/testenv/appframework_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ func GenerateAppFrameworkSpec(ctx context.Context, testenvInstance *TestCaseEnv,
}
case "gcp":
volumeSpec = []enterpriseApi.VolumeSpec{GenerateIndexVolumeSpec(volumeName, GetGCPEndpoint(), testenvInstance.GetIndexSecretName(), "gcp", "blob", GetDefaultS3Region())}

default:
testenvInstance.Log.Info("Failed to identify cluster provider name: Should be 'eks' or 'azure' or 'gcp' ")
}
Expand All @@ -402,6 +403,8 @@ func GenerateAppFrameworkSpec(ctx context.Context, testenvInstance *TestCaseEnv,
return appFrameworkSpec
}



// WaitforPhaseChange Wait for 2 mins or when phase change on is seen on a CR for any particular app
func WaitforPhaseChange(ctx context.Context, deployment *Deployment, testenvInstance *TestCaseEnv, name string, crKind string, appSourceName string, appList []string) {
startTime := time.Now()
Expand Down
62 changes: 56 additions & 6 deletions test/testenv/gcputils.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ func NewGCPClient() (*GCPClient, error) {
}

} else {
var creds google.Credentials
//var creds google.Credentials

err = json.Unmarshal([]byte(gcpCredentials), &creds)
if err != nil {
logf.Log.Error(err, "Secret key.json value is not parsable")
return nil, err
}
//err = json.Unmarshal([]byte(gcpCredentials), &creds)
//if err != nil {
// logf.Log.Error(err, "Secret key.json value is not parsable")
// return nil, err
//}
//client, err = storage.NewClient(ctx, option.WithCredentials(&creds))
//client, err = storage.NewClient(ctx, option.WithCredentialsFile("/Users/vivekr/Projects/splunk-operator/auth.json"))
client, err = storage.NewClient(ctx, option.WithCredentialsJSON([]byte(gcpCredentials)))
Expand Down Expand Up @@ -127,6 +127,54 @@ func CheckPrefixExistsOnGCP(prefix string) bool {
return false
}

// CreateBucketAndPathIfNotExist creates a bucket and path if they do not exist
func CreateBucketAndPathIfNotExist(bucketName, path string) error {
client, err := NewGCPClient()
if err != nil {
return err
}
defer client.Client.Close()

ctx, cancel := context.WithTimeout(client.Ctx, time.Minute*5)
defer cancel()

// Check if the bucket exists
_, err = client.Client.Bucket(bucketName).Attrs(ctx)
if err == storage.ErrBucketNotExist {
// Create the bucket
err = client.Client.Bucket(bucketName).Create(ctx, gcpProjectID, nil)
if err != nil {
logf.Log.Error(err, "Failed to create bucket", "Bucket Name", bucketName)
return err
}
logf.Log.Info("Bucket created", "Bucket Name", bucketName)
} else if err != nil {
logf.Log.Error(err, "Error checking bucket attributes", "Bucket Name", bucketName)
return err
}

// Check if the path exists by trying to get its attributes
_, err = client.Client.Bucket(bucketName).Object(path).Attrs(ctx)
if err == storage.ErrObjectNotExist {
// Create a zero-length object to represent the path
wc := client.Client.Bucket(bucketName).Object(path).NewWriter(ctx)
if _, err := wc.Write([]byte{}); err != nil {
logf.Log.Error(err, "Failed to create path", "Path", path)
return err
}
if err := wc.Close(); err != nil {
logf.Log.Error(err, "Failed to finalize path creation", "Path", path)
return err
}
logf.Log.Info("Path created", "Path", path)
} else if err != nil {
logf.Log.Error(err, "Error checking path attributes", "Path", path)
return err
}

return nil
}

// DownloadLicenseFromGCPBucket downloads the license file from GCP
func DownloadLicenseFromGCPBucket() (string, error) {
location := enterpriseLicenseLocationGCP
Expand Down Expand Up @@ -343,6 +391,8 @@ func UploadFilesToGCP(bucketName, gcpTestDir string, appList []string, uploadDir
return uploadedFiles, nil
}



// DisableAppsToGCP untars apps, modifies their config files to disable them, re-tars, and uploads the disabled versions to GCP
func DisableAppsToGCP(downloadDir string, appFileList []string, gcpTestDir string) ([]string, error) {
// Create directories for untarred and disabled apps
Expand Down
6 changes: 3 additions & 3 deletions test/testenv/testcaseenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,13 +532,13 @@ func (testenv *TestCaseEnv) createIndexSecret() error {
func (testenv *TestCaseEnv) createIndexSecretGCP() error {
secretName := testenv.s3IndexSecret
ns := testenv.namespace
encodedString := os.Getenv("GCP_SERVICE_ACCOUNT_KEY")
jsonBytes, err := base64.StdEncoding.DecodeString(encodedString)
encodedString := os.Getenv("GCP_SERVICE_ACCOUNT_KEY")
gcpCredentials, err := base64.StdEncoding.DecodeString(encodedString)
if err != nil {
testenv.Log.Error(err, "Unable to decode GCP service account key")
return err
}
data := map[string][]byte{"key.json": []byte(jsonBytes)}
data := map[string][]byte{"key.json": []byte(gcpCredentials)}
secret := newSecretSpec(ns, secretName, data)
if err := testenv.GetKubeClient().Create(context.TODO(), secret); err != nil {
testenv.Log.Error(err, "Unable to create GCP index secret object")
Expand Down

0 comments on commit 9e31a48

Please sign in to comment.