diff --git a/README.md b/README.md index 345b1ef7..048bdc0f 100644 --- a/README.md +++ b/README.md @@ -60,5 +60,6 @@ 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). Developers' documentation sits under [dev-docs](./dev-docs) folder. diff --git a/docs/index.md b/docs/index.md index 07c76d7e..e5d681e9 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 project-bound resources, use `project_name/resource_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/nobl9/helpers.go b/nobl9/helpers.go index 12ffdb7b..456a77bc 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, 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(), diff --git a/templates/index.md.tmpl b/templates/index.md.tmpl index 1eba5df9..0bc7a8c0 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 project-bound resources, use `project_name/resource_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.