Skip to content

Commit 2f7c4f2

Browse files
committed
Import ruler namespace ressource using namespace name instead of id
We can't query a ruler namespace using id
1 parent 842413e commit 2f7c4f2

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

docs/resources/ruler_namespace.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,10 @@ EOT
4646

4747
- `id` (String) The ID of this resource.
4848

49+
## Import
4950

51+
Resource can be imported using the namespace name, e.g.
52+
53+
```
54+
$ terraform import mimirtool_ruler_namespace.demo demo
55+
```

mimirtool/resource_ruler_namespace.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func resourceRulerNamespace() *schema.Resource {
2525
UpdateContext: rulerNamespaceUpdate,
2626
DeleteContext: rulerNamespaceDelete,
2727
Importer: &schema.ResourceImporter{
28-
StateContext: schema.ImportStatePassthroughContext,
28+
StateContext: rulerNamespaceImport,
2929
},
3030

3131
Schema: map[string]*schema.Schema{
@@ -213,6 +213,16 @@ func rulerNamespaceDelete(ctx context.Context, d *schema.ResourceData, meta any)
213213
return diags
214214
}
215215

216+
func rulerNamespaceImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
217+
namespace := d.Id()
218+
d.Set("namespace", namespace)
219+
d.SetId(hash(namespace))
220+
221+
results := make([]*schema.ResourceData, 1)
222+
results[0] = d
223+
return results, nil
224+
}
225+
216226
// Borrowed from https://github.com/grafana/terraform-provider-grafana/blob/master/grafana/resource_dashboard.go
217227
func normalizeNamespaceYAML(config any) string {
218228
configYAML := config.(string)

mimirtool/resource_ruler_namespace_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ func TestAccResourceNamespace(t *testing.T) {
3030
"mimirtool_ruler_namespace.demo", "config_yaml", testAccResourceNamespaceYamlAfterUpdate),
3131
),
3232
},
33+
{
34+
ResourceName: "mimirtool_ruler_namespace.demo",
35+
ImportStateId: "demo",
36+
ImportState: true,
37+
ImportStateVerify: true,
38+
ImportStateVerifyIgnore: []string{"recording_rule_check", "strict_recording_rule_check"},
39+
},
3340
},
3441
})
3542
}

0 commit comments

Comments
 (0)