Repository: crossplane-contrib/provider-terraform (go)

Issue:
Validation error when no 'format' specified
<!--
Thank you for helping to improve Crossplane!

Please be sure to search for open issues before raising a new one. We use issues
for bug reports and feature requests. Please find us at https://slack.crossplane.io
for questions, support, and discussion.
-->

### What happened?
According to https://github.com/crossplane-contrib/provider-terraform/blob/master/examples/workspace-remote.yaml#L23-L34 the `format` should be omittable and default to HCL

It does not happen in reality
```sh
error: error validating "examples/terraform/eks/eks.yaml": error validating data: ValidationError(Workspace.spec.forProvider.varFiles[0]): missing required field "format" in io.crossplane.tf.v1alpha1.Workspace.spec.forProvider.varFiles; if you choose to ignore these errors, turn validation off with --validate=false
```


### How can we reproduce it?
Apply Workspace resource with VarFile without `format` explicitly specified
Example: https://github.com/crossplane-contrib/provider-terraform/blob/master/examples/workspace-remote.yaml#L22-L26


Pull request description:
Do not require `format` for `VarFiles`
* Make `format` optional with default to `HCL`
* Fixes #27

Signed-off-by: Yury Tsarev <yury.tsarev@absa.africa>

<!--
Thank you for helping to improve Crossplane!

Please read through https://git.io/fj2m9 if this is your first time opening a
Crossplane pull request. Find us in https://slack.crossplane.io/messages/dev if
you need any help contributing.
-->

### Description of your changes

<!--
Briefly describe what this pull request does. Be sure to direct your reviewers'
attention to anything that needs special consideration.

We love pull requests that resolve an open Crossplane issue. If yours does, you
can uncomment the below line to indicate which issue your PR fixes, for example
"Fixes #500":

-->
Fixes #27 

I have:

- [x] Read and followed Crossplane's [contribution process].
- [x] Run `make reviewable test` to ensure this PR is ready for review.

### How has this code been tested

<!--
Before reviewers can be confident in the correctness of this pull request, it
needs to tested and shown to be correct. Briefly describe the testing that has
already been done or which is planned for this change.
-->
end-to-end
[contribution process]: https://git.io/fj2m9


Patch:
diff --git a/apis/v1alpha1/workspace_types.go b/apis/v1alpha1/workspace_types.go
index 0053836..32528e5 100644
--- a/apis/v1alpha1/workspace_types.go
+++ b/apis/v1alpha1/workspace_types.go
@@ -43,7 +43,7 @@ const (
 type VarFileFormat string
 
 // Vars file formats.
-const (
+var (
 	VarFileFormatHCL  VarFileFormat = "HCL"
 	VarFileFormatJSON VarFileFormat = "JSON"
 )
@@ -55,7 +55,8 @@ type VarFile struct {
 
 	// Format of this vars file.
 	// +kubebuilder:default=HCL
-	Format VarFileFormat `json:"format"`
+	// +optional
+	Format *VarFileFormat `json:"format,omitempty"`
 
 	// A ConfigMap key containing the vars file.
 	// +optional
diff --git a/apis/v1alpha1/zz_generated.deepcopy.go b/apis/v1alpha1/zz_generated.deepcopy.go
index e23e444..9412c37 100644
--- a/apis/v1alpha1/zz_generated.deepcopy.go
+++ b/apis/v1alpha1/zz_generated.deepcopy.go
@@ -233,6 +233,11 @@ func (in *Var) DeepCopy() *Var {
 // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
 func (in *VarFile) DeepCopyInto(out *VarFile) {
 	*out = *in
+	if in.Format != nil {
+		in, out := &in.Format, &out.Format
+		*out = new(VarFileFormat)
+		**out = **in
+	}
 	if in.ConfigMapKeyReference != nil {
 		in, out := &in.ConfigMapKeyReference, &out.ConfigMapKeyReference
 		*out = new(KeyReference)
diff --git a/internal/controller/workspace/workspace.go b/internal/controller/workspace/workspace.go
index 403aac8..c580f22 100644
--- a/internal/controller/workspace/workspace.go
+++ b/internal/controller/workspace/workspace.go
@@ -324,7 +324,7 @@ func (c *external) options(ctx context.Context, p v1alpha1.WorkspaceParameters)
 
 	for _, vf := range p.VarFiles {
 		fmt := terraform.HCL
-		if vf.Format == v1alpha1.VarFileFormatJSON {
+		if vf.Format == &v1alpha1.VarFileFormatJSON {
 			fmt = terraform.JSON
 		}
 
diff --git a/package/crds/tf.crossplane.io_workspaces.yaml b/package/crds/tf.crossplane.io_workspaces.yaml
index 8ab03ae..98840b6 100644
--- a/package/crds/tf.crossplane.io_workspaces.yaml
+++ b/package/crds/tf.crossplane.io_workspaces.yaml
@@ -106,7 +106,6 @@ spec:
                           - SecretKey
                           type: string
                       required:
-                      - format
                       - source
                       type: object
                     type: array


Test patch:
diff --git a/internal/controller/workspace/workspace_test.go b/internal/controller/workspace/workspace_test.go
index 42c42c6..625f01c 100644
--- a/internal/controller/workspace/workspace_test.go
+++ b/internal/controller/workspace/workspace_test.go
@@ -826,7 +826,7 @@ func TestCreate(t *testing.T) {
 								{
 									Source:             v1alpha1.VarFileSourceSecretKey,
 									SecretKeyReference: &v1alpha1.KeyReference{},
-									Format:             v1alpha1.VarFileFormatJSON,
+									Format:             &v1alpha1.VarFileFormatJSON,
 								},
 							},
 						},
@@ -975,7 +975,7 @@ func TestDelete(t *testing.T) {
 								{
 									Source:             v1alpha1.VarFileSourceSecretKey,
 									SecretKeyReference: &v1alpha1.KeyReference{},
-									Format:             v1alpha1.VarFileFormatJSON,
+									Format:             &v1alpha1.VarFileFormatJSON,
 								},
 							},
 						},