Skip to content

Commit c9d389d

Browse files
authored
bigquery analyticshub listing subscription (#12820)
1 parent 7a23ebb commit c9d389d

File tree

5 files changed

+267
-0
lines changed

5 files changed

+267
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
# Copyright 2025 Google Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
---
15+
name: 'ListingSubscription'
16+
description: A Bigquery Analytics Hub listing subscription
17+
references:
18+
guides:
19+
'Official Documentation': 'https://cloud.google.com/bigquery/docs/analytics-hub-introduction'
20+
api: 'https://cloud.google.com/bigquery/docs/reference/analytics-hub/rest/v1/projects.locations.subscriptions'
21+
base_url: 'projects/{{project}}/locations/{{location}}/subscriptions'
22+
self_link: 'projects/{{project}}/locations/{{location}}/subscriptions/{{subscription_id}}'
23+
create_url: 'projects/{{project}}/locations/{{location}}/dataExchanges/{{data_exchange_id}}/listings/{{listing_id}}:subscribe'
24+
immutable: true
25+
import_format:
26+
- 'projects/{{project}}/locations/{{location}}/subscriptions/{{subscription_id}}'
27+
custom_code:
28+
post_create: templates/terraform/post_create/analytics_hub_subscription.go.tmpl
29+
post_import: templates/terraform/post_import/analytics_hub_subscription.go.tmpl
30+
examples:
31+
- name: 'bigquery_analyticshub_listing_subscription_basic'
32+
primary_resource_id: 'subscription'
33+
primary_resource_name: 'fmt.Sprintf(\"tf_test_my_data_exchange%s\",
34+
context[\"\
35+
random_suffix\"]), fmt.Sprintf(\"tf_test_my_listing%s\",
36+
context[\"random_suffix\"\
37+
])'
38+
region_override: 'US'
39+
vars:
40+
data_exchange_id: 'my_data_exchange'
41+
listing_id: 'my_listing'
42+
destination_dataset_id: 'destination_dataset'
43+
description: 'example data exchange'
44+
parameters:
45+
- name: 'dataExchangeId'
46+
type: String
47+
description: |-
48+
The ID of the data exchange. Must contain only Unicode letters, numbers (0-9), underscores (_). Should not use characters that require URL-escaping, or characters outside of ASCII, spaces.
49+
url_param_only: true
50+
required: true
51+
- name: 'listingId'
52+
type: String
53+
description: |-
54+
The ID of the listing. Must contain only Unicode letters, numbers (0-9), underscores (_). Should not use characters that require URL-escaping, or characters outside of ASCII, spaces.
55+
url_param_only: true
56+
required: true
57+
- name: 'location'
58+
type: String
59+
description: |
60+
The name of the location for this subscription.
61+
url_param_only: true
62+
required: true
63+
custom_flatten: 'templates/terraform/custom_flatten/bigquery_dataset_location.go.tmpl'
64+
diff_suppress_func: 'tpgresource.CaseDiffSuppress'
65+
properties:
66+
- name: 'destinationDataset'
67+
type: NestedObject
68+
required: true
69+
ignore_read: true
70+
description:
71+
The destination dataset for this subscription.
72+
properties:
73+
- name: 'location'
74+
type: String
75+
description: |
76+
The geographic location where the dataset should reside.
77+
See https://cloud.google.com/bigquery/docs/locations for supported locations.
78+
required: true
79+
custom_flatten: 'templates/terraform/custom_flatten/bigquery_dataset_location.go.tmpl'
80+
diff_suppress_func: 'tpgresource.CaseDiffSuppress'
81+
- name: 'datasetReference'
82+
type: NestedObject
83+
required: true
84+
description: A reference that identifies the destination dataset.
85+
properties:
86+
- name: 'datasetId'
87+
type: String
88+
description: A unique ID for this dataset, without the project name. The ID must contain only letters (a-z, A-Z), numbers (0-9), or underscores (_). The maximum length is 1,024 characters.
89+
required: true
90+
- name: 'projectId'
91+
type: String
92+
description: The ID of the project containing this dataset.
93+
required: true
94+
- name: 'friendlyName'
95+
type: String
96+
description: A descriptive name for the dataset.
97+
- name: 'description'
98+
type: String
99+
description: A user-friendly description of the dataset.
100+
- name: 'labels'
101+
type: KeyValuePairs
102+
description: |
103+
The labels associated with this dataset. You can use these to
104+
organize and group your datasets.
105+
- name: 'name'
106+
type: String
107+
description: |-
108+
The resource name of the subscription. e.g. "projects/myproject/locations/US/subscriptions/123"
109+
output: true
110+
- name: 'subscriptionId'
111+
type: String
112+
description: |-
113+
The subscription id used to reference the subscription.
114+
output: true
115+
ignore_read: true
116+
- name: 'creationTime'
117+
type: Time
118+
description: |-
119+
Timestamp when the subscription was created.
120+
output: true
121+
- name: 'lastModifyTime'
122+
type: Time
123+
description: |-
124+
Timestamp when the subscription was last modified.
125+
output: true
126+
- name: 'organizationId'
127+
type: String
128+
description: |-
129+
Organization of the project this subscription belongs to.
130+
output: true
131+
- name: 'organizationDisplayName'
132+
type: String
133+
description: |-
134+
Display name of the project of this subscription.
135+
output: true
136+
- name: 'state'
137+
type: String
138+
description: |-
139+
Current state of the subscription.
140+
output: true
141+
- name: 'subscriberContact'
142+
type: String
143+
description: |-
144+
Email of the subscriber.
145+
output: true
146+
- name: 'resourceType'
147+
type: String
148+
description: |-
149+
Listing shared asset type.
150+
output: true
151+
- name: 'linkedDatasetMap'
152+
output: true
153+
type: Map
154+
description: |-
155+
Output only. Map of listing resource names to associated linked resource,
156+
e.g. projects/123/locations/US/dataExchanges/456/listings/789 -> projects/123/datasets/my_dataset
157+
key_name: resource_name
158+
key_description: The associated linked resource
159+
value_type:
160+
name: linked_resource
161+
type: NestedObject
162+
properties:
163+
- name: listing
164+
type: string
165+
description: Output only. Listing for which linked resource is created.
166+
output: true
167+
- name: linkedDataset
168+
type: string
169+
description: Output only. Name of the linked dataset, e.g. projects/subscriberproject/datasets/linkedDataset
170+
output: true
171+
- name: linkedResources
172+
type: Array
173+
description: |
174+
Output only. Linked resources created in the subscription. Only contains values if state = STATE_ACTIVE.
175+
output: true
176+
item_type:
177+
type: NestedObject
178+
properties:
179+
- name: listing
180+
type: string
181+
description: Output only. Listing for which linked resource is created.
182+
output: true
183+
- name: linkedDataset
184+
type: string
185+
description: Output only. Name of the linked dataset, e.g. projects/subscriberproject/datasets/linkedDataset
186+
output: true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
resource "google_bigquery_analytics_hub_data_exchange" "{{$.PrimaryResourceId}}" {
2+
location = "US"
3+
data_exchange_id = "{{index $.Vars "data_exchange_id"}}"
4+
display_name = "{{index $.Vars "data_exchange_id"}}"
5+
description = "{{index $.Vars "desc"}}"
6+
}
7+
8+
resource "google_bigquery_analytics_hub_listing" "{{$.PrimaryResourceId}}" {
9+
location = "US"
10+
data_exchange_id = google_bigquery_analytics_hub_data_exchange.{{$.PrimaryResourceId}}.data_exchange_id
11+
listing_id = "{{index $.Vars "listing_id"}}"
12+
display_name = "{{index $.Vars "listing_id"}}"
13+
description = "{{index $.Vars "desc"}}"
14+
15+
bigquery_dataset {
16+
dataset = google_bigquery_dataset.{{$.PrimaryResourceId}}.id
17+
}
18+
}
19+
20+
resource "google_bigquery_dataset" "{{$.PrimaryResourceId}}" {
21+
dataset_id = "{{index $.Vars "listing_id"}}"
22+
friendly_name = "{{index $.Vars "listing_id"}}"
23+
description = "{{index $.Vars "desc"}}"
24+
location = "US"
25+
}
26+
27+
resource "google_bigquery_analytics_hub_listing_subscription" "{{$.PrimaryResourceId}}" {
28+
location = "US"
29+
data_exchange_id = google_bigquery_analytics_hub_data_exchange.{{$.PrimaryResourceId}}.data_exchange_id
30+
listing_id = google_bigquery_analytics_hub_listing.{{$.PrimaryResourceId}}.listing_id
31+
destination_dataset {
32+
description = "A test subscription"
33+
friendly_name = "👋"
34+
labels = {
35+
testing = "123"
36+
}
37+
location = "US"
38+
dataset_reference {
39+
dataset_id = "{{index $.Vars "destination_dataset_id"}}"
40+
project_id = google_bigquery_dataset.{{$.PrimaryResourceId}}.project
41+
}
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
subscription, ok := res["subscription"]
2+
if ok {
3+
name, nok := subscription.(map[string]interface{})["name"]
4+
if nok {
5+
parts := strings.Split(name.(string), "/")
6+
d.SetId(name.(string))
7+
d.Set("name", name.(string))
8+
d.Set("subscription_id", parts[5])
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
2+
if err != nil {
3+
return nil, err
4+
}
5+
6+
projectNumber := d.Get("project").(string)
7+
resourceManager := config.NewResourceManagerV3Client(userAgent)
8+
projectData, err := resourceManager.Projects.Get("projects/" + d.Get("project").(string)).Do()
9+
if err != nil {
10+
return nil, err
11+
}
12+
13+
d.Set("project", projectData.ProjectId)
14+
15+
id = fmt.Sprintf("projects/%s/locations/%s/subscriptions/%s",
16+
projectNumber,
17+
d.Get("location"),
18+
d.Get("subscription_id"))
19+
20+
d.SetId(id)
21+
d.Set("name", id)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// check if this is a listing subscription instead of a data exchange subscription
2+
if _, ok := d.GetOk("listing_id"); ok {
3+
url, err = tpgresource.ReplaceVars(d, config, "{{BigqueryAnalyticsHubBasePath}}projects/{{project}}/locations/{{location}}/dataExchanges/{{data_exchange_id}}/listings/{{listing_id}}:subscribe")
4+
if err != nil {
5+
return err
6+
}
7+
}

0 commit comments

Comments
 (0)