(config TerraformState)
| 49 | } |
| 50 | |
| 51 | func PushToS3Backend(config TerraformState) error { |
| 52 | functionName := "PushToS3Backend" |
| 53 | |
| 54 | uploadSpinner := ux.NewSpinner("Uploading To S3 State Backend", "Diagram Uploaded To S3 State Backend", "Diagram Upload Failed!", true) |
| 55 | uploadSpinner.Start() |
| 56 | |
| 57 | s3Config := S3BackendConfig{} |
| 58 | backendErr := MapBackendConfig(config, &s3Config) |
| 59 | if backendErr != nil { |
| 60 | uploadSpinner.Fail() |
| 61 | return fmt.Errorf("loading s3 backend information failed -> %v: %w", functionName, backendErr) |
| 62 | } |
| 63 | |
| 64 | s3BucketConfig := &aws.Config{Region: aws.String(s3Config.Region)} |
| 65 | s3Session, sessionErr := session.NewSession(s3BucketConfig) |
| 66 | if sessionErr != nil { |
| 67 | uploadSpinner.Fail() |
| 68 | return fmt.Errorf("establishing s3 session failed -> %v: %w", functionName, sessionErr) |
| 69 | } |
| 70 | |
| 71 | uploader := s3manager.NewUploader(s3Session) |
| 72 | |
| 73 | diagram, diagramErr := os.ReadFile(filepath.Join(auxiliary.StateInstance.WorkingPath, "Infrastructure_Diagram.pdf")) |
| 74 | if diagramErr != nil { |
| 75 | uploadSpinner.Fail() |
| 76 | return fmt.Errorf("reading diagram from disk failed -> %v: %w", functionName, diagramErr) |
| 77 | } |
| 78 | |
| 79 | // Store diagram at same key "directory" as state |
| 80 | keyPath := filepath.Dir(s3Config.Key) |
| 81 | diagramPath := filepath.Join(keyPath, "Infrastructure_Diagram.pdf") |
| 82 | |
| 83 | s3Input := &s3manager.UploadInput{ |
| 84 | Bucket: &s3Config.Bucket, |
| 85 | Key: &diagramPath, |
| 86 | Body: bytes.NewReader(diagram), |
| 87 | ContentType: aws.String("application/pdf"), |
| 88 | } |
| 89 | |
| 90 | _, uploadErr := uploader.UploadWithContext(context.Background(), s3Input) |
| 91 | if uploadErr != nil { |
| 92 | uploadSpinner.Fail() |
| 93 | return fmt.Errorf("uploading diagram to s3 bucket failed -> %v: %w", functionName, uploadErr) |
| 94 | } |
| 95 | |
| 96 | uploadSpinner.Success() |
| 97 | return nil |
| 98 | } |
no test coverage detected