Skip to content

Commit 6e204c3

Browse files
Adding initial support for GCF Gen2 terraform integration (#5670) (#4093)
Signed-off-by: Modular Magician <[email protected]>
1 parent eeb05bf commit 6e204c3

12 files changed

+2594
-2
lines changed

.changelog/5670.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-resource
2+
`google_cloudfunctions2_function`
3+
```
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// ----------------------------------------------------------------------------
2+
//
3+
// *** AUTO GENERATED CODE *** Type: MMv1 ***
4+
//
5+
// ----------------------------------------------------------------------------
6+
//
7+
// This file is automatically generated by Magic Modules and manual
8+
// changes will be clobbered when the file is regenerated.
9+
//
10+
// Please read more about how to change this file in
11+
// .github/CONTRIBUTING.md.
12+
//
13+
// ----------------------------------------------------------------------------
14+
package google
15+
16+
import (
17+
"encoding/json"
18+
"fmt"
19+
"time"
20+
)
21+
22+
type Cloudfunctions2OperationWaiter struct {
23+
Config *Config
24+
UserAgent string
25+
Project string
26+
CommonOperationWaiter
27+
}
28+
29+
func (w *Cloudfunctions2OperationWaiter) QueryOp() (interface{}, error) {
30+
if w == nil {
31+
return nil, fmt.Errorf("Cannot query operation, it's unset or nil.")
32+
}
33+
// Returns the proper get.
34+
url := fmt.Sprintf("%s%s", w.Config.Cloudfunctions2BasePath, w.CommonOperationWaiter.Op.Name)
35+
36+
return sendRequest(w.Config, "GET", w.Project, url, w.UserAgent, nil)
37+
}
38+
39+
func createCloudfunctions2Waiter(config *Config, op map[string]interface{}, project, activity, userAgent string) (*Cloudfunctions2OperationWaiter, error) {
40+
w := &Cloudfunctions2OperationWaiter{
41+
Config: config,
42+
UserAgent: userAgent,
43+
Project: project,
44+
}
45+
if err := w.CommonOperationWaiter.SetOp(op); err != nil {
46+
return nil, err
47+
}
48+
return w, nil
49+
}
50+
51+
// nolint: deadcode,unused
52+
func cloudfunctions2OperationWaitTimeWithResponse(config *Config, op map[string]interface{}, response *map[string]interface{}, project, activity, userAgent string, timeout time.Duration) error {
53+
w, err := createCloudfunctions2Waiter(config, op, project, activity, userAgent)
54+
if err != nil {
55+
return err
56+
}
57+
if err := OperationWait(w, activity, timeout, config.PollInterval); err != nil {
58+
return err
59+
}
60+
return json.Unmarshal([]byte(w.CommonOperationWaiter.Op.Response), response)
61+
}
62+
63+
func cloudfunctions2OperationWaitTime(config *Config, op map[string]interface{}, project, activity, userAgent string, timeout time.Duration) error {
64+
if val, ok := op["name"]; !ok || val == "" {
65+
// This was a synchronous call - there is no operation to wait for.
66+
return nil
67+
}
68+
w, err := createCloudfunctions2Waiter(config, op, project, activity, userAgent)
69+
if err != nil {
70+
// If w is nil, the op was synchronous.
71+
return err
72+
}
73+
return OperationWait(w, activity, timeout, config.PollInterval)
74+
}

google-beta/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ type Config struct {
178178
CloudAssetBasePath string
179179
CloudBuildBasePath string
180180
CloudFunctionsBasePath string
181+
Cloudfunctions2BasePath string
181182
CloudIdentityBasePath string
182183
CloudIotBasePath string
183184
CloudRunBasePath string
@@ -284,6 +285,7 @@ const BinaryAuthorizationBasePathKey = "BinaryAuthorization"
284285
const CloudAssetBasePathKey = "CloudAsset"
285286
const CloudBuildBasePathKey = "CloudBuild"
286287
const CloudFunctionsBasePathKey = "CloudFunctions"
288+
const Cloudfunctions2BasePathKey = "Cloudfunctions2"
287289
const CloudIdentityBasePathKey = "CloudIdentity"
288290
const CloudIotBasePathKey = "CloudIot"
289291
const CloudRunBasePathKey = "CloudRun"
@@ -375,6 +377,7 @@ var DefaultBasePaths = map[string]string{
375377
CloudAssetBasePathKey: "https://cloudasset.googleapis.com/v1/",
376378
CloudBuildBasePathKey: "https://cloudbuild.googleapis.com/v1/",
377379
CloudFunctionsBasePathKey: "https://cloudfunctions.googleapis.com/v1/",
380+
Cloudfunctions2BasePathKey: "https://cloudfunctions.googleapis.com/v2beta/",
378381
CloudIdentityBasePathKey: "https://cloudidentity.googleapis.com/v1beta1/",
379382
CloudIotBasePathKey: "https://cloudiot.googleapis.com/v1/",
380383
CloudRunBasePathKey: "https://{{location}}-run.googleapis.com/",
@@ -1242,6 +1245,7 @@ func ConfigureBasePaths(c *Config) {
12421245
c.CloudAssetBasePath = DefaultBasePaths[CloudAssetBasePathKey]
12431246
c.CloudBuildBasePath = DefaultBasePaths[CloudBuildBasePathKey]
12441247
c.CloudFunctionsBasePath = DefaultBasePaths[CloudFunctionsBasePathKey]
1248+
c.Cloudfunctions2BasePath = DefaultBasePaths[Cloudfunctions2BasePathKey]
12451249
c.CloudIdentityBasePath = DefaultBasePaths[CloudIdentityBasePathKey]
12461250
c.CloudIotBasePath = DefaultBasePaths[CloudIotBasePathKey]
12471251
c.CloudRunBasePath = DefaultBasePaths[CloudRunBasePathKey]

google-beta/provider.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,14 @@ func Provider() *schema.Provider {
285285
"GOOGLE_CLOUD_FUNCTIONS_CUSTOM_ENDPOINT",
286286
}, DefaultBasePaths[CloudFunctionsBasePathKey]),
287287
},
288+
"cloudfunctions2_custom_endpoint": {
289+
Type: schema.TypeString,
290+
Optional: true,
291+
ValidateFunc: validateCustomEndpoint,
292+
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
293+
"GOOGLE_CLOUDFUNCTIONS2_CUSTOM_ENDPOINT",
294+
}, DefaultBasePaths[Cloudfunctions2BasePathKey]),
295+
},
288296
"cloud_identity_custom_endpoint": {
289297
Type: schema.TypeString,
290298
Optional: true,
@@ -903,9 +911,9 @@ func Provider() *schema.Provider {
903911
return provider
904912
}
905913

906-
// Generated resources: 245
914+
// Generated resources: 246
907915
// Generated IAM resources: 129
908-
// Total generated resources: 374
916+
// Total generated resources: 375
909917
func ResourceMap() map[string]*schema.Resource {
910918
resourceMap, _ := ResourceMapWithErrors()
911919
return resourceMap
@@ -984,6 +992,7 @@ func ResourceMapWithErrors() (map[string]*schema.Resource, error) {
984992
"google_cloudfunctions_function_iam_binding": ResourceIamBinding(CloudFunctionsCloudFunctionIamSchema, CloudFunctionsCloudFunctionIamUpdaterProducer, CloudFunctionsCloudFunctionIdParseFunc),
985993
"google_cloudfunctions_function_iam_member": ResourceIamMember(CloudFunctionsCloudFunctionIamSchema, CloudFunctionsCloudFunctionIamUpdaterProducer, CloudFunctionsCloudFunctionIdParseFunc),
986994
"google_cloudfunctions_function_iam_policy": ResourceIamPolicy(CloudFunctionsCloudFunctionIamSchema, CloudFunctionsCloudFunctionIamUpdaterProducer, CloudFunctionsCloudFunctionIdParseFunc),
995+
"google_cloudfunctions2_function": resourceCloudfunctions2function(),
987996
"google_cloud_identity_group": resourceCloudIdentityGroup(),
988997
"google_cloud_identity_group_membership": resourceCloudIdentityGroupMembership(),
989998
"google_cloudiot_registry": resourceCloudIotDeviceRegistry(),
@@ -1553,6 +1562,7 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData, p *schema.Pr
15531562
config.CloudAssetBasePath = d.Get("cloud_asset_custom_endpoint").(string)
15541563
config.CloudBuildBasePath = d.Get("cloud_build_custom_endpoint").(string)
15551564
config.CloudFunctionsBasePath = d.Get("cloud_functions_custom_endpoint").(string)
1565+
config.Cloudfunctions2BasePath = d.Get("cloudfunctions2_custom_endpoint").(string)
15561566
config.CloudIdentityBasePath = d.Get("cloud_identity_custom_endpoint").(string)
15571567
config.CloudIotBasePath = d.Get("cloud_iot_custom_endpoint").(string)
15581568
config.CloudRunBasePath = d.Get("cloud_run_custom_endpoint").(string)
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package google
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccCloudFunctions2Function_update(t *testing.T) {
10+
t.Parallel()
11+
12+
context := map[string]interface{}{
13+
"zip_path": "./test-fixtures/cloudfunctions2/function-source.zip",
14+
"random_suffix": randString(t, 10),
15+
}
16+
17+
vcrTest(t, resource.TestCase{
18+
PreCheck: func() { testAccPreCheck(t) },
19+
Providers: testAccProvidersOiCS,
20+
CheckDestroy: testAccCheckCloudfunctions2functionDestroyProducer(t),
21+
Steps: []resource.TestStep{
22+
{
23+
Config: testAccCloudfunctions2function_basic(context),
24+
},
25+
{
26+
ResourceName: "google_cloudfunctions2_function.terraform-test2",
27+
ImportState: true,
28+
ImportStateVerify: true,
29+
ImportStateVerifyIgnore: []string{"location", "build_config.0.source.0.storage_source.0.object", "build_config.0.source.0.storage_source.0.bucket"},
30+
},
31+
{
32+
Config: testAccCloudFunctions2Function_updated(context),
33+
},
34+
{
35+
ResourceName: "google_cloudfunctions2_function.terraform-test2",
36+
ImportState: true,
37+
ImportStateVerify: true,
38+
ImportStateVerifyIgnore: []string{"location", "build_config.0.source.0.storage_source.0.object", "build_config.0.source.0.storage_source.0.bucket"},
39+
},
40+
},
41+
})
42+
}
43+
44+
func testAccCloudfunctions2function_basic(context map[string]interface{}) string {
45+
return Nprintf(`
46+
resource "google_storage_bucket" "bucket" {
47+
provider = google-beta
48+
name = "tf-test-cloudfunctions2-function-bucket%{random_suffix}"
49+
location = "US"
50+
uniform_bucket_level_access = true
51+
}
52+
53+
resource "google_storage_bucket_object" "object" {
54+
provider = google-beta
55+
name = "function-source.zip"
56+
bucket = google_storage_bucket.bucket.name
57+
source = "%{zip_path}"
58+
}
59+
60+
resource "google_cloudfunctions2_function" "terraform-test2" {
61+
provider = google-beta
62+
name = "tf-test-test-function%{random_suffix}"
63+
location = "us-central1"
64+
description = "a new function"
65+
66+
build_config {
67+
runtime = "nodejs12"
68+
entry_point = "helloHttp"
69+
source {
70+
storage_source {
71+
bucket = google_storage_bucket.bucket.name
72+
object = google_storage_bucket_object.object.name
73+
}
74+
}
75+
}
76+
77+
service_config {
78+
max_instance_count = 1
79+
available_memory = "1536Mi"
80+
timeout_seconds = 30
81+
}
82+
}
83+
`, context)
84+
}
85+
86+
func testAccCloudFunctions2Function_updated(context map[string]interface{}) string {
87+
return Nprintf(`
88+
resource "google_storage_bucket" "bucket" {
89+
provider = google-beta
90+
name = "tf-test-cloudfunctions2-function-bucket%{random_suffix}"
91+
location = "US"
92+
uniform_bucket_level_access = true
93+
}
94+
95+
resource "google_storage_bucket_object" "object" {
96+
provider = google-beta
97+
name = "function-source.zip"
98+
bucket = google_storage_bucket.bucket.name
99+
source = "%{zip_path}"
100+
}
101+
102+
resource "google_cloudfunctions2_function" "terraform-test2" {
103+
provider = google-beta
104+
name = "tf-test-test-function%{random_suffix}"
105+
location = "us-west1"
106+
description = "function test"
107+
108+
build_config {
109+
runtime = "nodejs16"
110+
entry_point = "helloHttp"
111+
environment_variables = {
112+
BUILD_CONFIG_TEST = "build_test"
113+
}
114+
source {
115+
storage_source {
116+
bucket = google_storage_bucket.bucket.name
117+
object = google_storage_bucket_object.object.name
118+
}
119+
}
120+
}
121+
122+
service_config {
123+
max_instance_count = 5
124+
min_instance_count = 1
125+
available_memory = "256M"
126+
timeout_seconds = 60
127+
environment_variables = {
128+
SERVICE_CONFIG_TEST = "build_test"
129+
}
130+
}
131+
}
132+
`, context)
133+
}

0 commit comments

Comments
 (0)