Skip to content

Commit

Permalink
feat: Make import work for most organization level resources (#66)
Browse files Browse the repository at this point in the history
Also added example script for import such resources
  • Loading branch information
stanleyz authored Aug 26, 2024
1 parent f00bbc5 commit dcf2de3
Show file tree
Hide file tree
Showing 26 changed files with 206 additions and 47 deletions.
9 changes: 9 additions & 0 deletions docs/resources/organization_tag.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,12 @@ resource "terrakube_organization_tag" "example" {
### Read-Only

- `id` (String) Organization Tag Id

## Import

Import is supported using the following syntax:

```shell
# Organization Tag can be import with organization_id,id
terraform import terrakube_organization_tag.example 00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000
```
9 changes: 9 additions & 0 deletions docs/resources/organization_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,12 @@ flow:
### Read-Only

- `id` (String) Template Id

## Import

Import is supported using the following syntax:

```shell
# Organization Template can be import with organization_id,id
terraform import terrakube_organization_template.example 00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000
```
9 changes: 9 additions & 0 deletions docs/resources/organization_variable.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,12 @@ resource "terrakube_organization_variable" "sample2" {
### Read-Only

- `id` (String) Variable Id

## Import

Import is supported using the following syntax:

```shell
# Organization Variable can be import with organization_id,id
terraform import terrakube_organization_variable.example 00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000
```
9 changes: 9 additions & 0 deletions docs/resources/team.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,12 @@ resource "terrakube_team" "team" {
### Read-Only

- `id` (String) Team Id

## Import

Import is supported using the following syntax:

```shell
# Team can be import with organization_id,id
terraform import terrakube_team.example 00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000
```
9 changes: 9 additions & 0 deletions docs/resources/team_token.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,12 @@ resource "terrakube_team_token" "token1" {

- `id` (String) Team Token Id
- `value` (String, Sensitive) The value of the token.

## Import

Import is supported using the following syntax:

```shell
# Organization Template can be import with id
terraform import terrakube_team_token.example 00000000-0000-0000-0000-000000000000
```
9 changes: 9 additions & 0 deletions docs/resources/vcs.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,12 @@ resource "terrakube_vcs" "vcs" {
- `connect_url` (String) The connect URL of the VCS connection, after adding the VCS connection, please logon to this URL to connect.
- `id` (String) Variable Id
- `status` (String) The status of the VCS connection. IMPORTANT NOTE: if the status is not 'PENDING', please logon to the connect_url to connect!!.

## Import

Import is supported using the following syntax:

```shell
# Organization VCS can be import with organization_id,id
terraform import terrakube_vcs.example 00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000
```
9 changes: 9 additions & 0 deletions docs/resources/workspace_cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,12 @@ resource "terrakube_workspace_cli" "sample2" {
### Read-Only

- `id` (String) Workspace CLI Id

## Import

Import is supported using the following syntax:

```shell
# Workspace_cli can be import with organization_id,id
terraform import terrakube_workspace_cli.example 00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000
```
9 changes: 9 additions & 0 deletions docs/resources/workspace_variable.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,12 @@ resource "terrakube_workspace_variable" "sample2" {
### Read-Only

- `id` (String) Variable Id

## Import

Import is supported using the following syntax:

```shell
# Organization Workspace Variable can be import with organization_id,workspace_id,id
terraform import terrakube_workspace_variable.example 00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000
```
3 changes: 2 additions & 1 deletion docs/resources/workspace_vcs.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,6 @@ resource "terrakube_workspace_cli" "sample1" {
Import is supported using the following syntax:

```shell
terraform import terrakube_workspace_vcs.example_workspace_vcs 00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000
# Workspace_vcs can be import with organization_id,id
terraform import terrakube_workspace_vcs.example 00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000
```
2 changes: 2 additions & 0 deletions examples/resources/terrakube_organization_tag/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Organization Tag can be import with organization_id,id
terraform import terrakube_organization_tag.example 00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000
2 changes: 2 additions & 0 deletions examples/resources/terrakube_organization_template/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Organization Template can be import with organization_id,id
terraform import terrakube_organization_template.example 00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000
2 changes: 2 additions & 0 deletions examples/resources/terrakube_organization_variable/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Organization Variable can be import with organization_id,id
terraform import terrakube_organization_variable.example 00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000
2 changes: 2 additions & 0 deletions examples/resources/terrakube_team/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Team can be import with organization_id,id
terraform import terrakube_team.example 00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000
2 changes: 2 additions & 0 deletions examples/resources/terrakube_team_token/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Organization Template can be import with id
terraform import terrakube_team_token.example 00000000-0000-0000-0000-000000000000
2 changes: 2 additions & 0 deletions examples/resources/terrakube_vcs/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Organization VCS can be import with organization_id,id
terraform import terrakube_vcs.example 00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000
2 changes: 2 additions & 0 deletions examples/resources/terrakube_workspace_cli/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Workspace_cli can be import with organization_id,id
terraform import terrakube_workspace_cli.example 00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000
2 changes: 2 additions & 0 deletions examples/resources/terrakube_workspace_variable/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Organization Workspace Variable can be import with organization_id,workspace_id,id
terraform import terrakube_workspace_variable.example 00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000
3 changes: 2 additions & 1 deletion examples/resources/terrakube_workspace_vcs/import.sh
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
terraform import terrakube_workspace_vcs.example_workspace_vcs 00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000
# Workspace_vcs can be import with organization_id,id
terraform import terrakube_workspace_vcs.example 00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000
39 changes: 25 additions & 14 deletions internal/provider/organization_tag_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,20 +131,20 @@ func (r *OrganizationTagResource) Create(ctx context.Context, req resource.Creat

organizationTagResponse, err := r.client.Do(organizationTagRequest)
if err != nil {
resp.Diagnostics.AddError("Error executing organization tag resource request", fmt.Sprintf("Error executing organization tag resource request: %s", err))
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))
return
}

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

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

if err != nil {
resp.Diagnostics.AddError("Error unmarshal payload response", fmt.Sprintf("Error unmarshal payload response: %s", err))
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))
return
}

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

organizationTagResponse, err := r.client.Do(organizationTagRequest)
if err != nil {
resp.Diagnostics.AddError("Error executing organization tag resource request", fmt.Sprintf("Error executing organization tag resource request: %s", err))
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))
return
}

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

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

if err != nil {
resp.Diagnostics.AddError("Error unmarshal payload response", fmt.Sprintf("Error unmarshal payload response: %s", err))
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))
return
}

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

organizationTagResponse, err := r.client.Do(organizationTagRequest)
if err != nil {
resp.Diagnostics.AddError("Error executing organization tag resource request", fmt.Sprintf("Error executing organization tag resource request: %s", err))
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))
return
}

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

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

organizationTagResponse, err = r.client.Do(organizationTagRequest)
if err != nil {
resp.Diagnostics.AddError("Error executing organization tag resource request", fmt.Sprintf("Error executing organization tag resource request: %s", err))
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))
return
}

bodyResponse, err = io.ReadAll(organizationTagResponse.Body)
if err != nil {
resp.Diagnostics.AddError("Error reading organization tag resource response body", fmt.Sprintf("Error reading organization tag resource response body: %s", err))
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))
}

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

_, err = r.client.Do(reqOrg)
if err != nil {
resp.Diagnostics.AddError("Error executing organization tag resource request", fmt.Sprintf("Error executing organization tag resource request: %s", err))
organizationTagResponse, err := r.client.Do(reqOrg)
if err != nil || organizationTagResponse.StatusCode != http.StatusNoContent {
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))
return
}
}

func (r *OrganizationTagResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
idParts := strings.Split(req.ID, ",")

if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" {
resp.Diagnostics.AddError(
"Unexpected Import Identifier",
fmt.Sprintf("Expected import identifier with format: 'organization_ID,ID', Got: %q", req.ID),
)
return
}

resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("organization_id"), idParts[0])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), idParts[1])...)
}
37 changes: 24 additions & 13 deletions internal/provider/organization_template_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,20 +151,20 @@ func (r *OrganizationTemplateResource) Create(ctx context.Context, req resource.

organizationTemplateResponse, err := r.client.Do(organizationTemplateRequest)
if err != nil {
resp.Diagnostics.AddError("Error executing organization template resource request", fmt.Sprintf("Error executing organization template resource request: %s", err))
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))
return
}

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

err = jsonapi.UnmarshalPayload(strings.NewReader(string(bodyResponse)), organizationTemplate)
tflog.Info(ctx, string(bodyResponse))
if err != nil {
resp.Diagnostics.AddError("Error unmarshal payload response", fmt.Sprintf("Error unmarshal payload response: %s", err))
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))
return
}

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

organizationTemplateResponse, err := r.client.Do(organizationTemplateRequest)
if err != nil {
resp.Diagnostics.AddError("Error executing organization template resource request", fmt.Sprintf("Error executing organization template resource request: %s", err))
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))
return
}

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

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

organizationTemplateResponse, err := r.client.Do(organizationTemplateRequest)
if err != nil {
resp.Diagnostics.AddError("Error executing organization template resource request", fmt.Sprintf("Error executing organization template resource request: %s", err))
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))
return
}

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

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

organizationTemplateResponse, err = r.client.Do(organizationTemplateRequest)
if err != nil {
resp.Diagnostics.AddError("Error executing organization template resource request", fmt.Sprintf("Error executing organization template resource request: %s", err))
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))
return
}

bodyResponse, err = io.ReadAll(organizationTemplateResponse.Body)
if err != nil {
resp.Diagnostics.AddError("Error reading organization template resource response body", fmt.Sprintf("Error reading organization template resource response body: %s", err))
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))
}

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

_, err = r.client.Do(organizationTemplateRequest)
if err != nil {
resp.Diagnostics.AddError("Error executing organization template resource request", fmt.Sprintf("Error executing organization template resource request: %s", err))
organizationTemplateResponse, err := r.client.Do(organizationTemplateRequest)
if err != nil || organizationTemplateResponse.StatusCode != http.StatusNoContent {
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))
return
}
}

func (r *OrganizationTemplateResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
idParts := strings.Split(req.ID, ",")

if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" {
resp.Diagnostics.AddError(
"Unexpected Import Identifier",
fmt.Sprintf("Expected import identifier with format: 'organization_ID,ID', Got: %q", req.ID),
)
return
}

resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("organization_id"), idParts[0])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), idParts[1])...)
}
Loading

0 comments on commit dcf2de3

Please sign in to comment.