@@ -2,9 +2,12 @@ package oncall
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
6
+ "time"
5
7
6
8
onCallAPI "github.com/grafana/amixr-api-go-client"
7
9
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
10
+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
8
11
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9
12
)
10
13
@@ -30,24 +33,33 @@ func dataSourceTeam() *schema.Resource {
30
33
}
31
34
32
35
func dataSourceTeamRead (ctx context.Context , d * schema.ResourceData , client * onCallAPI.Client ) diag.Diagnostics {
33
- options := & onCallAPI.ListTeamOptions {}
34
- nameData := d .Get ("name" ).(string )
36
+ var team * onCallAPI.Team
35
37
36
- options .Name = nameData
38
+ // Retry because the team might not be immediately available
39
+ err := retry .RetryContext (ctx , 1 * time .Minute , func () * retry.RetryError {
40
+ options := & onCallAPI.ListTeamOptions {}
41
+ nameData := d .Get ("name" ).(string )
37
42
38
- teamsResponse , _ , err := client .Teams .ListTeams (options )
43
+ options .Name = nameData
44
+
45
+ teamsResponse , _ , err := client .Teams .ListTeams (options )
46
+ if err != nil {
47
+ return retry .NonRetryableError (err )
48
+ }
49
+
50
+ if len (teamsResponse .Teams ) == 0 {
51
+ return retry .RetryableError (fmt .Errorf ("couldn't find a team matching: %s" , options .Name ))
52
+ } else if len (teamsResponse .Teams ) != 1 {
53
+ return retry .RetryableError (fmt .Errorf ("more than one team found matching: %s" , options .Name ))
54
+ }
55
+
56
+ team = teamsResponse .Teams [0 ]
57
+ return nil
58
+ })
39
59
if err != nil {
40
60
return diag .FromErr (err )
41
61
}
42
62
43
- if len (teamsResponse .Teams ) == 0 {
44
- return diag .Errorf ("couldn't find a team matching: %s" , options .Name )
45
- } else if len (teamsResponse .Teams ) != 1 {
46
- return diag .Errorf ("more than one team found matching: %s" , options .Name )
47
- }
48
-
49
- team := teamsResponse .Teams [0 ]
50
-
51
63
d .Set ("name" , team .Name )
52
64
d .Set ("email" , team .Email )
53
65
d .Set ("avatar_url" , team .AvatarUrl )
0 commit comments