Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pubsubConfigs to cloud source repository #2769

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
c2thorn marked this conversation as resolved.
Show resolved Hide resolved
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'
c2thorn marked this conversation as resolved.
Show resolved Hide resolved
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.
c2thorn marked this conversation as resolved.
Show resolved Hide resolved
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)
}