From 1643bdc1d3a7b0f77b94a32afcf4429bfc61dac4 Mon Sep 17 00:00:00 2001 From: Karthikeyan C Date: Wed, 31 Jul 2024 15:09:59 +0530 Subject: [PATCH] track cmd/gcp.go --- registry-automation/cmd/ci.go | 9 +++---- registry-automation/cmd/gcp.go | 46 ++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 6 deletions(-) create mode 100644 registry-automation/cmd/gcp.go diff --git a/registry-automation/cmd/ci.go b/registry-automation/cmd/ci.go index b7014fb1..a205d3ed 100644 --- a/registry-automation/cmd/ci.go +++ b/registry-automation/cmd/ci.go @@ -425,7 +425,8 @@ func buildRegistryPayload( } func updateRegistryGQL(payload []ConnectorVersion) error { - client := graphql.NewClient("http://localhost:8081/v1/graphql") + var respData map[string]interface{} + client := graphql.NewClient(cmdArgs.ConnectorRegistryGQL) ctx := context.Background() req := graphql.NewRequest(` @@ -440,12 +441,8 @@ mutation InsertConnectorVersion($connectorVersion: [hub_registry_connector_versi // add the payload to the request req.Var("connectorVersion", payload) - // add a new key value to req - - var respData map[string]interface{} - req.Header.Set("x-hasura-role", "connector_publishing_automation") - req.Header.Set("x-connector-publication-key", "usnEu*pYp8wiUjbzv3g4iruemTzDgfi@") // TODO: The value of the header should be fetched from the environment variable CONNECTOR_PUBLICATION_KEY + req.Header.Set("x-connector-publication-key", cmdArgs.ConnectorPublicationKey) // Execute the GraphQL query and check the response. if err := client.Run(ctx, req, &respData); err != nil { diff --git a/registry-automation/cmd/gcp.go b/registry-automation/cmd/gcp.go new file mode 100644 index 00000000..40d41d62 --- /dev/null +++ b/registry-automation/cmd/gcp.go @@ -0,0 +1,46 @@ +// Description: This file contains the functions to interact with Google Cloud Storage. +package cmd + +import ( + "cloud.google.com/go/storage" + "context" + "fmt" + "io" + "os" +) + +// deleteFile deletes a file from Google Cloud Storage +func deleteFile(client *storage.Client, bucketName, objectName string) error { + bucket := client.Bucket(bucketName) + object := bucket.Object(objectName) + + return object.Delete(context.Background()) +} + +// uploadFile uploads a file to Google Cloud Storage +// document this function with comments +func uploadFile(client *storage.Client, bucketName, objectName, filePath string) (string, error) { + bucket := client.Bucket(bucketName) + object := bucket.Object(objectName) + newCtx := context.Background() + wc := object.NewWriter(newCtx) + + file, err := os.Open(filePath) + if err != nil { + return "", fmt.Errorf("failed to open file: %v", err) + } + defer file.Close() + + if _, err := io.Copy(wc, file); err != nil { + return "", fmt.Errorf("failed to upload file: %w", err) + } + if err := wc.Close(); err != nil { + return "", fmt.Errorf("failed to close writer: %w", err) + } + + // Return the public URL of the uploaded object. + publicURL := fmt.Sprintf("https://storage.googleapis.com/%s/%s", bucketName, objectName) + + fmt.Printf("File %s uploaded to bucket %s as %s and is available at %s.\n", filePath, bucketName, objectName, publicURL) + return publicURL, nil +}