Skip to content

Commit

Permalink
Add pubsubConfigs to cloud source repository
Browse files Browse the repository at this point in the history
  • Loading branch information
c2thorn committed Dec 2, 2019
1 parent dbd2c1a commit 9495d3f
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 3 deletions.
33 changes: 30 additions & 3 deletions products/sourcerepo/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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/'
Expand All @@ -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/<project>/topics/<topic>.
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.
9 changes: 9 additions & 0 deletions products/sourcerepo/terraform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,22 @@ 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
custom_flatten: templates/terraform/custom_flatten/repository_short_name_from_name.go.erb
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.
Expand Down
17 changes: 17 additions & 0 deletions templates/terraform/examples/sourcerepo_repository_full.tf.erb
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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)
}
28 changes: 28 additions & 0 deletions templates/terraform/update_encoder/source_repo_repository.erb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 9495d3f

Please sign in to comment.