9
9
10
10
"github.com/grafana/grafana-com-public-clients/go/gcom"
11
11
goapi "github.com/grafana/grafana-openapi-client-go/client"
12
- "github.com/grafana/grafana-openapi-client-go/client/service_accounts"
13
- "github.com/grafana/grafana-openapi-client-go/models"
14
12
"github.com/grafana/terraform-provider-grafana/v2/internal/common"
15
13
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
16
14
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -42,7 +40,6 @@ Required access policy scopes:
42
40
43
41
CreateContext : withClient [schema.CreateContextFunc ](createStackServiceAccount ),
44
42
ReadContext : withClient [schema.ReadContextFunc ](readStackServiceAccount ),
45
- UpdateContext : withClient [schema.UpdateContextFunc ](updateStackServiceAccount ),
46
43
DeleteContext : withClient [schema.DeleteContextFunc ](deleteStackServiceAccount ),
47
44
Importer : & schema.ResourceImporter {
48
45
StateContext : schema .ImportStatePassthroughContext ,
@@ -56,21 +53,22 @@ Required access policy scopes:
56
53
"name" : {
57
54
Type : schema .TypeString ,
58
55
Required : true ,
59
- ForceNew : true ,
60
56
Description : "The name of the service account." ,
57
+ ForceNew : true ,
61
58
},
62
59
"role" : {
63
60
Type : schema .TypeString ,
64
61
Optional : true ,
65
62
ValidateFunc : validation .StringInSlice ([]string {"Viewer" , "Editor" , "Admin" }, false ),
66
63
Description : "The basic role of the service account in the organization." ,
64
+ ForceNew : true , // The grafana API does not support updating the service account
67
65
},
68
66
"is_disabled" : {
69
67
Type : schema .TypeBool ,
70
68
Optional : true ,
71
69
Default : false ,
72
- ForceNew : true ,
73
70
Description : "The disabled status for the service account." ,
71
+ ForceNew : true , // The grafana API does not support updating the service account
74
72
},
75
73
},
76
74
}
@@ -84,25 +82,21 @@ Required access policy scopes:
84
82
85
83
func createStackServiceAccount (ctx context.Context , d * schema.ResourceData , cloudClient * gcom.APIClient ) diag.Diagnostics {
86
84
stackSlug := d .Get ("stack_slug" ).(string )
87
- client , cleanup , err := CreateTemporaryStackGrafanaClient (ctx , cloudClient , stackSlug , "terraform-temp-" )
88
- if err != nil {
89
- return diag .FromErr (err )
90
- }
91
- defer cleanup ()
92
-
93
- req := service_accounts .NewCreateServiceAccountParams ().WithBody (& models.CreateServiceAccountForm {
85
+ req := gcom.PostInstanceServiceAccountsRequest {
94
86
Name : d .Get ("name" ).(string ),
95
87
Role : d .Get ("role" ).(string ),
96
- IsDisabled : d .Get ("is_disabled" ).(bool ),
97
- })
98
- resp , err := client .ServiceAccounts .CreateServiceAccount (req )
88
+ IsDisabled : common .Ref (d .Get ("is_disabled" ).(bool )),
89
+ }
90
+ resp , _ , err := cloudClient .InstancesAPI .PostInstanceServiceAccounts (ctx , stackSlug ).
91
+ PostInstanceServiceAccountsRequest (req ).
92
+ XRequestId (ClientRequestID ()).
93
+ Execute ()
99
94
if err != nil {
100
95
return diag .FromErr (err )
101
96
}
102
- sa := resp .Payload
103
97
104
- d .SetId (resourceStackServiceAccountID .Make (stackSlug , sa . ID ))
105
- return readStackServiceAccountWithClient ( client , d , sa . ID )
98
+ d .SetId (resourceStackServiceAccountID .Make (stackSlug , resp . Id ))
99
+ return readStackServiceAccount ( ctx , d , cloudClient )
106
100
}
107
101
108
102
func readStackServiceAccount (ctx context.Context , d * schema.ResourceData , cloudClient * gcom.APIClient ) diag.Diagnostics {
@@ -122,69 +116,22 @@ func readStackServiceAccount(ctx context.Context, d *schema.ResourceData, cloudC
122
116
stackSlug , serviceAccountID = split [0 ].(string ), split [1 ].(int64 )
123
117
}
124
118
125
- client , cleanup , err := CreateTemporaryStackGrafanaClient (ctx , cloudClient , stackSlug , "terraform-temp-" )
126
- if err != nil {
127
- return diag .FromErr (err )
128
- }
129
- defer cleanup ()
130
-
131
- d .Set ("stack_slug" , stackSlug )
132
- d .SetId (resourceStackServiceAccountID .Make (stackSlug , serviceAccountID ))
133
-
134
- return readStackServiceAccountWithClient (client , d , serviceAccountID )
135
- }
136
-
137
- func readStackServiceAccountWithClient (client * goapi.GrafanaHTTPAPI , d * schema.ResourceData , serviceAccountID int64 ) diag.Diagnostics {
138
- resp , err := client .ServiceAccounts .RetrieveServiceAccount (serviceAccountID )
139
- if err != nil {
140
- return diag .FromErr (err )
141
- }
142
- sa := resp .Payload
143
-
144
- err = d .Set ("name" , sa .Name )
145
- if err != nil {
146
- return diag .FromErr (err )
147
- }
148
- err = d .Set ("role" , sa .Role )
149
- if err != nil {
150
- return diag .FromErr (err )
151
- }
152
- err = d .Set ("is_disabled" , sa .IsDisabled )
153
- if err != nil {
154
- return diag .FromErr (err )
155
- }
156
-
157
- return nil
158
- }
159
-
160
- func updateStackServiceAccount (ctx context.Context , d * schema.ResourceData , cloudClient * gcom.APIClient ) diag.Diagnostics {
161
- split , err := resourceStackServiceAccountID .Split (d .Id ())
162
- if err != nil {
163
- return diag .FromErr (err )
119
+ resp , httpResp , err := cloudClient .InstancesAPI .GetInstanceServiceAccount (ctx , stackSlug , strconv .FormatInt (serviceAccountID , 10 )).Execute ()
120
+ if httpResp != nil && httpResp .StatusCode == 404 {
121
+ d .SetId ("" )
122
+ return nil
164
123
}
165
- stackSlug , serviceAccountID := split [0 ].(string ), split [1 ].(int64 )
166
-
167
- client , cleanup , err := CreateTemporaryStackGrafanaClient (ctx , cloudClient , stackSlug , "terraform-temp-" )
168
124
if err != nil {
169
125
return diag .FromErr (err )
170
126
}
171
- defer cleanup ()
172
-
173
- updateRequest := service_accounts .NewUpdateServiceAccountParams ().
174
- WithBody (& models.UpdateServiceAccountForm {
175
- Name : d .Get ("name" ).(string ),
176
- Role : d .Get ("role" ).(string ),
177
- IsDisabled : d .Get ("is_disabled" ).(bool ),
178
- }).
179
- WithServiceAccountID (serviceAccountID )
180
127
181
- if _ , err := client .ServiceAccounts .UpdateServiceAccount (updateRequest ); err != nil {
182
- return diag .FromErr (err )
183
- }
184
128
d .Set ("stack_slug" , stackSlug )
129
+ d .Set ("name" , resp .Name )
130
+ d .Set ("role" , resp .Role )
131
+ d .Set ("is_disabled" , resp .IsDisabled )
185
132
d .SetId (resourceStackServiceAccountID .Make (stackSlug , serviceAccountID ))
186
133
187
- return readStackServiceAccountWithClient ( client , d , serviceAccountID )
134
+ return nil
188
135
}
189
136
190
137
func deleteStackServiceAccount (ctx context.Context , d * schema.ResourceData , cloudClient * gcom.APIClient ) diag.Diagnostics {
@@ -194,13 +141,9 @@ func deleteStackServiceAccount(ctx context.Context, d *schema.ResourceData, clou
194
141
}
195
142
stackSlug , serviceAccountID := split [0 ].(string ), split [1 ].(int64 )
196
143
197
- client , cleanup , err := CreateTemporaryStackGrafanaClient (ctx , cloudClient , stackSlug , "terraform-temp-" )
198
- if err != nil {
199
- return diag .FromErr (err )
200
- }
201
- defer cleanup ()
202
-
203
- _ , err = client .ServiceAccounts .DeleteServiceAccount (serviceAccountID )
144
+ _ , err = cloudClient .InstancesAPI .DeleteInstanceServiceAccount (ctx , stackSlug , strconv .FormatInt (serviceAccountID , 10 )).
145
+ XRequestId (ClientRequestID ()).
146
+ Execute ()
204
147
return diag .FromErr (err )
205
148
}
206
149
0 commit comments