(config TerraformState)
| 24 | } |
| 25 | |
| 26 | func PushToGCSBackend(config TerraformState) error { |
| 27 | functionName := "PushToGCSBackend" |
| 28 | |
| 29 | uploadSpinner := ux.NewSpinner("Uploading To GCS State Backend", "Diagram Uploaded To GCS State Backend", "Diagram Upload Failed!", true) |
| 30 | uploadSpinner.Start() |
| 31 | |
| 32 | gcsConfig := GCSBackendConfig{} |
| 33 | backendErr := MapBackendConfig(config, &gcsConfig) |
| 34 | if backendErr != nil { |
| 35 | uploadSpinner.Fail() |
| 36 | return fmt.Errorf("loading gcs backend information failed -> %v: %w", functionName, backendErr) |
| 37 | } |
| 38 | |
| 39 | ctx := context.Background() |
| 40 | gcsClient, clientErr := storage.NewClient(ctx) |
| 41 | if clientErr != nil { |
| 42 | uploadSpinner.Fail() |
| 43 | return fmt.Errorf("creating gcs client failed -> %v: %w", functionName, clientErr) |
| 44 | } |
| 45 | |
| 46 | diagram, diagramErr := os.ReadFile(filepath.Join(auxiliary.StateInstance.WorkingPath, "Infrastructure_Diagram.pdf")) |
| 47 | if diagramErr != nil { |
| 48 | uploadSpinner.Fail() |
| 49 | return fmt.Errorf("reading diagram from disk failed -> %v: %w", functionName, diagramErr) |
| 50 | } |
| 51 | |
| 52 | // Store diagram at same key "directory" as state |
| 53 | diagramPath := filepath.Join(gcsConfig.Prefix, "Infrastructure_Diagram.pdf") |
| 54 | gcsObject := gcsClient.Bucket(gcsConfig.Bucket).Object(diagramPath) |
| 55 | |
| 56 | gcsWriter := gcsObject.NewWriter(ctx) |
| 57 | if _, uploadErr := io.Copy(gcsWriter, bytes.NewReader(diagram)); uploadErr != nil { |
| 58 | uploadSpinner.Fail() |
| 59 | return fmt.Errorf("uploading diagram to s3 bucket failed -> %v: %w", functionName, uploadErr) |
| 60 | } |
| 61 | if closeErr := gcsWriter.Close(); closeErr != nil { |
| 62 | uploadSpinner.Fail() |
| 63 | return fmt.Errorf("uploading diagram to s3 bucket failed -> %v: %w", functionName, closeErr) |
| 64 | } |
| 65 | |
| 66 | uploadSpinner.Success() |
| 67 | return nil |
| 68 | } |
no test coverage detected