From ba5780b817e25ec8a7de270a627d86a930376ef7 Mon Sep 17 00:00:00 2001 From: Cameron Thornton Date: Thu, 5 Dec 2019 10:03:04 -0800 Subject: [PATCH] Add pubsubConfigs to cloud source repository (#2769) Merged PR #2769. --- build/ansible | 2 +- build/inspec | 2 +- build/terraform | 2 +- build/terraform-beta | 2 +- build/terraform-mapper | 2 +- products/sourcerepo/ansible.yaml | 4 ++ products/sourcerepo/api.yaml | 34 ++++++++++-- products/sourcerepo/terraform.yaml | 12 +++++ .../sourcerepo_repository_full.tf.erb | 17 ++++++ .../source_repo_repository_update.go.erb | 5 ++ .../update_encoder/source_repo_repository.erb | 28 ++++++++++ .../resource_sourcerepo_repository_test.go | 53 +++++++++++++++++++ 12 files changed, 155 insertions(+), 8 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/build/ansible b/build/ansible index 352731c9dbe5..bf8746245597 160000 --- a/build/ansible +++ b/build/ansible @@ -1 +1 @@ -Subproject commit 352731c9dbe5db69e0cb5a7d205199167b33d292 +Subproject commit bf874624559786f95cdee2df7924b0cfc9772237 diff --git a/build/inspec b/build/inspec index 4116b49a9608..357f043a78aa 160000 --- a/build/inspec +++ b/build/inspec @@ -1 +1 @@ -Subproject commit 4116b49a96082163a5790fe5207f3e5d1d129235 +Subproject commit 357f043a78aa1b78e3ac31bb429c9e8c07da560d diff --git a/build/terraform b/build/terraform index 7345305fbee1..aced676a69d8 160000 --- a/build/terraform +++ b/build/terraform @@ -1 +1 @@ -Subproject commit 7345305fbee156d9fd785439db3bded5e7517941 +Subproject commit aced676a69d885a899c2c0745e34f8fa3cc180d7 diff --git a/build/terraform-beta b/build/terraform-beta index bcdb7db92aa4..6e00ac5db052 160000 --- a/build/terraform-beta +++ b/build/terraform-beta @@ -1 +1 @@ -Subproject commit bcdb7db92aa4eddad9fc83d828dc0d7919226325 +Subproject commit 6e00ac5db0525dc0852fe7392ba0d4a54736743a diff --git a/build/terraform-mapper b/build/terraform-mapper index 7add15334616..92ff367b389c 160000 --- a/build/terraform-mapper +++ b/build/terraform-mapper @@ -1 +1 @@ -Subproject commit 7add15334616591b0a6758869d6140d1db869e5e +Subproject commit 92ff367b389cb469a8cccb846dec8c698b0d694a diff --git a/products/sourcerepo/ansible.yaml b/products/sourcerepo/ansible.yaml index 709337246144..2f29097e2d7e 100644 --- a/products/sourcerepo/ansible.yaml +++ b/products/sourcerepo/ansible.yaml @@ -18,6 +18,10 @@ datasources: !ruby/object:Overrides::ResourceOverrides facts: !ruby/object:Provider::Ansible::FactsOverride has_filters: false overrides: !ruby/object:Overrides::ResourceOverrides + Repository: !ruby/object:Overrides::Ansible::ResourceOverride + properties: + pubsubConfigs: !ruby/object:Overrides::Ansible::PropertyOverride + exclude: true files: !ruby/object:Provider::Config::Files resource: <%= lines(indent(compile('provider/ansible/resource~compile.yaml'), 4)) -%> diff --git a/products/sourcerepo/api.yaml b/products/sourcerepo/api.yaml index dec7d0751a18..e4d9cf53f02a 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,32 @@ 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 + required: true + - !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..3c703884debb 100644 --- a/products/sourcerepo/terraform.yaml +++ b/products/sourcerepo/terraform.yaml @@ -26,6 +26,13 @@ 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" + vars: + repository_name: "my-repository" + account_id: "my-account" + topic_name: "my-topic" properties: name: !ruby/object:Overrides::Terraform::PropertyOverride custom_expand: templates/terraform/custom_expand/shortname_to_url.go.erb @@ -33,6 +40,11 @@ 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` + pubsubConfigs.serviceAccountEmail: !ruby/object:Overrides::Terraform::PropertyOverride + default_from_api: true + 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..9360ce609b8f --- /dev/null +++ b/templates/terraform/examples/sourcerepo_repository_full.tf.erb @@ -0,0 +1,17 @@ +resource "google_service_account" "test-account" { + account_id = "<%= ctx[:vars]['account_id'] %>" + display_name = "Test Service Account" +} + +resource "google_pubsub_topic" "topic" { + name = "<%= ctx[:vars]['topic_name'] %>" +} + +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 diff --git a/third_party/terraform/tests/resource_sourcerepo_repository_test.go b/third_party/terraform/tests/resource_sourcerepo_repository_test.go index cdb35bb6af46..77f0a4d327ee 100644 --- a/third_party/terraform/tests/resource_sourcerepo_repository_test.go +++ b/third_party/terraform/tests/resource_sourcerepo_repository_test.go @@ -29,6 +29,37 @@ func TestAccSourceRepoRepository_basic(t *testing.T) { }) } +func TestAccSourceRepoRepository_update(t *testing.T) { + t.Parallel() + + repositoryName := fmt.Sprintf("source-repo-repository-test-%s", acctest.RandString(10)) + accountId := fmt.Sprintf("account-id-%s", acctest.RandString(10)) + topicName := fmt.Sprintf("topic-name-%s", acctest.RandString(10)) + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckSourceRepoRepositoryDestroy, + Steps: []resource.TestStep{ + { + Config: testAccSourceRepoRepository_basic(repositoryName), + }, + { + ResourceName: "google_sourcerepo_repository.acceptance", + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccSourceRepoRepository_extended(accountId, topicName, repositoryName), + }, + { + ResourceName: "google_sourcerepo_repository.acceptance", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + func testAccSourceRepoRepository_basic(repositoryName string) string { return fmt.Sprintf(` resource "google_sourcerepo_repository" "acceptance" { @@ -36,3 +67,25 @@ resource "google_sourcerepo_repository" "acceptance" { } `, repositoryName) } + +func testAccSourceRepoRepository_extended(accountId string, topicName string, repositoryName string) string { + return fmt.Sprintf(` + resource "google_service_account" "test-account" { + account_id = "%s" + display_name = "Test Service Account" + } + + resource "google_pubsub_topic" "topic" { + name = "%s" + } + + resource "google_sourcerepo_repository" "acceptance" { + name = "%s" + pubsub_configs { + topic = google_pubsub_topic.topic.id + message_format = "JSON" + service_account_email = google_service_account.test-account.email + } + } +`, accountId, topicName, repositoryName) +}