Skip to content

Commit

Permalink
Add pubsubConfigs to cloud source repository (#2769)
Browse files Browse the repository at this point in the history
Merged PR #2769.
  • Loading branch information
c2thorn authored and modular-magician committed Dec 5, 2019
1 parent 6df2630 commit ba5780b
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 8 deletions.
2 changes: 1 addition & 1 deletion build/ansible
2 changes: 1 addition & 1 deletion build/terraform
2 changes: 1 addition & 1 deletion build/terraform-beta
2 changes: 1 addition & 1 deletion build/terraform-mapper
4 changes: 4 additions & 0 deletions products/sourcerepo/ansible.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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)) -%>
34 changes: 31 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,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/<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
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.
12 changes: 12 additions & 0 deletions products/sourcerepo/terraform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,25 @@ 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
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`
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.
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 = "<%= 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
}
}
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
53 changes: 53 additions & 0 deletions third_party/terraform/tests/resource_sourcerepo_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,63 @@ 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" {
name = "%s"
}
`, 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)
}

0 comments on commit ba5780b

Please sign in to comment.