Skip to content

Commit 1c146c8

Browse files
Merge pull request #656 from inspec/CHEF-12238-V2-MAGIC-MODULE-monitoring_v3-Projects__group
CHEF-12238-V2-MAGIC-MODULE-monitoring_v3-Projects__group - Resource Implementation
2 parents 19dcf30 + f145f4b commit 1c146c8

9 files changed

Lines changed: 354 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ The following resources are available in the InSpec GCP Profile
329329
| [google_logging_project_sink](docs/resources/google_logging_project_sink.md) | [google_logging_project_sinks](docs/resources/google_logging_project_sinks.md) |
330330
| [google_memcache_instance](docs/resources/google_memcache_instance.md) | [google_memcache_instances](docs/resources/google_memcache_instances.md) |
331331
| [google_ml_engine_model](docs/resources/google_ml_engine_model.md) | [google_ml_engine_models](docs/resources/google_ml_engine_models.md) |
332+
| [google_monitoring_group](docs/resources/google_monitoring_group.md) | [google_monitoring_groups](docs/resources/google_monitoring_groups.md) |
332333
| [google_organization](docs/resources/google_organization.md) | [google_organizations](docs/resources/google_organizations.md) |
333334
| No Singular Resource | [google_orgpolicy_folder_constraints](docs/resources/google_orgpolicy_folder_constraints.md) |
334335
| No Singular Resource | [google_orgpolicy_organization_constraints](docs/resources/google_orgpolicy_project_constraints.md) |
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
+++
2+
3+
title = "google_monitoring_group Resource"
4+
platform = "gcp"
5+
draft = false
6+
gh_repo = "inspec-gcp"
7+
8+
9+
[menu.inspec]
10+
11+
title = "google_monitoring_group"
12+
identifier = "inspec/resources/gcp/google_monitoring_group Resource"
13+
parent = "inspec/resources/gcp"
14+
+++
15+
16+
Use the `google_monitoring_group` InSpec audit resource to test the properties of a test a Google Group.
17+
18+
## Installation
19+
{{% inspec_gcp_install %}}
20+
21+
## Syntax
22+
A `google_monitoring_group` is used to test a Google Group resource
23+
24+
## Examples
25+
```
26+
describe google_monitoring_group(name: 'projects/*/groups/value_group_id') do
27+
it { should exist }
28+
its('name') { should cmp 'value_name' }
29+
its('display_name') { should cmp 'value_displayname' }
30+
its('filter') { should cmp 'value_filter' }
31+
end
32+
33+
describe google_monitoring_group(name: "does_not_exit") do
34+
it { should_not exist }
35+
end
36+
```
37+
38+
## Parameters
39+
Properties that can be accessed from the `google_monitoring_group` resource:
40+
41+
## Properties
42+
Properties that can be accessed from the `google_monitoring_group` resource:
43+
44+
45+
* `name`: Output only. The name of this group. The format is: projects/[PROJECT_ID_OR_NUMBER]/groups/[GROUP_ID] When creating a group, this field is ignored and a new name is created consisting of the project specified in the call to CreateGroup and a unique [GROUP_ID] that is generated automatically.
46+
47+
* `display_name`: A user-assigned name for this group, used only for display purposes.
48+
49+
* `parent_name`: The name of the group's parent, if it has one. The format is: projects/[PROJECT_ID_OR_NUMBER]/groups/[GROUP_ID] For groups with no parent, parent_name is the empty string, "".
50+
51+
* `filter`: The filter used to determine which monitored resources belong to this group.
52+
53+
* `is_cluster`: If true, the members of this group are considered to be a cluster. The system can perform additional analysis on groups that are clusters.
54+
55+
56+
## GCP Permissions
57+
58+
Ensure the [Stackdriver Monitoring API](https://console.cloud.google.com/apis/library/monitoring.googleapis.com/) is enabled for the current project.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
+++
2+
3+
title = "google_monitoring_groups Resource"
4+
platform = "gcp"
5+
draft = false
6+
gh_repo = "inspec-gcp"
7+
8+
9+
[menu.inspec]
10+
11+
title = "google_monitoring_groups"
12+
identifier = "inspec/resources/gcp/google_monitoring_groups Resource"
13+
parent = "inspec/resources/gcp"
14+
+++
15+
16+
Use the `google_monitoring_groups` InSpec audit resource to test the properties of a test a Google Group.
17+
18+
## Installation
19+
{{% inspec_gcp_install %}}
20+
21+
## Syntax
22+
A `google_monitoring_groups` is used to test a Google Group resource
23+
24+
## Examples
25+
```
26+
describe google_monitoring_groups(name: 'projects/*') do
27+
it { should exist }
28+
its('names') { should include 'value_name' }
29+
its('display_names') { should include 'value_displayname' }
30+
its('filters') { should include 'value_filter' }
31+
end
32+
```
33+
34+
## Parameters
35+
Properties that can be accessed from the `google_monitoring_groups` resource:
36+
37+
See [google_monitoring_group.md](google_monitoring_group.md) for more detailed information
38+
* `names`: an array of `google_monitoring_group` name
39+
* `display_names`: an array of `google_monitoring_group` display_name
40+
* `parent_names`: an array of `google_monitoring_group` parent_name
41+
* `filters`: an array of `google_monitoring_group` filter
42+
* `is_clusters`: an array of `google_monitoring_group` is_cluster
43+
## Properties
44+
Properties that can be accessed from the `google_monitoring_groups` resource:
45+
46+
See [google_monitoring_group.md](google_monitoring_group.md) for more detailed information
47+
* `names`: an array of `google_monitoring_group` name
48+
* `display_names`: an array of `google_monitoring_group` display_name
49+
* `parent_names`: an array of `google_monitoring_group` parent_name
50+
* `filters`: an array of `google_monitoring_group` filter
51+
* `is_clusters`: an array of `google_monitoring_group` is_cluster
52+
53+
## Filter Criteria
54+
This resource supports all of the above properties as filter criteria, which can be used
55+
with `where` as a block or a method.
56+
57+
## GCP Permissions
58+
59+
Ensure the [Stackdriver Monitoring API](https://console.cloud.google.com/apis/library/monitoring.googleapis.com/) is enabled for the current project.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# frozen_string_literal: false
2+
3+
# ----------------------------------------------------------------------------
4+
#
5+
# *** AUTO GENERATED CODE *** Type: MMv1 ***
6+
#
7+
# ----------------------------------------------------------------------------
8+
#
9+
# This file is automatically generated by Magic Modules and manual
10+
# changes will be clobbered when the file is regenerated.
11+
#
12+
# Please read more about how to change this file in README.md and
13+
# CONTRIBUTING.md located at the root of this package.
14+
#
15+
# ----------------------------------------------------------------------------
16+
require 'gcp_backend'
17+
18+
# A provider to manage Cloud (Stackdriver) Monitoring resources.
19+
class MonitoringGroup < GcpResourceBase
20+
name 'google_monitoring_group'
21+
desc 'Group'
22+
supports platform: 'gcp'
23+
24+
attr_reader :params
25+
attr_reader :name
26+
attr_reader :display_name
27+
attr_reader :parent_name
28+
attr_reader :filter
29+
attr_reader :is_cluster
30+
31+
def initialize(params)
32+
super(params.merge({ use_http_transport: true }))
33+
@params = params
34+
@fetched = @connection.fetch(product_url(params[:beta]), resource_base_url, params, 'Get')
35+
parse unless @fetched.nil?
36+
end
37+
38+
def parse
39+
@name = @fetched['name']
40+
@display_name = @fetched['displayName']
41+
@parent_name = @fetched['parentName']
42+
@filter = @fetched['filter']
43+
@is_cluster = @fetched['isCluster']
44+
end
45+
46+
def exists?
47+
!@fetched.nil?
48+
end
49+
50+
def to_s
51+
"Group #{@params[:name]}"
52+
end
53+
54+
private
55+
56+
def product_url(_ = nil)
57+
'https://monitoring.googleapis.com/v3/'
58+
end
59+
60+
def resource_base_url
61+
'{{name}}'
62+
end
63+
end
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# frozen_string_literal: false
2+
3+
# ----------------------------------------------------------------------------
4+
#
5+
# *** AUTO GENERATED CODE *** Type: MMv1 ***
6+
#
7+
# ----------------------------------------------------------------------------
8+
#
9+
# This file is automatically generated by Magic Modules and manual
10+
# changes will be clobbered when the file is regenerated.
11+
#
12+
# Please read more about how to change this file in README.md and
13+
# CONTRIBUTING.md located at the root of this package.
14+
#
15+
# ----------------------------------------------------------------------------
16+
require 'gcp_backend'
17+
class MonitoringGroups < GcpResourceBase
18+
name 'google_monitoring_groups'
19+
desc 'Group plural resource'
20+
supports platform: 'gcp'
21+
22+
attr_reader :table
23+
24+
filter_table_config = FilterTable.create
25+
26+
filter_table_config.add(:names, field: :name)
27+
filter_table_config.add(:display_names, field: :display_name)
28+
filter_table_config.add(:parent_names, field: :parent_name)
29+
filter_table_config.add(:filters, field: :filter)
30+
filter_table_config.add(:is_clusters, field: :is_cluster)
31+
32+
filter_table_config.connect(self, :table)
33+
34+
def initialize(params = {})
35+
super(params.merge({ use_http_transport: true }))
36+
@params = params
37+
@table = fetch_wrapped_resource('group')
38+
end
39+
40+
def fetch_wrapped_resource(wrap_path)
41+
# fetch_resource returns an array of responses (to handle pagination)
42+
result = @connection.fetch_all(product_url, resource_base_url, @params, 'Get')
43+
return if result.nil?
44+
45+
# Conversion of string -> object hash to symbol -> object hash that InSpec needs
46+
converted = []
47+
result.each do |response|
48+
next if response.nil? || !response.key?(wrap_path)
49+
response[wrap_path].each do |hash|
50+
hash_with_symbols = {}
51+
hash.each_key do |key|
52+
name, value = transform(key, hash)
53+
hash_with_symbols[name] = value
54+
end
55+
converted.push(hash_with_symbols)
56+
end
57+
end
58+
59+
converted
60+
end
61+
62+
def transform(key, value)
63+
return transformers[key].call(value) if transformers.key?(key)
64+
65+
[key.to_sym, value]
66+
end
67+
68+
def transformers
69+
{
70+
'name' => ->(obj) { [:name, obj['name']] },
71+
'displayName' => ->(obj) { [:display_name, obj['displayName']] },
72+
'parentName' => ->(obj) { [:parent_name, obj['parentName']] },
73+
'filter' => ->(obj) { [:filter, obj['filter']] },
74+
'isCluster' => ->(obj) { [:is_cluster, obj['isCluster']] },
75+
}
76+
end
77+
78+
private
79+
80+
def product_url(_ = nil)
81+
'https://monitoring.googleapis.com/v3/'
82+
end
83+
84+
def resource_base_url
85+
'{{name}}/groups'
86+
end
87+
end

test/integration/build/gcp-mm.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,9 @@ variable "cloud_run_jobs" {
272272
variable "dataproc_serverless_batches" {
273273
type = any
274274
}
275+
variable "monitoring_group" {
276+
type = any
277+
}
275278
resource "google_compute_ssl_policy" "custom-ssl-policy" {
276279
name = var.ssl_policy["name"]
277280
min_tls_version = var.ssl_policy["min_tls_version"]
@@ -2272,3 +2275,8 @@ resource "google_dataproc_batch" "inspec_batch_spark" {
22722275
jar_file_uris = [var.dataproc_serverless_batches.path]
22732276
}
22742277
}
2278+
resource "google_monitoring_group" "inspec-test-group" {
2279+
project = var.gcp_project_id
2280+
display_name = var.monitoring_group.name
2281+
filter = var.monitoring_group.filter
2282+
}

test/integration/configuration/mm-attributes.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,3 +758,7 @@ dataproc_serverless_batches:
758758
main_class: "org.apache.spark.examples.SparkPi"
759759
args: "10"
760760
path: "file:///usr/lib/spark/examples/jars/spark-examples.jar"
761+
762+
monitoring_group:
763+
name: "inspec-test-group"
764+
filter: "resource.metadata.name = has_substring(\"inspec\")"
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# ----------------------------------------------------------------------------
2+
#
3+
# *** AUTO GENERATED CODE *** Type: MMv1 ***
4+
#
5+
# ----------------------------------------------------------------------------
6+
#
7+
# This file is automatically generated by Magic Modules and manual
8+
# changes will be clobbered when the file is regenerated.
9+
#
10+
# Please read more about how to change this file in README.md and
11+
# CONTRIBUTING.md located at the root of this package.
12+
#
13+
# ----------------------------------------------------------------------------
14+
15+
title 'Test GCP google_monitoring_group resource.'
16+
17+
gcp_project_id = input(:gcp_project_id, value: 'gcp_project_id', description: 'The GCP project identifier.')
18+
19+
group = input('group', value: {
20+
"name": "projects/ppradhan/groups/1973775459523949291",
21+
"display_name": "inspec-test-group",
22+
"parent_name": "projects/ppradhan",
23+
"filter": "resource.metadata.name = has_substring(\"inspec\")"
24+
}, description: 'group description')
25+
control 'google_monitoring_group-1.0' do
26+
impact 1.0
27+
title 'google_monitoring_group resource test'
28+
29+
describe google_monitoring_group(name: group['name']) do
30+
it { should exist }
31+
its('name') { should cmp group['name'] }
32+
its('display_name') { should cmp group['display_name'] }
33+
its('filter') { should cmp group['filter'] }
34+
end
35+
36+
describe google_monitoring_group(name: "does_not_exit") do
37+
it { should_not exist }
38+
end
39+
end
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# ----------------------------------------------------------------------------
2+
#
3+
# *** AUTO GENERATED CODE *** Type: MMv1 ***
4+
#
5+
# ----------------------------------------------------------------------------
6+
#
7+
# This file is automatically generated by Magic Modules and manual
8+
# changes will be clobbered when the file is regenerated.
9+
#
10+
# Please read more about how to change this file in README.md and
11+
# CONTRIBUTING.md located at the root of this package.
12+
#
13+
# ----------------------------------------------------------------------------
14+
15+
title 'Test GCP google_monitoring_groups resource.'
16+
17+
gcp_project_id = input(:gcp_project_id, value: 'gcp_project_id', description: 'The GCP project identifier.')
18+
19+
group = input('group', value: {
20+
"name": "projects/ppradhan/groups/1973775459523949291",
21+
"display_name": "inspec-test-group",
22+
"parent_name": "projects/ppradhan",
23+
"filter": "resource.metadata.name = has_substring(\"inspec\")"
24+
}, description: 'group description')
25+
control 'google_monitoring_groups-1.0' do
26+
impact 1.0
27+
title 'google_monitoring_groups resource test'
28+
29+
describe google_monitoring_groups(name: group['name']) do
30+
it { should exist }
31+
its('names') { should include group['name'] }
32+
its('display_names') { should include group['display_name'] }
33+
its('filters') { should include group['filter'] }
34+
end
35+
end

0 commit comments

Comments
 (0)