@@ -4,13 +4,13 @@ import (
44 "context"
55 "encoding/json"
66 "fmt"
7+ "strconv"
78 "strings"
89
910 "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1011 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1112
1213 gapi "github.com/grafana/grafana-api-golang-client"
13- "github.com/grafana/terraform-provider-grafana/internal/common"
1414)
1515
1616func ResourceLibraryPanel () * schema.Resource {
@@ -23,10 +23,10 @@ Manages Grafana library panels.
2323* [HTTP API](https://grafana.com/docs/grafana/latest/developers/http_api/library_element/)
2424` ,
2525
26- CreateContext : CreateLibraryPanel ,
27- ReadContext : ReadLibraryPanel ,
28- UpdateContext : UpdateLibraryPanel ,
29- DeleteContext : DeleteLibraryPanel ,
26+ CreateContext : createLibraryPanel ,
27+ ReadContext : readLibraryPanel ,
28+ UpdateContext : updateLibraryPanel ,
29+ DeleteContext : deleteLibraryPanel ,
3030 Importer : & schema.ResourceImporter {
3131 StateContext : schema .ImportStatePassthroughContext ,
3232 },
@@ -46,9 +46,13 @@ Manages Grafana library panels.
4646 Description : "The numeric ID of the library panel computed by Grafana." ,
4747 },
4848 "org_id" : {
49- Type : schema .TypeInt ,
50- Computed : true ,
51- Description : "The numeric ID of the library panel computed by Grafana." ,
49+ Type : schema .TypeString ,
50+ Optional : true ,
51+ Description : "The Organization ID. If not set, the Org ID defined in the provider block will be used." ,
52+ ForceNew : true ,
53+ DiffSuppressFunc : func (k , old , new string , d * schema.ResourceData ) bool {
54+ return new == "" // Ignore the case where we have a global org_id set
55+ },
5256 },
5357 "folder_id" : {
5458 Type : schema .TypeInt ,
@@ -112,30 +116,29 @@ Manages Grafana library panels.
112116 }
113117}
114118
115- func CreateLibraryPanel (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
116- client := meta .(* common.Client ).GrafanaAPI
119+ func createLibraryPanel (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
120+ client , _ := ClientFromOrgIDAttr (meta , d )
121+
117122 panel := makeLibraryPanel (d )
118123 resp , err := client .NewLibraryPanel (panel )
119124 if err != nil {
120125 return diag .FromErr (err )
121126 }
122- d .SetId (resp .UID )
123- d .Set ("uid" , resp .UID )
124- return ReadLibraryPanel (ctx , d , meta )
127+ d .SetId (MakeOSSOrgID (resp .OrgID , resp .UID ))
128+ return readLibraryPanel (ctx , d , meta )
125129}
126130
127- func ReadLibraryPanel (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
128- client := meta .(* common.Client ).GrafanaAPI
129- uid := d .Id ()
131+ func readLibraryPanel (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
132+ client , _ , uid := ClientFromOSSOrgID (meta , d .Id ())
130133
131134 panel , err := client .LibraryPanelByUID (uid )
132135 var diags diag.Diagnostics
133136 if err != nil {
134137 if strings .HasPrefix (err .Error (), "status: 404" ) {
135138 diags = append (diags , diag.Diagnostic {
136139 Severity : diag .Warning ,
137- Summary : fmt .Sprintf ("Library Panel %q is in state, but no longer exists in grafana" , panel . Name ),
138- Detail : fmt .Sprintf ("%q will be recreated when you apply" , panel . Name ),
140+ Summary : fmt .Sprintf ("Library Panel %s is in state, but no longer exists in grafana" , uid ),
141+ Detail : fmt .Sprintf ("%s will be recreated when you apply" , uid ),
139142 })
140143 d .SetId ("" )
141144 return diags
@@ -154,10 +157,9 @@ func ReadLibraryPanel(ctx context.Context, d *schema.ResourceData, meta interfac
154157 }
155158 modelJSON := normalizeLibraryPanelModelJSON (remotePanelJSON )
156159
157- d .SetId (panel .UID )
158160 d .Set ("uid" , panel .UID )
159161 d .Set ("panel_id" , panel .ID )
160- d .Set ("org_id" , panel .OrgID )
162+ d .Set ("org_id" , strconv . FormatInt ( panel .OrgID , 10 ) )
161163 d .Set ("folder_id" , panel .Folder )
162164 d .Set ("description" , panel .Description )
163165 d .Set ("type" , panel .Type )
@@ -183,28 +185,22 @@ func ReadLibraryPanel(ctx context.Context, d *schema.ResourceData, meta interfac
183185 return diags
184186}
185187
186- func UpdateLibraryPanel (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
187- client := meta .(* common.Client ).GrafanaAPI
188- uid := d .Id ()
188+ func updateLibraryPanel (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
189+ client , _ , uid := ClientFromOSSOrgID (meta , d .Id ())
189190 panel := makeLibraryPanel (d )
190191
191192 resp , err := client .PatchLibraryPanel (uid , panel )
192193 if err != nil {
193194 return diag .FromErr (err )
194195 }
195- d .SetId (resp .UID )
196- d .Set ("uid" , resp .UID )
197- return ReadLibraryPanel (ctx , d , meta )
196+ d .SetId (MakeOSSOrgID (resp .OrgID , resp .UID ))
197+ return readLibraryPanel (ctx , d , meta )
198198}
199199
200- func DeleteLibraryPanel (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
201- client := meta .(* common.Client ).GrafanaAPI
202- uid := d .Id ()
200+ func deleteLibraryPanel (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
201+ client , _ , uid := ClientFromOSSOrgID (meta , d .Id ())
203202 _ , err := client .DeleteLibraryPanel (uid )
204- if err != nil {
205- return diag .FromErr (err )
206- }
207- return nil
203+ return diag .FromErr (err )
208204}
209205
210206func makeLibraryPanel (d * schema.ResourceData ) gapi.LibraryPanel {
0 commit comments