|
| 1 | +import * as React from 'react' |
| 2 | +import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card' |
| 3 | +import { Button } from '@/components/ui/button' |
| 4 | +import { Label } from '@/components/ui/label' |
| 5 | +import { Copy, Check, Github, TerminalSquare } from 'lucide-react' |
| 6 | + |
| 7 | +function CopyButton({ content }: { content: string }) { |
| 8 | + const [copied, setCopied] = React.useState(false) |
| 9 | + const copy = () => { |
| 10 | + navigator.clipboard.writeText(content) |
| 11 | + setCopied(true) |
| 12 | + setTimeout(() => setCopied(false), 1500) |
| 13 | + } |
| 14 | + return ( |
| 15 | + <Button size="icon" variant="ghost" className="absolute top-2 right-2 h-8 w-8" onClick={copy}> |
| 16 | + {copied ? <Check className="h-4 w-4 text-green-500" /> : <Copy className="h-4 w-4" />} |
| 17 | + </Button> |
| 18 | + ) |
| 19 | +} |
| 20 | + |
| 21 | +type Props = { |
| 22 | + unitId: string |
| 23 | + organisationId: string |
| 24 | + publicHostname: string |
| 25 | + onGoToGithub?: () => void |
| 26 | + onGoToLocal?: () => void |
| 27 | + showNextActions?: boolean |
| 28 | +} |
| 29 | + |
| 30 | +export default function UnitConfigureInstructions({ unitId, organisationId, publicHostname, onGoToGithub, onGoToLocal, showNextActions = true }: Props) { |
| 31 | + const tfBlock = `terraform {\n cloud {\n hostname = "${publicHostname}"\n organization = "${organisationId}" \n workspaces {\n name = "${unitId}"\n }\n }\n}` |
| 32 | + return ( |
| 33 | + <> |
| 34 | + <Card> |
| 35 | + <CardHeader> |
| 36 | + <CardTitle>Terraform Configuration</CardTitle> |
| 37 | + <CardDescription>Add this configuration block to your Terraform code to use this unit</CardDescription> |
| 38 | + </CardHeader> |
| 39 | + <CardContent> |
| 40 | + <div className="mb-4"> |
| 41 | + <p className="text-sm text-muted-foreground mb-4"> |
| 42 | + To use this unit in your Terraform configuration, add the following block to your Terraform code: |
| 43 | + </p> |
| 44 | + <div className="relative"> |
| 45 | + <pre className="bg-muted p-4 rounded-lg overflow-x-auto font-mono text-sm">{tfBlock}</pre> |
| 46 | + <CopyButton content={tfBlock} /> |
| 47 | + </div> |
| 48 | + </div> |
| 49 | + |
| 50 | + <div className="space-y-6"> |
| 51 | + <div> |
| 52 | + <h3 className="font-semibold mb-2">1. Login to the remote backend</h3> |
| 53 | + <p className="text-sm text-muted-foreground mb-2">First, authenticate with the remote backend:</p> |
| 54 | + <div className="relative"> |
| 55 | + <pre className="bg-muted p-4 rounded-lg overflow-x-auto font-mono text-sm">terraform login {publicHostname}</pre> |
| 56 | + <CopyButton content={`terraform login ${publicHostname}`} /> |
| 57 | + </div> |
| 58 | + </div> |
| 59 | + |
| 60 | + <div> |
| 61 | + <h3 className="font-semibold mb-2">2. Initialize Terraform</h3> |
| 62 | + <p className="text-sm text-muted-foreground mb-2">After adding the configuration block above, initialize your working directory:</p> |
| 63 | + <div className="relative"> |
| 64 | + <pre className="bg-muted p-4 rounded-lg overflow-x-auto font-mono text-sm">terraform init</pre> |
| 65 | + <CopyButton content="terraform init" /> |
| 66 | + </div> |
| 67 | + </div> |
| 68 | + |
| 69 | + <div> |
| 70 | + <h3 className="font-semibold mb-2">3. Review Changes</h3> |
| 71 | + <p className="text-sm text-muted-foreground mb-2">Preview any changes that will be made to your infrastructure:</p> |
| 72 | + <div className="relative"> |
| 73 | + <pre className="bg-muted p-4 rounded-lg overflow-x-auto font-mono text-sm">terraform plan</pre> |
| 74 | + <CopyButton content="terraform plan" /> |
| 75 | + </div> |
| 76 | + </div> |
| 77 | + |
| 78 | + <div> |
| 79 | + <h3 className="font-semibold mb-2">4. Apply Changes</h3> |
| 80 | + <p className="text-sm text-muted-foreground mb-2">Apply the changes to your infrastructure:</p> |
| 81 | + <div className="relative"> |
| 82 | + <pre className="bg-muted p-4 rounded-lg overflow-x-auto font-mono text-sm">terraform apply</pre> |
| 83 | + <CopyButton content="terraform apply" /> |
| 84 | + </div> |
| 85 | + </div> |
| 86 | + |
| 87 | + <div className="mt-6 bg-blue-50 dark:bg-blue-950 p-4 rounded-lg"> |
| 88 | + <h3 className="font-semibold text-blue-700 dark:text-blue-300 mb-2">Note</h3> |
| 89 | + <p className="text-sm text-blue-600 dark:text-blue-400"> |
| 90 | + After completing these steps, your Terraform state will be managed by this unit. All state operations will be automatically versioned and you can roll back to previous versions if needed. |
| 91 | + </p> |
| 92 | + </div> |
| 93 | + |
| 94 | + {showNextActions && ( |
| 95 | + <div className="pt-4"> |
| 96 | + <Label className="text-right">What would you like to do next?</Label> |
| 97 | + <div className="mt-3 grid grid-cols-1 md:grid-cols-2 gap-3"> |
| 98 | + <div |
| 99 | + onClick={onGoToLocal} |
| 100 | + className={ |
| 101 | + `relative flex cursor-pointer items-start gap-4 rounded-lg border p-4 md:p-5 transition-colors hover:bg-muted/50 border-muted` |
| 102 | + } |
| 103 | + > |
| 104 | + <div className="flex h-10 w-10 items-center justify-center rounded-md bg-muted text-muted-foreground"> |
| 105 | + <TerminalSquare className="h-5 w-5" /> |
| 106 | + </div> |
| 107 | + <div className="space-y-1"> |
| 108 | + <div className="flex items-center gap-2"> |
| 109 | + <span className="text-base font-semibold">Run locally for now</span> |
| 110 | + </div> |
| 111 | + <p className="text-sm text-muted-foreground"> |
| 112 | + Stick to local <code className="font-mono">terraform plan</code>/<code className="font-mono">apply</code> |
| 113 | + using the configuration above. You can enable PR automation later. |
| 114 | + </p> |
| 115 | + </div> |
| 116 | + </div> |
| 117 | + |
| 118 | + <div |
| 119 | + onClick={onGoToGithub} |
| 120 | + className={ |
| 121 | + `relative flex cursor-pointer items-start gap-4 rounded-lg border p-4 md:p-5 transition-colors hover:bg-muted/50 border-muted` |
| 122 | + } |
| 123 | + > |
| 124 | + <div className="flex h-10 w-10 items-center justify-center rounded-md bg-primary/10 text-primary"> |
| 125 | + <Github className="h-5 w-5" /> |
| 126 | + </div> |
| 127 | + <div className="space-y-1"> |
| 128 | + <div className="flex items-center gap-2"> |
| 129 | + <span className="text-base font-semibold">Use PR automation</span> |
| 130 | + </div> |
| 131 | + <p className="text-sm text-muted-foreground"> |
| 132 | + Create a pull request that touches your Terraform directories. Digger will |
| 133 | + comment with plans and manage runs automatically. |
| 134 | + </p> |
| 135 | + </div> |
| 136 | + </div> |
| 137 | + </div> |
| 138 | + </div> |
| 139 | + )} |
| 140 | + </div> |
| 141 | + </CardContent> |
| 142 | + </Card> |
| 143 | + </> |
| 144 | + ) |
| 145 | +} |
| 146 | + |
| 147 | + |
0 commit comments