Skip to content

Commit

Permalink
Add default workspace handling for resources
Browse files Browse the repository at this point in the history
  • Loading branch information
htahir1 committed Oct 28, 2024
1 parent fbe08f1 commit b802920
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 19 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ resource "zenml_service_connector" "gcp" {
name = "gcp-connector"
type = "gcp"
auth_method = "service-account"
# workspace defaults to "default" if not specified
resource_types = [
"artifact-store",
Expand All @@ -95,6 +96,7 @@ resource "zenml_stack_component" "artifact_store" {
name = "gcs-store"
type = "artifact_store"
flavor = "gcp"
# workspace defaults to "default" if not specified
configuration = {
path = "gs://my-bucket/artifacts"
Expand All @@ -110,6 +112,7 @@ resource "zenml_stack_component" "artifact_store" {
# Create a stack using the components
resource "zenml_stack" "ml_stack" {
name = "production-stack"
# workspace defaults to "default" if not specified
components = {
artifact_store = zenml_stack_component.artifact_store.id
Expand All @@ -121,6 +124,8 @@ resource "zenml_stack" "ml_stack" {
}
```

> **Note:** All resources support an optional `workspace` parameter that defaults to "default" if not specified. You can override this by setting `workspace = "your-workspace-name"` in any resource.
See the [examples](./examples/) directory for more complete examples.

## Development
Expand Down
1 change: 0 additions & 1 deletion docs/guides/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ Here's a simple example creating a stack with a component:
```hcl
# Create a stack component
resource "zenml_stack_component" "artifact_store" {
workspace = "default"
name = "my-artifact-store"
type = "artifact_store"
flavor = "local"
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/service_connector.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ resource "zenml_service_connector" "gcp_connector" {
* Azure: `service-principal`, `managed-identity`
* Kubernetes: `kubeconfig`, `service-account`
* `user` - (Required, Forces new resource) The ID of the user who owns this connector.
* `workspace` - (Required, Forces new resource) The ID of the workspace this connector belongs to.
* `workspace` - (Optional) The workspace this connector belongs to. Defaults to "default". Forces new resource if changed.
* `resource_types` - (Optional) A list of resource types this connector can be used for (e.g., `artifact-store`, `container-registry`, `orchestrator`).
* `configuration` - (Required, Sensitive) A map of configuration key-value pairs for the connector.
* `secrets` - (Optional, Sensitive) A map of secret key-value pairs for the connector.
Expand Down
3 changes: 2 additions & 1 deletion docs/resources/stack.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ resource "zenml_stack" "my_stack" {
* `feature_store`
* `image_builder`
* `labels` - (Optional) A map of labels to associate with the stack.
* `workspace` - (Optional) The workspace to create the stack in. Defaults to "default". Forces new resource if changed.

-> **Note** The stack will be created in the default workspace. Future versions may allow workspace configuration.
-> **Note** If no workspace is specified, the stack will be created in the "default" workspace.

## Attributes Reference

Expand Down
5 changes: 3 additions & 2 deletions internal/provider/data_source_service_connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ func dataSourceServiceConnector() *schema.Resource {
ReadContext: dataSourceServiceConnectorRead,
Schema: map[string]*schema.Schema{
"workspace": {
Description: "Name of the workspace",
Description: "Name of the workspace (defaults to 'default')",
Type: schema.TypeString,
Required: true,
Optional: true,
Default: "default",
},
"name": {
Description: "Name of the service connector",
Expand Down
5 changes: 3 additions & 2 deletions internal/provider/data_source_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ func dataSourceStack() *schema.Resource {
ReadContext: dataSourceStackRead,
Schema: map[string]*schema.Schema{
"workspace": {
Description: "Name of the workspace",
Description: "Name of the workspace (defaults to 'default')",
Type: schema.TypeString,
Required: true,
Optional: true,
Default: "default",
},
"name": {
Description: "Name of the stack",
Expand Down
5 changes: 3 additions & 2 deletions internal/provider/data_source_stack_component.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ func dataSourceStackComponent() *schema.Resource {
ReadContext: dataSourceStackComponentRead,
Schema: map[string]*schema.Schema{
"workspace": {
Description: "Name of the workspace",
Description: "Name of the workspace (defaults to 'default')",
Type: schema.TypeString,
Required: true,
Optional: true,
Default: "default",
},
"name": {
Description: "Name of the stack component",
Expand Down
3 changes: 2 additions & 1 deletion internal/provider/resource_service_connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ func resourceServiceConnector() *schema.Resource {
},
"workspace": {
Type: schema.TypeString,
Required: true,
Optional: true,
Default: "default",
ForceNew: true,
},
"labels": {
Expand Down
22 changes: 14 additions & 8 deletions internal/provider/resource_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ func resourceStack() *schema.Resource {
Delete: resourceStackDelete,

Schema: map[string]*schema.Schema{
"workspace": {
Type: schema.TypeString,
Optional: true,
Default: "default",
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -68,21 +74,21 @@ func resourceStack() *schema.Resource {
func resourceStackCreate(d *schema.ResourceData, m interface{}) error {
client := m.(*Client)

// Get the workspace from configuration or use a default
workspace := "default" // You may want to make this configurable
// Get the workspace from schema instead of hardcoding
workspace := d.Get("workspace").(string)

stack := StackRequest{
Name: d.Get("name").(string),
}

// Handle components
if v, ok := d.GetOk("components"); ok {
components := make(map[string][]string)
for k, v := range v.(map[string]interface{}) {
// Convert single ID to array of IDs since API expects array
components[k] = []string{v.(string)}
}
stack.Components = components
components := make(map[string][]string)
for k, v := range v.(map[string]interface{}) {
// Convert single ID to array of IDs since API expects array
components[k] = []string{v.(string)}
}
stack.Components = components
}

resp, err := client.CreateStack(workspace, stack)
Expand Down
3 changes: 2 additions & 1 deletion internal/provider/resource_stack_component.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ func resourceStackComponent() *schema.Resource {
Schema: map[string]*schema.Schema{
"workspace": {
Type: schema.TypeString,
Required: true,
Optional: true,
Default: "default",
ForceNew: true,
},
"name": {
Expand Down
Binary file modified terraform-provider-zenml
Binary file not shown.

0 comments on commit b802920

Please sign in to comment.