Skip to content

Commit f9cd123

Browse files
authored
Alerting: Import contact points by name instead of by UID list (#646)
* Import contact points by name instead of by uid list * Fix linter error
1 parent 0d91da8 commit f9cd123

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

grafana/resource_alerting_contact_point.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Manages Grafana Alerting contact points.
4545
DeleteContext: deleteContactPoint,
4646

4747
Importer: &schema.ResourceImporter{
48-
StateContext: schema.ImportStatePassthroughContext,
48+
StateContext: importContactPoint,
4949
},
5050

5151
SchemaVersion: 0,
@@ -70,6 +70,28 @@ Manages Grafana Alerting contact points.
7070
return resource
7171
}
7272

73+
func importContactPoint(ctx context.Context, data *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
74+
name := data.Id()
75+
client := meta.(*client).gapi
76+
77+
ps, err := client.ContactPointsByName(name)
78+
if err != nil {
79+
return nil, err
80+
}
81+
82+
if len(ps) == 0 {
83+
return nil, fmt.Errorf("no contact points with the given name were found to import")
84+
}
85+
86+
uids := make([]string, 0, len(ps))
87+
for _, p := range ps {
88+
uids = append(uids, p.UID)
89+
}
90+
91+
data.SetId(packUIDs(uids))
92+
return []*schema.ResourceData{data}, nil
93+
}
94+
7395
func readContactPoint(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
7496
client := meta.(*client).gapi
7597

grafana/resource_alerting_contact_point_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ func TestAccContactPoint_basic(t *testing.T) {
3636
{
3737
ResourceName: "grafana_contact_point.my_contact_point",
3838
ImportState: true,
39+
ImportStateId: "My Contact Point",
3940
ImportStateVerify: true,
4041
},
4142
// Test update content.

0 commit comments

Comments
 (0)