Skip to content

Commit

Permalink
Adding a new cloud logging resource LogScope to Terraform (#11763)
Browse files Browse the repository at this point in the history
  • Loading branch information
tyh2333 authored Sep 20, 2024
1 parent a864502 commit ec8609a
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
85 changes: 85 additions & 0 deletions mmv1/products/logging/LogScope.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Copyright 2024 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.

--- !ruby/object:Api::Resource
name: 'LogScope'
base_url: '{{parent}}/locations/{{location}}/logScopes'
create_url: '{{parent}}/locations/{{location}}/logScopes?logScopeId={{name}}'
self_link: '{{parent}}/locations/{{location}}/logScopes/{{name}}'
import_format: ['{{%parent}}/locations/{{location}}/logScopes/{{name}}']
update_verb: :PATCH
update_mask: true
references: !ruby/object:Api::Resource::ReferenceLinks
guides:
'Official Documentation': 'https://cloud.google.com/logging/docs/apis'
api: 'https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.locations.logScopes'
description: 'Describes a group of resources to read log entries from'
examples:
- !ruby/object:Provider::Terraform::Examples
name: 'logging_log_scope_basic'
primary_resource_id: 'logging_log_scope'
vars:
log_scope_name: 'my-log-scope'
log_view_name_1: 'view1'
log_view_name_2: 'view2'
test_env_vars:
project: :PROJECT_NAME
custom_code: !ruby/object:Provider::Terraform::CustomCode
encoder: templates/terraform/encoders/logging_log_scope.go.erb
parameters:
- !ruby/object:Api::Type::String
name: parent
description: The parent of the resource.
url_param_only: true
immutable: true
default_from_api: true
diff_suppress_func: 'tpgresource.CompareSelfLinkOrResourceName'
- !ruby/object:Api::Type::String
name: location
description:
'The location of the resource. The supported locations are: global,
us-central1, us-east1, us-west1, asia-east1, europe-west1.'
url_param_only: true
immutable: true
default_from_api: true
properties:
- !ruby/object:Api::Type::String
name: name
description:
'The resource name of the log scope. For example:
\`projects/my-project/locations/global/logScopes/my-log-scope\`'
required: true
immutable: true
ignore_read: true
diff_suppress_func: 'tpgresource.CompareResourceNames'
- !ruby/object:Api::Type::Array
# This has to be camelCase, even though it's snakeCase in proto definiation.
name: resourceNames
item_type: Api::Type::String
required: true
description:
'Names of one or more parent resources : * \`projects/[PROJECT_ID]\`
May alternatively be one or more views :
* \`projects/[PROJECT_ID]/locations/[LOCATION_ID]/buckets/[BUCKET_ID]/views/[VIEW_ID]\`
A log scope can include a maximum of 50 projects and a maximum of 100 resources in total.'
- !ruby/object:Api::Type::String
name: description
description: Describes this log scopes.
- !ruby/object:Api::Type::String
name: createTime
description: Output only. The creation timestamp of the log scopes.
output: true
- !ruby/object:Api::Type::String
name: updateTime
description: Output only. The last update timestamp of the log scopes.
output: true
34 changes: 34 additions & 0 deletions mmv1/templates/terraform/encoders/logging_log_scope.go.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<%- # the license inside this block applies to this file
# Copyright 2023 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.
-%>
// Extract any empty fields from the bucket field.
// Extract parent, location from name
parent := d.Get("parent").(string)
name := d.Get("name").(string)
parent, err := tpgresource.ExtractFieldByPattern("parent", parent, name, "((projects|folders|organizations|billingAccounts)/[a-z0-9A-Z-]*)/locations/.*")
if err != nil {
return nil, fmt.Errorf("error extracting parent field: %s", err)
}
location := d.Get("location").(string)
location, err = tpgresource.ExtractFieldByPattern("location", location, name, "[a-zA-Z]*/[a-z0-9A-Z-]*/locations/([a-z0-9-]*)/logScopes/.*")
if err != nil {
return nil, fmt.Errorf("error extracting location field: %s", err)
}
// Set parent to the extracted value.
d.Set("parent", parent)
// Set all the other fields to their short forms before forming url and setting ID.
name = tpgresource.GetResourceNameFromSelfLink(name)
d.Set("location", location)
d.Set("name", name)
return obj, nil
11 changes: 11 additions & 0 deletions mmv1/templates/terraform/examples/logging_log_scope_basic.tf.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
resource "google_logging_log_scope" "<%= ctx[:primary_resource_id] %>" {
parent = "projects/<%= ctx[:test_env_vars]['project'] %>"
location = "global"
name = "projects/<%= ctx[:test_env_vars]['project'] %>/locations/global/logScopes/<%= ctx[:vars]['log_scope_name'] %>"
resource_names = [
"projects/<%= ctx[:test_env_vars]['project'] %>",
"projects/<%= ctx[:test_env_vars]['project'] %>/locations/global/buckets/_Default/views/<%= ctx[:vars]['log_view_name_1'] %>",
"projects/<%= ctx[:test_env_vars]['project'] %>/locations/global/buckets/_Default/views/<%= ctx[:vars]['log_view_name_2'] %>"
]
description = "A log scope configured with Terraform"
}

0 comments on commit ec8609a

Please sign in to comment.