|
| 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 | +} |
0 commit comments