Skip to content

Commit 0cc4b3a

Browse files
wip
1 parent df00c22 commit 0cc4b3a

File tree

9 files changed

+550
-0
lines changed

9 files changed

+550
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package resourceshowoutputassert
2+
3+
import (
4+
"testing"
5+
6+
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/assert"
7+
)
8+
9+
// ListingsDatasourceShowOutput is a temporary workaround to have better show output assertions in data source acceptance tests.
10+
func ListingsDatasourceShowOutput(t *testing.T, name string) *ListingShowOutputAssert {
11+
t.Helper()
12+
13+
l := ListingShowOutputAssert{
14+
ResourceAssert: assert.NewDatasourceAssert("data."+name, "show_output", "listings.0."),
15+
}
16+
l.AddAssertion(assert.ValueSet("show_output.#", "1"))
17+
return &l
18+
}

pkg/acceptance/bettertestspoc/config/datasourcemodel/gen/datasource_schema_def.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ var allDatasourcesSchemaDefs = []DatasourceSchemaDef{
110110
name: "Services",
111111
schema: datasources.Services().Schema,
112112
},
113+
{
114+
name: "Listings",
115+
schema: datasources.Listings().Schema,
116+
},
113117
{
114118
name: "Streamlits",
115119
schema: datasources.Streamlits().Schema,
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package datasourcemodel
2+
3+
import (
4+
tfconfig "github.com/hashicorp/terraform-plugin-testing/config"
5+
)
6+
7+
func (l *ListingsModel) WithLimit(rows int) *ListingsModel {
8+
return l.WithLimitValue(
9+
tfconfig.ObjectVariable(map[string]tfconfig.Variable{
10+
"rows": tfconfig.IntegerVariable(rows),
11+
}),
12+
)
13+
}
14+
15+
func (l *ListingsModel) WithRowsAndFrom(rows int, from string) *ListingsModel {
16+
return l.WithLimitValue(
17+
tfconfig.ObjectVariable(map[string]tfconfig.Variable{
18+
"rows": tfconfig.IntegerVariable(rows),
19+
"from": tfconfig.StringVariable(from),
20+
}),
21+
)
22+
}
23+
24+

pkg/acceptance/bettertestspoc/config/datasourcemodel/listings_model_gen.go

Lines changed: 111 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/datasources/listings.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package datasources
2+
3+
import (
4+
"context"
5+
6+
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider"
7+
providerdatasources "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources"
8+
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources"
9+
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/schemas"
10+
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
13+
)
14+
15+
var listingsSchema = map[string]*schema.Schema{
16+
"with_describe": {
17+
Type: schema.TypeBool,
18+
Optional: true,
19+
Default: true,
20+
Description: "Runs DESC LISTING for each listing returned by SHOW LISTINGS. The output of describe is saved to the description field. By default this value is set to true.",
21+
},
22+
"like": likeSchema,
23+
"starts_with": startsWithSchema,
24+
"limit": limitFromSchema,
25+
"listings": {
26+
Type: schema.TypeList,
27+
Computed: true,
28+
Description: "Holds the aggregated output of all listings details queries.",
29+
Elem: &schema.Resource{
30+
Schema: map[string]*schema.Schema{
31+
resources.ShowOutputAttributeName: {
32+
Type: schema.TypeList,
33+
Computed: true,
34+
Description: "Holds the output of SHOW LISTINGS.",
35+
Elem: &schema.Resource{Schema: schemas.ShowListingSchema},
36+
},
37+
resources.DescribeOutputAttributeName: {
38+
Type: schema.TypeList,
39+
Computed: true,
40+
Description: "Holds the output of DESCRIBE LISTING.",
41+
Elem: &schema.Resource{Schema: schemas.DescribeListingSchema},
42+
},
43+
},
44+
},
45+
},
46+
}
47+
48+
func Listings() *schema.Resource {
49+
return &schema.Resource{
50+
ReadContext: TrackingReadWrapper(providerdatasources.Listings, ReadListings),
51+
Schema: listingsSchema,
52+
Description: "Data source used to get details of filtered listings. Filtering is aligned with the current possibilities for SHOW LISTINGS query (`like`, `starts_with`, and `limit` are supported). The results of SHOW and DESCRIBE are encapsulated in one output collection.",
53+
}
54+
}
55+
56+
func ReadListings(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
57+
client := meta.(*provider.Context).Client
58+
req := sdk.NewShowListingRequest()
59+
60+
if v, ok := d.GetOk("like"); ok {
61+
req.WithLike(sdk.Like{Pattern: sdk.String(v.(string))})
62+
}
63+
if v, ok := d.GetOk("starts_with"); ok {
64+
req.WithStartsWith(v.(string))
65+
}
66+
if v, ok := d.GetOk("limit"); ok {
67+
l := v.([]any)[0].(map[string]any)
68+
limit := sdk.LimitFrom{}
69+
if rv, ok := l["rows"]; ok {
70+
limit.Rows = sdk.Int(rv.(int))
71+
}
72+
if fv, ok := l["from"]; ok {
73+
limit.From = sdk.String(fv.(string))
74+
}
75+
req.WithLimit(limit)
76+
}
77+
78+
listings, err := client.Listings.Show(ctx, req)
79+
if err != nil {
80+
return diag.FromErr(err)
81+
}
82+
d.SetId("listings_read")
83+
84+
flattened := make([]map[string]any, len(listings))
85+
for i, listing := range listings {
86+
listing := listing
87+
var describe []map[string]any
88+
if d.Get("with_describe").(bool) {
89+
details, err := client.Listings.Describe(ctx, sdk.NewDescribeListingRequest(listing.ID()))
90+
if err != nil {
91+
return diag.FromErr(err)
92+
}
93+
describe = []map[string]any{schemas.ListingDetailsToSchema(details)}
94+
}
95+
flattened[i] = map[string]any{
96+
resources.ShowOutputAttributeName: []map[string]any{schemas.ListingToSchema(&listing)},
97+
resources.DescribeOutputAttributeName: describe,
98+
}
99+
}
100+
if err := d.Set("listings", flattened); err != nil {
101+
return diag.FromErr(err)
102+
}
103+
return nil
104+
}

pkg/provider/datasources/datasources.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const (
3838
SecurityIntegrations datasource = "snowflake_security_integrations"
3939
SemanticViews datasource = "snowflake_semantic_views"
4040
Services datasource = "snowflake_services"
41+
Listings datasource = "snowflake_listings"
4142
Sequences datasource = "snowflake_sequences"
4243
Shares datasource = "snowflake_shares"
4344
Stages datasource = "snowflake_stages"

pkg/provider/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,7 @@ func getDataSources() map[string]*schema.Resource {
618618
"snowflake_secrets": datasources.Secrets(),
619619
"snowflake_security_integrations": datasources.SecurityIntegrations(),
620620
"snowflake_services": datasources.Services(),
621+
"snowflake_listings": datasources.Listings(),
621622
"snowflake_sequences": datasources.Sequences(),
622623
"snowflake_shares": datasources.Shares(),
623624
"snowflake_stages": datasources.Stages(),

0 commit comments

Comments
 (0)