Skip to content

Commit 1aabc2a

Browse files
committed
feat: Make import work for most organization level resources
Also added example script for import such resources
1 parent f00bbc5 commit 1aabc2a

File tree

26 files changed

+206
-47
lines changed

26 files changed

+206
-47
lines changed

docs/resources/organization_tag.md

+9
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,12 @@ resource "terrakube_organization_tag" "example" {
3030
### Read-Only
3131

3232
- `id` (String) Organization Tag Id
33+
34+
## Import
35+
36+
Import is supported using the following syntax:
37+
38+
```shell
39+
# Organization Tag can be import with organization_id,id
40+
terraform import terrakube_organization_tag.example 00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000
41+
```

docs/resources/organization_template.md

+9
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,12 @@ flow:
4444
### Read-Only
4545

4646
- `id` (String) Template Id
47+
48+
## Import
49+
50+
Import is supported using the following syntax:
51+
52+
```shell
53+
# Organization Template can be import with organization_id,id
54+
terraform import terrakube_organization_template.example 00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000
55+
```

docs/resources/organization_variable.md

+9
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,12 @@ resource "terrakube_organization_variable" "sample2" {
5050
### Read-Only
5151

5252
- `id` (String) Variable Id
53+
54+
## Import
55+
56+
Import is supported using the following syntax:
57+
58+
```shell
59+
# Organization Variable can be import with organization_id,id
60+
terraform import terrakube_organization_variable.example 00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000
61+
```

docs/resources/team.md

+9
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,12 @@ resource "terrakube_team" "team" {
4040
### Read-Only
4141

4242
- `id` (String) Team Id
43+
44+
## Import
45+
46+
Import is supported using the following syntax:
47+
48+
```shell
49+
# Team can be import with organization_id,id
50+
terraform import terrakube_team.example 00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000
51+
```

docs/resources/team_token.md

+9
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,12 @@ resource "terrakube_team_token" "token1" {
3737

3838
- `id` (String) Team Token Id
3939
- `value` (String, Sensitive) The value of the token.
40+
41+
## Import
42+
43+
Import is supported using the following syntax:
44+
45+
```shell
46+
# Organization Template can be import with id
47+
terraform import terrakube_team_token.example 00000000-0000-0000-0000-000000000000
48+
```

docs/resources/vcs.md

+9
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,12 @@ resource "terrakube_vcs" "vcs" {
4747
- `connect_url` (String) The connect URL of the VCS connection, after adding the VCS connection, please logon to this URL to connect.
4848
- `id` (String) Variable Id
4949
- `status` (String) The status of the VCS connection. IMPORTANT NOTE: if the status is not 'PENDING', please logon to the connect_url to connect!!.
50+
51+
## Import
52+
53+
Import is supported using the following syntax:
54+
55+
```shell
56+
# Organization VCS can be import with organization_id,id
57+
terraform import terrakube_vcs.example 00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000
58+
```

docs/resources/workspace_cli.md

+9
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,12 @@ resource "terrakube_workspace_cli" "sample2" {
4747
### Read-Only
4848

4949
- `id` (String) Workspace CLI Id
50+
51+
## Import
52+
53+
Import is supported using the following syntax:
54+
55+
```shell
56+
# Workspace_cli can be import with organization_id,id
57+
terraform import terrakube_workspace_cli.example 00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000
58+
```

docs/resources/workspace_variable.md

+9
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,12 @@ resource "terrakube_workspace_variable" "sample2" {
5353
### Read-Only
5454

5555
- `id` (String) Variable Id
56+
57+
## Import
58+
59+
Import is supported using the following syntax:
60+
61+
```shell
62+
# Organization Workspace Variable can be import with organization_id,workspace_id,id
63+
terraform import terrakube_workspace_variable.example 00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000
64+
```

docs/resources/workspace_vcs.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,6 @@ resource "terrakube_workspace_cli" "sample1" {
5656
Import is supported using the following syntax:
5757

5858
```shell
59-
terraform import terrakube_workspace_vcs.example_workspace_vcs 00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000
59+
# Workspace_vcs can be import with organization_id,id
60+
terraform import terrakube_workspace_vcs.example 00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000
6061
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Organization Tag can be import with organization_id,id
2+
terraform import terrakube_organization_tag.example 00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Organization Template can be import with organization_id,id
2+
terraform import terrakube_organization_template.example 00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Organization Variable can be import with organization_id,id
2+
terraform import terrakube_organization_variable.example 00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Team can be import with organization_id,id
2+
terraform import terrakube_team.example 00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Organization Template can be import with id
2+
terraform import terrakube_team_token.example 00000000-0000-0000-0000-000000000000
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Organization VCS can be import with organization_id,id
2+
terraform import terrakube_vcs.example 00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Workspace_cli can be import with organization_id,id
2+
terraform import terrakube_workspace_cli.example 00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Organization Workspace Variable can be import with organization_id,workspace_id,id
2+
terraform import terrakube_workspace_variable.example 00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
terraform import terrakube_workspace_vcs.example_workspace_vcs 00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000
1+
# Workspace_vcs can be import with organization_id,id
2+
terraform import terrakube_workspace_vcs.example 00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000

internal/provider/organization_tag_resource.go

+25-14
Original file line numberDiff line numberDiff line change
@@ -131,20 +131,20 @@ func (r *OrganizationTagResource) Create(ctx context.Context, req resource.Creat
131131

132132
organizationTagResponse, err := r.client.Do(organizationTagRequest)
133133
if err != nil {
134-
resp.Diagnostics.AddError("Error executing organization tag resource request", fmt.Sprintf("Error executing organization tag resource request: %s", err))
134+
resp.Diagnostics.AddError("Error executing organization tag resource request", fmt.Sprintf("Error executing organization tag resource request, response status: %s, response body: %s, body: %s", organizationTagResponse.Status, organizationTagResponse.Body, err))
135135
return
136136
}
137137

138138
bodyResponse, err := io.ReadAll(organizationTagResponse.Body)
139139
if err != nil {
140-
tflog.Error(ctx, "Error reading organization tag resource response")
140+
tflog.Error(ctx, fmt.Sprintf("Error reading organization tag resource response, response status: %s, response body: %s, body: %s", organizationTagResponse.Status, organizationTagResponse.Body, err))
141141
}
142142
newOrganizationTag := &client.OrganizationTagEntity{}
143143

144144
err = jsonapi.UnmarshalPayload(strings.NewReader(string(bodyResponse)), newOrganizationTag)
145145

146146
if err != nil {
147-
resp.Diagnostics.AddError("Error unmarshal payload response", fmt.Sprintf("Error unmarshal payload response: %s", err))
147+
resp.Diagnostics.AddError("Error unmarshal payload response", fmt.Sprintf("Error unmarshal payload response, response status: %s, response body: %s, body: %s", organizationTagResponse.Status, organizationTagResponse.Body, err))
148148
return
149149
}
150150

@@ -176,21 +176,21 @@ func (r *OrganizationTagResource) Read(ctx context.Context, req resource.ReadReq
176176

177177
organizationTagResponse, err := r.client.Do(organizationTagRequest)
178178
if err != nil {
179-
resp.Diagnostics.AddError("Error executing organization tag resource request", fmt.Sprintf("Error executing organization tag resource request: %s", err))
179+
resp.Diagnostics.AddError("Error executing organization tag resource request", fmt.Sprintf("Error executing organization tag resource request, response status: %s, response body: %s, body: %s", organizationTagResponse.Status, organizationTagResponse.Body, err))
180180
return
181181
}
182182

183183
bodyResponse, err := io.ReadAll(organizationTagResponse.Body)
184184
if err != nil {
185-
tflog.Error(ctx, "Error reading organization tag resource response")
185+
tflog.Error(ctx, fmt.Sprintf("Error reading organization tag resource response, response status: %s, response body: %s, body: %s", organizationTagResponse.Status, organizationTagResponse.Body, err))
186186
}
187187
organizationTag := &client.OrganizationTagEntity{}
188188

189189
tflog.Info(ctx, "Body Response", map[string]any{"bodyResponse": string(bodyResponse)})
190190
err = jsonapi.UnmarshalPayload(strings.NewReader(string(bodyResponse)), organizationTag)
191191

192192
if err != nil {
193-
resp.Diagnostics.AddError("Error unmarshal payload response", fmt.Sprintf("Error unmarshal payload response: %s", err))
193+
resp.Diagnostics.AddError("Error unmarshal payload response", fmt.Sprintf("Error unmarshal payload response, response status: %s, response body: %s, body: %s", organizationTagResponse.Status, organizationTagResponse.Body, err))
194194
return
195195
}
196196

@@ -241,13 +241,13 @@ func (r *OrganizationTagResource) Update(ctx context.Context, req resource.Updat
241241

242242
organizationTagResponse, err := r.client.Do(organizationTagRequest)
243243
if err != nil {
244-
resp.Diagnostics.AddError("Error executing organization tag resource request", fmt.Sprintf("Error executing organization tag resource request: %s", err))
244+
resp.Diagnostics.AddError("Error executing organization tag resource request", fmt.Sprintf("Error executing organization tag resource request, response status: %s, response body: %s, body: %s", organizationTagResponse.Status, organizationTagResponse.Body, err))
245245
return
246246
}
247247

248248
bodyResponse, err := io.ReadAll(organizationTagResponse.Body)
249249
if err != nil {
250-
tflog.Error(ctx, "Error reading organization tag resource response")
250+
tflog.Error(ctx, fmt.Sprintf("Error reading organization tag resource response, response status: %s, response body: %s, body: %s", organizationTagResponse.Status, organizationTagResponse.Body, err))
251251
}
252252

253253
tflog.Info(ctx, "Body Response", map[string]any{"success": string(bodyResponse)})
@@ -262,13 +262,13 @@ func (r *OrganizationTagResource) Update(ctx context.Context, req resource.Updat
262262

263263
organizationTagResponse, err = r.client.Do(organizationTagRequest)
264264
if err != nil {
265-
resp.Diagnostics.AddError("Error executing organization tag resource request", fmt.Sprintf("Error executing organization tag resource request: %s", err))
265+
resp.Diagnostics.AddError("Error executing organization tag resource request", fmt.Sprintf("Error executing organization tag resource request, response status: %s, response body: %s, body: %s", organizationTagResponse.Status, organizationTagResponse.Body, err))
266266
return
267267
}
268268

269269
bodyResponse, err = io.ReadAll(organizationTagResponse.Body)
270270
if err != nil {
271-
resp.Diagnostics.AddError("Error reading organization tag resource response body", fmt.Sprintf("Error reading organization tag resource response body: %s", err))
271+
resp.Diagnostics.AddError("Error reading organization tag resource response body", fmt.Sprintf("Error reading organization tag resource response body, response status: %s, response body: %s, body: %s", organizationTagResponse.Status, organizationTagResponse.Body, err))
272272
}
273273

274274
tflog.Info(ctx, "Body Response", map[string]any{"bodyResponse": string(bodyResponse)})
@@ -304,13 +304,24 @@ func (r *OrganizationTagResource) Delete(ctx context.Context, req resource.Delet
304304
return
305305
}
306306

307-
_, err = r.client.Do(reqOrg)
308-
if err != nil {
309-
resp.Diagnostics.AddError("Error executing organization tag resource request", fmt.Sprintf("Error executing organization tag resource request: %s", err))
307+
organizationTagResponse, err := r.client.Do(reqOrg)
308+
if err != nil || organizationTagResponse.StatusCode != http.StatusNoContent {
309+
resp.Diagnostics.AddError("Error executing organization tag resource request", fmt.Sprintf("Error executing organization tag resource request, response status: %s, response body: %s, body: %s", organizationTagResponse.Status, organizationTagResponse.Body, err))
310310
return
311311
}
312312
}
313313

314314
func (r *OrganizationTagResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
315-
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
315+
idParts := strings.Split(req.ID, ",")
316+
317+
if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" {
318+
resp.Diagnostics.AddError(
319+
"Unexpected Import Identifier",
320+
fmt.Sprintf("Expected import identifier with format: 'organization_ID,ID', Got: %q", req.ID),
321+
)
322+
return
323+
}
324+
325+
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("organization_id"), idParts[0])...)
326+
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), idParts[1])...)
316327
}

internal/provider/organization_template_resource.go

+24-13
Original file line numberDiff line numberDiff line change
@@ -151,20 +151,20 @@ func (r *OrganizationTemplateResource) Create(ctx context.Context, req resource.
151151

152152
organizationTemplateResponse, err := r.client.Do(organizationTemplateRequest)
153153
if err != nil {
154-
resp.Diagnostics.AddError("Error executing organization template resource request", fmt.Sprintf("Error executing organization template resource request: %s", err))
154+
resp.Diagnostics.AddError("Error executing organization template resource request", fmt.Sprintf("Error executing organization template resource request, response status: %s, response body: %s, error: %s", organizationTemplateResponse.Status, organizationTemplateResponse.Body, err))
155155
return
156156
}
157157

158158
bodyResponse, err := io.ReadAll(organizationTemplateResponse.Body)
159159
if err != nil {
160-
tflog.Error(ctx, "Error reading organization template resource response")
160+
tflog.Error(ctx, fmt.Sprintf("Error reading organization template resource response, response status: %s, response body: %s, error: %s", organizationTemplateResponse.Status, organizationTemplateResponse.Body, err))
161161
}
162162
organizationTemplate := &client.OrganizationTemplateEntity{}
163163

164164
err = jsonapi.UnmarshalPayload(strings.NewReader(string(bodyResponse)), organizationTemplate)
165165
tflog.Info(ctx, string(bodyResponse))
166166
if err != nil {
167-
resp.Diagnostics.AddError("Error unmarshal payload response", fmt.Sprintf("Error unmarshal payload response: %s", err))
167+
resp.Diagnostics.AddError("Error unmarshal payload response", fmt.Sprintf("Error unmarshal payload response, response status: %s, response body: %s, error: %s", organizationTemplateResponse.Status, organizationTemplateResponse.Body, err))
168168
return
169169
}
170170

@@ -204,13 +204,13 @@ func (r *OrganizationTemplateResource) Read(ctx context.Context, req resource.Re
204204

205205
organizationTemplateResponse, err := r.client.Do(organizationTemplateRequest)
206206
if err != nil {
207-
resp.Diagnostics.AddError("Error executing organization template resource request", fmt.Sprintf("Error executing organization template resource request: %s", err))
207+
resp.Diagnostics.AddError("Error executing organization template resource request", fmt.Sprintf("Error executing organization template resource request, response status: %s, response body: %s, error: %s", organizationTemplateResponse.Status, organizationTemplateResponse.Body, err))
208208
return
209209
}
210210

211211
bodyResponse, err := io.ReadAll(organizationTemplateResponse.Body)
212212
if err != nil {
213-
tflog.Error(ctx, "Error reading organization template resource response")
213+
tflog.Error(ctx, fmt.Sprintf("Error reading organization template resource response, response status: %s, response body: %s, error: %s", organizationTemplateResponse.Status, organizationTemplateResponse.Body, err))
214214
}
215215
organizationTemplate := &client.OrganizationTemplateEntity{}
216216

@@ -283,13 +283,13 @@ func (r *OrganizationTemplateResource) Update(ctx context.Context, req resource.
283283

284284
organizationTemplateResponse, err := r.client.Do(organizationTemplateRequest)
285285
if err != nil {
286-
resp.Diagnostics.AddError("Error executing organization template resource request", fmt.Sprintf("Error executing organization template resource request: %s", err))
286+
resp.Diagnostics.AddError("Error executing organization template resource request", fmt.Sprintf("Error executing organization template resource request, response status: %s, response body: %s, error: %s", organizationTemplateResponse.Status, organizationTemplateResponse.Body, err))
287287
return
288288
}
289289

290290
bodyResponse, err := io.ReadAll(organizationTemplateResponse.Body)
291291
if err != nil {
292-
tflog.Error(ctx, "Error reading organization template resource response")
292+
tflog.Error(ctx, fmt.Sprintf("Error reading organization template resource response, response status: %s, response body: %s, error: %s", organizationTemplateResponse.Status, organizationTemplateResponse.Body, err))
293293
}
294294

295295
tflog.Info(ctx, "Body Response", map[string]any{"success": string(bodyResponse)})
@@ -304,13 +304,13 @@ func (r *OrganizationTemplateResource) Update(ctx context.Context, req resource.
304304

305305
organizationTemplateResponse, err = r.client.Do(organizationTemplateRequest)
306306
if err != nil {
307-
resp.Diagnostics.AddError("Error executing organization template resource request", fmt.Sprintf("Error executing organization template resource request: %s", err))
307+
resp.Diagnostics.AddError("Error executing organization template resource request", fmt.Sprintf("Error executing organization template resource request, response status: %s, response body: %s, error: %s", organizationTemplateResponse.Status, organizationTemplateResponse.Body, err))
308308
return
309309
}
310310

311311
bodyResponse, err = io.ReadAll(organizationTemplateResponse.Body)
312312
if err != nil {
313-
resp.Diagnostics.AddError("Error reading organization template resource response body", fmt.Sprintf("Error reading organization template resource response body: %s", err))
313+
resp.Diagnostics.AddError("Error reading organization template resource response body", fmt.Sprintf("Error reading organization template resource response body, response status: %s, response body: %s, error: %s", organizationTemplateResponse.Status, organizationTemplateResponse.Body, err))
314314
}
315315

316316
tflog.Info(ctx, "Body Response", map[string]any{"bodyResponse": string(bodyResponse)})
@@ -354,13 +354,24 @@ func (r *OrganizationTemplateResource) Delete(ctx context.Context, req resource.
354354
return
355355
}
356356

357-
_, err = r.client.Do(organizationTemplateRequest)
358-
if err != nil {
359-
resp.Diagnostics.AddError("Error executing organization template resource request", fmt.Sprintf("Error executing organization template resource request: %s", err))
357+
organizationTemplateResponse, err := r.client.Do(organizationTemplateRequest)
358+
if err != nil || organizationTemplateResponse.StatusCode != http.StatusNoContent {
359+
resp.Diagnostics.AddError("Error executing organization template resource request", fmt.Sprintf("Error executing organization template resource request, response status: %s, response body: %s, error: %s", organizationTemplateResponse.Status, organizationTemplateResponse.Body, err))
360360
return
361361
}
362362
}
363363

364364
func (r *OrganizationTemplateResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
365-
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
365+
idParts := strings.Split(req.ID, ",")
366+
367+
if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" {
368+
resp.Diagnostics.AddError(
369+
"Unexpected Import Identifier",
370+
fmt.Sprintf("Expected import identifier with format: 'organization_ID,ID', Got: %q", req.ID),
371+
)
372+
return
373+
}
374+
375+
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("organization_id"), idParts[0])...)
376+
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), idParts[1])...)
366377
}

0 commit comments

Comments
 (0)