From 783187467b06ef8d07660ec6fc9c1b2144705299 Mon Sep 17 00:00:00 2001 From: bsski Date: Mon, 16 Dec 2024 19:05:23 +0100 Subject: [PATCH 1/7] PC-14787 Enable specifying project in TF import command --- README.md | 2 +- nobl9/helpers.go | 8 ++++++++ nobl9/resource_agent.go | 13 ++++++++++++- nobl9/resource_direct.go | 15 +++++++++++++-- nobl9/resource_slo.go | 13 ++++++++++++- 5 files changed, 46 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 345b1ef7..600b66c9 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,6 @@ resource "nobl9_service" "test" { # Documentation -Generated documentation is located under [docs](./docs) folder. +Generated documentation is located under [docs](./docs) folder. It's available online [here](https://registry.terraform.io/providers/nobl9/nobl9/latest/docs). Developers' documentation sits under [dev-docs](./dev-docs) folder. diff --git a/nobl9/helpers.go b/nobl9/helpers.go index 12ffdb7b..3ad1e04b 100644 --- a/nobl9/helpers.go +++ b/nobl9/helpers.go @@ -86,3 +86,11 @@ func equalSlices(a, b []interface{}) bool { } return true } + +func parseImportID(id string) (project string, resourceID string) { + parts := strings.Split(id, "/") + if len(parts) == 1 { + return "", id + } + return parts[0], parts[1] +} diff --git a/nobl9/resource_agent.go b/nobl9/resource_agent.go index bd572b43..0f01585f 100644 --- a/nobl9/resource_agent.go +++ b/nobl9/resource_agent.go @@ -30,7 +30,7 @@ func resourceAgent() *schema.Resource { DeleteContext: resourceAgentDelete, ReadContext: resourceAgentRead, Importer: &schema.ResourceImporter{ - StateContext: schema.ImportStatePassthroughContext, + StateContext: resourceAgentImport, }, Description: "[Agent configuration | Nobl9 Documentation](https://docs.nobl9.com/nobl9-agent/)", } @@ -213,6 +213,17 @@ func resourceAgentDelete(ctx context.Context, d *schema.ResourceData, meta inter return nil } +func resourceAgentImport(_ context.Context, d *schema.ResourceData, _ interface{}) ([]*schema.ResourceData, error) { + project, resourceID := parseImportID(d.Id()) + if project != "" { + if err := d.Set("project", project); err != nil { + return nil, fmt.Errorf("error setting project: %w", err) + } + } + d.SetId(resourceID) + return []*schema.ResourceData{d}, nil +} + //nolint:unparam func marshalAgent(d resourceInterface) (*v1alphaAgent.Agent, diag.Diagnostics) { var displayName string diff --git a/nobl9/resource_direct.go b/nobl9/resource_direct.go index 5e1f8f68..af0e9ba2 100644 --- a/nobl9/resource_direct.go +++ b/nobl9/resource_direct.go @@ -3,6 +3,7 @@ package nobl9 import ( "context" "errors" + "fmt" "time" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -60,7 +61,7 @@ func resourceDirectFactory(directSpec directSpecResource) *schema.Resource { DeleteContext: i.resourceDirectDelete, ReadContext: i.resourceDirectRead, Importer: &schema.ResourceImporter{ - StateContext: schema.ImportStatePassthroughContext, + StateContext: resourceDirectImport, }, Description: directSpec.GetDescription(), } @@ -137,7 +138,6 @@ func (dr directResource) resourceDirectRead( project := d.Get("project").(string) if project == "" { - // project is empty when importing project = config.Project } directs, err := client.Objects().V1().GetV1alphaDirects(ctx, v1Objects.GetDirectsRequest{ @@ -178,6 +178,17 @@ func (dr directResource) resourceDirectDelete( return nil } +func resourceDirectImport(_ context.Context, d *schema.ResourceData, _ interface{}) ([]*schema.ResourceData, error) { + project, resourceID := parseImportID(d.Id()) + if project != "" { + if err := d.Set("project", project); err != nil { + return nil, fmt.Errorf("error setting project: %w", err) + } + } + d.SetId(resourceID) + return []*schema.ResourceData{d}, nil +} + //nolint:unparam func (dr directResource) marshalDirect(r resourceInterface) (*v1alphaDirect.Direct, diag.Diagnostics) { var diags diag.Diagnostics diff --git a/nobl9/resource_slo.go b/nobl9/resource_slo.go index 14768938..67a7d75e 100644 --- a/nobl9/resource_slo.go +++ b/nobl9/resource_slo.go @@ -34,7 +34,7 @@ func resourceSLO() *schema.Resource { DeleteContext: resourceSLODelete, ReadContext: resourceSLORead, Importer: &schema.ResourceImporter{ - StateContext: schema.ImportStatePassthroughContext, + StateContext: resourceSLOImport, }, Description: "[SLO configuration documentation](https://docs.nobl9.com/yaml-guide#slo)", } @@ -456,6 +456,17 @@ func resourceSLODelete(ctx context.Context, d *schema.ResourceData, meta interfa return nil } +func resourceSLOImport(_ context.Context, d *schema.ResourceData, _ interface{}) ([]*schema.ResourceData, error) { + project, resourceID := parseImportID(d.Id()) + if project != "" { + if err := d.Set("project", project); err != nil { + return nil, fmt.Errorf("error setting project: %w", err) + } + } + d.SetId(resourceID) + return []*schema.ResourceData{d}, nil +} + func schemaMetricSpec() *schema.Resource { metricSchemaDefinitions := []map[string]*schema.Schema{ schemaMetricAmazonPrometheus(), From 6193c13d537166acc4c03b0f64d48791a4684f0c Mon Sep 17 00:00:00 2001 From: bsski Date: Mon, 16 Dec 2024 19:07:40 +0100 Subject: [PATCH 2/7] PC-14787 Fix too long line in README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 600b66c9..048bdc0f 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,7 @@ resource "nobl9_service" "test" { # Documentation -Generated documentation is located under [docs](./docs) folder. It's available online [here](https://registry.terraform.io/providers/nobl9/nobl9/latest/docs). +Generated documentation is located under [docs](./docs) folder. +It's available online [here](https://registry.terraform.io/providers/nobl9/nobl9/latest/docs). Developers' documentation sits under [dev-docs](./dev-docs) folder. From 9ac41cb6c22f7edbabeece9bb3e690763e34315d Mon Sep 17 00:00:00 2001 From: bsski Date: Mon, 16 Dec 2024 19:18:21 +0100 Subject: [PATCH 3/7] PC-14787 Add docs --- docs/index.md | 2 ++ templates/index.md.tmpl | 2 ++ 2 files changed, 4 insertions(+) diff --git a/docs/index.md b/docs/index.md index 07c76d7e..633d63cd 100644 --- a/docs/index.md +++ b/docs/index.md @@ -27,6 +27,8 @@ The Nobl9 Terraform Provider does not support the configuration of the following - [SLO Annotations](https://docs.nobl9.com/features/slo-annotations/) - [Alert Silence](https://docs.nobl9.com/alerting/alert-silence/) +The Nobl9 Terraform Provider supports `terraform import` command. For resources that are project-bound, use `project_name/service_name` format. + ## Configuration To start using Nobl9 Terraform Provider, you must configure the provider with the proper credentials. Then, use the navigation on the left to learn more about the available resources. diff --git a/templates/index.md.tmpl b/templates/index.md.tmpl index 1eba5df9..fba4e12f 100644 --- a/templates/index.md.tmpl +++ b/templates/index.md.tmpl @@ -27,6 +27,8 @@ The Nobl9 Terraform Provider does not support the configuration of the following - [SLO Annotations](https://docs.nobl9.com/features/slo-annotations/) - [Alert Silence](https://docs.nobl9.com/alerting/alert-silence/) +The Nobl9 Terraform Provider supports `terraform import` command. For resources that are project-bound, use `project_name/service_name` format. + ## Configuration To start using Nobl9 Terraform Provider, you must configure the provider with the proper credentials. Then, use the navigation on the left to learn more about the available resources. From 35659e9e26210abbe03c798e4f3e74aaed1a8679 Mon Sep 17 00:00:00 2001 From: bsski Date: Mon, 16 Dec 2024 20:06:38 +0100 Subject: [PATCH 4/7] PC-14787 Fix lint: param type combine --- nobl9/helpers.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nobl9/helpers.go b/nobl9/helpers.go index 3ad1e04b..456a77bc 100644 --- a/nobl9/helpers.go +++ b/nobl9/helpers.go @@ -87,7 +87,7 @@ func equalSlices(a, b []interface{}) bool { return true } -func parseImportID(id string) (project string, resourceID string) { +func parseImportID(id string) (project, resourceID string) { parts := strings.Split(id, "/") if len(parts) == 1 { return "", id From 8e2c90290d021b8ccc3ea9123d4073a2ce2e5ca0 Mon Sep 17 00:00:00 2001 From: bsski <50927653+BSski@users.noreply.github.com> Date: Tue, 17 Dec 2024 11:14:23 +0100 Subject: [PATCH 5/7] Update templates/index.md.tmpl Co-authored-by: Tomek Labuk <89924840+labtom@users.noreply.github.com> --- templates/index.md.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/index.md.tmpl b/templates/index.md.tmpl index fba4e12f..35b50acf 100644 --- a/templates/index.md.tmpl +++ b/templates/index.md.tmpl @@ -27,7 +27,7 @@ The Nobl9 Terraform Provider does not support the configuration of the following - [SLO Annotations](https://docs.nobl9.com/features/slo-annotations/) - [Alert Silence](https://docs.nobl9.com/alerting/alert-silence/) -The Nobl9 Terraform Provider supports `terraform import` command. For resources that are project-bound, use `project_name/service_name` format. +The Nobl9 Terraform Provider supports `terraform import` command. For project-bound resources, use `project_name/service_name` format. ## Configuration From 2bf8e5cce33097c59dde90b20af55fab1ea433d8 Mon Sep 17 00:00:00 2001 From: bsski <50927653+BSski@users.noreply.github.com> Date: Tue, 17 Dec 2024 11:14:29 +0100 Subject: [PATCH 6/7] Update docs/index.md Co-authored-by: Tomek Labuk <89924840+labtom@users.noreply.github.com> --- docs/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/index.md b/docs/index.md index 633d63cd..c3d08f9a 100644 --- a/docs/index.md +++ b/docs/index.md @@ -27,7 +27,7 @@ The Nobl9 Terraform Provider does not support the configuration of the following - [SLO Annotations](https://docs.nobl9.com/features/slo-annotations/) - [Alert Silence](https://docs.nobl9.com/alerting/alert-silence/) -The Nobl9 Terraform Provider supports `terraform import` command. For resources that are project-bound, use `project_name/service_name` format. +The Nobl9 Terraform Provider supports `terraform import` command. For project-bound resources, use `project_name/service_name` format. ## Configuration From ac8a0d7365b0118c9871eee37bb50b3759f75279 Mon Sep 17 00:00:00 2001 From: bsski Date: Thu, 19 Dec 2024 14:52:51 +0100 Subject: [PATCH 7/7] PC-14787 Fix typo --- docs/index.md | 2 +- templates/index.md.tmpl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/index.md b/docs/index.md index c3d08f9a..e5d681e9 100644 --- a/docs/index.md +++ b/docs/index.md @@ -27,7 +27,7 @@ The Nobl9 Terraform Provider does not support the configuration of the following - [SLO Annotations](https://docs.nobl9.com/features/slo-annotations/) - [Alert Silence](https://docs.nobl9.com/alerting/alert-silence/) -The Nobl9 Terraform Provider supports `terraform import` command. For project-bound resources, use `project_name/service_name` format. +The Nobl9 Terraform Provider supports `terraform import` command. For project-bound resources, use `project_name/resource_name` format. ## Configuration diff --git a/templates/index.md.tmpl b/templates/index.md.tmpl index 35b50acf..0bc7a8c0 100644 --- a/templates/index.md.tmpl +++ b/templates/index.md.tmpl @@ -27,7 +27,7 @@ The Nobl9 Terraform Provider does not support the configuration of the following - [SLO Annotations](https://docs.nobl9.com/features/slo-annotations/) - [Alert Silence](https://docs.nobl9.com/alerting/alert-silence/) -The Nobl9 Terraform Provider supports `terraform import` command. For project-bound resources, use `project_name/service_name` format. +The Nobl9 Terraform Provider supports `terraform import` command. For project-bound resources, use `project_name/resource_name` format. ## Configuration