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

Fix google_monitoring_slo import #11696

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

nyuta01
Copy link

@nyuta01 nyuta01 commented May 13, 2022

Description

Fixed a problem that the service field was null when importing google_monitoring_slo.
The current version of terraform-provider-google was implemented in such a way that the value of service was null when importing google_monitoring_slo, and it was regenerated when the apply was executed.
In this PR, the service value is changed to be set when import is executed.

OAT

Before

$ terraform import -state=tmp.tfstate google_monitoring_custom_service.hoge CUSTOM_SERVICE_ID
google_monitoring_custom_service.hoge: Importing from ID "CUSTOM_SERVICE_ID"...
google_monitoring_custom_service.hoge: Import prepared!
  Prepared google_monitoring_custom_service for import
google_monitoring_custom_service.hoge: Refreshing state... [id=CUSTOM_SERVICE_ID]

Import successful!

The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.

$ terraform import -state=tmp.tfstate google_monitoring_slo.hoge SLO_ID
google_monitoring_slo.hoge: Importing from ID "SLO_ID"...
google_monitoring_slo.hoge: Import prepared!
  Prepared google_monitoring_slo for import
google_monitoring_slo.hoge: Refreshing state... [id=SLO_ID]

Import successful!

The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.

$ cat tmp.tfstate
{
  "version": 4,
  "terraform_version": "1.1.7",
  "serial": 2,
  "lineage": "503a2f57-8b7a-fbc9-b0a1-82e860b46776",
  "outputs": {},
  "resources": [
    {
      "mode": "managed",
      "type": "google_monitoring_custom_service",
      "name": "hoge",
      "provider": "provider[\"registry.terraform.io/hashicorp/google\"]",
      "instances": [
        {
          "schema_version": 0,
          "attributes": {
            "display_name": "SERVICE_ID",
            "id": "CUSTOM_SERVICE_ID",
            "name": "CUSTOM_SERVICE_ID",
            "project": "PROJECT_ID",
            "service_id": "SERVICE_ID",
            "telemetry": [],
            "timeouts": {
              "create": null,
              "delete": null,
              "update": null
            }
          },
          "sensitive_attributes": [],
          "private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjoxMjAwMDAwMDAwMDAwLCJkZWxldGUiOjEyMDAwMDAwMDAwMDAsInVwZGF0ZSI6MTIwMDAwMDAwMDAwMH0sInNjaGVtYV92ZXJzaW9uIjoiMCJ9"
        }
      ]
    },
    {
      "mode": "managed",
      "type": "google_monitoring_slo",
      "name": "hoge",
      "provider": "provider[\"registry.terraform.io/hashicorp/google\"]",
      "instances": [
        {
          "schema_version": 0,
          "attributes": {
            "basic_sli": [],
            "calendar_period": "WEEK",
            "display_name": "hoge",
            "goal": 0.8,
            "id": "SLO_ID",
            "name": "SLO_ID",
            "project": "PROJECT_ID",
            "request_based_sli": [
              {
                "distribution_cut": [
                  {
                    distribution_filter = "metric.type=\"serviceruntime.googleapis.com/api/request_latencies\" resource.type=\"api\"  "
                    "range": [
                      {
                        "max": 10,
                        "min": 0
                      }
                    ]
                  }
                ],
                "good_total_ratio": []
              }
            ],
            "rolling_period_days": 0,
            "service": null,
            "slo_id": "SERVICE_ID-hoge",
            "timeouts": {
              "create": null,
              "delete": null,
              "update": null
            },
            "windows_based_sli": []
          },
          "sensitive_attributes": [],
          "private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjoxMjAwMDAwMDAwMDAwLCJkZWxldGUiOjEyMDAwMDAwMDAwMDAsInVwZGF0ZSI6MTIwMDAwMDAwMDAwMH0sInNjaGVtYV92ZXJzaW9uIjoiMCJ9"
        }
      ]
    }
  ]
}

$ terraform plan -state=tmp.tfstate
google_monitoring_custom_service.hoge: Refreshing state... [id=CUSTOM_SERVICE_ID]
google_monitoring_slo.hoge: Refreshing state... [id=SLO_ID]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
-/+ destroy and then create replacement

Terraform will perform the following actions:

  # google_monitoring_slo.hoge must be replaced
-/+ resource "google_monitoring_slo" "hoge" {
      ~ id                  = "SLO_ID" -> (known after apply)
      ~ name                = "SLO_ID" -> (known after apply)
      ~ project             = "PROJECT_ID" -> (known after apply)
      - rolling_period_days = 0 -> null
      + service             = "SERVICE_ID" # forces replacement
        # (4 unchanged attributes hidden)


      - timeouts {}
        # (1 unchanged block hidden)
    }

Plan: 1 to add, 0 to change, 1 to destroy.

─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.

After

$ terraform import -state=tmp.tfstate google_monitoring_custom_service.hoge CUSTOM_SERVICE_ID
google_monitoring_custom_service.hoge: Importing from ID "CUSTOM_SERVICE_ID"...
google_monitoring_custom_service.hoge: Import prepared!
  Prepared google_monitoring_custom_service for import
google_monitoring_custom_service.hoge: Refreshing state... [id=CUSTOM_SERVICE_ID]

Import successful!

The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.

$ terraform import -state=tmp.tfstate google_monitoring_slo.hoge SLO_ID
google_monitoring_slo.hoge: Importing from ID "SLO_ID"...
google_monitoring_slo.hoge: Import prepared!
  Prepared google_monitoring_slo for import
google_monitoring_slo.hoge: Refreshing state... [id=SLO_ID]

Import successful!

The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.

$ cat tmp.tfstate
{
  "version": 4,
  "terraform_version": "1.1.7",
  "serial": 2,
  "lineage": "a1ab70dd-8283-420a-a6ac-4ec2afbcdfb4",
  "outputs": {},
  "resources": [
    {
      "mode": "managed",
      "type": "google_monitoring_custom_service",
      "name": "hoge",
      "provider": "provider[\"registry.terraform.io/hashicorp/google\"]",
      "instances": [
        {
          "schema_version": 0,
          "attributes": {
            "display_name": "SERVICE_ID",
            "id": "CUSTOM_SERVICE_ID",
            "name": "CUSTOM_SERVICE_ID",
            "project": "PROJECT_ID",
            "service_id": "SERVICE_ID",
            "telemetry": [],
            "timeouts": {
              "create": null,
              "delete": null,
              "update": null
            }
          },
          "sensitive_attributes": [],
          "private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjoxMjAwMDAwMDAwMDAwLCJkZWxldGUiOjEyMDAwMDAwMDAwMDAsInVwZGF0ZSI6MTIwMDAwMDAwMDAwMH0sInNjaGVtYV92ZXJzaW9uIjoiMCJ9"
        }
      ]
    },
    {
      "mode": "managed",
      "type": "google_monitoring_slo",
      "name": "hoge",
      "provider": "provider[\"registry.terraform.io/hashicorp/google\"]",
      "instances": [
        {
          "schema_version": 0,
          "attributes": {
            "basic_sli": [],
            "calendar_period": "WEEK",
            "display_name": "hoge",
            "goal": 0.8,
            "id": "SLO_ID",
            "name": "SLO_ID",
            "project": "PROJECT_ID",
            "request_based_sli": [
              {
                "distribution_cut": [
                  {
                    distribution_filter = "metric.type=\"serviceruntime.googleapis.com/api/request_latencies\" resource.type=\"api\"  "
                    "range": [
                      {
                        "max": 10,
                        "min": 0
                      }
                    ]
                  }
                ],
                "good_total_ratio": []
              }
            ],
            "rolling_period_days": 0,
            "service": "SERVICE_ID",
            "slo_id": "SERVICE_ID-hoge",
            "timeouts": {
              "create": null,
              "delete": null,
              "update": null
            },
            "windows_based_sli": []
          },
          "sensitive_attributes": [],
          "private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjoxMjAwMDAwMDAwMDAwLCJkZWxldGUiOjEyMDAwMDAwMDAwMDAsInVwZGF0ZSI6MTIwMDAwMDAwMDAwMH0sInNjaGVtYV92ZXJzaW9uIjoiMCJ9"
        }
      ]
    }
  ]
}
$ terraform plan -state=tmp.tfstate
╷
│ Warning: Provider development overrides are in effect
│
│ The following provider development overrides are set in the CLI configuration:
│  - hashicorp/google in nyuta/google
│
│ The behavior may therefore not match any released version of the provider and applying changes may cause the state to become incompatible with published releases.
╵
google_monitoring_custom_service.hoge: Refreshing state... [id=CUSTOM_SERVICE_ID]
google_monitoring_slo.hoge: Refreshing state... [id=SLO_ID]

No changes. Your infrastructure matches the configuration.

Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed.

@github-actions github-actions bot requested a review from rileykarson May 13, 2022 12:11
@nyuta01 nyuta01 changed the title Fix MonitoringSlo Read Fix google_monitoring_slo import May 13, 2022
@rileykarson
Copy link
Collaborator

Hey @nyuta01! As a generated file, contributions to this resource need to be made against the generator source in https://github.com/GoogleCloudPlatform/magic-modules. This field may be difficult to change, as it's currently expected to not read from the API based on https://github.com/GoogleCloudPlatform/magic-modules/blob/7f5dc1a44c1bbc68306977b232e17a88cbbf0258/mmv1/products/monitoring/api.yaml#L1083 (but there is definitely a bug w/ the import format)

@nyuta01
Copy link
Author

nyuta01 commented May 19, 2022

@rileykarson

Thanks for your review!
I see, so this resource is automatically generated from magic-modules.
If so, it seems that it is the implementation of magic-modules that needs to be modified.
By the way, am I correct in understanding that when you make modifications to magic-modules, i should follow the link below and add a new custom_flatten for service property?
https://github.com/GoogleCloudPlatform/magic-modules/blob/07f36b0b334afd02d5bfa057f09ec5907e33b4aa/mmv1/products/monitoring/terraform.yaml#L180

Also, the current process causes the SLO service to be replaced when I run apply after import.
Is there any good solution to this problem?

@rileykarson rileykarson requested review from c2thorn and removed request for rileykarson June 7, 2022 16:31
@c2thorn
Copy link
Collaborator

c2thorn commented Jun 8, 2022

Hi @nyuta01,
It would likely be best to add a custom import function that parses the service from the name field. You can find a similar example of this with
https://github.com/GoogleCloudPlatform/magic-modules/blob/main/mmv1/products/datacatalog/terraform.yaml#L40
and https://github.com/GoogleCloudPlatform/magic-modules/blob/main/mmv1/templates/terraform/custom_import/data_catalog_entry_group.go.erb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants