Skip to content

Commit

Permalink
Migrate inspec project_metric (logging_metric in normal land), test f…
Browse files Browse the repository at this point in the history
…ixes
  • Loading branch information
slevenick committed Jan 22, 2020
1 parent 13959ea commit 97516db
Show file tree
Hide file tree
Showing 16 changed files with 74 additions and 7 deletions.
6 changes: 6 additions & 0 deletions products/logging/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ objects:
A concise name for the metric, which can be displayed in user interfaces. Use sentence case
without an ending period, for example "Request count". This field is optional but it is
recommended to be set for any metrics associated with user-visible concepts, such as Quota.
- !ruby/object:Api::Type::String
name: type
output: true
description: |
The metric type, including its DNS name prefix. The type is not URL-encoded.
All user-defined metric types have the DNS name `custom.googleapis.com` or `external.googleapis.com`.
- !ruby/object:Api::Type::KeyValuePairs
name: labelExtractors
description: |
Expand Down
9 changes: 8 additions & 1 deletion products/logging/inspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@
--- !ruby/object:Provider::Inspec::Config
overrides: !ruby/object:Overrides::ResourceOverrides
Metric: !ruby/object:Overrides::Inspec::ResourceOverride
exclude: true
resource_name: google_project_metric
plural_custom_logic: third_party/inspec/custom_functions/google_project_metrics.erb
plural_custom_attr_readers: ['metric_type']
properties:
name: !ruby/object:Overrides::Inspec::PropertyOverride
override_name: metric_name
filter: !ruby/object:Overrides::Inspec::PropertyOverride
override_name: metric_filter
OrganizationLogSink: !ruby/object:Overrides::Inspec::ResourceOverride
# Creating a log sink requires organization level privileges
privileged: true
Expand Down
2 changes: 2 additions & 0 deletions products/resourcemanager/inspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ overrides: !ruby/object:Overrides::ResourceOverrides
name: project_id
name: !ruby/object:Overrides::Inspec::PropertyOverride
override_name: project_name
number: !ruby/object:Overrides::Inspec::PropertyOverride
override_name: project_number
iam_policy: !ruby/object:Api::Resource::IamPolicy
exclude: false
method_name_separator: ':'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ describe google_cloud_scheduler_job(project: <%= doc_generation ? "#{gcp_project
its('description') { should cmp <%= doc_generation ? "'#{scheduler_job['description']}'" : "scheduler_job['description']" -%> }
its('schedule') { should cmp <%= doc_generation ? "'#{scheduler_job['schedule']}'" : "scheduler_job['schedule']" -%> }
its('time_zone') { should cmp <%= doc_generation ? "'#{scheduler_job['time_zone']}'" : "scheduler_job['time_zone']" -%> }
its('attempt_deadline') { should cmp <%= doc_generation ? "'#{scheduler_job['attempt_deadline']}'" : "scheduler_job['attempt_deadline']" -%> }
its('http_target.http_method') { should cmp <%= doc_generation ? "'#{scheduler_job['http_method']}'" : "scheduler_job['http_method']" -%> }
its('http_target.uri') { should cmp <%= doc_generation ? "'#{scheduler_job['http_target_uri']}'" : "scheduler_job['http_target_uri']" -%> }
end
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ google_cloud_scheduler_jobs(project: <%= doc_generation ? "#{gcp_project_id}" :
its('description') { should cmp <%= doc_generation ? "'#{scheduler_job['description']}'" : "scheduler_job['description']" -%> }
its('schedule') { should cmp <%= doc_generation ? "'#{scheduler_job['schedule']}'" : "scheduler_job['schedule']" -%> }
its('time_zone') { should cmp <%= doc_generation ? "'#{scheduler_job['time_zone']}'" : "scheduler_job['time_zone']" -%> }
its('attempt_deadline') { should cmp <%= doc_generation ? "'#{scheduler_job['attempt_deadline']}'" : "scheduler_job['attempt_deadline']" -%> }
its('http_target.http_method') { should cmp <%= doc_generation ? "'#{scheduler_job['http_method']}'" : "scheduler_job['http_method']" -%> }
its('http_target.uri') { should cmp <%= doc_generation ? "'#{scheduler_job['http_target_uri']}'" : "scheduler_job['http_target_uri']" -%> }
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<% gcp_project_id = "#{external_attribute('gcp_project_id', doc_generation)}" -%>
describe google_compute_zone(project: <%= gcp_project_id -%>, "us-central1-a") do
describe google_compute_zone(project: <%= gcp_project_id -%>, name: "us-central1-a") do
it { should exist }
it { should be_up }
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<% gcp_project_id = "#{external_attribute('gcp_project_id', doc_generation)}" -%>
google_compute_zones(project: <%= gcp_project_id -%>).zone_names.each do |zone_name|
describe google_compute_zones(project: <%= gcp_project_id -%>, name: zone_name) do
describe google_compute_zone(project: <%= gcp_project_id -%>, name: zone_name) do
it { should exist }
it { should be_up }
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<% gcp_project_id = "#{external_attribute('gcp_project_id', doc_generation)}" -%>
<% logging_metric = grab_attributes['logging_metric'] -%>
describe google_project_metric(project: <%= gcp_project_id -%>, name: <%= doc_generation ? "'#{logging_metric['name']}'" : "logging_metric['name']" -%>) do
it { should exist }
its('filter') { should cmp <%= doc_generation ? "'#{logging_metric['filter']}'" : "logging_metric['filter']" -%> }
its('metric_descriptor.metric_kind') { should cmp <%= doc_generation ? "'#{logging_metric['metric_kind']}'" : "logging_metric['metric_kind']" -%> }
its('metric_descriptor.value_type') { should cmp <%= doc_generation ? "'#{logging_metric['value_type']}'" : "logging_metric['value_type']" -%> }
end

describe google_project_metric(project: <%= gcp_project_id -%>, name: 'nonexistent') do
it { should_not exist }
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
gcp_project_id = attribute(:gcp_project_id, default: '<%= external_attribute('gcp_project_id') -%>', description: 'The GCP project identifier.')
logging_metric = attribute('logging_metric', default: <%= JSON.pretty_generate(grab_attributes['logging_metric']) -%>, description: 'Logging metric definition')
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<% gcp_project_id = "#{external_attribute('gcp_project_id', doc_generation)}" -%>
<% logging_metric = grab_attributes['logging_metric'] -%>
describe google_project_metrics(project: <%= gcp_project_id -%>) do
it { should exist }
its('metric_filters') { should include <%= doc_generation ? "'#{logging_metric['filter']}'" : "logging_metric['filter']" -%> }
its('metric_names') { should include <%= doc_generation ? "'#{logging_metric['name']}'" : "logging_metric['name']" -%> }
end

describe.one do
google_project_metrics(project: <%= gcp_project_id -%>).metric_types.each do |metric_type|
describe metric_type do
it { should match <%= doc_generation ? "'#{logging_metric['name']}'" : "logging_metric['name']" -%> }
end
end
end
14 changes: 14 additions & 0 deletions templates/inspec/tests/integration/build/gcp-mm.tf
Original file line number Diff line number Diff line change
Expand Up @@ -1173,3 +1173,17 @@ resource "google_dns_managed_zone" "example-zone" {
}
}
}

variable "logging_metric" {
type = any
}

resource "google_logging_metric" "logging_metric" {
project = var.gcp_project_id
name = var.logging_metric["name"]
filter = var.logging_metric["filter"]
metric_descriptor {
metric_kind = var.logging_metric["metric_kind"]
value_type = var.logging_metric["value_type"]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -427,3 +427,9 @@ dns_managed_zone:
name: example-zone
description: example description
dnssec_config_state: 'on'

logging_metric:
name: some/metric
filter: 'resource.type=gae_app AND severity>=ERROR'
metric_kind: DELTA
value_type: INT64
2 changes: 1 addition & 1 deletion third_party/inspec/custom_functions/dns_managed_zones.erb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
hash_with_symbols[:dnssec_config_state] = hash['dnssecConfig']&.['state']&.downcase == 'on'
hash_with_symbols[:dnssec_config_state] = hash.dig('dnssecConfig', 'state')&.downcase == 'on'
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# helper method for retrieving a region name
def region_name
@region&.split('/').last
@region&.split('/')&.last
end

def up?
Expand Down
4 changes: 4 additions & 0 deletions third_party/inspec/custom_functions/google_project.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ def project_name
@name
end

def project_number
@number&.to_i
end

def label_value_by_key(label_key)
return nil if @labels.nil? || [email protected]_key(label_key)
@labels[label_key]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hash_with_symbols[:metric_type] = hash.dig('metricDescriptor', 'type')

0 comments on commit 97516db

Please sign in to comment.