Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: reading warehouse with underscore in name #1793

Merged
merged 2 commits into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions pkg/resources/warehouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,17 +237,11 @@ func ReadWarehouse(d *schema.ResourceData, meta interface{}) error {

id := helpers.DecodeSnowflakeID(d.Id()).(sdk.AccountObjectIdentifier)

warehouses, err := client.Warehouses.Show(ctx, &sdk.WarehouseShowOptions{
Like: &sdk.Like{
Pattern: sdk.String(id.Name()),
},
})
w, err := client.Warehouses.ShowById(ctx, id)
if err != nil {
return err
}

w := warehouses[0]

if err = d.Set("name", w.Name); err != nil {
return err
}
Expand Down
34 changes: 34 additions & 0 deletions pkg/resources/warehouse_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,28 @@ func TestAcc_Warehouse(t *testing.T) {
})
}

func TestAcc_WarehousePattern(t *testing.T) {
if _, ok := os.LookupEnv("SKIP_WAREHOUSE_TESTS"); ok {
t.Skip("Skipping TestAccWarehouse")
}

prefix := "tst-terraform" + strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))

resource.ParallelTest(t, resource.TestCase{
Providers: providers(),
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Config: wConfigPattern(prefix),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("snowflake_warehouse.w1", "name", fmt.Sprintf("%s_", prefix)),
resource.TestCheckResourceAttr("snowflake_warehouse.w2", "name", fmt.Sprintf("%s1", prefix)),
),
},
},
})
}

func wConfig(prefix string) string {
s := `
resource "snowflake_warehouse" "w" {
Expand Down Expand Up @@ -106,3 +128,15 @@ resource "snowflake_warehouse" "w" {
`
return fmt.Sprintf(s, prefix)
}

func wConfigPattern(prefix string) string {
s := `
resource "snowflake_warehouse" "w1" {
name = "%s_"
}
resource "snowflake_warehouse" "w2" {
name = "%s1"
}
`
return fmt.Sprintf(s, prefix, prefix)
}
20 changes: 20 additions & 0 deletions pkg/sdk/warehouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type Warehouses interface {
Drop(ctx context.Context, id AccountObjectIdentifier, opts *WarehouseDropOptions) error
// Show returns a list of warehouses.
Show(ctx context.Context, opts *WarehouseShowOptions) ([]*Warehouse, error)
// Show + filter to return SHOW data for a single warehouse.
ShowById(ctx context.Context, id ObjectIdentifier) (*Warehouse, error)
// Describe returns the details of a warehouse.
Describe(ctx context.Context, id AccountObjectIdentifier) (*WarehouseDetails, error)
}
Expand Down Expand Up @@ -416,6 +418,24 @@ func (c *warehouses) Show(ctx context.Context, opts *WarehouseShowOptions) ([]*W
return resultList, nil
}

func (c *warehouses) ShowById(ctx context.Context, id ObjectIdentifier) (*Warehouse, error) {
results, err := c.Show(ctx, &WarehouseShowOptions{
Like: &Like{
Pattern: String(id.Name()),
},
})
if err != nil {
return nil, err
}

for _, res := range results {
if res.ID().name == id.Name() {
return res, nil
}
}
return nil, ErrObjectNotExistOrAuthorized
}

type warehouseDescribeOptions struct {
describe bool `ddl:"static" db:"DESCRIBE"` //lint:ignore U1000 This is used in the ddl tag
warehouse bool `ddl:"static" db:"WAREHOUSE"` //lint:ignore U1000 This is used in the ddl tag
Expand Down