Skip to content

Commit

Permalink
feat: project import
Browse files Browse the repository at this point in the history
  • Loading branch information
henryde committed May 15, 2024
1 parent a5599f9 commit 6fe941b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 23 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
timeout-minutes: 5
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0
- uses: actions/setup-go@v5
with:
go-version-file: 'go.mod'
cache: true
Expand All @@ -38,7 +38,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0
- uses: actions/setup-go@v5
with:
go-version-file: 'go.mod'
cache: true
Expand Down Expand Up @@ -66,7 +66,7 @@ jobs:
- '1.4.*'
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0
- uses: actions/setup-go@v5
with:
go-version-file: 'go.mod'
cache: true
Expand Down
30 changes: 21 additions & 9 deletions examples/provider-install-verification/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,36 @@ terraform {
}
}

locals {
workspace = "henrys-workspace"
}

provider "meshstack" {
endpoint = "https://federation.dev.meshcloud.io"
apikey = "c0da6389-217a-4581-bed0-2728a1fed78b"
apisecret = "tnPKGcVoFKeaCXRyknRfx0V2xXL7bKbo"
apikey = "9a2708d1-bc40-459c-8102-c9aeb4fd476a"
apisecret = "pceKXXSOvHSyow3SvHhsoLQbyVE4Ke7z"
}

data "meshstack_buildingblock" "test" {
data "meshstack_project" "test" {
metadata = {
uuid = "a797d382-b316-4827-95a1-5af8e6a1217f"
owned_by_workspace = local.workspace
name = "henrys-project-dev"
}
}

data "meshstack_buildingblock" "test_failed" {
resource "meshstack_project" "test" {
metadata = {
uuid = "4570f298-1f6a-48d0-8f93-13dac606ceb0"
owned_by_workspace = local.workspace
name = "test-project-api-2-dev"
}
}

output "bb_provider_uuid" {
value = data.meshstack_buildingblock.test.metadata.definition_uuid
spec = {
display_name = "Test Project Two"
payment_method_identifier = "free"
tags = {
environment = ["dev"]
confidentiality = ["Internal"]
BusinessUnit = ["Sales"]
}
}
}
1 change: 0 additions & 1 deletion internal/provider/project_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,4 @@ func (d *projectDataSource) Read(ctx context.Context, req datasource.ReadRequest

// client data maps directly to the schema so we just need to set the state
resp.Diagnostics.Append(resp.State.Set(ctx, project)...)
return
}
41 changes: 31 additions & 10 deletions internal/provider/project_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package provider
import (
"context"
"fmt"
"strings"

"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"
Expand All @@ -15,8 +17,9 @@ import (

// Ensure the implementation satisfies the expected interfaces.
var (
_ resource.Resource = &projectResource{}
_ resource.ResourceWithConfigure = &projectResource{}
_ resource.Resource = &projectResource{}
_ resource.ResourceWithConfigure = &projectResource{}
_ resource.ResourceWithImportState = &projectResource{}
)

// NewProjectResource is a helper function to simplify the provider implementation.
Expand Down Expand Up @@ -63,6 +66,7 @@ func (r *projectResource) Schema(_ context.Context, _ resource.SchemaRequest, re
"api_version": schema.StringAttribute{
MarkdownDescription: "Project datatype version",
Computed: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
},

"kind": schema.StringAttribute{
Expand All @@ -71,6 +75,7 @@ func (r *projectResource) Schema(_ context.Context, _ resource.SchemaRequest, re
Validators: []validator.String{
stringvalidator.OneOf([]string{"meshProject"}...),
},
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
},

"metadata": schema.SingleNestedAttribute{
Expand All @@ -85,7 +90,10 @@ func (r *projectResource) Schema(_ context.Context, _ resource.SchemaRequest, re
Required: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()},
},
"created_on": schema.StringAttribute{Computed: true},
"created_on": schema.StringAttribute{
Computed: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
},
"deleted_on": schema.StringAttribute{Computed: true},
},
},
Expand Down Expand Up @@ -194,14 +202,12 @@ func (r *projectResource) Create(ctx context.Context, req resource.CreateRequest

// Read refreshes the Terraform state with the latest data.
func (r *projectResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var state MeshProject
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// read needed attributes individually instead of the full data structure which can break because of missing elements
var workspace, name string
resp.Diagnostics.Append(req.State.GetAttribute(ctx, path.Root("metadata").AtName("owned_by_workspace"), &workspace)...)
resp.Diagnostics.Append(req.State.GetAttribute(ctx, path.Root("metadata").AtName("name"), &name)...)

project, err := r.client.ReadProject(state.Metadata.OwnedByWorkspace, state.Metadata.Name)
project, err := r.client.ReadProject(workspace, name)
if err != nil {
resp.Diagnostics.AddError("Unable to read project", err.Error())
}
Expand Down Expand Up @@ -284,3 +290,18 @@ func (r *projectResource) Delete(ctx context.Context, req resource.DeleteRequest
return
}
}

func (r *projectResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
identifier := strings.Split(req.ID, ".")

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

resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("metadata").AtName("owned_by_workspace"), identifier[0])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("metadata").AtName("name"), identifier[1])...)
}

0 comments on commit 6fe941b

Please sign in to comment.