-
Notifications
You must be signed in to change notification settings - Fork 992
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
450 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
package provider | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-go/tfprotov5" | ||
"github.com/hashicorp/terraform-plugin-go/tftypes" | ||
"github.com/hashicorp/terraform-provider-kubernetes/manifest/morph" | ||
"github.com/hashicorp/terraform-provider-kubernetes/manifest/payload" | ||
|
||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/api/meta" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
) | ||
|
||
// ReadDataSource function | ||
func (s *RawProviderServer) ReadDataSource(ctx context.Context, req *tfprotov5.ReadDataSourceRequest) (*tfprotov5.ReadDataSourceResponse, error) { | ||
s.logger.Trace("[ReadDataSource][Request]\n%s\n", dump(*req)) | ||
|
||
resp := &tfprotov5.ReadDataSourceResponse{} | ||
|
||
execDiag := s.canExecute() | ||
if len(execDiag) > 0 { | ||
resp.Diagnostics = append(resp.Diagnostics, execDiag...) | ||
return resp, nil | ||
} | ||
|
||
rt, err := GetDataSourceType(req.TypeName) | ||
if err != nil { | ||
resp.Diagnostics = append(resp.Diagnostics, &tfprotov5.Diagnostic{ | ||
Severity: tfprotov5.DiagnosticSeverityError, | ||
Summary: "Failed to determine data source type", | ||
Detail: err.Error(), | ||
}) | ||
return resp, nil | ||
} | ||
|
||
config, err := req.Config.Unmarshal(rt) | ||
if err != nil { | ||
resp.Diagnostics = append(resp.Diagnostics, &tfprotov5.Diagnostic{ | ||
Severity: tfprotov5.DiagnosticSeverityError, | ||
Summary: "Failed to unmarshal data source configuration", | ||
Detail: err.Error(), | ||
}) | ||
return resp, nil | ||
} | ||
|
||
var dsConfig map[string]tftypes.Value | ||
err = config.As(&dsConfig) | ||
if err != nil { | ||
resp.Diagnostics = append(resp.Diagnostics, &tfprotov5.Diagnostic{ | ||
Severity: tfprotov5.DiagnosticSeverityError, | ||
Summary: "Failed to extract attributes from data source configuration", | ||
Detail: err.Error(), | ||
}) | ||
return resp, nil | ||
} | ||
|
||
rm, err := s.getRestMapper() | ||
if err != nil { | ||
resp.Diagnostics = append(resp.Diagnostics, &tfprotov5.Diagnostic{ | ||
Severity: tfprotov5.DiagnosticSeverityError, | ||
Summary: "Failed to get RESTMapper client", | ||
Detail: err.Error(), | ||
}) | ||
return resp, nil | ||
} | ||
|
||
client, err := s.getDynamicClient() | ||
if err != nil { | ||
resp.Diagnostics = append(resp.Diagnostics, &tfprotov5.Diagnostic{ | ||
Severity: tfprotov5.DiagnosticSeverityError, | ||
Summary: "failed to get Dynamic client", | ||
Detail: err.Error(), | ||
}) | ||
return resp, nil | ||
} | ||
|
||
var apiVersion, kind string | ||
dsConfig["api_version"].As(&apiVersion) | ||
dsConfig["kind"].As(&kind) | ||
|
||
gvr, err := getGVR(apiVersion, kind, rm) | ||
if err != nil { | ||
resp.Diagnostics = append(resp.Diagnostics, &tfprotov5.Diagnostic{ | ||
Severity: tfprotov5.DiagnosticSeverityError, | ||
Summary: "Failed to determine resource GroupVersion", | ||
Detail: err.Error(), | ||
}) | ||
return resp, nil | ||
} | ||
|
||
gvk := gvr.GroupVersion().WithKind(kind) | ||
ns, err := IsResourceNamespaced(gvk, rm) | ||
if err != nil { | ||
resp.Diagnostics = append(resp.Diagnostics, &tfprotov5.Diagnostic{ | ||
Severity: tfprotov5.DiagnosticSeverityError, | ||
Summary: "Failed determine if resource is namespaced", | ||
Detail: err.Error(), | ||
}) | ||
return resp, nil | ||
} | ||
rcl := client.Resource(gvr) | ||
|
||
objectType, err := s.TFTypeFromOpenAPI(ctx, gvk, false) | ||
if err != nil { | ||
resp.Diagnostics = append(resp.Diagnostics, &tfprotov5.Diagnostic{ | ||
Severity: tfprotov5.DiagnosticSeverityError, | ||
Summary: "Failed to save resource state", | ||
Detail: err.Error(), | ||
}) | ||
return resp, nil | ||
} | ||
|
||
var metadataBlock []tftypes.Value | ||
dsConfig["metadata"].As(&metadataBlock) | ||
|
||
var metadata map[string]tftypes.Value | ||
metadataBlock[0].As(&metadata) | ||
|
||
var name string | ||
metadata["name"].As(&name) | ||
|
||
var res *unstructured.Unstructured | ||
if ns { | ||
var namespace string | ||
metadata["namespace"].As(&namespace) | ||
if namespace == "" { | ||
namespace = "default" | ||
} | ||
res, err = rcl.Namespace(namespace).Get(ctx, name, metav1.GetOptions{}) | ||
} else { | ||
res, err = rcl.Get(ctx, name, metav1.GetOptions{}) | ||
} | ||
if err != nil { | ||
if apierrors.IsNotFound(err) { | ||
return resp, nil | ||
} | ||
d := tfprotov5.Diagnostic{ | ||
Severity: tfprotov5.DiagnosticSeverityError, | ||
Summary: fmt.Sprintf("Failed to get data source"), | ||
Detail: err.Error(), | ||
} | ||
resp.Diagnostics = append(resp.Diagnostics, &d) | ||
return resp, nil | ||
} | ||
|
||
fo := RemoveServerSideFields(res.Object) | ||
nobj, err := payload.ToTFValue(fo, objectType, tftypes.NewAttributePath()) | ||
if err != nil { | ||
resp.Diagnostics = append(resp.Diagnostics, &tfprotov5.Diagnostic{ | ||
Severity: tfprotov5.DiagnosticSeverityError, | ||
Summary: "Failed to convert API response to Terraform value type", | ||
Detail: err.Error(), | ||
}) | ||
return resp, nil | ||
} | ||
|
||
nobj, err = morph.DeepUnknown(objectType, nobj, tftypes.NewAttributePath()) | ||
if err != nil { | ||
resp.Diagnostics = append(resp.Diagnostics, &tfprotov5.Diagnostic{ | ||
Severity: tfprotov5.DiagnosticSeverityError, | ||
Summary: "Failed to save resource state", | ||
Detail: err.Error(), | ||
}) | ||
return resp, nil | ||
} | ||
rawState := make(map[string]tftypes.Value) | ||
err = config.As(&rawState) | ||
if err != nil { | ||
resp.Diagnostics = append(resp.Diagnostics, &tfprotov5.Diagnostic{ | ||
Severity: tfprotov5.DiagnosticSeverityError, | ||
Summary: "Failed to save resource state", | ||
Detail: err.Error(), | ||
}) | ||
return resp, nil | ||
} | ||
rawState["object"] = morph.UnknownToNull(nobj) | ||
|
||
v := tftypes.NewValue(rt, rawState) | ||
state, err := tfprotov5.NewDynamicValue(v.Type(), v) | ||
if err != nil { | ||
resp.Diagnostics = append(resp.Diagnostics, &tfprotov5.Diagnostic{ | ||
Severity: tfprotov5.DiagnosticSeverityError, | ||
Summary: "Failed to save resource state", | ||
Detail: err.Error(), | ||
}) | ||
return resp, nil | ||
} | ||
resp.State = &state | ||
return resp, nil | ||
} | ||
|
||
func getGVR(apiVersion, kind string, m meta.RESTMapper) (schema.GroupVersionResource, error) { | ||
gv, err := schema.ParseGroupVersion(apiVersion) | ||
if err != nil { | ||
return schema.GroupVersionResource{}, err | ||
} | ||
mapping, err := m.RESTMapping(gv.WithKind(kind).GroupKind(), gv.Version) | ||
if err != nil { | ||
return schema.GroupVersionResource{}, err | ||
} | ||
return mapping.Resource, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// +build acceptance | ||
|
||
package acceptance | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/hashicorp/go-hclog" | ||
"github.com/hashicorp/terraform-provider-kubernetes/manifest/provider" | ||
"github.com/hashicorp/terraform-provider-kubernetes/manifest/test/helper/kubernetes" | ||
|
||
tfstatehelper "github.com/hashicorp/terraform-provider-kubernetes/manifest/test/helper/state" | ||
) | ||
|
||
func TestDataSourceKubernetesResource_ConfigMap(t *testing.T) { | ||
name := randName() | ||
name2 := randName() | ||
namespace := randName() | ||
|
||
k8shelper.CreateNamespace(t, namespace) | ||
defer k8shelper.DeleteResource(t, namespace, kubernetes.NewGroupVersionResource("v1", "namespaces")) | ||
|
||
// STEP 1: Create a ConfigMap to use as a data source | ||
tf := tfhelper.RequireNewWorkingDir(t) | ||
tf.SetReattachInfo(reattachInfo) | ||
defer func() { | ||
tf.RequireDestroy(t) | ||
tf.Close() | ||
k8shelper.AssertNamespacedResourceDoesNotExist(t, "v1", "configmaps", namespace, name) | ||
}() | ||
|
||
tfvars := TFVARS{ | ||
"name": name, | ||
"name2": name2, | ||
"namespace": namespace, | ||
} | ||
tfconfig := loadTerraformConfig(t, "datasource/step1.tf", tfvars) | ||
tf.RequireSetConfig(t, tfconfig) | ||
tf.RequireInit(t) | ||
tf.RequireApply(t) | ||
|
||
k8shelper.AssertNamespacedResourceExists(t, "v1", "configmaps", namespace, name) | ||
|
||
// STEP 2: Create another ConfigMap using the ConfigMap from step 1 as a data source | ||
reattachInfo2, err := provider.ServeTest(context.TODO(), hclog.Default()) | ||
if err != nil { | ||
t.Errorf("Failed to create additional provider instance: %q", err) | ||
} | ||
step2 := tfhelper.RequireNewWorkingDir(t) | ||
step2.SetReattachInfo(reattachInfo2) | ||
defer func() { | ||
step2.RequireDestroy(t) | ||
step2.Close() | ||
k8shelper.AssertNamespacedResourceDoesNotExist(t, "v1", "configmaps", namespace, name2) | ||
}() | ||
|
||
tfconfig = loadTerraformConfig(t, "datasource/step2.tf", tfvars) | ||
step2.RequireSetConfig(t, string(tfconfig)) | ||
step2.RequireInit(t) | ||
step2.RequireApply(t) | ||
|
||
tfstate := tfstatehelper.NewHelper(step2.RequireState(t)) | ||
|
||
// check the data source | ||
tfstate.AssertAttributeValues(t, tfstatehelper.AttributeValues{ | ||
"data.kubernetes_resource.test_config.object.data.TEST": "hello world", | ||
}) | ||
// check the resource was created with the correct value | ||
tfstate.AssertAttributeValues(t, tfstatehelper.AttributeValues{ | ||
"kubernetes_manifest.test_config2.object.data.TEST": "hello world", | ||
}) | ||
} |
Oops, something went wrong.