Skip to content

Commit

Permalink
fix: implement missing validateStackComponent function
Browse files Browse the repository at this point in the history
  • Loading branch information
htahir1 committed Oct 27, 2024
1 parent 07bda2d commit f26ebc9
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 1 deletion.
58 changes: 58 additions & 0 deletions internal/provider/data_source_service_connector.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand Down
56 changes: 56 additions & 0 deletions internal/provider/data_source_stack_component.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand Down
12 changes: 11 additions & 1 deletion internal/provider/resource_stack_component.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit f26ebc9

Please sign in to comment.