|
8 | 8 | "time"
|
9 | 9 |
|
10 | 10 | "github.com/go-openapi/runtime"
|
| 11 | + goapi "github.com/grafana/grafana-openapi-client-go/client" |
11 | 12 | "github.com/grafana/grafana-openapi-client-go/client/provisioning"
|
12 | 13 | "github.com/grafana/grafana-openapi-client-go/models"
|
13 | 14 | "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
|
@@ -103,48 +104,37 @@ This resource requires Grafana 9.1.0 or later.
|
103 | 104 | "grafana_contact_point",
|
104 | 105 | orgResourceIDString("name"),
|
105 | 106 | resource,
|
106 |
| - ) |
| 107 | + ).WithLister(listerFunctionOrgResource(listContactPoints)) |
107 | 108 | }
|
108 | 109 |
|
109 |
| -// TODO: Fix contact points lister. Terraform doesn't read any of the sensitive fields (or their container) |
110 |
| -// It outputs an empty `email {}` block for example, which is not valid. |
111 |
| -// func listContactPoints(ctx context.Context, client *goapi.GrafanaHTTPAPI, data *ListerData) ([]string, error) { |
112 |
| -// orgIDs, err := data.OrgIDs(client) |
113 |
| -// if err != nil { |
114 |
| -// return nil, err |
115 |
| -// } |
116 |
| - |
117 |
| -// idMap := map[string]bool{} |
118 |
| -// for _, orgID := range orgIDs { |
119 |
| -// client = client.Clone().WithOrgID(orgID) |
120 |
| - |
121 |
| -// // Retry if the API returns 500 because it may be that the alertmanager is not ready in the org yet. |
122 |
| -// // The alertmanager is provisioned asynchronously when the org is created. |
123 |
| -// if err := retry.RetryContext(ctx, 2*time.Minute, func() *retry.RetryError { |
124 |
| -// resp, err := client.Provisioning.GetContactpoints(provisioning.NewGetContactpointsParams()) |
125 |
| -// if err != nil { |
126 |
| -// if orgID > 1 && (err.(*runtime.APIError).IsCode(500) || err.(*runtime.APIError).IsCode(403)) { |
127 |
| -// return retry.RetryableError(err) |
128 |
| -// } |
129 |
| -// return retry.NonRetryableError(err) |
130 |
| -// } |
131 |
| - |
132 |
| -// for _, contactPoint := range resp.Payload { |
133 |
| -// idMap[MakeOrgResourceID(orgID, contactPoint.Name)] = true |
134 |
| -// } |
135 |
| -// return nil |
136 |
| -// }); err != nil { |
137 |
| -// return nil, err |
138 |
| -// } |
139 |
| -// } |
140 |
| - |
141 |
| -// var ids []string |
142 |
| -// for id := range idMap { |
143 |
| -// ids = append(ids, id) |
144 |
| -// } |
145 |
| - |
146 |
| -// return ids, nil |
147 |
| -// } |
| 110 | +func listContactPoints(ctx context.Context, client *goapi.GrafanaHTTPAPI, orgID int64) ([]string, error) { |
| 111 | + idMap := map[string]bool{} |
| 112 | + // Retry if the API returns 500 because it may be that the alertmanager is not ready in the org yet. |
| 113 | + // The alertmanager is provisioned asynchronously when the org is created. |
| 114 | + if err := retry.RetryContext(ctx, 2*time.Minute, func() *retry.RetryError { |
| 115 | + resp, err := client.Provisioning.GetContactpoints(provisioning.NewGetContactpointsParams()) |
| 116 | + if err != nil { |
| 117 | + if orgID > 1 && (err.(*runtime.APIError).IsCode(500) || err.(*runtime.APIError).IsCode(403)) { |
| 118 | + return retry.RetryableError(err) |
| 119 | + } |
| 120 | + return retry.NonRetryableError(err) |
| 121 | + } |
| 122 | + |
| 123 | + for _, contactPoint := range resp.Payload { |
| 124 | + idMap[MakeOrgResourceID(orgID, contactPoint.Name)] = true |
| 125 | + } |
| 126 | + return nil |
| 127 | + }); err != nil { |
| 128 | + return nil, err |
| 129 | + } |
| 130 | + |
| 131 | + var ids []string |
| 132 | + for id := range idMap { |
| 133 | + ids = append(ids, id) |
| 134 | + } |
| 135 | + |
| 136 | + return ids, nil |
| 137 | +} |
148 | 138 |
|
149 | 139 | func readContactPoint(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
150 | 140 | client, orgID, name := OAPIClientFromExistingOrgResource(meta, data.Id())
|
|
0 commit comments