-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds Properties and Required fields to CustomResourceDefinition (#28)
* Adds CRD fields: property and required --------- Co-authored-by: Nick Schuch <nick@myschuch.com>
- Loading branch information
1 parent
0be2e66
commit 461181f
Showing
9 changed files
with
176 additions
and
13 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
37 changes: 37 additions & 0 deletions
37
internal/resources/apiextensions/v1beta1/crd/property/expand.go
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,37 @@ | ||
package property | ||
|
||
import ( | ||
"k8s.io/utils/pointer" | ||
|
||
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
) | ||
|
||
// Expand will return a structured object. | ||
func Expand(in []interface{}) map[string]apiextensionsv1.JSONSchemaProps { | ||
if len(in) == 0 { | ||
return nil | ||
} | ||
|
||
vars := make(map[string]apiextensionsv1.JSONSchemaProps, len(in)) | ||
|
||
for _, v := range in { | ||
value := v.(map[string]interface{}) | ||
|
||
prop := apiextensionsv1.JSONSchemaProps{} | ||
|
||
if val, ok := value[FieldType]; ok && val != "" { | ||
prop.Type = val.(string) | ||
} | ||
|
||
if val, ok := value[FieldPreserveUnknownFields]; ok && val == true { | ||
prop.XPreserveUnknownFields = pointer.Bool(true) | ||
} | ||
|
||
if val, ok := value[FieldName]; ok && val != "" { | ||
vars[val.(string)] = prop | ||
} | ||
} | ||
|
||
return vars | ||
} | ||
|
38 changes: 38 additions & 0 deletions
38
internal/resources/apiextensions/v1beta1/crd/property/fields.go
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,38 @@ | ||
package property | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
const ( | ||
// FieldName is a field identifier. | ||
FieldName = "name" | ||
// FieldType is a field identifier. | ||
FieldType = "type" | ||
// FieldPreserveUnknownFields is a field identifier. | ||
FieldPreserveUnknownFields = "preserve_unknown_fields" | ||
) | ||
|
||
// Fields returns the fields for this package. | ||
func Fields() *schema.Schema { | ||
return &schema.Schema{ | ||
Type: schema.TypeSet, | ||
Optional: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
FieldName: { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
FieldType: { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
FieldPreserveUnknownFields: { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
}, | ||
}, | ||
}, | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
internal/resources/apiextensions/v1beta1/crd/property/flatten.go
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,29 @@ | ||
package property | ||
|
||
import ( | ||
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
) | ||
|
||
// Flatten structured object into unstructured. | ||
func Flatten(in map[string]apiextensionsv1.JSONSchemaProps) []interface{} { | ||
flattened := make([]interface{}, 0) | ||
|
||
for name, value := range in { | ||
row := map[string]interface{}{} | ||
|
||
row[FieldName] = name | ||
|
||
if value.Type != "" { | ||
row[FieldType] = value.Type | ||
} | ||
|
||
if value.XPreserveUnknownFields != nil { | ||
row[FieldPreserveUnknownFields] = *value.XPreserveUnknownFields | ||
} | ||
|
||
flattened = append(flattened, row) | ||
} | ||
|
||
return flattened | ||
} | ||
|
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
12 changes: 12 additions & 0 deletions
12
internal/resources/apiextensions/v1beta1/crd/required/expand.go
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,12 @@ | ||
package required | ||
|
||
// Expand will return a structured object. | ||
func Expand(s []interface{}) []string { | ||
result := make([]string, len(s)) | ||
|
||
for k, v := range s { | ||
result[k] = v.(string) | ||
} | ||
|
||
return result | ||
} |
16 changes: 16 additions & 0 deletions
16
internal/resources/apiextensions/v1beta1/crd/required/fields.go
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,16 @@ | ||
package required | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
// Fields returns the fields for this package. | ||
func Fields() *schema.Schema { | ||
return &schema.Schema{ | ||
Type: schema.TypeList, | ||
Optional: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
} | ||
} |
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