Skip to content

Commit

Permalink
Add vpc access connector resource and vpc_connector attribute to clou…
Browse files Browse the repository at this point in the history
…d functions

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
megan07 authored and modular-magician committed Aug 12, 2019
1 parent 73c8ee8 commit 0bbf888
Show file tree
Hide file tree
Showing 10 changed files with 743 additions and 0 deletions.
1 change: 1 addition & 0 deletions google-beta/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ type Config struct {
SqlBasePath string
StorageBasePath string
TpuBasePath string
VpcAccessBasePath string

CloudBillingBasePath string
clientBilling *cloudbilling.APIService
Expand Down
4 changes: 4 additions & 0 deletions google-beta/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ func Provider() terraform.ResourceProvider {
SqlCustomEndpointEntryKey: SqlCustomEndpointEntry,
StorageCustomEndpointEntryKey: StorageCustomEndpointEntry,
TpuCustomEndpointEntryKey: TpuCustomEndpointEntry,
VpcAccessCustomEndpointEntryKey: VpcAccessCustomEndpointEntry,

// Handwritten Products / Versioned / Atypical Entries
// start beta-only products
Expand Down Expand Up @@ -242,6 +243,7 @@ func ResourceMapWithErrors() (map[string]*schema.Resource, error) {
GeneratedSqlResourcesMap,
GeneratedStorageResourcesMap,
GeneratedTpuResourcesMap,
GeneratedVpcAccessResourcesMap,
map[string]*schema.Resource{
"google_app_engine_application": resourceAppEngineApplication(),
"google_bigquery_dataset": resourceBigQueryDataset(),
Expand Down Expand Up @@ -432,6 +434,7 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
config.SqlBasePath = d.Get(SqlCustomEndpointEntryKey).(string)
config.StorageBasePath = d.Get(StorageCustomEndpointEntryKey).(string)
config.TpuBasePath = d.Get(TpuCustomEndpointEntryKey).(string)
config.VpcAccessBasePath = d.Get(VpcAccessCustomEndpointEntryKey).(string)

// Handwritten Products / Versioned / Atypical Entries
config.IAPBasePath = d.Get(IAPCustomEndpointEntryKey).(string)
Expand Down Expand Up @@ -494,6 +497,7 @@ func ConfigureBasePaths(c *Config) {
c.SqlBasePath = SqlDefaultBasePath
c.StorageBasePath = StorageDefaultBasePath
c.TpuBasePath = TpuDefaultBasePath
c.VpcAccessBasePath = VpcAccessDefaultBasePath

// Handwritten Products / Versioned / Atypical Entries
// start beta-only products
Expand Down
34 changes: 34 additions & 0 deletions google-beta/provider_vpc_access_gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// ----------------------------------------------------------------------------
//
// *** AUTO GENERATED CODE *** AUTO GENERATED CODE ***
//
// ----------------------------------------------------------------------------
//
// This file is automatically generated by Magic Modules and manual
// changes will be clobbered when the file is regenerated.
//
// Please read more about how to change this file in
// .github/CONTRIBUTING.md.
//
// ----------------------------------------------------------------------------

package google

import "github.com/hashicorp/terraform/helper/schema"

// If the base path has changed as a result of your PR, make sure to update
// the provider_reference page!
var VpcAccessDefaultBasePath = "https://vpcaccess.googleapis.com/v1beta1/"
var VpcAccessCustomEndpointEntryKey = "vpc_access_custom_endpoint"
var VpcAccessCustomEndpointEntry = &schema.Schema{
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateCustomEndpoint,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"GOOGLE_VPC_ACCESS_CUSTOM_ENDPOINT",
}, VpcAccessDefaultBasePath),
}

var GeneratedVpcAccessResourcesMap = map[string]*schema.Resource{
"google_vpc_access_connector": resourceVpcAccessConnector(),
}
11 changes: 11 additions & 0 deletions google-beta/resource_cloudfunctions_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,12 @@ func resourceCloudFunctionsFunction() *schema.Resource {
ForceNew: true,
},

"vpc_connector": {
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: compareSelfLinkOrResourceName,
},

"environment_variables": {
Type: schema.TypeMap,
Optional: true,
Expand Down Expand Up @@ -367,6 +373,10 @@ func resourceCloudFunctionsCreate(d *schema.ResourceData, meta interface{}) erro
function.EnvironmentVariables = expandEnvironmentVariables(d)
}

if v, ok := d.GetOk("vpc_connector"); ok {
function.VpcConnector = v.(string)
}

if v, ok := d.GetOk("max_instances"); ok {
function.MaxInstances = int64(v.(int))
}
Expand Down Expand Up @@ -417,6 +427,7 @@ func resourceCloudFunctionsRead(d *schema.ResourceData, meta interface{}) error
d.Set("runtime", function.Runtime)
d.Set("service_account_email", function.ServiceAccountEmail)
d.Set("environment_variables", function.EnvironmentVariables)
d.Set("vpc_connector", function.VpcConnector)
if function.SourceArchiveUrl != "" {
// sourceArchiveUrl should always be a Google Cloud Storage URL (e.g. gs://bucket/object)
// https://cloud.google.com/functions/docs/reference/rest/v1/projects.locations.functions
Expand Down
79 changes: 79 additions & 0 deletions google-beta/resource_cloudfunctions_function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,32 @@ func TestAccCloudFunctionsFunction_serviceAccountEmail(t *testing.T) {
})
}

func TestAccCloudFunctionsFunction_vpcConnector(t *testing.T) {
t.Parallel()

functionName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
bucketName := fmt.Sprintf("tf-test-bucket-%d", acctest.RandInt())
vpcConnectorName := fmt.Sprintf("tf-test-connector-%s", acctest.RandString(5))
zipFilePath, err := createZIPArchiveForIndexJs(testHTTPTriggerPath)
projectNumber := os.Getenv("GOOGLE_PROJECT_NUMBER")

if err != nil {
t.Fatal(err.Error())
}
defer os.Remove(zipFilePath) // clean up

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProvidersOiCS,
CheckDestroy: testAccCheckCloudFunctionsFunctionDestroy,
Steps: []resource.TestStep{
{
Config: testAccCloudFunctionsFunction_vpcConnector(projectNumber, functionName, bucketName, zipFilePath, vpcConnectorName),
},
},
})
}

func testAccCheckCloudFunctionsFunctionDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)

Expand Down Expand Up @@ -708,3 +734,56 @@ resource "google_cloudfunctions_function" "function" {
entry_point = "helloGET"
}`, bucketName, zipFilePath, functionName)
}

func testAccCloudFunctionsFunction_vpcConnector(projectNumber, functionName, bucketName, zipFilePath, vpcConnectorName string) string {
return fmt.Sprintf(`
provider "google-beta" { }
resource "google_project_iam_member" "project" {
provider = "google-beta"
role = "roles/editor"
member = "serviceAccount:service-%[email protected]"
}
resource "google_vpc_access_connector" "connector" {
provider = "google-beta"
name = "%s"
region = "us-central1"
ip_cidr_range = "10.10.0.0/28"
network = "default"
}
resource "google_storage_bucket" "bucket" {
provider = "google-beta"
name = "%s"
}
resource "google_storage_bucket_object" "archive" {
provider = "google-beta"
name = "index.zip"
bucket = google_storage_bucket.bucket.name
source = "%s"
}
resource "google_cloudfunctions_function" "function" {
name = "%s"
provider = "google-beta"
description = "test function"
available_memory_mb = 128
source_archive_bucket = google_storage_bucket.bucket.name
source_archive_object = google_storage_bucket_object.archive.name
trigger_http = true
timeout = 61
entry_point = "helloGET"
labels = {
my-label = "my-label-value"
}
environment_variables = {
TEST_ENV_VARIABLE = "test-env-variable-value"
}
max_instances = 10
vpc_connector = google_vpc_access_connector.connector.self_link
}`, projectNumber, vpcConnectorName, bucketName, zipFilePath, functionName)
}
Loading

0 comments on commit 0bbf888

Please sign in to comment.