Added Environment Variables configuration to Cloud Functions (#1830)

This commit is contained in:
Harry Pidcock 2018-09-11 10:37:43 +10:00 committed by Nathan McKinley
parent 37de4312e3
commit 65669fc6d6
7 changed files with 846 additions and 719 deletions

View File

@ -169,6 +169,11 @@ func resourceCloudFunctionsFunction() *schema.Resource {
Optional: true,
},
"environment_variables": {
Type: schema.TypeMap,
Optional: true,
},
"trigger_bucket": {
Type: schema.TypeString,
Optional: true,
@ -319,6 +324,10 @@ func resourceCloudFunctionsCreate(d *schema.ResourceData, meta interface{}) erro
function.Labels = expandLabels(d)
}
if _, ok := d.GetOk("environment_variables"); ok {
function.EnvironmentVariables = expandEnvironmentVariables(d)
}
log.Printf("[DEBUG] Creating cloud function: %s", function.Name)
op, err := config.clientCloudFunctions.Projects.Locations.Functions.Create(
cloudFuncId.locationId(), function).Do()
@ -361,6 +370,7 @@ func resourceCloudFunctionsRead(d *schema.ResourceData, meta interface{}) error
}
d.Set("timeout", timeout)
d.Set("labels", function.Labels)
d.Set("environment_variables", function.EnvironmentVariables)
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
@ -440,6 +450,11 @@ func resourceCloudFunctionsUpdate(d *schema.ResourceData, meta interface{}) erro
updateMaskArr = append(updateMaskArr, "labels")
}
if d.HasChange("environment_variables") {
function.EnvironmentVariables = expandEnvironmentVariables(d)
updateMaskArr = append(updateMaskArr, "environment_variables")
}
if d.HasChange("retry_on_failure") {
if d.Get("retry_on_failure").(bool) {
if function.EventTrigger == nil {

View File

@ -66,6 +66,8 @@ func TestAccCloudFunctionsFunction_basic(t *testing.T) {
resource.TestCheckResourceAttr(funcResourceName,
"trigger_http", "true"),
testAccCloudFunctionsFunctionHasLabel("my-label", "my-label-value", &function),
testAccCloudFunctionsFunctionHasEnvironmentVariable("TEST_ENV_VARIABLE",
"test-env-variable-value", &function),
),
},
{
@ -119,6 +121,10 @@ func TestAccCloudFunctionsFunction_update(t *testing.T) {
"timeout", "91"),
testAccCloudFunctionsFunctionHasLabel("my-label", "my-updated-label-value", &function),
testAccCloudFunctionsFunctionHasLabel("a-new-label", "a-new-label-value", &function),
testAccCloudFunctionsFunctionHasEnvironmentVariable("TEST_ENV_VARIABLE",
"test-env-variable-value", &function),
testAccCloudFunctionsFunctionHasEnvironmentVariable("NEW_ENV_VARIABLE",
"new-env-variable-value", &function),
),
},
},
@ -347,6 +353,21 @@ func testAccCloudFunctionsFunctionHasLabel(key, value string,
}
}
func testAccCloudFunctionsFunctionHasEnvironmentVariable(key, value string,
function *cloudfunctions.CloudFunction) resource.TestCheckFunc {
return func(s *terraform.State) error {
if val, ok := function.EnvironmentVariables[key]; ok {
if val != value {
return fmt.Errorf("Environment Variable value did not match for key %s: expected %s but found %s",
key, value, val)
}
} else {
return fmt.Errorf("Environment Variable with key %s not found", key)
}
return nil
}
}
func createZIPArchiveForIndexJs(sourcePath string) (string, error) {
source, err := ioutil.ReadFile(sourcePath)
if err != nil {
@ -411,6 +432,9 @@ resource "google_cloudfunctions_function" "function" {
labels {
my-label = "my-label-value"
}
environment_variables {
TEST_ENV_VARIABLE = "test-env-variable-value"
}
}
`, bucketName, zipFilePath, functionName)
}
@ -440,6 +464,10 @@ resource "google_cloudfunctions_function" "function" {
my-label = "my-updated-label-value"
a-new-label = "a-new-label-value"
}
environment_variables {
TEST_ENV_VARIABLE = "test-env-variable-value"
NEW_ENV_VARIABLE = "new-env-variable-value"
}
}`, bucketName, zipFilePath, functionName)
}

View File

@ -272,6 +272,11 @@ func expandLabels(d *schema.ResourceData) map[string]string {
return expandStringMap(d, "labels")
}
// expandEnvironmentVariables pulls the value of "environment_variables" out of a schema.ResourceData as a map[string]string.
func expandEnvironmentVariables(d *schema.ResourceData) map[string]string {
return expandStringMap(d, "environment_variables")
}
// expandStringMap pulls the value of key out of a schema.ResourceData as a map[string]string.
func expandStringMap(d *schema.ResourceData, key string) map[string]string {
v, ok := d.GetOk(key)

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,4 @@
// Package cloudfunctions provides access to the Google Cloud Functions API.
// Package cloudfunctions provides access to the Cloud Functions API.
//
// See https://cloud.google.com/functions
//
@ -216,6 +216,12 @@ type CloudFunction struct {
// in `source_location`.
EntryPoint string `json:"entryPoint,omitempty"`
// EnvironmentVariables: **Beta Feature**
//
// Environment variables that shall be available during function
// execution.
EnvironmentVariables map[string]string `json:"environmentVariables,omitempty"`
// EventTrigger: A source that fires events in response to a condition
// in another service.
EventTrigger *EventTrigger `json:"eventTrigger,omitempty"`
@ -227,11 +233,47 @@ type CloudFunction struct {
// Labels: Labels associated with this Cloud Function.
Labels map[string]string `json:"labels,omitempty"`
// MaxInstances: The limit on the maximum number of function instances
// that may coexist at a
// given time. This feature is currently in alpha, available only
// for
// whitelisted users.
MaxInstances int64 `json:"maxInstances,omitempty"`
// Name: A user-defined name of the function. Function names must be
// unique
// globally and match pattern `projects/*/locations/*/functions/*`
Name string `json:"name,omitempty"`
// Network: The VPC Network that this cloud function can connect to. It
// can be
// either the fully-qualified URI, or the short name of the network
// resource.
// If the short network name is used, the network must belong to the
// same
// project. Otherwise, it must belong to a project within the
// same
// organization. The format of this field is
// either
// `projects/{project}/global/networks/{network}` or `{network}`,
// where
// {project} is a project id where the network is defined, and {network}
// is
// the short name of the network.
//
// See [the VPC
// documentation](https://cloud.google.com/compute/docs/vpc) for
// more information on connecting Cloud projects.
//
// This feature is currently in alpha, available only for whitelisted
// users.
Network string `json:"network,omitempty"`
// Runtime: The runtime in which the function is going to run. If empty,
// defaults to
// Node.js 6.
Runtime string `json:"runtime,omitempty"`
// ServiceAccountEmail: Output only. The email of the function's service
// account.
ServiceAccountEmail string `json:"serviceAccountEmail,omitempty"`
@ -323,14 +365,15 @@ type EventTrigger struct {
//
// Event types match pattern `providers/*/eventTypes/*.*`.
// The pattern contains:
// 1. namespace: For example, `cloud.storage` and
// `google.firebase.analytics`.
// 2. resource type: The type of resource on which event occurs. For
// example, the Google Cloud Storage API includes the type
//
// 1. namespace: For example, `cloud.storage` and
// `google.firebase.analytics`.
// 2. resource type: The type of resource on which event occurs. For
// example, the Google Cloud Storage API includes the type
// `object`.
// 3. action: The action that generates the event. For example, action
// 3. action: The action that generates the event. For example, action
// for
// a Google Cloud Storage Object is 'change'.
// a Google Cloud Storage Object is 'change'.
// These parts are lower case.
EventType string `json:"eventType,omitempty"`
@ -680,6 +723,11 @@ func (s *ListOperationsResponse) MarshalJSON() ([]byte, error) {
// Location: A resource that represents Google Cloud Platform location.
type Location struct {
// DisplayName: The friendly name for this location, typically a nearby
// city name.
// For example, "Tokyo".
DisplayName string `json:"displayName,omitempty"`
// Labels: Cross-service attributes for the location. For example
//
// {"cloud.googleapis.com/region": "us-east1"}
@ -699,7 +747,7 @@ type Location struct {
// For example: "projects/example-project/locations/us-east1"
Name string `json:"name,omitempty"`
// ForceSendFields is a list of field names (e.g. "Labels") to
// ForceSendFields is a list of field names (e.g. "DisplayName") to
// unconditionally include in API requests. By default, fields with
// empty values are omitted from API requests. However, any non-pointer,
// non-interface field appearing in ForceSendFields will be sent to the
@ -707,10 +755,10 @@ type Location struct {
// used to include empty fields in Patch requests.
ForceSendFields []string `json:"-"`
// NullFields is a list of field names (e.g. "Labels") to include in API
// requests with the JSON null value. By default, fields with empty
// values are omitted from API requests. However, any field with an
// empty value appearing in NullFields will be sent to the server as
// NullFields is a list of field names (e.g. "DisplayName") to include
// in API requests with the JSON null value. By default, fields with
// empty values are omitted from API requests. However, any field with
// an empty value appearing in NullFields will be sent to the server as
// null. It is an error if a field in this list has a non-empty value.
// This may be used to include null fields in Patch requests.
NullFields []string `json:"-"`
@ -2197,11 +2245,24 @@ type ProjectsLocationsFunctionsGenerateUploadUrlCall struct {
// source code.
// For more information about the signed URL usage
// see:
// https://cloud.google.com/storage/docs/access-control/signed-urls
// https://cloud.google.com/storage/docs/access-control/signed-urls.
//
// Once the function source code upload is complete, the used signed
// URL should be provided in CreateFunction or UpdateFunction request
// as a reference to the function source code.
//
// When uploading source code to the generated signed URL, please
// follow
// these restrictions:
//
// * Source file type should be a zip file.
// * Source file size should not exceed 100MB limit.
//
// When making a HTTP PUT request, these two headers need to be
// specified:
//
// * `content-type: application/zip`
// * `x-goog-content-length-range: 0,104857600`
func (r *ProjectsLocationsFunctionsService) GenerateUploadUrl(parent string, generateuploadurlrequest *GenerateUploadUrlRequest) *ProjectsLocationsFunctionsGenerateUploadUrlCall {
c := &ProjectsLocationsFunctionsGenerateUploadUrlCall{s: r.s, urlParams_: make(gensupport.URLParams)}
c.parent = parent
@ -2295,7 +2356,7 @@ func (c *ProjectsLocationsFunctionsGenerateUploadUrlCall) Do(opts ...googleapi.C
}
return ret, nil
// {
// "description": "Returns a signed URL for uploading a function source code.\nFor more information about the signed URL usage see:\nhttps://cloud.google.com/storage/docs/access-control/signed-urls\nOnce the function source code upload is complete, the used signed\nURL should be provided in CreateFunction or UpdateFunction request\nas a reference to the function source code.",
// "description": "Returns a signed URL for uploading a function source code.\nFor more information about the signed URL usage see:\nhttps://cloud.google.com/storage/docs/access-control/signed-urls.\nOnce the function source code upload is complete, the used signed\nURL should be provided in CreateFunction or UpdateFunction request\nas a reference to the function source code.\n\nWhen uploading source code to the generated signed URL, please follow\nthese restrictions:\n\n* Source file type should be a zip file.\n* Source file size should not exceed 100MB limit.\n\nWhen making a HTTP PUT request, these two headers need to be specified:\n\n* `content-type: application/zip`\n* `x-goog-content-length-range: 0,104857600`",
// "flatPath": "v1/projects/{projectsId}/locations/{locationsId}/functions:generateUploadUrl",
// "httpMethod": "POST",
// "id": "cloudfunctions.projects.locations.functions.generateUploadUrl",
@ -2304,7 +2365,7 @@ func (c *ProjectsLocationsFunctionsGenerateUploadUrlCall) Do(opts ...googleapi.C
// ],
// "parameters": {
// "parent": {
// "description": "The project and location in which the Google Cloud Storage signed URL\nshould be generated, specified in the format `projects/*/locations/*",
// "description": "The project and location in which the Google Cloud Storage signed URL\nshould be generated, specified in the format `projects/*/locations/*`.",
// "location": "path",
// "pattern": "^projects/[^/]+/locations/[^/]+$",
// "required": true,

6
vendor/vendor.json vendored
View File

@ -1262,10 +1262,10 @@
"revisionTime": "2018-04-19T00:06:09Z"
},
{
"checksumSHA1": "M6qPoWx14U0JlqP2c/Mvx9fYmDo=",
"checksumSHA1": "M4KP9cR8pFbyJyG15Dbl2JRAgGw=",
"path": "google.golang.org/api/cloudfunctions/v1",
"revision": "f4694fe510ef1f58a08157aa15e795ffea4d4766",
"revisionTime": "2017-12-15T00:04:04Z"
"revision": "31ca0e01cd791f07750cb23fc99327721f753290",
"revisionTime": "2018-07-30T00:09:01Z"
},
{
"checksumSHA1": "uLifIDdJHnoBlTG0G4T6DGSB9l4=",

View File

@ -38,6 +38,9 @@ resource "google_cloudfunctions_function" "function" {
labels {
my-label = "my-label-value"
}
environment_variables {
MY_ENV_VAR = "my-env-var-value"
}
}
```
@ -69,6 +72,8 @@ The following arguments are supported:
* `labels` - (Optional) A set of key/value label pairs to assign to the function.
* `environment_variables` - (Optional) A set of key/value environment variable pairs to assign to the function.
* `retry_on_failure` - (Optional) Whether the function should be retried on failure. This only applies to bucket and topic triggers, not HTTPS triggers.
## Attributes Reference