blob: 25511f09676b84da6120b2efcd18265460d3728f [file] [log] [blame]
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package pubsub
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
)
func DataSourceGooglePubsubSubscription() *schema.Resource {
dsSchema := tpgresource.DatasourceSchemaFromResourceSchema(ResourcePubsubSubscription().Schema)
tpgresource.AddRequiredFieldsToSchema(dsSchema, "name")
tpgresource.AddOptionalFieldsToSchema(dsSchema, "project")
return &schema.Resource{
Read: dataSourceGooglePubsubSubscriptionRead,
Schema: dsSchema,
}
}
func dataSourceGooglePubsubSubscriptionRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*transport_tpg.Config)
id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/subscriptions/{{name}}")
if err != nil {
return fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)
err = resourcePubsubSubscriptionRead(d, meta)
if err != nil {
return err
}
if err := tpgresource.SetDataSourceLabels(d); err != nil {
return err
}
if d.Id() == "" {
return fmt.Errorf("%s not found", id)
}
return nil
}