-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate monitoring monitored project to mmv1 (#8008)
* Migrate monitoring monitored project to mmv1 * Use function from tpgresource package * Remove extra blank line
- Loading branch information
Showing
11 changed files
with
158 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# 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. | ||
|
||
--- !ruby/object:Api::Resource | ||
name: MonitoredProject | ||
base_url: v1/locations/global/metricsScopes | ||
create_url: v1/locations/global/metricsScopes/{{metrics_scope}}/projects | ||
delete_url: v1/locations/global/metricsScopes/{{metrics_scope}}/projects/{{name}} | ||
self_link: v1/locations/global/metricsScopes/{{metrics_scope}} | ||
id_format: v1/locations/global/metricsScopes/{{metrics_scope}}/projects/{{name}} | ||
import_format: | ||
- v1/locations/global/metricsScopes/{{metrics_scope}}/projects/{{name}} | ||
references: !ruby/object:Api::Resource::ReferenceLinks | ||
guides: | ||
'Official Documentation': 'https://cloud.google.com/monitoring/settings/manage-api' | ||
api: 'https://cloud.google.com/monitoring/api/ref_v3/rest/v1/locations.global.metricsScopes.projects' | ||
nested_query: !ruby/object:Api::Resource::NestedQuery | ||
keys: | ||
- monitoredProjects | ||
description: "A [project being monitored](https://cloud.google.com/monitoring/settings/multiple-projects#create-multi) by a Metrics Scope." | ||
immutable: true | ||
custom_code: !ruby/object:Provider::Terraform::CustomCode | ||
encoder: templates/terraform/encoders/monitoring_monitored_project.go.erb | ||
decoder: templates/terraform/decoders/monitoring_monitored_project.go.erb | ||
test_check_destroy: templates/terraform/custom_check_destroy/monitoring_monitored_project.go.erb | ||
examples: | ||
- !ruby/object:Provider::Terraform::Examples | ||
name: 'monitoring_monitored_project_basic' | ||
primary_resource_id: 'primary' | ||
vars: | ||
monitored_project: 'm-id' | ||
test_env_vars: | ||
org_id: :ORG_ID | ||
project_id: :PROJECT_NAME | ||
parameters: | ||
- !ruby/object:Api::Type::String | ||
name: metricsScope | ||
description: 'Required. The resource name of the existing Metrics Scope that will monitor this project. Example: locations/global/metricsScopes/{SCOPING_PROJECT_ID_OR_NUMBER}' | ||
url_param_only: true | ||
required: true | ||
properties: | ||
- !ruby/object:Api::Type::String | ||
name: name | ||
description: 'Immutable. The resource name of the `MonitoredProject`. On input, the resource name includes the scoping project ID and monitored project ID. On output, it contains the equivalent project numbers. Example: `locations/global/metricsScopes/{SCOPING_PROJECT_ID_OR_NUMBER}/projects/{MONITORED_PROJECT_ID_OR_NUMBER}`' | ||
required: true | ||
- !ruby/object:Api::Type::String | ||
name: createTime | ||
description: Output only. The time when this `MonitoredProject` was created. | ||
output: true |
44 changes: 44 additions & 0 deletions
44
mmv1/templates/terraform/custom_check_destroy/monitoring_monitored_project.go.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<%# 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. | ||
-%> | ||
config := GoogleProviderConfig(t) | ||
|
||
url, err := tpgresource.ReplaceVarsForTest(config, rs, "{{MonitoringBasePath}}v1/locations/global/metricsScopes/{{metrics_scope}}") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
billingProject := "" | ||
|
||
if config.BillingProject != "" { | ||
billingProject = config.BillingProject | ||
} | ||
|
||
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ | ||
Config: config, | ||
Method: "GET", | ||
Project: billingProject, | ||
RawURL: url, | ||
UserAgent: config.UserAgent, | ||
}) | ||
|
||
rName := tpgresource.GetResourceNameFromSelfLink(rs.Primary.Attributes["name"]) | ||
project, err := config.NewResourceManagerClient(config.UserAgent).Projects.Get(rName).Do() | ||
rName = strconv.FormatInt(project.ProjectNumber, 10) | ||
|
||
for _, monitoredProject := range res["monitoredProjects"].([]any) { | ||
if strings.HasSuffix(monitoredProject.(map[string]any)["name"].(string), rName) { | ||
return fmt.Errorf("MonitoringMonitoredProject still exists at %s", url) | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
mmv1/templates/terraform/decoders/monitoring_monitored_project.go.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<%# 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. | ||
-%> | ||
config := meta.(*transport_tpg.Config) | ||
name := res["name"].(string) | ||
name = tpgresource.GetResourceNameFromSelfLink(name) | ||
if name != "" { | ||
project, err := config.NewResourceManagerClient(config.UserAgent).Projects.Get(name).Do() | ||
if err != nil { | ||
return nil, err | ||
} | ||
res["name"] = project.Name | ||
} | ||
return res, nil |
20 changes: 20 additions & 0 deletions
20
mmv1/templates/terraform/encoders/monitoring_monitored_project.go.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<%- # 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. | ||
-%> | ||
name := d.Get("name").(string) | ||
name = tpgresource.GetResourceNameFromSelfLink(name) | ||
metricsScope := d.Get("metrics_scope").(string) | ||
metricsScope = tpgresource.GetResourceNameFromSelfLink(metricsScope) | ||
obj["name"] = fmt.Sprintf("locations/global/metricsScopes/%s/projects/%s", metricsScope, name) | ||
return obj, nil |
10 changes: 10 additions & 0 deletions
10
mmv1/templates/terraform/examples/monitoring_monitored_project_basic.tf.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
resource "google_monitoring_monitored_project" "<%= ctx[:primary_resource_id] %>" { | ||
metrics_scope = "<%= ctx[:test_env_vars]['project_id'] %>" | ||
name = google_project.basic.name | ||
} | ||
|
||
resource "google_project" "basic" { | ||
project_id = "<%= ctx[:vars]['monitored_project'] %>" | ||
name = "<%= ctx[:vars]['monitored_project'] %>" | ||
org_id = "<%= ctx[:test_env_vars]['org_id'] %>" | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
tpgtools/overrides/monitoring/samples/monitoredproject/meta.yaml
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.