From 9495d3f9b338de02cc7d6ae9c9181ff9cbc03ed8 Mon Sep 17 00:00:00 2001 From: Cameron Thornton Date: Mon, 2 Dec 2019 14:37:26 -0800 Subject: [PATCH] Add pubsubConfigs to cloud source repository --- products/sourcerepo/api.yaml | 33 +++++++++++++++++-- products/sourcerepo/terraform.yaml | 9 +++++ .../sourcerepo_repository_full.tf.erb | 17 ++++++++++ .../source_repo_repository_update.go.erb | 5 +++ .../update_encoder/source_repo_repository.erb | 28 ++++++++++++++++ 5 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 templates/terraform/examples/sourcerepo_repository_full.tf.erb create mode 100644 templates/terraform/post_create/source_repo_repository_update.go.erb create mode 100644 templates/terraform/update_encoder/source_repo_repository.erb diff --git a/products/sourcerepo/api.yaml b/products/sourcerepo/api.yaml index dec7d0751a18..5394d2241f93 100644 --- a/products/sourcerepo/api.yaml +++ b/products/sourcerepo/api.yaml @@ -29,9 +29,8 @@ objects: name: 'Repository' base_url: projects/{{project}}/repos self_link: 'projects/{{project}}/repos/{{name}}' - # this is a lie- the resource supports update but is missing the field - # with update right now - input: true + update_verb: :PATCH + update_mask: true references: !ruby/object:Api::Resource::ReferenceLinks guides: 'Official Documentation': 'https://cloud.google.com/source-repositories/' @@ -58,3 +57,31 @@ objects: output: true description: | The disk usage of the repo, in bytes. + - !ruby/object:Api::Type::Map + name: 'pubsubConfigs' + description: | + How this repository publishes a change in the repository through Cloud Pub/Sub. + Keyed by the topic names. + key_name: topic + key_description: | + A topic of Cloud Pub/Sub. Values are of the form projects//topics/. + The project needs to be the same project as this config is in. + value_type: !ruby/object:Api::Type::NestedObject + name: pubsubConfig + properties: + - !ruby/object:Api::Type::Enum + name: 'messageFormat' + description: | + The format of the Cloud Pub/Sub messages. + - PROTOBUF: The message payload is a serialized protocol buffer of SourceRepoEvent. + - JSON: The message payload is a JSON string of SourceRepoEvent. + values: + - :PROTOBUF + - :JSON + - !ruby/object:Api::Type::String + name: 'serviceAccountEmail' + description: | + Email address of the service account used for publishing Cloud Pub/Sub messages. + This service account needs to be in the same project as the PubsubConfig. When added, + the caller needs to have iam.serviceAccounts.actAs permission on this service account. + If unspecified, it defaults to the compute engine default service account. \ No newline at end of file diff --git a/products/sourcerepo/terraform.yaml b/products/sourcerepo/terraform.yaml index c280265ce574..b2443755dde5 100644 --- a/products/sourcerepo/terraform.yaml +++ b/products/sourcerepo/terraform.yaml @@ -26,6 +26,12 @@ overrides: !ruby/object:Overrides::ResourceOverrides primary_resource_name: "fmt.Sprintf(\"my-repository%s\", context[\"random_suffix\"])" vars: repository_name: "my-repository" + - !ruby/object:Provider::Terraform::Examples + name: "sourcerepo_repository_full" + primary_resource_id: "my-repo" + primary_resource_name: "fmt.Sprintf(\"my-repository%s\", context[\"random_suffix\"])" + vars: + repository_name: "my-repository" properties: name: !ruby/object:Overrides::Terraform::PropertyOverride custom_expand: templates/terraform/custom_expand/shortname_to_url.go.erb @@ -33,6 +39,9 @@ overrides: !ruby/object:Overrides::ResourceOverrides description: | Resource name of the repository, of the form `{{repo}}`. The repo name may contain slashes. eg, `name/with/slash` + custom_code: !ruby/object:Provider::Terraform::CustomCode + update_encoder: templates/terraform/update_encoder/source_repo_repository.erb + post_create: templates/terraform/post_create/source_repo_repository_update.go.erb # This is for copying files over files: !ruby/object:Provider::Config::Files # These files have templating (ERB) code that will be run. diff --git a/templates/terraform/examples/sourcerepo_repository_full.tf.erb b/templates/terraform/examples/sourcerepo_repository_full.tf.erb new file mode 100644 index 000000000000..e89f5087c256 --- /dev/null +++ b/templates/terraform/examples/sourcerepo_repository_full.tf.erb @@ -0,0 +1,17 @@ +resource "google_service_account" "test-account" { + account_id = "my-service-account" + display_name = "Test Service Account" +} + +resource "google_pubsub_topic" "topic" { + name = "my-topic" +} + +resource "google_sourcerepo_repository" "<%= ctx[:primary_resource_id] %>" { + name = "<%= ctx[:vars]['repository_name'] %>" + pubsub_configs { + topic = google_pubsub_topic.topic.id + message_format = "JSON" + service_account_email = google_service_account.test-account.email + } +} \ No newline at end of file diff --git a/templates/terraform/post_create/source_repo_repository_update.go.erb b/templates/terraform/post_create/source_repo_repository_update.go.erb new file mode 100644 index 000000000000..edb458d7f3e0 --- /dev/null +++ b/templates/terraform/post_create/source_repo_repository_update.go.erb @@ -0,0 +1,5 @@ +if v, ok := d.GetOkExists("pubsub_configs"); !isEmptyValue(reflect.ValueOf(pubsubConfigsProp)) && (ok || !reflect.DeepEqual(v, pubsubConfigsProp)) { + log.Printf("[DEBUG] Calling update after create to patch in pubsub_configs") + // pubsub_configs cannot be added on create + return resourceSourceRepoRepositoryUpdate(d, meta) +} \ No newline at end of file diff --git a/templates/terraform/update_encoder/source_repo_repository.erb b/templates/terraform/update_encoder/source_repo_repository.erb new file mode 100644 index 000000000000..903db5ead031 --- /dev/null +++ b/templates/terraform/update_encoder/source_repo_repository.erb @@ -0,0 +1,28 @@ +<%# The license inside this block applies to this file. + # Copyright 2019 Google Inc. + # Licensed under the Apache License, Version 2.0 (the "License"); + # you may not use this file except in compliance with the License. + # You may obtain a copy of the License at + # + # http://www.apache.org/licenses/LICENSE-2.0 + # + # Unless required by applicable law or agreed to in writing, software + # distributed under the License is distributed on an "AS IS" BASIS, + # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + # See the License for the specific language governing permissions and + # limitations under the License. +-%> + // Add "topic" field using pubsubConfig map key + pubsubConfigsVal := obj["pubsubConfigs"] + if pubsubConfigsVal != nil { + pubsubConfigs := pubsubConfigsVal.(map[string]interface{}) + for key := range pubsubConfigs { + config := pubsubConfigs[key].(map[string]interface{}) + config["topic"] = key + } + } + + // Nest request body in "repo" field + newObj := make(map[string]interface{}) + newObj["repo"] = obj + return newObj, nil