Skip to content

Commit 7591124

Browse files
authored
Merge pull request #290 from grafana/import_probe_auth_token
Add support for importing probe auth_token
2 parents df09871 + 9e4d9f4 commit 7591124

File tree

3 files changed

+102
-1
lines changed

3 files changed

+102
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
terraform import grafana_synthetic_monitoring_probe.probe {{probe-id}}
2+
terraform import grafana_synthetic_monitoring_probe.probe {{probe-id}}:{{auth_token}}

grafana/resource_synthetic_monitoring_probe.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package grafana
33
import (
44
"context"
55
"encoding/base64"
6+
"fmt"
67
"strconv"
8+
"strings"
79

810
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
911
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -28,7 +30,7 @@ Grafana Synthetic Monitoring Agent.
2830
UpdateContext: resourceSyntheticMonitoringProbeUpdate,
2931
DeleteContext: resourceSyntheticMonitoringProbeDelete,
3032
Importer: &schema.ResourceImporter{
31-
StateContext: schema.ImportStatePassthroughContext,
33+
StateContext: importProbeStateWithToken,
3234
},
3335

3436
Schema: map[string]*schema.Schema{
@@ -188,3 +190,29 @@ func makeProbe(d *schema.ResourceData) *sm.Probe {
188190
Public: d.Get("public").(bool),
189191
}
190192
}
193+
194+
// importProbeStateWithToken is an implementation of StateContextFunc
195+
// that can be used to pass the ID of the probe and the existing
196+
// auth_token.
197+
func importProbeStateWithToken(ctx context.Context, d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
198+
parts := strings.SplitN(d.Id(), ":", 2)
199+
200+
// the auth_token is optional
201+
if len(parts) == 2 {
202+
if parts[0] == "" || parts[1] == "" {
203+
return nil, fmt.Errorf("invalid id %q, expected format 'probe_id:auth_token'", d.Id())
204+
}
205+
206+
if _, err := base64.StdEncoding.DecodeString(parts[1]); err != nil {
207+
return nil, fmt.Errorf("invalid auth_token %q, expecting a base64-encoded string", parts[1])
208+
}
209+
210+
if err := d.Set("auth_token", parts[1]); err != nil {
211+
return nil, fmt.Errorf("failed to set auth_token: %s", err)
212+
}
213+
}
214+
215+
d.SetId(parts[0])
216+
217+
return []*schema.ResourceData{d}, nil
218+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package grafana
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
)
9+
10+
func TestImportProbeStateWithToken(t *testing.T) {
11+
testcases := map[string]struct {
12+
input string
13+
expectError bool
14+
expectedID string
15+
expectedAuthToken string
16+
}{
17+
"valid id, no auth_token": {
18+
input: "1",
19+
expectError: false,
20+
expectedID: "1",
21+
expectedAuthToken: "",
22+
},
23+
"valid id, valid auth_token": {
24+
input: "1:aGVsbG8=",
25+
expectError: false,
26+
expectedID: "1",
27+
expectedAuthToken: "aGVsbG8=",
28+
},
29+
"valid id, invalid auth_token": {
30+
input: "1:xxx",
31+
expectError: true,
32+
},
33+
"invalid id, valid auth_token": {
34+
input: ":aGVsbG8=",
35+
expectError: true,
36+
},
37+
}
38+
39+
for name, tc := range testcases {
40+
t.Run(name, func(t *testing.T) {
41+
d := schema.TestResourceDataRaw(t, syntheticMonitoringProbe.Schema, nil)
42+
d.SetId(tc.input)
43+
44+
res, err := importProbeStateWithToken(context.Background(), d, nil)
45+
switch {
46+
case tc.expectError && err == nil:
47+
t.Fatalf("calling importProbeStateWithToken with id %q, expecting error, got nil", tc.input)
48+
49+
case !tc.expectError && err != nil:
50+
t.Fatalf("calling importProbeStateWithToken with id %q, expecting no error, got %s", tc.input, err)
51+
52+
case !tc.expectError:
53+
if len(res) != 1 {
54+
t.Fatalf("expecting 1 ResourceData, got %d", len(res))
55+
}
56+
57+
if tc.expectedID != res[0].Id() {
58+
t.Fatalf("expecting id %q, got %q", tc.expectedID, res[0].Id())
59+
}
60+
61+
if tc.expectedAuthToken != "" {
62+
output, ok := res[0].GetOk("auth_token")
63+
if !ok {
64+
t.Fatalf("expecting auth_token to be set")
65+
} else if str, ok := output.(string); !ok || str != tc.expectedAuthToken {
66+
t.Fatalf("expecting auth_token to match string %q, got %#v", tc.expectedAuthToken, output)
67+
}
68+
}
69+
}
70+
})
71+
}
72+
}

0 commit comments

Comments
 (0)