From f00bbc5f88a2c2ff409cd1b50721eb3ec0e58a2d Mon Sep 17 00:00:00 2001 From: Stanley Zhang Date: Sat, 24 Aug 2024 03:20:03 +1200 Subject: [PATCH] feat: Make import work for workspace_vcs (#64) Additional changes: 1. Rename the examples for resource and data source so that doc generated includes examples 2. Add import example for workspace_vcs --- docs/data-sources/organization.md | 8 ++- docs/data-sources/organization_tag.md | 13 +++- docs/data-sources/organization_template.md | 13 +++- docs/data-sources/ssh.md | 13 +++- docs/data-sources/vcs.md | 13 +++- docs/resources/module.md | 32 ++++++++- docs/resources/organization.md | 10 ++- docs/resources/organization_tag.md | 9 ++- docs/resources/organization_template.md | 17 ++++- docs/resources/organization_variable.md | 26 ++++++- docs/resources/team.md | 14 +++- docs/resources/team_token.md | 12 +++- docs/resources/vcs.md | 15 ++++- docs/resources/workspace_cli.md | 24 ++++++- docs/resources/workspace_tag.md | 10 ++- docs/resources/workspace_variable.md | 28 +++++++- docs/resources/workspace_vcs.md | 35 ++++++++-- .../data-source.tf | 0 .../data-source.tf | 0 .../data-source.tf | 0 .../data-source.tf | 0 .../data-source.tf | 0 .../resource.tf | 0 .../resource.tf | 0 .../resource.tf | 0 .../resource.tf | 0 .../resource.tf | 0 .../resource.tf | 0 .../resource.tf | 0 .../resource.tf | 0 .../resource.tf | 0 .../resource.tf | 0 .../resource.tf | 0 .../terrakube_workspace_vcs/import.sh | 1 + .../resource.tf | 0 internal/provider/organization_data_source.go | 17 ++--- internal/provider/workspace_vcs_resource.go | 67 +++++++++++++------ main.go | 5 +- 38 files changed, 326 insertions(+), 56 deletions(-) rename examples/data-sources/{organization_example => terrakube_organization}/data-source.tf (100%) rename examples/data-sources/{organization_tag_example => terrakube_organization_tag}/data-source.tf (100%) rename examples/data-sources/{organization_template_example => terrakube_organization_template}/data-source.tf (100%) rename examples/data-sources/{ssh_example => terrakube_ssh}/data-source.tf (100%) rename examples/data-sources/{vcs_example => terrakube_vcs}/data-source.tf (100%) rename examples/resources/{module_resource => terrakube_module}/resource.tf (100%) rename examples/resources/{organization_resource => terrakube_organization}/resource.tf (100%) rename examples/resources/{organization_tag_resource => terrakube_organization_tag}/resource.tf (100%) rename examples/resources/{organization_template_resource => terrakube_organization_template}/resource.tf (100%) rename examples/resources/{organization_variable_resource => terrakube_organization_variable}/resource.tf (100%) rename examples/resources/{team_resource => terrakube_team}/resource.tf (100%) rename examples/resources/{team_token_resource => terrakube_team_token}/resource.tf (100%) rename examples/resources/{vcs_resource => terrakube_vcs}/resource.tf (100%) rename examples/resources/{workspace_cli_resource => terrakube_workspace_cli}/resource.tf (100%) rename examples/resources/{workspace_tag_resource => terrakube_workspace_tag}/resource.tf (100%) rename examples/resources/{workspace_variable_resource => terrakube_workspace_variable}/resource.tf (100%) create mode 100644 examples/resources/terrakube_workspace_vcs/import.sh rename examples/resources/{workspace_vcs_resource => terrakube_workspace_vcs}/resource.tf (100%) diff --git a/docs/data-sources/organization.md b/docs/data-sources/organization.md index 9a398c4..d83fb6f 100644 --- a/docs/data-sources/organization.md +++ b/docs/data-sources/organization.md @@ -1,6 +1,6 @@ --- # generated by https://github.com/hashicorp/terraform-plugin-docs -page_title: "terrakube_organization Data Source - terraform-provider-terrakube" +page_title: "terrakube_organization Data Source - terrakube" subcategory: "" description: |- @@ -10,7 +10,13 @@ description: |- +## Example Usage +```terraform +data "terrakube_organization" "org" { + name = "simple" +} +``` ## Schema diff --git a/docs/data-sources/organization_tag.md b/docs/data-sources/organization_tag.md index ea410fc..6d3df5b 100644 --- a/docs/data-sources/organization_tag.md +++ b/docs/data-sources/organization_tag.md @@ -1,6 +1,6 @@ --- # generated by https://github.com/hashicorp/terraform-plugin-docs -page_title: "terrakube_organization_tag Data Source - terraform-provider-terrakube" +page_title: "terrakube_organization_tag Data Source - terrakube" subcategory: "" description: |- @@ -10,7 +10,18 @@ description: |- +## Example Usage +```terraform +data "terrakube_organization" "org" { + name = "simple" +} + +data "terrakube_organization_tag" "tag" { + organization_id = data.terraform_organization.org.id + name = "test" +} +``` ## Schema diff --git a/docs/data-sources/organization_template.md b/docs/data-sources/organization_template.md index 683d3ee..8335d13 100644 --- a/docs/data-sources/organization_template.md +++ b/docs/data-sources/organization_template.md @@ -1,6 +1,6 @@ --- # generated by https://github.com/hashicorp/terraform-plugin-docs -page_title: "terrakube_organization_template Data Source - terraform-provider-terrakube" +page_title: "terrakube_organization_template Data Source - terrakube" subcategory: "" description: |- @@ -10,7 +10,18 @@ description: |- +## Example Usage +```terraform +data "terrakube_organization" "org" { + name = "simple" +} + +data "terrakube_organization_template" "template" { + name = "sample" + organization_id = data.terrakube_organization.org.id +} +``` ## Schema diff --git a/docs/data-sources/ssh.md b/docs/data-sources/ssh.md index 1ee29d7..abf0ddf 100644 --- a/docs/data-sources/ssh.md +++ b/docs/data-sources/ssh.md @@ -1,6 +1,6 @@ --- # generated by https://github.com/hashicorp/terraform-plugin-docs -page_title: "terrakube_ssh Data Source - terraform-provider-terrakube" +page_title: "terrakube_ssh Data Source - terrakube" subcategory: "" description: |- @@ -10,7 +10,18 @@ description: |- +## Example Usage +```terraform +data "terrakube_organization" "org" { + name = "simple" +} + +data "terrakube_ssh" "ssh" { + name = "sample" + organization_id = data.terrakube_organization.org.id +} +``` ## Schema diff --git a/docs/data-sources/vcs.md b/docs/data-sources/vcs.md index c7bce7c..82adf59 100644 --- a/docs/data-sources/vcs.md +++ b/docs/data-sources/vcs.md @@ -1,6 +1,6 @@ --- # generated by https://github.com/hashicorp/terraform-plugin-docs -page_title: "terrakube_vcs Data Source - terraform-provider-terrakube" +page_title: "terrakube_vcs Data Source - terrakube" subcategory: "" description: |- @@ -10,7 +10,18 @@ description: |- +## Example Usage +```terraform +data "terrakube_organization" "org" { + name = "simple" +} + +data "terrakube_vcs" "vcs" { + name = "sample" + organization_id = data.terrakube_organization.org.id +} +``` ## Schema diff --git a/docs/resources/module.md b/docs/resources/module.md index a5f2210..0a48900 100644 --- a/docs/resources/module.md +++ b/docs/resources/module.md @@ -1,6 +1,6 @@ --- # generated by https://github.com/hashicorp/terraform-plugin-docs -page_title: "terrakube_module Resource - terraform-provider-terrakube" +page_title: "terrakube_module Resource - terrakube" subcategory: "" description: |- @@ -10,7 +10,35 @@ description: |- - +## Example Usage + +```terraform +resource "terrakube_module" "module" { + name = "vpc" + organization_id = data.terrakube_organization.org.id + description = "cloudposse module" + provider_name = "aws" + source = "https://github.com/terraform-aws-modules/terraform-aws-vpc.git" +} + +resource "terrakube_module" "module" { + name = "vpc_private" + organization_id = data.terrakube_ssh.ssh.organization_id + description = "cloudposse module" + provider_name = "aws" + source = "https://github.com/terraform-aws-modules/terraform-aws-vpc.git" + vcs_id = data.terrakube_vcs.vcs.id +} + +resource "terrakube_module" "module" { + name = "vpc_private_ssh" + organization_id = data.terrakube_ssh.ssh.organization_id + description = "cloudposse module" + provider_name = "aws" + source = "https://github.com/terraform-aws-modules/terraform-aws-vpc.git" + ssh_id = data.terrakube_ssh.ssh.id +} +``` ## Schema diff --git a/docs/resources/organization.md b/docs/resources/organization.md index 6be1b63..65c8554 100644 --- a/docs/resources/organization.md +++ b/docs/resources/organization.md @@ -1,6 +1,6 @@ --- # generated by https://github.com/hashicorp/terraform-plugin-docs -page_title: "terrakube_organization Resource - terraform-provider-terrakube" +page_title: "terrakube_organization Resource - terrakube" subcategory: "" description: |- @@ -10,7 +10,15 @@ description: |- +## Example Usage +```terraform +resource "terrakube_organization" "organization" { + name = "sample-organization" + description = "sample organization description" + executionModule = "remote" +} +``` ## Schema diff --git a/docs/resources/organization_tag.md b/docs/resources/organization_tag.md index 0568de4..9c35ca4 100644 --- a/docs/resources/organization_tag.md +++ b/docs/resources/organization_tag.md @@ -1,6 +1,6 @@ --- # generated by https://github.com/hashicorp/terraform-plugin-docs -page_title: "terrakube_organization_tag Resource - terraform-provider-terrakube" +page_title: "terrakube_organization_tag Resource - terrakube" subcategory: "" description: |- @@ -10,7 +10,14 @@ description: |- +## Example Usage +```terraform +resource "terrakube_organization_tag" "example" { + name = "example" + organization_id = terraform.workspace.id +} +``` ## Schema diff --git a/docs/resources/organization_template.md b/docs/resources/organization_template.md index 755635f..6a1ab17 100644 --- a/docs/resources/organization_template.md +++ b/docs/resources/organization_template.md @@ -1,6 +1,6 @@ --- # generated by https://github.com/hashicorp/terraform-plugin-docs -page_title: "terrakube_organization_template Resource - terraform-provider-terrakube" +page_title: "terrakube_organization_template Resource - terrakube" subcategory: "" description: |- @@ -10,7 +10,22 @@ description: |- +## Example Usage +```terraform +resource "terrakube_organization_template" "example" { + name = "example" + organization_id = terrakube_organization.example.id + description = "Example organization template" + version = "1.0.0" + content = < ## Schema diff --git a/docs/resources/organization_variable.md b/docs/resources/organization_variable.md index 05cf07d..014c558 100644 --- a/docs/resources/organization_variable.md +++ b/docs/resources/organization_variable.md @@ -1,6 +1,6 @@ --- # generated by https://github.com/hashicorp/terraform-plugin-docs -page_title: "terrakube_organization_variable Resource - terraform-provider-terrakube" +page_title: "terrakube_organization_variable Resource - terrakube" subcategory: "" description: |- @@ -10,7 +10,29 @@ description: |- - +## Example Usage + +```terraform +resource "terrakube_organization_variable" "sample1" { + organization_id = data.terrakube_organization.org.id + key = "sample-env-var" + value = "sample-var2" + description = "sample env var2213241243" + category = "ENV" + sensitive = false + hcl = false +} + +resource "terrakube_organization_variable" "sample2" { + organization_id = data.terrakube_organization.org.id + key = "sample-terra-var" + value = "sample-terraform234" + description = "sample env var222212341234" + category = "TERRAFORM" + sensitive = false + hcl = false +} +``` ## Schema diff --git a/docs/resources/team.md b/docs/resources/team.md index 97bc372..e8c1936 100644 --- a/docs/resources/team.md +++ b/docs/resources/team.md @@ -1,6 +1,6 @@ --- # generated by https://github.com/hashicorp/terraform-plugin-docs -page_title: "terrakube_team Resource - terraform-provider-terrakube" +page_title: "terrakube_team Resource - terrakube" subcategory: "" description: |- @@ -10,7 +10,19 @@ description: |- +## Example Usage +```terraform +resource "terrakube_team" "team" { + name = "TERRAKUBE_SUPER_ADMIN" + organization_id = data.terrakube_organization.org.id + manage_workspace = false + manage_module = false + manage_provider = true + manage_vcs = true + manage_template = true +} +``` ## Schema diff --git a/docs/resources/team_token.md b/docs/resources/team_token.md index a067c02..ce3d01c 100644 --- a/docs/resources/team_token.md +++ b/docs/resources/team_token.md @@ -1,6 +1,6 @@ --- # generated by https://github.com/hashicorp/terraform-plugin-docs -page_title: "terrakube_team_token Resource - terraform-provider-terrakube" +page_title: "terrakube_team_token Resource - terrakube" subcategory: "" description: |- @@ -10,7 +10,17 @@ description: |- +## Example Usage +```terraform +resource "terrakube_team_token" "token1" { + description = "test" + team_name = "AZURE_DEVELOPERS" + days = 1 + hours = 1 + minutes = 1 +} +``` ## Schema diff --git a/docs/resources/vcs.md b/docs/resources/vcs.md index 554844e..b8d7bba 100644 --- a/docs/resources/vcs.md +++ b/docs/resources/vcs.md @@ -1,6 +1,6 @@ --- # generated by https://github.com/hashicorp/terraform-plugin-docs -page_title: "terrakube_vcs Resource - terraform-provider-terrakube" +page_title: "terrakube_vcs Resource - terrakube" subcategory: "" description: |- @@ -10,7 +10,20 @@ description: |- +## Example Usage +```terraform +resource "terrakube_vcs" "vcs" { + organization_id = data.terrakube_organization.org.id + name = "Github" + description = "test github connection" + vcs_type = "GITHUB" + client_id = "Iv1.1b9b7b1b1b1b1b1b" + client_secret = "hellotest" + endpoint = "https://github.com" + api_url = "https://api.github.com" +} +``` ## Schema diff --git a/docs/resources/workspace_cli.md b/docs/resources/workspace_cli.md index f8a2d79..19d605a 100644 --- a/docs/resources/workspace_cli.md +++ b/docs/resources/workspace_cli.md @@ -1,6 +1,6 @@ --- # generated by https://github.com/hashicorp/terraform-plugin-docs -page_title: "terrakube_workspace_cli Resource - terraform-provider-terrakube" +page_title: "terrakube_workspace_cli Resource - terrakube" subcategory: "" description: |- @@ -10,7 +10,27 @@ description: |- - +## Example Usage + +```terraform +resource "terrakube_workspace_cli" "sample1" { + organization_id = data.terrakube_organization.org.id + name = "work-from-provider1" + description = "sample" + execution_mode = "remote" + iac_type = "terraform" + iac_version = "1.5.7" +} + +resource "terrakube_workspace_cli" "sample2" { + organization_id = data.terrakube_organization.org.id + name = "work-from-provider2" + description = "sample" + execution_mode = "local" + iac_type = "tofu" + iac_version = "1.7.0" +} +``` ## Schema diff --git a/docs/resources/workspace_tag.md b/docs/resources/workspace_tag.md index 97fbba7..1d6c0ee 100644 --- a/docs/resources/workspace_tag.md +++ b/docs/resources/workspace_tag.md @@ -1,6 +1,6 @@ --- # generated by https://github.com/hashicorp/terraform-plugin-docs -page_title: "terrakube_workspace_tag Resource - terraform-provider-terrakube" +page_title: "terrakube_workspace_tag Resource - terrakube" subcategory: "" description: |- @@ -10,7 +10,15 @@ description: |- +## Example Usage +```terraform +resource "terrakube_workspace_tag" "example" { + tag_id = terrakube_tag.example.id + workspace_id = terrakube_workspace.example.id + organization_id = terrakube_organization.example.id +} +``` ## Schema diff --git a/docs/resources/workspace_variable.md b/docs/resources/workspace_variable.md index e4c04e9..b60f676 100644 --- a/docs/resources/workspace_variable.md +++ b/docs/resources/workspace_variable.md @@ -1,6 +1,6 @@ --- # generated by https://github.com/hashicorp/terraform-plugin-docs -page_title: "terrakube_workspace_variable Resource - terraform-provider-terrakube" +page_title: "terrakube_workspace_variable Resource - terrakube" subcategory: "" description: |- @@ -10,7 +10,31 @@ description: |- - +## Example Usage + +```terraform +resource "terrakube_workspace_variable" "sample1" { + organization_id = data.terrakube_organization.org.id + workspace_id = terrakube_workspace_cli.sample1.id + key = "sample-env-var" + value = "sample-value2222" + description = "sample env var" + category = "ENV" + sensitive = false + hcl = false +} + +resource "terrakube_workspace_variable" "sample2" { + organization_id = data.terrakube_organization.org.id + workspace_id = terrakube_workspace_cli.sample1.id + key = "sample-terra-var" + value = "sample-TERRAFORM" + description = "sample env var" + category = "TERRAFORM" + sensitive = false + hcl = false +} +``` ## Schema diff --git a/docs/resources/workspace_vcs.md b/docs/resources/workspace_vcs.md index b447e1f..c5d0e2b 100644 --- a/docs/resources/workspace_vcs.md +++ b/docs/resources/workspace_vcs.md @@ -1,6 +1,6 @@ --- # generated by https://github.com/hashicorp/terraform-plugin-docs -page_title: "terrakube_workspace_vcs Resource - terraform-provider-terrakube" +page_title: "terrakube_workspace_vcs Resource - terrakube" subcategory: "" description: |- @@ -10,18 +10,28 @@ description: |- +## Example Usage +```terraform +resource "terrakube_workspace_cli" "sample1" { + organization_id = data.terrakube_organization.org.id + name = "work-from-provider1" + description = "sample" + execution_mode = "remote" + repository = "https://github.com/AzBuilder/terrakube-docker-compose.git" + branch = "main" + folder = "/" + template_id = terrakube_organization_template.example.id + iac_type = "terraform" + iac_version = "1.5.7" +} +``` ## Schema ### Required -- `branch` (String) Workspace VCS branch -- `description` (String) Workspace VCS description -- `execution_mode` (String) Workspace VCS execution mode (remote or local) -- `folder` (String) Workspace VCS folder -- `iac_type` (String) Workspace VCS IaC type (Supported values terraform or tofu) - `iac_version` (String) Workspace VCS VCS type - `name` (String) Workspace VCS name - `organization_id` (String) Terrakube organization id @@ -30,8 +40,21 @@ description: |- ### Optional +- `branch` (String) Workspace VCS branch +- `description` (String) Workspace VCS description +- `execution_mode` (String) Workspace VCS execution mode (remote or local) +- `folder` (String) Workspace VCS folder +- `iac_type` (String) Workspace VCS IaC type (Supported values terraform or tofu) - `vcs_id` (String) VCS connection ID for private workspaces ### Read-Only - `id` (String) Workspace CLI Id + +## Import + +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 +``` diff --git a/examples/data-sources/organization_example/data-source.tf b/examples/data-sources/terrakube_organization/data-source.tf similarity index 100% rename from examples/data-sources/organization_example/data-source.tf rename to examples/data-sources/terrakube_organization/data-source.tf diff --git a/examples/data-sources/organization_tag_example/data-source.tf b/examples/data-sources/terrakube_organization_tag/data-source.tf similarity index 100% rename from examples/data-sources/organization_tag_example/data-source.tf rename to examples/data-sources/terrakube_organization_tag/data-source.tf diff --git a/examples/data-sources/organization_template_example/data-source.tf b/examples/data-sources/terrakube_organization_template/data-source.tf similarity index 100% rename from examples/data-sources/organization_template_example/data-source.tf rename to examples/data-sources/terrakube_organization_template/data-source.tf diff --git a/examples/data-sources/ssh_example/data-source.tf b/examples/data-sources/terrakube_ssh/data-source.tf similarity index 100% rename from examples/data-sources/ssh_example/data-source.tf rename to examples/data-sources/terrakube_ssh/data-source.tf diff --git a/examples/data-sources/vcs_example/data-source.tf b/examples/data-sources/terrakube_vcs/data-source.tf similarity index 100% rename from examples/data-sources/vcs_example/data-source.tf rename to examples/data-sources/terrakube_vcs/data-source.tf diff --git a/examples/resources/module_resource/resource.tf b/examples/resources/terrakube_module/resource.tf similarity index 100% rename from examples/resources/module_resource/resource.tf rename to examples/resources/terrakube_module/resource.tf diff --git a/examples/resources/organization_resource/resource.tf b/examples/resources/terrakube_organization/resource.tf similarity index 100% rename from examples/resources/organization_resource/resource.tf rename to examples/resources/terrakube_organization/resource.tf diff --git a/examples/resources/organization_tag_resource/resource.tf b/examples/resources/terrakube_organization_tag/resource.tf similarity index 100% rename from examples/resources/organization_tag_resource/resource.tf rename to examples/resources/terrakube_organization_tag/resource.tf diff --git a/examples/resources/organization_template_resource/resource.tf b/examples/resources/terrakube_organization_template/resource.tf similarity index 100% rename from examples/resources/organization_template_resource/resource.tf rename to examples/resources/terrakube_organization_template/resource.tf diff --git a/examples/resources/organization_variable_resource/resource.tf b/examples/resources/terrakube_organization_variable/resource.tf similarity index 100% rename from examples/resources/organization_variable_resource/resource.tf rename to examples/resources/terrakube_organization_variable/resource.tf diff --git a/examples/resources/team_resource/resource.tf b/examples/resources/terrakube_team/resource.tf similarity index 100% rename from examples/resources/team_resource/resource.tf rename to examples/resources/terrakube_team/resource.tf diff --git a/examples/resources/team_token_resource/resource.tf b/examples/resources/terrakube_team_token/resource.tf similarity index 100% rename from examples/resources/team_token_resource/resource.tf rename to examples/resources/terrakube_team_token/resource.tf diff --git a/examples/resources/vcs_resource/resource.tf b/examples/resources/terrakube_vcs/resource.tf similarity index 100% rename from examples/resources/vcs_resource/resource.tf rename to examples/resources/terrakube_vcs/resource.tf diff --git a/examples/resources/workspace_cli_resource/resource.tf b/examples/resources/terrakube_workspace_cli/resource.tf similarity index 100% rename from examples/resources/workspace_cli_resource/resource.tf rename to examples/resources/terrakube_workspace_cli/resource.tf diff --git a/examples/resources/workspace_tag_resource/resource.tf b/examples/resources/terrakube_workspace_tag/resource.tf similarity index 100% rename from examples/resources/workspace_tag_resource/resource.tf rename to examples/resources/terrakube_workspace_tag/resource.tf diff --git a/examples/resources/workspace_variable_resource/resource.tf b/examples/resources/terrakube_workspace_variable/resource.tf similarity index 100% rename from examples/resources/workspace_variable_resource/resource.tf rename to examples/resources/terrakube_workspace_variable/resource.tf diff --git a/examples/resources/terrakube_workspace_vcs/import.sh b/examples/resources/terrakube_workspace_vcs/import.sh new file mode 100644 index 0000000..0ea2fc3 --- /dev/null +++ b/examples/resources/terrakube_workspace_vcs/import.sh @@ -0,0 +1 @@ +terraform import terrakube_workspace_vcs.example_workspace_vcs 00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000 \ No newline at end of file diff --git a/examples/resources/workspace_vcs_resource/resource.tf b/examples/resources/terrakube_workspace_vcs/resource.tf similarity index 100% rename from examples/resources/workspace_vcs_resource/resource.tf rename to examples/resources/terrakube_workspace_vcs/resource.tf diff --git a/internal/provider/organization_data_source.go b/internal/provider/organization_data_source.go index 3253221..2b0529b 100644 --- a/internal/provider/organization_data_source.go +++ b/internal/provider/organization_data_source.go @@ -4,16 +4,17 @@ import ( "context" "crypto/tls" "fmt" - "github.com/google/jsonapi" - "github.com/hashicorp/terraform-plugin-framework/datasource" - "github.com/hashicorp/terraform-plugin-framework/datasource/schema" - "github.com/hashicorp/terraform-plugin-framework/types" - "github.com/hashicorp/terraform-plugin-log/tflog" "io" "net/http" "reflect" "strings" "terraform-provider-terrakube/internal/client" + + "github.com/google/jsonapi" + "github.com/hashicorp/terraform-plugin-framework/datasource" + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" + "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/hashicorp/terraform-plugin-log/tflog" ) var ( @@ -109,12 +110,12 @@ func (d *OrganizationDataSource) Read(ctx context.Context, req datasource.ReadRe resOrg, err := d.client.Do(reqOrg) if err != nil { - tflog.Error(ctx, "Error executing organization datasource request") + tflog.Error(ctx, fmt.Sprintf("Error executing organization datasource request, response status: %s, response body: %s, error: %s", resOrg.Status, resOrg.Body, err)) } body, err := io.ReadAll(resOrg.Body) if err != nil { - tflog.Error(ctx, "Error reading organization response") + tflog.Error(ctx, fmt.Sprintf("Error reading organization response, response status: %s, response body: %s, error: %s", resOrg.Status, resOrg.Body, err)) } var orgs []interface{} @@ -122,7 +123,7 @@ func (d *OrganizationDataSource) Read(ctx context.Context, req datasource.ReadRe orgs, err = jsonapi.UnmarshalManyPayload(strings.NewReader(string(body)), reflect.TypeOf(new(client.OrganizationEntity))) if err != nil { - resp.Diagnostics.AddError("Unable to unmarshal payload", fmt.Sprintf("Unable to marshal payload: %s", err)) + resp.Diagnostics.AddError("Unable to unmarshal payload", fmt.Sprintf("Unable to marshal payload, response status: %s, response body: %s, error: %s", resOrg.Status, resOrg.Body, err)) return } diff --git a/internal/provider/workspace_vcs_resource.go b/internal/provider/workspace_vcs_resource.go index bc465d9..6935b59 100644 --- a/internal/provider/workspace_vcs_resource.go +++ b/internal/provider/workspace_vcs_resource.go @@ -13,11 +13,14 @@ import ( "terraform-provider-terrakube/internal/client" "github.com/google/jsonapi" + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault" "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" + "github.com/hashicorp/terraform-plugin-framework/schema/validator" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-plugin-log/tflog" ) @@ -74,16 +77,26 @@ func (r *WorkspaceVcsResource) Schema(ctx context.Context, req resource.SchemaRe Description: "Workspace VCS name", }, "description": schema.StringAttribute{ - Required: true, + Optional: true, Description: "Workspace VCS description", }, "execution_mode": schema.StringAttribute{ - Required: true, + Optional: true, + Computed: true, + Default: stringdefault.StaticString("remote"), Description: "Workspace VCS execution mode (remote or local)", + Validators: []validator.String{ + stringvalidator.OneOf("remote", "local"), + }, }, "iac_type": schema.StringAttribute{ - Required: true, + Optional: true, + Computed: true, + Default: stringdefault.StaticString("terraform"), Description: "Workspace VCS IaC type (Supported values terraform or tofu)", + Validators: []validator.String{ + stringvalidator.OneOf("terraform", "tofu"), + }, }, "iac_version": schema.StringAttribute{ Required: true, @@ -98,11 +111,15 @@ func (r *WorkspaceVcsResource) Schema(ctx context.Context, req resource.SchemaRe Description: "Default template ID for the workspace", }, "branch": schema.StringAttribute{ - Required: true, + Optional: true, + Computed: true, + Default: stringdefault.StaticString("main"), Description: "Workspace VCS branch", }, "folder": schema.StringAttribute{ - Required: true, + Optional: true, + Computed: true, + Default: stringdefault.StaticString("/"), Description: "Workspace VCS folder", }, "vcs_id": schema.StringAttribute{ @@ -190,13 +207,13 @@ func (r *WorkspaceVcsResource) Create(ctx context.Context, req resource.CreateRe workspaceVcsResponse, err := r.client.Do(workspaceVcsRequest) if err != nil { - resp.Diagnostics.AddError("Error executing workspace vcs resource request", fmt.Sprintf("Error executing workspace vcs resource request: %s", err)) + resp.Diagnostics.AddError("Error executing workspace vcs resource request", fmt.Sprintf("Error executing workspace vcs resource request, response status: %s, response body: %s, error: %s", workspaceVcsResponse.Status, workspaceVcsResponse.Body, err)) return } bodyResponse, err := io.ReadAll(workspaceVcsResponse.Body) if err != nil { - tflog.Error(ctx, "Error reading workspace vcs resource response") + tflog.Error(ctx, fmt.Sprintf("Error reading workspace vcs resource response, response status: %s, response body: %s, error: %s", workspaceVcsResponse.Status, workspaceVcsResponse.Body, err)) } newWorkspaceVcs := &client.WorkspaceEntity{} @@ -204,7 +221,7 @@ func (r *WorkspaceVcsResource) Create(ctx context.Context, req resource.CreateRe err = jsonapi.UnmarshalPayload(strings.NewReader(string(bodyResponse)), newWorkspaceVcs) 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", workspaceVcsResponse.Status, workspaceVcsResponse.Body, err)) return } @@ -248,13 +265,13 @@ func (r *WorkspaceVcsResource) Read(ctx context.Context, req resource.ReadReques workspaceResponse, err := r.client.Do(workspaceRequest) if err != nil { - resp.Diagnostics.AddError("Error executing workspace vcs resource request", fmt.Sprintf("Error executing workspace cli resource request: %s", err)) + resp.Diagnostics.AddError("Error executing workspace vcs resource request", fmt.Sprintf("Error executing workspace cli resource request, response status: %s, response body: %s, error: %s", workspaceResponse.Status, workspaceResponse.Body, err)) return } bodyResponse, err := io.ReadAll(workspaceResponse.Body) if err != nil { - tflog.Error(ctx, "Error reading workspace vcs resource response") + tflog.Error(ctx, fmt.Sprintf("Error reading workspace vcs resource response, response status: %s, response body: %s, error: %s", workspaceResponse.Status, workspaceResponse.Body, err)) } workspace := &client.WorkspaceEntity{} @@ -262,7 +279,7 @@ func (r *WorkspaceVcsResource) Read(ctx context.Context, req resource.ReadReques err = jsonapi.UnmarshalPayload(strings.NewReader(string(bodyResponse)), workspace) 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", workspaceResponse.Status, workspaceResponse.Body, err)) return } @@ -330,13 +347,13 @@ func (r *WorkspaceVcsResource) Update(ctx context.Context, req resource.UpdateRe organizationResponse, err := r.client.Do(organizationRequest) if err != nil { - resp.Diagnostics.AddError("Error executing workspace vcs resource request", fmt.Sprintf("Error executing workspace vcs resource request: %s", err)) + resp.Diagnostics.AddError("Error executing workspace vcs resource request", fmt.Sprintf("Error executing workspace vcs resource request, response status: %s, response body: %s, error: %s", organizationResponse.Status, organizationResponse.Body, err)) return } bodyResponse, err := io.ReadAll(organizationResponse.Body) if err != nil { - tflog.Error(ctx, "Error reading workspace vcs resource response") + tflog.Error(ctx, fmt.Sprintf("Error reading workspace vcs resource response, response status: %s, response body: %s, error: %s", organizationResponse.Status, organizationResponse.Body, err)) } tflog.Info(ctx, "Body Response", map[string]any{"success": string(bodyResponse)}) @@ -351,13 +368,13 @@ func (r *WorkspaceVcsResource) Update(ctx context.Context, req resource.UpdateRe organizationResponse, err = r.client.Do(organizationRequest) if err != nil { - resp.Diagnostics.AddError("Error executing workspace vcs resource request", fmt.Sprintf("Error executing workspace vcs resource request: %s", err)) + resp.Diagnostics.AddError("Error executing workspace vcs resource request", fmt.Sprintf("Error executing workspace vcs resource request, response status: %s, response body: %s, error: %s", organizationResponse.Status, organizationResponse.Body, err)) return } bodyResponse, err = io.ReadAll(organizationResponse.Body) if err != nil { - resp.Diagnostics.AddError("Error reading workspace vcs resource response body", fmt.Sprintf("Error reading workspace vcs resource response body: %s", err)) + resp.Diagnostics.AddError("Error reading workspace vcs resource response body", fmt.Sprintf("Error reading workspace vcs resource response body, response status: %s, response body: %s, error: %s", organizationResponse.Status, organizationResponse.Body, err)) } tflog.Info(ctx, "Body Response", map[string]any{"bodyResponse": string(bodyResponse)}) @@ -366,7 +383,7 @@ func (r *WorkspaceVcsResource) Update(ctx context.Context, req resource.UpdateRe err = jsonapi.UnmarshalPayload(strings.NewReader(string(bodyResponse)), workspace) 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", organizationResponse.Status, organizationResponse.Body, err)) return } @@ -447,15 +464,25 @@ func (r *WorkspaceVcsResource) Delete(ctx context.Context, req resource.DeleteRe } workspaceVcsResponse, err := r.client.Do(workspaceVcsRequest) - if err != nil { - resp.Diagnostics.AddError("Error executing vcs resource request", fmt.Sprintf("Error executing vcs resource request: %s", err)) + if err != nil || workspaceVcsResponse.StatusCode != http.StatusNoContent { + resp.Diagnostics.AddError("Error executing vcs resource request", fmt.Sprintf("Error executing vcs resource request, response status: %s, response body: %s, error: %s", workspaceVcsResponse.Status, workspaceVcsResponse.Body, err)) return } tflog.Info(ctx, "Delete response code: "+strconv.Itoa(workspaceVcsResponse.StatusCode)) - } func (r *WorkspaceVcsResource) 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,Workspace_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])...) } diff --git a/main.go b/main.go index 198e366..204d117 100644 --- a/main.go +++ b/main.go @@ -5,8 +5,9 @@ import ( "flag" "log" - "github.com/hashicorp/terraform-plugin-framework/providerserver" "terraform-provider-terrakube/internal/provider" + + "github.com/hashicorp/terraform-plugin-framework/providerserver" ) // Run "go generate" to format example terraform files and generate the docs for the registry/website @@ -17,7 +18,7 @@ import ( // Run the docs generation tool, check its repository for more information on how it works and how docs // can be customized. -//go:generate go run github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs +//go:generate go run github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs generate -provider-name terrakube var ( // these will be set by the goreleaser configuration