Skip to content

Commit 07bd226

Browse files
committed
setup example
1 parent 66033cb commit 07bd226

6 files changed

Lines changed: 317 additions & 4 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,5 @@ website/vendor
3333

3434
# Keep windows files with windows line endings
3535
*.winfile eol=crlf
36+
37+
.env

examples/.terraformrc.example

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Terraform CLI configuration for local provider development
2+
#
3+
# To use this configuration:
4+
# 1. Copy this file to ~/.terraformrc (or ~/.tofurc for OpenTofu)
5+
# 2. Update the path below to point to your built provider binary
6+
# 3. Build the provider: go build -o terraform-provider-spiceai .
7+
#
8+
# Note: When dev_overrides is active, you don't need to run `terraform init`
9+
# and the provider will be loaded directly from the specified path.
10+
11+
provider_installation {
12+
dev_overrides {
13+
# Update this path to where your terraform-provider-spiceai binary is located
14+
"spiceai/spiceai" = "/Users/evgenii/Developer/Spice/terraform-provider-spiceai"
15+
}
16+
17+
# Fall back to the default installation behavior for all other providers
18+
direct {}
19+
}

examples/test/main.tf

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Simple test configuration for local development
2+
#
3+
# Prerequisites:
4+
# 1. Set environment variables:
5+
# export SPICEAI_CLIENT_ID="your-client-id"
6+
# export SPICEAI_CLIENT_SECRET="your-client-secret"
7+
#
8+
# 2. Configure ~/.terraformrc with dev_overrides (see ../.terraformrc.example)
9+
#
10+
# 3. Build the provider:
11+
# cd ../.. && go build -o terraform-provider-spiceai .
12+
#
13+
# Usage:
14+
# terraform plan
15+
# terraform apply
16+
17+
terraform {
18+
required_providers {
19+
spiceai = {
20+
source = "spiceai/spiceai"
21+
}
22+
}
23+
}
24+
25+
provider "spiceai" {
26+
# Credentials are read from environment variables:
27+
# - SPICEAI_CLIENT_ID
28+
# - SPICEAI_CLIENT_SECRET
29+
}
30+
31+
# Test 1: List all existing apps (data source)
32+
data "spiceai_apps" "all" {}
33+
34+
output "existing_apps" {
35+
description = "List of all existing apps"
36+
value = [for app in data.spiceai_apps.all.apps : { id = app.id, name = app.name }]
37+
}
38+
39+
# Test 2: Create a new app
40+
resource "spiceai_app" "test" {
41+
name = "terraform-test-app"
42+
description = "Test app created by Terraform provider"
43+
visibility = "private"
44+
}
45+
46+
output "created_app_id" {
47+
description = "ID of the created app"
48+
value = spiceai_app.test.id
49+
}
50+
51+
output "created_app_api_key" {
52+
description = "API key of the created app"
53+
value = spiceai_app.test.api_key
54+
sensitive = true
55+
}
56+
57+
# Test 3: Apply configuration to the app
58+
resource "spiceai_app_config" "test" {
59+
app_id = spiceai_app.test.id
60+
61+
spicepod = <<-YAML
62+
version: v1beta1
63+
kind: Spicepod
64+
name: terraform-test-app
65+
YAML
66+
67+
replicas = 1
68+
}
69+
70+
# Test 4: Create a deployment (uncomment to test)
71+
# resource "spiceai_deployment" "test" {
72+
# app_id = spiceai_app.test.id
73+
#
74+
# depends_on = [spiceai_app_config.test]
75+
# }
76+
#
77+
# output "deployment_id" {
78+
# value = spiceai_deployment.test.id
79+
# }
80+
#
81+
# output "deployment_status" {
82+
# value = spiceai_deployment.test.status
83+
# }

internal/provider/provider.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,34 @@ func (p *SpiceAIProvider) Configure(ctx context.Context, req provider.ConfigureR
7777
}
7878

7979
// Get values from config or environment variables
80-
clientID := data.ClientID.ValueString()
80+
var clientID string
81+
if !data.ClientID.IsNull() && !data.ClientID.IsUnknown() {
82+
clientID = data.ClientID.ValueString()
83+
}
8184
if clientID == "" {
8285
clientID = os.Getenv("SPICEAI_CLIENT_ID")
8386
}
8487

85-
clientSecret := data.ClientSecret.ValueString()
88+
var clientSecret string
89+
if !data.ClientSecret.IsNull() && !data.ClientSecret.IsUnknown() {
90+
clientSecret = data.ClientSecret.ValueString()
91+
}
8692
if clientSecret == "" {
8793
clientSecret = os.Getenv("SPICEAI_CLIENT_SECRET")
8894
}
8995

90-
apiEndpoint := data.APIEndpoint.ValueString()
96+
var apiEndpoint string
97+
if !data.APIEndpoint.IsNull() && !data.APIEndpoint.IsUnknown() {
98+
apiEndpoint = data.APIEndpoint.ValueString()
99+
}
91100
if apiEndpoint == "" {
92101
apiEndpoint = os.Getenv("SPICEAI_API_ENDPOINT")
93102
}
94103

95-
oauthEndpoint := data.OAuthEndpoint.ValueString()
104+
var oauthEndpoint string
105+
if !data.OAuthEndpoint.IsNull() && !data.OAuthEndpoint.IsUnknown() {
106+
oauthEndpoint = data.OAuthEndpoint.ValueString()
107+
}
96108
if oauthEndpoint == "" {
97109
oauthEndpoint = os.Getenv("SPICEAI_OAUTH_ENDPOINT")
98110
}

scripts/local-test.sh

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
#!/bin/bash
2+
# Helper script for local testing of the Spice.ai Terraform provider
3+
#
4+
# Usage:
5+
# ./scripts/local-test.sh [command]
6+
#
7+
# Commands:
8+
# build - Build the provider binary
9+
# setup - Set up ~/.terraformrc with dev_overrides
10+
# plan - Run terraform plan in examples/test
11+
# apply - Run terraform apply in examples/test
12+
# destroy - Run terraform destroy in examples/test
13+
# clean - Clean up terraform state files
14+
# all - Build, setup, and run plan
15+
16+
set -e
17+
18+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
19+
PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
20+
PROVIDER_BINARY="$PROJECT_DIR/terraform-provider-spiceai"
21+
TEST_DIR="$PROJECT_DIR/examples/test"
22+
TERRAFORMRC="$HOME/.terraformrc"
23+
24+
# Colors for output
25+
RED='\033[0;31m'
26+
GREEN='\033[0;32m'
27+
YELLOW='\033[1;33m'
28+
NC='\033[0m' # No Color
29+
30+
log_info() {
31+
echo -e "${GREEN}[INFO]${NC} $1"
32+
}
33+
34+
log_warn() {
35+
echo -e "${YELLOW}[WARN]${NC} $1"
36+
}
37+
38+
log_error() {
39+
echo -e "${RED}[ERROR]${NC} $1"
40+
}
41+
42+
check_credentials() {
43+
if [ -z "$SPICEAI_CLIENT_ID" ]; then
44+
log_error "SPICEAI_CLIENT_ID environment variable is not set"
45+
echo " export SPICEAI_CLIENT_ID=\"your-client-id\""
46+
return 1
47+
fi
48+
if [ -z "$SPICEAI_CLIENT_SECRET" ]; then
49+
log_error "SPICEAI_CLIENT_SECRET environment variable is not set"
50+
echo " export SPICEAI_CLIENT_SECRET=\"your-client-secret\""
51+
return 1
52+
fi
53+
log_info "Credentials configured"
54+
return 0
55+
}
56+
57+
cmd_build() {
58+
log_info "Building provider..."
59+
cd "$PROJECT_DIR"
60+
go build -o terraform-provider-spiceai .
61+
log_info "Provider built: $PROVIDER_BINARY"
62+
}
63+
64+
cmd_setup() {
65+
log_info "Setting up ~/.terraformrc with dev_overrides..."
66+
67+
if [ -f "$TERRAFORMRC" ]; then
68+
if grep -q "spiceai/spiceai" "$TERRAFORMRC"; then
69+
log_warn "~/.terraformrc already contains spiceai/spiceai override"
70+
log_warn "Please verify the path is correct: $PROJECT_DIR"
71+
return 0
72+
fi
73+
log_warn "~/.terraformrc exists, creating backup..."
74+
cp "$TERRAFORMRC" "$TERRAFORMRC.backup.$(date +%Y%m%d%H%M%S)"
75+
fi
76+
77+
cat > "$TERRAFORMRC" << EOF
78+
# Terraform CLI configuration for local provider development
79+
# Generated by terraform-provider-spiceai/scripts/local-test.sh
80+
81+
provider_installation {
82+
dev_overrides {
83+
"spiceai/spiceai" = "$PROJECT_DIR"
84+
}
85+
86+
direct {}
87+
}
88+
EOF
89+
90+
log_info "Created ~/.terraformrc with dev_overrides"
91+
log_info "Provider path: $PROJECT_DIR"
92+
}
93+
94+
cmd_plan() {
95+
check_credentials || exit 1
96+
97+
if [ ! -f "$PROVIDER_BINARY" ]; then
98+
log_warn "Provider binary not found, building..."
99+
cmd_build
100+
fi
101+
102+
log_info "Running terraform plan..."
103+
cd "$TEST_DIR"
104+
terraform plan
105+
}
106+
107+
cmd_apply() {
108+
check_credentials || exit 1
109+
110+
if [ ! -f "$PROVIDER_BINARY" ]; then
111+
log_warn "Provider binary not found, building..."
112+
cmd_build
113+
fi
114+
115+
log_info "Running terraform apply..."
116+
cd "$TEST_DIR"
117+
terraform apply
118+
}
119+
120+
cmd_destroy() {
121+
check_credentials || exit 1
122+
123+
log_info "Running terraform destroy..."
124+
cd "$TEST_DIR"
125+
terraform destroy
126+
}
127+
128+
cmd_clean() {
129+
log_info "Cleaning up terraform state files..."
130+
cd "$TEST_DIR"
131+
rm -rf .terraform .terraform.lock.hcl terraform.tfstate terraform.tfstate.backup
132+
log_info "Cleaned up state files"
133+
}
134+
135+
cmd_all() {
136+
cmd_build
137+
cmd_setup
138+
cmd_plan
139+
}
140+
141+
cmd_help() {
142+
echo "Spice.ai Terraform Provider - Local Testing Helper"
143+
echo ""
144+
echo "Usage: $0 [command]"
145+
echo ""
146+
echo "Commands:"
147+
echo " build - Build the provider binary"
148+
echo " setup - Set up ~/.terraformrc with dev_overrides"
149+
echo " plan - Run terraform plan in examples/test"
150+
echo " apply - Run terraform apply in examples/test"
151+
echo " destroy - Run terraform destroy in examples/test"
152+
echo " clean - Clean up terraform state files"
153+
echo " all - Build, setup, and run plan"
154+
echo " help - Show this help message"
155+
echo ""
156+
echo "Environment variables required for plan/apply/destroy:"
157+
echo " SPICEAI_CLIENT_ID - OAuth client ID"
158+
echo " SPICEAI_CLIENT_SECRET - OAuth client secret"
159+
echo ""
160+
echo "Example:"
161+
echo " export SPICEAI_CLIENT_ID=\"your-client-id\""
162+
echo " export SPICEAI_CLIENT_SECRET=\"your-client-secret\""
163+
echo " $0 all"
164+
}
165+
166+
# Main
167+
case "${1:-help}" in
168+
build)
169+
cmd_build
170+
;;
171+
setup)
172+
cmd_setup
173+
;;
174+
plan)
175+
cmd_plan
176+
;;
177+
apply)
178+
cmd_apply
179+
;;
180+
destroy)
181+
cmd_destroy
182+
;;
183+
clean)
184+
cmd_clean
185+
;;
186+
all)
187+
cmd_all
188+
;;
189+
help|--help|-h)
190+
cmd_help
191+
;;
192+
*)
193+
log_error "Unknown command: $1"
194+
cmd_help
195+
exit 1
196+
;;
197+
esac

terraform-provider-spiceai

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)