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

feat: PC-14787 Enable specifying project in the command for importing #354

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 2 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions nobl9/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
13 changes: 12 additions & 1 deletion nobl9/resource_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/)",
}
Expand Down Expand Up @@ -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 != "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what If project is empty? will it eventually use the default project? just asking

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
Expand Down
15 changes: 13 additions & 2 deletions nobl9/resource_direct.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package nobl9
import (
"context"
"errors"
"fmt"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -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(),
}
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion nobl9/resource_slo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
}
Expand Down Expand Up @@ -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
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

placeholder comment: why we're not adding acceptance tests for import?

func schemaMetricSpec() *schema.Resource {
metricSchemaDefinitions := []map[string]*schema.Schema{
schemaMetricAmazonPrometheus(),
Expand Down
2 changes: 2 additions & 0 deletions templates/index.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading