Skip to content

Commit 40852db

Browse files
Allow importing a datasource either by ID or UID (#396)
* Allow importing a datasource either by ID or UID Closes #252 * Lint
1 parent abb549f commit 40852db

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

docs/resources/data_source.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,11 @@ Optional:
183183
- **tls_client_cert** (String, Sensitive) (All) TLS Client cert for outgoing requests.
184184
- **tls_client_key** (String, Sensitive) (All) TLS Client key for outgoing requests.
185185

186+
## Import
186187

188+
Import is supported using the following syntax:
189+
190+
```shell
191+
terraform import grafana_data_source.by_integer_id {{datasource id}}
192+
terraform import grafana_data_source.by_uid {{datasource uid}}
193+
```
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
terraform import grafana_data_source.by_integer_id {{datasource id}}
2+
terraform import grafana_data_source.by_uid {{datasource uid}}

grafana/resource_data_source.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,23 @@ source selected (via the 'type' argument).
3131
DeleteContext: DeleteDataSource,
3232
ReadContext: ReadDataSource,
3333

34+
// Import either by ID or UID
35+
Importer: &schema.ResourceImporter{
36+
StateContext: func(c context.Context, rd *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
37+
_, err := strconv.ParseInt(rd.Id(), 10, 64)
38+
if err != nil {
39+
// If the ID is not a number, then it may be a UID
40+
client := meta.(*client).gapi
41+
ds, err := client.DataSourceByUID(rd.Id())
42+
if err != nil {
43+
return nil, fmt.Errorf("failed to find datasource by ID or UID '%s': %w", rd.Id(), err)
44+
}
45+
rd.SetId(strconv.FormatInt(ds.ID, 10))
46+
}
47+
return []*schema.ResourceData{rd}, nil
48+
},
49+
},
50+
3451
Schema: map[string]*schema.Schema{
3552
"access_mode": {
3653
Type: schema.TypeString,

grafana/resource_data_source_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,27 @@ func TestAccDataSource_basic(t *testing.T) {
540540
append(checks, test.additionalChecks...)...,
541541
),
542542
},
543+
// Test import using ID
544+
{
545+
ResourceName: test.resource,
546+
ImportState: true,
547+
},
548+
// Test import using UID
549+
{
550+
ResourceName: test.resource,
551+
ImportState: true,
552+
ImportStateIdFunc: func(s *terraform.State) (string, error) {
553+
rs, ok := s.RootModule().Resources[test.resource]
554+
if !ok {
555+
return "", fmt.Errorf("resource not found: %s", test.resource)
556+
}
557+
558+
if rs.Primary.ID == "" {
559+
return "", fmt.Errorf("resource id not set")
560+
}
561+
return rs.Primary.Attributes["uid"], nil
562+
},
563+
},
543564
},
544565
})
545566
}

0 commit comments

Comments
 (0)