Skip to content

Commit b68b4ee

Browse files
committed
Add resource identity import support
1 parent 079d023 commit b68b4ee

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

manifest/provider/import.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1717
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
18+
"k8s.io/apimachinery/pkg/runtime/schema"
1819
)
1920

2021
// ImportResourceState function
@@ -49,7 +50,14 @@ func (s *RawProviderServer) ImportResourceState(ctx context.Context, req *tfprot
4950
return resp, nil
5051
}
5152

52-
gvk, name, namespace, err := util.ParseResourceID(req.ID)
53+
var gvk schema.GroupVersionKind
54+
var name, namespace string
55+
var err error
56+
if req.Identity != nil {
57+
gvk, name, namespace, err = parseResourceIdentityData(req.Identity)
58+
} else {
59+
gvk, name, namespace, err = util.ParseResourceID(req.ID)
60+
}
5361
if err != nil {
5462
resp.Diagnostics = append(resp.Diagnostics, &tfprotov5.Diagnostic{
5563
Severity: tfprotov5.DiagnosticSeverityError,
@@ -58,6 +66,7 @@ func (s *RawProviderServer) ImportResourceState(ctx context.Context, req *tfprot
5866
})
5967
}
6068
s.logger.Trace("[ImportResourceState]", "[ID]", gvk, name, namespace)
69+
6170
rt, err := GetResourceType(req.TypeName)
6271
if err != nil {
6372
resp.Diagnostics = append(resp.Diagnostics, &tfprotov5.Diagnostic{
@@ -196,16 +205,24 @@ func (s *RawProviderServer) ImportResourceState(ctx context.Context, req *tfprot
196205
Detail: err.Error(),
197206
})
198207
}
208+
idData, err := createIdentityData(ro)
209+
if err != nil {
210+
return resp, err
211+
}
199212
nr := &tfprotov5.ImportedResource{
200213
TypeName: req.TypeName,
201214
State: &impState,
202215
Private: fb,
216+
Identity: &tfprotov5.ResourceIdentityData{
217+
IdentityData: &idData,
218+
},
203219
}
204220
resp.ImportedResources = append(resp.ImportedResources, nr)
205221
resp.Diagnostics = append(resp.Diagnostics, &tfprotov5.Diagnostic{
206222
Severity: tfprotov5.DiagnosticSeverityWarning,
207223
Summary: "Apply needed after 'import'",
208224
Detail: "Please run apply after a successful import to realign the resource state to the configuration in Terraform.",
209225
})
226+
210227
return resp, nil
211228
}

manifest/provider/resourceidentity.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package provider
22

33
import (
44
"context"
5+
"fmt"
56

67
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
78
"github.com/hashicorp/terraform-plugin-go/tftypes"
89
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
10+
"k8s.io/apimachinery/pkg/runtime/schema"
911
)
1012

1113
func (s *RawProviderServer) GetResourceIdentitySchemas(ctx context.Context, req *tfprotov5.GetResourceIdentitySchemasRequest) (*tfprotov5.GetResourceIdentitySchemasResponse, error) {
@@ -18,7 +20,7 @@ func (s *RawProviderServer) GetResourceIdentitySchemas(ctx context.Context, req
1820
{Name: "api_version", RequiredForImport: true, Type: tftypes.String},
1921
{Name: "kind", RequiredForImport: true, Type: tftypes.String},
2022
{Name: "name", RequiredForImport: true, Type: tftypes.String},
21-
{Name: "namespace", RequiredForImport: true, Type: tftypes.String},
23+
{Name: "namespace", OptionalForImport: true, Type: tftypes.String},
2224
},
2325
},
2426
},
@@ -32,6 +34,28 @@ func (s *RawProviderServer) UpgradeResourceIdentity(ctx context.Context, req *tf
3234
return resp, nil
3335
}
3436

37+
func parseResourceIdentityData(rid *tfprotov5.ResourceIdentityData) (schema.GroupVersionKind, string, string, error) {
38+
namespace := "default"
39+
var apiVersion, kind, name string
40+
41+
iddata, err := rid.IdentityData.Unmarshal(getIdentityType())
42+
if err != nil {
43+
return schema.GroupVersionKind{}, "", "",
44+
fmt.Errorf("could not unmarshal identity data: %v", err.Error())
45+
}
46+
47+
var idvals map[string]tftypes.Value
48+
iddata.As(&idvals)
49+
50+
idvals["api_version"].As(&apiVersion)
51+
idvals["kind"].As(&kind)
52+
idvals["namespace"].As(&namespace)
53+
idvals["name"].As(&name)
54+
55+
gvk := schema.FromAPIVersionAndKind(apiVersion, kind)
56+
return gvk, name, namespace, nil
57+
}
58+
3559
func getIdentityType() tftypes.Type {
3660
return tftypes.Object{
3761
AttributeTypes: map[string]tftypes.Type{

0 commit comments

Comments
 (0)