# Templates for tbi-environments Repository

Copy these templates to the `tbi-environments` repository.

## 1. Values File Template

**Location**: `tbi-environments/dev/values/tbi-acc-hotel-mapping-service.yaml`

```yaml
image: IMAGE_TO_REPLACE
name: tbi-acc-hotel-mapping-service
replicas: 1
service_type: "ClusterIP"
port: "80"
targetPort: "3000"
node_env: "development"
group: "redtag"

# Optional: Uncomment to enable ingress
# ingress:
#   enabled: true
#   host: tbi-acc-hotel-mapping-service.dev.tbidevelopment.ca

# Optional: Uncomment to enable autoscaling
# autoscaling:
#   enabled: true
#   minReplicas: 1
#   maxReplicas: 3
#   targetCPUUtilizationPercentage: 70
#   targetMemoryUtilizationPercentage: 80

# Optional: Uncomment to enable pod disruption budget
# podDisruptionBudget:
#   enabled: true
#   minAvailable: 1
```

---

## 2. Secrets File Template

**Location**: `tbi-environments/dev/secrets/secrets.tbi-acc-hotel-mapping-service.yaml`

**BEFORE ENCRYPTION** - Replace all placeholder values with actual credentials:

```yaml
configs:
    dotenv: |-
        NODE_ENV=development
        PORT=3000
        
        # Database Configuration
        DB_HOST=your-database-host.rds.amazonaws.com
        DB_PORT=5432
        DB_USERNAME=your_db_username
        DB_PASSWORD=your_secure_db_password
        DB_NAME=hotel_mapping_db
        DB_SSL=true
        
        # Redis Configuration
        REDIS_HOST=your-redis-host.cache.amazonaws.com
        REDIS_PORT=6379
        REDIS_PASSWORD=your_redis_password
        
        # AWS Configuration
        AWS_REGION=ca-central-1
        AWS_ACCOUNT_ID=594765360358
        
        # API Keys (if needed)
        # EXTERNAL_API_KEY=your_api_key
        # EXTERNAL_API_SECRET=your_api_secret
        
        # Logging
        LOG_LEVEL=info
        
        # Add any other environment variables your service needs
```

**CRITICAL FORMAT REQUIREMENTS**:
- ✅ Use `|-` literal block scalar
- ✅ Each variable on its own line (actual newlines)
- ✅ 8-space indentation (2 levels: configs + dotenv)
- ❌ NO quotes around the entire block
- ❌ NO literal `\n` characters
- ❌ NO duplicate keys

**After creating the file, encrypt it**:
```bash
cd tbi-environments/dev
sops -e -i secrets/secrets.tbi-acc-hotel-mapping-service.yaml
```

---

## 3. Update requirements.yaml

**Location**: `tbi-environments/dev/requirements.yaml`

Add this entry to the file:

```yaml
- name: tbi-acc-hotel-mapping-service
  version: 1.0.0-develop-1
  repository: https://tbi-charts.s3.amazonaws.com
```

**Note**: The version will be automatically updated by the deployment pipeline.

---

## 4. Common Configuration (if not exists)

If `tbi-environments/dev/common.yaml` doesn't have these, you may need to add:

```yaml
# Common configuration for all services in dev
cluster: dev
environment: development
```

---

## 5. Common Secrets (if not exists)

If `tbi-environments/dev/secrets.common.yaml` doesn't exist, create it:

```yaml
# Common secrets for all services in dev
# This file should be encrypted with SOPS
configs:
    common: |-
        SHARED_SECRET=some_shared_value
```

Then encrypt:
```bash
cd tbi-environments/dev
sops -e -i secrets.common.yaml
```

---

## Complete Setup Commands

Run these commands in the `tbi-environments` repository:

```bash
# 1. Create values file
cat > dev/values/tbi-acc-hotel-mapping-service.yaml << 'EOF'
image: IMAGE_TO_REPLACE
name: tbi-acc-hotel-mapping-service
replicas: 1
service_type: "ClusterIP"
port: "80"
targetPort: "3000"
node_env: "development"
group: "redtag"
EOF

# 2. Create secrets file (REPLACE PLACEHOLDERS WITH ACTUAL VALUES!)
cat > dev/secrets/secrets.tbi-acc-hotel-mapping-service.yaml << 'EOF'
configs:
    dotenv: |-
        NODE_ENV=development
        PORT=3000
        DB_HOST=REPLACE_WITH_ACTUAL_HOST
        DB_PORT=5432
        DB_USERNAME=REPLACE_WITH_ACTUAL_USERNAME
        DB_PASSWORD=REPLACE_WITH_ACTUAL_PASSWORD
        DB_NAME=hotel_mapping_db
        REDIS_HOST=REPLACE_WITH_ACTUAL_HOST
        REDIS_PORT=6379
        AWS_REGION=ca-central-1
        LOG_LEVEL=info
EOF

# 3. Edit the secrets file to add actual values
# Use your preferred editor to replace placeholders
nano dev/secrets/secrets.tbi-acc-hotel-mapping-service.yaml

# 4. Encrypt the secrets file
cd dev
sops -e -i secrets/secrets.tbi-acc-hotel-mapping-service.yaml
cd ..

# 5. Update requirements.yaml
# Add the service entry manually or use this command:
cat >> dev/requirements.yaml << 'EOF'
- name: tbi-acc-hotel-mapping-service
  version: 1.0.0-develop-1
  repository: https://tbi-charts.s3.amazonaws.com
EOF

# 6. Verify files were created
ls -la dev/values/tbi-acc-hotel-mapping-service.yaml
ls -la dev/secrets/secrets.tbi-acc-hotel-mapping-service.yaml
grep "tbi-acc-hotel-mapping-service" dev/requirements.yaml

# 7. Commit and push
git add dev/values/tbi-acc-hotel-mapping-service.yaml
git add dev/secrets/secrets.tbi-acc-hotel-mapping-service.yaml
git add dev/requirements.yaml
git commit -m "Add configuration for tbi-acc-hotel-mapping-service in dev environment

- Added values file with service configuration
- Added encrypted secrets file
- Updated requirements.yaml with chart reference
"
git push origin master
```

---

## Environment Variables Checklist

Make sure your secrets file includes all variables your application needs:

### Database
- [ ] DB_HOST
- [ ] DB_PORT
- [ ] DB_USERNAME
- [ ] DB_PASSWORD
- [ ] DB_NAME
- [ ] DB_SSL (if needed)

### Redis/Cache
- [ ] REDIS_HOST
- [ ] REDIS_PORT
- [ ] REDIS_PASSWORD (if needed)

### AWS
- [ ] AWS_REGION
- [ ] AWS_ACCOUNT_ID (if needed)
- [ ] AWS_ACCESS_KEY_ID (if using AWS SDK)
- [ ] AWS_SECRET_ACCESS_KEY (if using AWS SDK)

### Application
- [ ] NODE_ENV
- [ ] PORT
- [ ] LOG_LEVEL

### External APIs
- [ ] API keys
- [ ] API secrets
- [ ] Service endpoints

### Other
- [ ] Any service-specific configuration

---

## Verification

After creating and encrypting the files:

### 1. Verify secrets file is encrypted
```bash
cat dev/secrets/secrets.tbi-acc-hotel-mapping-service.yaml
# Should show encrypted content starting with "sops:"
```

### 2. Test decryption
```bash
cd dev
sops -d secrets/secrets.tbi-acc-hotel-mapping-service.yaml
# Should show decrypted content
```

### 3. Verify format
```bash
cd dev
sops -d secrets/secrets.tbi-acc-hotel-mapping-service.yaml | grep -A 20 "dotenv:"
# Should show variables on separate lines, not as a single line with \n
```

### 4. Check requirements.yaml
```bash
grep -A 2 "tbi-acc-hotel-mapping-service" dev/requirements.yaml
# Should show the service entry
```

---

## Troubleshooting

### SOPS encryption fails
**Error**: `config file not found`
**Solution**: Make sure you're in the `dev/` directory when running `sops -e -i`

### Wrong format after encryption
**Error**: Variables appear as single line with `\n`
**Solution**: Recreate the file ensuring actual newlines, not literal `\n` characters

### Can't decrypt
**Error**: `Failed to get the data key`
**Solution**: Ensure you have AWS credentials configured and access to the KMS key

---

## Next Steps

After creating these files in tbi-environments:
1. Proceed with CircleCI setup
2. Trigger first deployment
3. Verify pods are running
4. Test secrets management workflow

Refer to `SETUP-SUMMARY.md` for complete deployment instructions.
