Skip to content

Commit e4b7c95

Browse files
SarahFrenchgontech
authored andcommitted
Add missing test for data.google_firebase_web_app_config, clean up plugin-framework Firebase code a bit (GoogleCloudPlatform#11927)
1 parent 5f10b36 commit e4b7c95

4 files changed

+87
-28
lines changed

mmv1/third_party/terraform/services/firebase/data_source_google_firebase_android_app_config.go.tmpl

+11-7
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ func (d *GoogleFirebaseAndroidAppConfigDataSource) Schema(ctx context.Context, r
7575
Computed: true,
7676
},
7777

78+
// This is included for backwards compatibility with the original, SDK-implemented data source.
7879
"id": schema.StringAttribute{
7980
Description: "Firebase Android App Config identifier",
8081
MarkdownDescription: "Firebase Android App Config identifier",
@@ -116,25 +117,26 @@ func (d *GoogleFirebaseAndroidAppConfigDataSource) Read(ctx context.Context, req
116117
return
117118
}
118119

119-
d.client.UserAgent = fwtransport.GenerateFrameworkUserAgentString(metaData, d.client.UserAgent)
120-
121-
client := firebase.NewProjectsAndroidAppsService(d.client)
122-
123120
// Read Terraform configuration data into the model
124121
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
125122
if resp.Diagnostics.HasError() {
126123
return
127124
}
128125

126+
// Use provider_meta to set User-Agent
127+
d.client.UserAgent = fwtransport.GenerateFrameworkUserAgentString(metaData, d.client.UserAgent)
128+
129129
data.Project = fwresource.GetProjectFramework(data.Project, d.project, &resp.Diagnostics)
130130
if resp.Diagnostics.HasError() {
131131
return
132132
}
133133

134-
appName := fmt.Sprintf("projects/%s/androidApps/%s/config", data.Project.ValueString(), data.AppId.ValueString())
135-
data.Id = types.StringValue(appName)
136134

137-
clientResp, err := client.GetConfig(appName).Do()
135+
136+
// GET Request
137+
service := firebase.NewProjectsAndroidAppsService(d.client)
138+
appName := fmt.Sprintf("projects/%s/androidApps/%s/config", data.Project.ValueString(), data.AppId.ValueString())
139+
clientResp, err := service.GetConfig(appName).Do()
138140
if err != nil {
139141
fwtransport.HandleDatasourceNotFoundError(ctx, err, &resp.State, fmt.Sprintf("dataSourceFirebaseAndroidAppConfig %q", data.AppId.ValueString()), &resp.Diagnostics)
140142
if resp.Diagnostics.HasError() {
@@ -144,6 +146,8 @@ func (d *GoogleFirebaseAndroidAppConfigDataSource) Read(ctx context.Context, req
144146

145147
tflog.Trace(ctx, "read firebase android app config data source")
146148

149+
// Put data in model
150+
data.Id = types.StringValue(appName)
147151
data.ConfigFilename = types.StringValue(clientResp.ConfigFilename)
148152
data.ConfigFileContents = types.StringValue(clientResp.ConfigFileContents)
149153

mmv1/third_party/terraform/services/firebase/data_source_google_firebase_apple_app_config.go.tmpl

+10-8
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ func (d *GoogleFirebaseAppleAppConfigDataSource) Schema(ctx context.Context, req
7575
Computed: true,
7676
},
7777

78+
// This is included for backwards compatibility with the original, SDK-implemented data source.
7879
"id": schema.StringAttribute{
7980
Description: "Firebase Apple App Config identifier",
8081
MarkdownDescription: "Firebase Apple App Config identifier",
@@ -116,25 +117,24 @@ func (d *GoogleFirebaseAppleAppConfigDataSource) Read(ctx context.Context, req d
116117
return
117118
}
118119

119-
d.client.UserAgent = fwtransport.GenerateFrameworkUserAgentString(metaData, d.client.UserAgent)
120-
121-
client := firebase.NewProjectsIosAppsService(d.client)
122-
123120
// Read Terraform configuration data into the model
124121
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
125122
if resp.Diagnostics.HasError() {
126123
return
127124
}
128125

126+
// Use provider_meta to set User-Agent
127+
d.client.UserAgent = fwtransport.GenerateFrameworkUserAgentString(metaData, d.client.UserAgent)
128+
129129
data.Project = fwresource.GetProjectFramework(data.Project, d.project, &resp.Diagnostics)
130130
if resp.Diagnostics.HasError() {
131131
return
132132
}
133133

134+
// GET Request
135+
service := firebase.NewProjectsIosAppsService(d.client)
134136
appName := fmt.Sprintf("projects/%s/iosApps/%s/config", data.Project.ValueString(), data.AppId.ValueString())
135-
data.Id = types.StringValue(appName)
136-
137-
clientResp, err := client.GetConfig(appName).Do()
137+
clientResp, err := service.GetConfig(appName).Do()
138138
if err != nil {
139139
fwtransport.HandleDatasourceNotFoundError(ctx, err, &resp.State, fmt.Sprintf("dataSourceFirebaseAppleAppConfig %q", data.AppId.ValueString()), &resp.Diagnostics)
140140
if resp.Diagnostics.HasError() {
@@ -144,10 +144,12 @@ func (d *GoogleFirebaseAppleAppConfigDataSource) Read(ctx context.Context, req d
144144

145145
tflog.Trace(ctx, "read firebase apple app config data source")
146146

147+
// Put data in model
148+
data.Id = types.StringValue(appName)
147149
data.ConfigFilename = types.StringValue(clientResp.ConfigFilename)
148150
data.ConfigFileContents = types.StringValue(clientResp.ConfigFileContents)
149151

150152
// Save data into Terraform state
151153
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
152154
}
153-
{{- end }}
155+
{{- end }}

mmv1/third_party/terraform/services/firebase/data_source_google_firebase_web_app_config.go.tmpl

+15-13
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,17 @@ func (d *GoogleFirebaseWebAppConfigDataSource) Schema(ctx context.Context, req d
8787
},
8888

8989
"location_id": schema.StringAttribute{
90-
Description: "The ID of the project's default GCP resource location. The location is one of the available GCP resource locations. " +
91-
"This field is omitted if the default GCP resource location has not been finalized yet. To set your project's " +
90+
Description: "The ID of the project's default GCP resource location. The location is one of the available GCP resource locations. " +
91+
"This field is omitted if the default GCP resource location has not been finalized yet. To set your project's " +
9292
"default GCP resource location, call defaultLocation.finalize after you add Firebase services to your project.",
9393
MarkdownDescription: "The ID of the project's default GCP resource location. The location is one of the available GCP resource locations. " +
94-
"This field is omitted if the default GCP resource location has not been finalized yet. To set your project's " +
94+
"This field is omitted if the default GCP resource location has not been finalized yet. To set your project's " +
9595
"default GCP resource location, call defaultLocation.finalize after you add Firebase services to your project.",
96-
Computed: true,
96+
Computed: true,
9797
},
9898

9999
"measurement_id": schema.StringAttribute{
100-
Description: "The unique Google-assigned identifier of the Google Analytics web stream associated with the Firebase Web App. " +
100+
Description: "The unique Google-assigned identifier of the Google Analytics web stream associated with the Firebase Web App. " +
101101
"Firebase SDKs use this ID to interact with Google Analytics APIs. " +
102102
"This field is only present if the App is linked to a web stream in a Google Analytics App + Web property. " +
103103
"Learn more about this ID and Google Analytics web streams in the Analytics documentation. " +
@@ -107,7 +107,7 @@ func (d *GoogleFirebaseWebAppConfigDataSource) Schema(ctx context.Context, req d
107107
"This field is only present if the App is linked to a web stream in a Google Analytics App + Web property. " +
108108
"Learn more about this ID and Google Analytics web streams in the Analytics documentation. " +
109109
"To generate a measurementId and link the Web App with a Google Analytics web stream, call projects.addGoogleAnalytics.",
110-
Computed: true,
110+
Computed: true,
111111
},
112112

113113
"messaging_sender_id": schema.StringAttribute{
@@ -122,6 +122,7 @@ func (d *GoogleFirebaseWebAppConfigDataSource) Schema(ctx context.Context, req d
122122
Computed: true,
123123
},
124124

125+
// This is included for backwards compatibility with the original, SDK-implemented data source.
125126
"id": schema.StringAttribute{
126127
Description: "Firebase Web App Config identifier",
127128
MarkdownDescription: "Firebase Web App Config identifier",
@@ -163,25 +164,24 @@ func (d *GoogleFirebaseWebAppConfigDataSource) Read(ctx context.Context, req dat
163164
return
164165
}
165166

166-
d.client.UserAgent = fwtransport.GenerateFrameworkUserAgentString(metaData, d.client.UserAgent)
167-
168-
client := firebase.NewProjectsWebAppsService(d.client)
169-
170167
// Read Terraform configuration data into the model
171168
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
172169
if resp.Diagnostics.HasError() {
173170
return
174171
}
175172

173+
// Use provider_meta to set User-Agent
174+
d.client.UserAgent = fwtransport.GenerateFrameworkUserAgentString(metaData, d.client.UserAgent)
175+
176176
data.Project = fwresource.GetProjectFramework(data.Project, d.project, &resp.Diagnostics)
177177
if resp.Diagnostics.HasError() {
178178
return
179179
}
180180

181+
// GET Request
182+
service := firebase.NewProjectsWebAppsService(d.client)
181183
appName := fmt.Sprintf("projects/%s/webApps/%s/config", data.Project.ValueString(), data.WebAppId.ValueString())
182-
data.Id = data.WebAppId
183-
184-
clientResp, err := client.GetConfig(appName).Do()
184+
clientResp, err := service.GetConfig(appName).Do()
185185
if err != nil {
186186
fwtransport.HandleDatasourceNotFoundError(ctx, err, &resp.State, fmt.Sprintf("dataSourceFirebaseWebAppConfig %q", data.WebAppId.ValueString()), &resp.Diagnostics)
187187
if resp.Diagnostics.HasError() {
@@ -191,6 +191,8 @@ func (d *GoogleFirebaseWebAppConfigDataSource) Read(ctx context.Context, req dat
191191

192192
tflog.Trace(ctx, "read firebase web app config data source")
193193

194+
// Put data in model
195+
data.Id = data.WebAppId
194196
data.ApiKey = types.StringValue(clientResp.ApiKey)
195197
data.AuthDomain = types.StringValue(clientResp.AuthDomain)
196198
data.DatabaseUrl = types.StringValue(clientResp.DatabaseURL)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package firebase_test
2+
{{- if ne $.TargetVersionName "ga" }}
3+
4+
import (
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
8+
"github.com/hashicorp/terraform-provider-google/google/acctest"
9+
"github.com/hashicorp/terraform-provider-google/google/envvar"
10+
)
11+
12+
func TestAccDataSourceGoogleFirebaseWebAppConfig(t *testing.T) {
13+
// TODO: https://github.com/hashicorp/terraform-provider-google/issues/14158
14+
acctest.SkipIfVcr(t)
15+
t.Parallel()
16+
context := map[string]interface{}{
17+
"project_id": envvar.GetTestProjectFromEnv(),
18+
"display_name": "tf_test Display Name WebApp DataSource",
19+
}
20+
21+
acctest.VcrTest(t, resource.TestCase{
22+
PreCheck: func() { acctest.AccTestPreCheck(t) },
23+
CheckDestroy: testAccCheckFirebaseWebAppDestroyProducer(t),
24+
Steps: []resource.TestStep{
25+
{
26+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
27+
Config: testAccDataSourceGoogleFirebaseWebAppConfig(context),
28+
Check: resource.ComposeTestCheckFunc(
29+
resource.TestCheckResourceAttrSet("data.google_firebase_web_app_config.my_app_config", "api_key"),
30+
resource.TestCheckResourceAttrSet("data.google_firebase_web_app_config.my_app_config", "auth_domain"),
31+
resource.TestCheckResourceAttrSet("data.google_firebase_web_app_config.my_app_config", "database_url"),
32+
resource.TestCheckResourceAttrSet("data.google_firebase_web_app_config.my_app_config", "storage_bucket"),
33+
),
34+
},
35+
},
36+
})
37+
}
38+
39+
func testAccDataSourceGoogleFirebaseWebAppConfig(context map[string]interface{}) string {
40+
return acctest.Nprintf(`
41+
resource "google_firebase_web_app" "my_app" {
42+
project = "%{project_id}"
43+
display_name = "%{display_name}"
44+
}
45+
46+
data "google_firebase_web_app_config" "my_app_config" {
47+
web_app_id = google_firebase_web_app.my_app.app_id
48+
}
49+
`, context)
50+
}
51+
{{- end }}

0 commit comments

Comments
 (0)