Skip to content

Commit 951b608

Browse files
authored
Merge pull request #5 from KusionStack/feat/import
feat: support import
2 parents ae78964 + 5d60642 commit 951b608

File tree

8 files changed

+121
-13
lines changed

8 files changed

+121
-13
lines changed

docs/data-sources/cluster.md

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,47 @@
33
page_title: "karpor_cluster Data Source - karpor"
44
subcategory: ""
55
description: |-
6-
6+
Get cluster information
77
---
88

99
# karpor_cluster (Data Source)
1010

11-
12-
13-
11+
Get cluster information
12+
13+
## Example Usage
14+
15+
```terraform
16+
terraform {
17+
required_providers {
18+
karpor = {
19+
source = "registry.terraform.io/KusionStack/karpor"
20+
version = "0.1.0"
21+
}
22+
}
23+
}
24+
25+
provider "karpor" {
26+
api_endpoint = "https://127.0.0.1:7443"
27+
api_key = "your-api-key-here"
28+
skip_tls_verify = true
29+
}
30+
31+
data "karpor_cluster" "example" {
32+
cluster_name = "local-cluster"
33+
}
34+
35+
output "cluster_name" {
36+
value = data.karpor_cluster.example.cluster_name
37+
}
38+
39+
output "display_name" {
40+
value = data.karpor_cluster.example.display_name
41+
}
42+
43+
output "description" {
44+
value = data.karpor_cluster.example.description
45+
}
46+
```
1447

1548
<!-- schema generated by tfplugindocs -->
1649
## Schema

docs/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
page_title: "karpor Provider"
44
subcategory: ""
55
description: |-
6-
6+
Interact with Karpor
77
---
88

99
# karpor Provider
1010

11-
11+
Interact with Karpor
1212

1313
## Example Usage
1414

docs/resources/cluster_registration.md

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,73 @@
33
page_title: "karpor_cluster_registration Resource - karpor"
44
subcategory: ""
55
description: |-
6-
6+
Manage cluster registration
77
---
88

99
# karpor_cluster_registration (Resource)
1010

11+
Manage cluster registration
1112

13+
## Example Usage
1214

15+
```terraform
16+
terraform {
17+
required_providers {
18+
karpor = {
19+
source = "registry.terraform.io/KusionStack/karpor"
20+
version = "0.1.0"
21+
}
22+
}
23+
}
1324
25+
provider "karpor" {
26+
api_endpoint = "https://127.0.0.1:7443"
27+
api_key = "your-api-key-here"
28+
skip_tls_verify = true
29+
}
30+
31+
resource "karpor_cluster_registration" "example" {
32+
cluster_name = "local-cluster"
33+
display_name = "local-cluster-display-name"
34+
credentials = file("~/config")
35+
description = "local-cluster-description"
36+
}
37+
38+
39+
# make sure you have a existing demo cluster in karpor
40+
# id is the cluster name
41+
import {
42+
to = karpor_cluster_registration.imported
43+
id = "demo"
44+
}
45+
46+
resource "karpor_cluster_registration" "imported" {
47+
cluster_name = "demo"
48+
# Setting credentials in imported resource will destroy and then create replacement
49+
# credentials = file("~/config")
50+
description = "demo-description"
51+
display_name = "demo-display-name"
52+
}
53+
54+
output "cluster_name" {
55+
value = karpor_cluster_registration.example.cluster_name
56+
}
57+
58+
output "display_name" {
59+
value = karpor_cluster_registration.example.display_name
60+
}
61+
```
1462

1563
<!-- schema generated by tfplugindocs -->
1664
## Schema
1765

1866
### Required
1967

2068
- `cluster_name` (String) Unique name for the cluster
21-
- `credentials` (String, Sensitive) Path to kubeconfig file
2269

2370
### Optional
2471

72+
- `credentials` (String, Sensitive) Path to kubeconfig file
2573
- `description` (String) Human-readable description
2674
- `display_name` (String) Human-readable display name
2775

File renamed without changes.

examples/resources/resource.tf renamed to examples/resources/karpor_cluster_registration/resource.tf

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,29 @@ resource "karpor_cluster_registration" "example" {
1717
cluster_name = "local-cluster"
1818
display_name = "local-cluster-display-name"
1919
credentials = file("~/config")
20-
description = "Local Kubernetes cluster"
20+
description = "local-cluster-description"
2121
}
2222

2323

24+
# make sure you have a existing demo cluster in karpor
25+
# id is the cluster name
26+
import {
27+
to = karpor_cluster_registration.imported
28+
id = "demo"
29+
}
30+
31+
resource "karpor_cluster_registration" "imported" {
32+
cluster_name = "demo"
33+
# Setting credentials in imported resource will destroy and then create replacement
34+
# credentials = file("~/config")
35+
description = "demo-description"
36+
display_name = "demo-display-name"
37+
}
38+
2439
output "cluster_name" {
2540
value = karpor_cluster_registration.example.cluster_name
2641
}
2742

2843
output "display_name" {
2944
value = karpor_cluster_registration.example.display_name
30-
}
45+
}

internal/provider/cluster_data_source.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func (d *ClusterDataSource) Metadata(ctx context.Context, req datasource.Metadat
4141
// Schema returns the schema for the datasource.
4242
func (d *ClusterDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
4343
resp.Schema = schema.Schema{
44+
Description: "Get cluster information",
4445
Attributes: map[string]schema.Attribute{
4546
"cluster_name": schema.StringAttribute{
4647
Required: true,

internal/provider/cluster_registration_resource.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"time"
77

8+
"github.com/hashicorp/terraform-plugin-framework/path"
89
"github.com/hashicorp/terraform-plugin-framework/resource"
910
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
1011
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
@@ -15,8 +16,9 @@ import (
1516

1617
// Ensure the implementation satisfies the expected interfaces.
1718
var (
18-
_ resource.Resource = &ClusterRegistrationResource{}
19-
_ resource.ResourceWithConfigure = &ClusterRegistrationResource{}
19+
_ resource.Resource = &ClusterRegistrationResource{}
20+
_ resource.ResourceWithConfigure = &ClusterRegistrationResource{}
21+
_ resource.ResourceWithImportState = &ClusterRegistrationResource{}
2022
)
2123

2224
// NewClusterRegistrationResource returns a new resource.Resource.
@@ -47,6 +49,7 @@ func (r *ClusterRegistrationResource) Metadata(_ context.Context, req resource.M
4749
// Schema returns the resource schema.
4850
func (r *ClusterRegistrationResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
4951
resp.Schema = schema.Schema{
52+
Description: "Manage cluster registration",
5053
Attributes: map[string]schema.Attribute{
5154
"cluster_name": schema.StringAttribute{
5255
Required: true,
@@ -60,7 +63,7 @@ func (r *ClusterRegistrationResource) Schema(_ context.Context, _ resource.Schem
6063
Description: "Human-readable display name",
6164
},
6265
"credentials": schema.StringAttribute{
63-
Required: true,
66+
Optional: true,
6467
Sensitive: true,
6568
Description: "Path to kubeconfig file",
6669
PlanModifiers: []planmodifier.String{
@@ -143,6 +146,7 @@ func (c *ClusterRegistrationResource) Read(ctx context.Context, req resource.Rea
143146

144147
// Overwrite items with refreshed state
145148
state.ClusterName = remoteState.ClusterName
149+
state.DisplayName = remoteState.DisplayName
146150
state.Description = remoteState.Description
147151
state.Id = remoteState.Id
148152

@@ -214,6 +218,12 @@ func (c *ClusterRegistrationResource) Delete(ctx context.Context, req resource.D
214218
}
215219
}
216220

221+
// ImportState imports the resource.
222+
func (c *ClusterRegistrationResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
223+
// Terraform will automatically call the resource's Read method to import the rest of the Terraform state
224+
resource.ImportStatePassthroughID(ctx, path.Root("cluster_name"), req, resp)
225+
}
226+
217227
// Configure configures the resource.
218228
func (c *ClusterRegistrationResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
219229
if req.ProviderData == nil {

internal/provider/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func (p *KarporProvider) Metadata(_ context.Context, _ provider.MetadataRequest,
4343
// Schema returns the provider schema.
4444
func (p *KarporProvider) Schema(_ context.Context, _ provider.SchemaRequest, resp *provider.SchemaResponse) {
4545
resp.Schema = schema.Schema{
46+
Description: "Interact with Karpor",
4647
Attributes: map[string]schema.Attribute{
4748
"api_endpoint": schema.StringAttribute{
4849
Optional: true,

0 commit comments

Comments
 (0)