# Workspace Management Examples

Practical examples of managing infrastructure across multiple workspaces.

## Creating and Using Workspaces

Set up isolated workspaces for different projects:

```bash
# Create development workspace
provisioning workspace create dev-environment \
  --description "Development infrastructure" \
  --owner platform-team \
  --tags environment=dev,tier=shared

# Create staging workspace
provisioning workspace create staging-environment \
  --description "Staging for pre-production testing" \
  --owner platform-team \
  --tags environment=staging,tier=shared

# Create production workspace
provisioning workspace create prod-environment \
  --description "Production infrastructure" \
  --owner platform-team \
  --tags environment=prod,tier=critical \
  --require-approval

# List all workspaces
provisioning workspace list

# Switch to a workspace
provisioning workspace use dev-environment

# Get current workspace
provisioning workspace current
```

## Workspace Structure

Directory layout for organized infrastructure:

```text
workspaces/
├── dev-environment/
│   ├── config/
│   │   ├── dev-config.ncl          # Dev-specific config
│   │   └── secrets.yaml            # Dev secrets
│   ├── infra/
│   │   ├── main.ncl                # Main infrastructure
│   │   ├── servers.ncl             # Server definitions
│   │   └── database.ncl            # Database config
│   ├── schemas/                    # Custom schemas (extends core)
│   ├── extensions/                 # Custom extensions
│   ├── scripts/
│   │   ├── deploy.nu               # Deployment script
│   │   └── verify.nu               # Verification script
│   └── .workspace/
│       ├── metadata.yaml
│       ├── state.json              # Current state
│       └── history/                # Deployment history
├── staging-environment/
│   ├── config/
│   ├── infra/
│   ├── scripts/
│   └── .workspace/
└── prod-environment/
    ├── config/
    ├── infra/
    ├── scripts/
    └── .workspace/
```

## Configuration Hierarchy in Workspaces

Example of multi-level configuration:

```nickel
# provisioning/schemas/main.ncl (core defaults)
{
  app_name = "my-app",
  database = {
    engine = "postgres",
    version = "15"
  },
  servers = {
    count = 2,
    cpu = 2,
    memory = 4
  }
}
```

```nickel
# workspaces/dev-environment/config/dev-config.ncl
# Extends and overrides core
let base = import "provisioning/schemas/main.ncl"

base & {
  servers = {
    count = 1,           # Dev: single server
    cpu = 2,
    memory = 4
  },
  database = {
    version = "15",
    backup_enabled = false
  }
}
```

```nickel
# workspaces/prod-environment/config/prod-config.ncl
let base = import "provisioning/schemas/main.ncl"

base & {
  servers = {
    count = 5,           # Prod: multiple servers
    cpu = 4,
    memory = 8,
    availability_zones = ["us-east-1a", "us-east-1b", "us-east-1c"]
  },
  database = {
    version = "15",
    backup_enabled = true,
    backup_retention = 30,
    multi_az = true
  }
}
```

## Workspace-Scoped Secrets

Manage secrets per workspace:

```bash
# Store secrets in dev workspace
provisioning workspace use dev-environment
provisioning vault add --name "db_password" --value "dev-secret-123"
provisioning vault add --name "api_key" --value "dev-api-key"

# Store secrets in prod workspace
provisioning workspace use prod-environment
provisioning vault add --name "db_password" --value "prod-secret-456"
provisioning vault add --name "api_key" --value "prod-api-key"
provisioning vault add --name "backup_encryption_key" --value "..."

# Secrets are isolated per workspace
provisioning vault list  # Shows only current workspace secrets
```

Access secrets in scripts:

```nushell
#!/usr/bin/env nu

# Get secrets for current workspace
let db_password = provisioning vault get "db_password"
let api_key = provisioning vault get "api_key"

# Use in environment
with-env {
  DATABASE_PASSWORD: $db_password,
  API_KEY: $api_key
} {
  ./deploy.sh
}
```

## Multi-Workspace Deployment

Deploy to multiple workspaces in sequence:

```bash
#!/usr/bin/env nu

# Define workspaces
let workspaces = ["dev-environment", "staging-environment", "prod-environment"]

# Deploy to each workspace
$workspaces | each { | ws |
  print $"Deploying to ($ws)..."

  provisioning workspace use $ws
  provisioning infra validate
  provisioning infra apply --auto-approve
  provisioning infra verify

  print $"Deployment to ($ws) completed"
}
```

Or using batch workflows:

```nickel
{
  batch = {
    name = "multi-workspace-deploy",
    description = "Deploy to dev, staging, and prod",

    operations = [
      {
        name = "deploy-dev",
        action = "deploy_infrastructure",
        workspace = "dev-environment",
        dry_run = false
      },
      {
        name = "test-dev",
        action = "run_tests",
        workspace = "dev-environment",
        depends_on = ["deploy-dev"]
      },
      {
        name = "deploy-staging",
        action = "deploy_infrastructure",
        workspace = "staging-environment",
        depends_on = ["test-dev"]
      },
      {
        name = "test-staging",
        action = "run_tests",
        workspace = "staging-environment",
        depends_on = ["deploy-staging"]
      },
      {
        name = "deploy-prod",
        action = "deploy_infrastructure",
        workspace = "prod-environment",
        depends_on = ["test-staging"],
        require_approval = true
      }
    ]
  }
}
```

## Workspace Settings

Configure workspace-specific settings:

```bash
# Set workspace owner
provisioning workspace set --owner "platform-team"

# Set workspace description
provisioning workspace set --description "Production infrastructure for main application"

# Set workspace tags
provisioning workspace set --tags "env=prod,tier=critical,compliance=pci-dss"

# Set require-approval for safety
provisioning workspace set --require-approval

# Set terraform backend (if using Terraform)
provisioning workspace set --backend-type s3 \
  --backend-config "bucket=provisioning-tfstate" \
  --backend-config "key=prod-environment"

# View workspace settings
provisioning workspace show
```

## Workspace Backup and Restore

Backup workspace state:

```bash
# Create workspace backup
provisioning workspace backup --output backups/dev-environment-2026-01-16.tar.gz

# List backups
ls -la backups/

# Restore from backup
provisioning workspace restore backups/dev-environment-2026-01-16.tar.gz
```

## Workspace Collaboration

Share workspace with team members:

```bash
# Grant read access to developer
provisioning workspace share \
  --workspace prod-environment \
  --user [john@example.com](mailto:john@example.com) \
  --role reader

# Grant write access to senior engineer
provisioning workspace share \
  --workspace prod-environment \
  --user [alice@example.com](mailto:alice@example.com) \
  --role writer

# Grant admin access to team lead
provisioning workspace share \
  --workspace prod-environment \
  --user [bob@example.com](mailto:bob@example.com) \
  --role admin

# View workspace permissions
provisioning workspace permissions prod-environment

# Revoke access
provisioning workspace unshare \
  --workspace prod-environment \
  --user [john@example.com](mailto:john@example.com)
```

## Workspace Environment Variables

Set workspace-specific environment:

```bash
# In dev workspace
provisioning workspace use dev-environment
export PROVISIONING_LOG_LEVEL=debug
export PROVISIONING_CONTEXT=development

# In prod workspace
provisioning workspace use prod-environment
export PROVISIONING_LOG_LEVEL=info
export PROVISIONING_CONTEXT=production
export PROVISIONING_REQUIRE_APPROVAL=true
```

## Workspace Migration

Move infrastructure between workspaces:

```bash
# Export infrastructure from old workspace
provisioning workspace use old-workspace
provisioning infra export --output infrastructure.json

# Create new workspace
provisioning workspace create new-workspace --from-export infrastructure.json

# Verify migration
provisioning workspace use new-workspace
provisioning infra validate

# Switch to new workspace
provisioning workspace use new-workspace
provisioning infra apply --dry-run
```

## Workspace Monitoring

Monitor activities across workspaces:

```bash
# View all workspace activities
provisioning workspace list --show-activity

# Audit workspace changes
provisioning workspace audit dev-environment --since 7d

# Get workspace statistics
provisioning workspace stats
# Output:
# Total workspaces: 3
# Active infrastructure: 45 resources
# Total cost: $12,450/month
```

## Workspace Templates

Create workspaces from templates:

```bash
# Create from built-in template
provisioning workspace create \
  --template "microservices-prod" \
  --name "api-service-prod"

# Use custom template
provisioning workspace create \
  --template ./templates/my-template.ncl \
  --name "custom-workspace"
```

Template example:

```nickel
# templates/microservices-prod.ncl
{
  description = "Production microservices infrastructure",
  owner = "platform-team",

  configuration = {
    environment = "production",
    database = {
      engine = "postgres",
      version = "15",
      multi_az = true,
      backup_retention = 30
    },
    servers = {
      count = 5,
      instance_type = "large"
    }
  },

  extensions = [
    "monitoring",
    "security",
    "backup"
  ]
}
```

## Team Workspace Organization

Enterprise workspace structure:

```text
workspaces/
├── platform/                      # Shared infrastructure
│   ├── core-services/
│   ├── networking/
│   └── storage/
├── applications/
│   ├── api-service-dev/
│   ├── api-service-staging/
│   ├── api-service-prod/
│   ├── web-app-dev/
│   ├── web-app-staging/
│   └── web-app-prod/
├── databases/
│   ├── main-db-staging/
│   └── main-db-prod/
└── security/
    ├── vault-prod/
    └── ca-services/
```

## Related Documentation

- [Workspace Management](../features/workspace-management.md)
- [Infrastructure Configuration](../infrastructure/configuration-system.md)
- [Multi-Cloud Deployment](./multi-cloud.md)
- [Batch Workflows](./batch-operations-example.md)
