diff --git a/internal/provider/data_source_service_connector.go b/internal/provider/data_source_service_connector.go index 52df996..4616642 100644 --- a/internal/provider/data_source_service_connector.go +++ b/internal/provider/data_source_service_connector.go @@ -1,5 +1,63 @@ package provider +import ( + "fmt" + "context" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func dataSourceServiceConnectorRead(d *schema.ResourceData, m interface{}) error { + client := m.(*Client) + + // Try to find by ID first + if id, ok := d.GetOk("id"); ok { + connector, err := client.GetServiceConnector(id.(string)) + if err != nil { + return fmt.Errorf("error reading service connector: %v", err) + } + d.SetId(connector.ID) + return setServiceConnectorFields(d, connector) + } + + // Try to find by name and workspace + name, hasName := d.GetOk("name") + workspace, hasWorkspace := d.GetOk("workspace") + + if !hasName { + return fmt.Errorf("either id or name must be specified") + } + + var workspaceStr string + if hasWorkspace { + workspaceStr = workspace.(string) + } + + connector, err := client.GetServiceConnectorByName(name.(string), workspaceStr) + if err != nil { + return fmt.Errorf("error reading service connector: %v", err) + } + + d.SetId(connector.ID) + return setServiceConnectorFields(d, connector) +} + +func setServiceConnectorFields(d *schema.ResourceData, connector *ServiceConnectorResponse) error { + d.Set("name", connector.Name) + d.Set("type", connector.Type) + d.Set("auth_method", connector.AuthMethod) + + if connector.Body != nil { + if connector.Body.ResourceTypes != nil { + d.Set("resource_types", connector.Body.ResourceTypes) + } + if connector.Body.Workspace != "" { + d.Set("workspace", connector.Body.Workspace) + } + } + + return nil +} + import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) diff --git a/internal/provider/data_source_stack_component.go b/internal/provider/data_source_stack_component.go index c414ad5..fcd89b2 100644 --- a/internal/provider/data_source_stack_component.go +++ b/internal/provider/data_source_stack_component.go @@ -1,5 +1,61 @@ package provider +import ( + "fmt" + "context" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func dataSourceStackComponentRead(d *schema.ResourceData, m interface{}) error { + client := m.(*Client) + + // Try to find by ID first + if id, ok := d.GetOk("id"); ok { + component, err := client.GetComponent(id.(string)) + if err != nil { + return fmt.Errorf("error reading stack component: %v", err) + } + d.SetId(component.ID) + return setStackComponentFields(d, component) + } + + // Try to find by name and workspace + name, hasName := d.GetOk("name") + workspace, hasWorkspace := d.GetOk("workspace") + + if !hasName { + return fmt.Errorf("either id or name must be specified") + } + + var workspaceStr string + if hasWorkspace { + workspaceStr = workspace.(string) + } + + component, err := client.GetComponentByName(name.(string), workspaceStr) + if err != nil { + return fmt.Errorf("error reading stack component: %v", err) + } + + d.SetId(component.ID) + return setStackComponentFields(d, component) +} + +func setStackComponentFields(d *schema.ResourceData, component *ComponentResponse) error { + d.Set("name", component.Name) + d.Set("type", component.Type) + d.Set("flavor", component.Flavor) + + if component.Body != nil { + d.Set("configuration", component.Body.Configuration) + if component.Body.Workspace != "" { + d.Set("workspace", component.Body.Workspace) + } + } + + return nil +} + import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) diff --git a/internal/provider/resource_stack_component.go b/internal/provider/resource_stack_component.go index 83b6ac0..9008239 100644 --- a/internal/provider/resource_stack_component.go +++ b/internal/provider/resource_stack_component.go @@ -13,7 +13,17 @@ func resourceStackComponent() *schema.Resource { Update: resourceStackComponentUpdate, Delete: resourceStackComponentDelete, - CustomizeDiff: validateStackComponent, + CustomizeDiff: func(ctx context.Context, d *schema.ResourceDiff, m interface{}) error { + // Validate that if connector is set, connector_resource_id should also be set + connector, hasConnector := d.GetOk("connector") + connectorResourceID, hasConnectorResourceID := d.GetOk("connector_resource_id") + + if hasConnector && connector.(string) != "" && (!hasConnectorResourceID || connectorResourceID.(string) == "") { + return fmt.Errorf("connector_resource_id must be set when connector is specified") + } + + return nil + }, Schema: map[string]*schema.Schema{ "name": {