kubernetes: Make generatable name optional in metadataSchema

This commit is contained in:
Radek Simko 2017-04-02 07:08:16 +01:00
parent 735dfc416d
commit 793ce368fc
No known key found for this signature in database
GPG Key ID: 6823F3DCCE01BB19
3 changed files with 23 additions and 17 deletions

View File

@ -25,7 +25,7 @@ func resourceKubernetesNamespace() *schema.Resource {
},
Schema: map[string]*schema.Schema{
"metadata": metadataSchema("namespace"),
"metadata": metadataSchema("namespace", true),
},
}
}

View File

@ -26,13 +26,12 @@ func metadataFields(objectName string) map[string]*schema.Schema {
ValidateFunc: validateLabels,
},
"name": {
Type: schema.TypeString,
Description: fmt.Sprintf("Name of the %s, must be unique. Cannot be updated. More info: http://kubernetes.io/docs/user-guide/identifiers#names", objectName),
Optional: true,
ForceNew: true,
Computed: true,
ValidateFunc: validateName,
ConflictsWith: []string{"metadata.generate_name"},
Type: schema.TypeString,
Description: fmt.Sprintf("Name of the %s, must be unique. Cannot be updated. More info: http://kubernetes.io/docs/user-guide/identifiers#names", objectName),
Optional: true,
ForceNew: true,
Computed: true,
ValidateFunc: validateName,
},
"resource_version": {
Type: schema.TypeString,
@ -52,15 +51,19 @@ func metadataFields(objectName string) map[string]*schema.Schema {
}
}
func metadataSchema(objectName string) *schema.Schema {
func metadataSchema(objectName string, generatableName bool) *schema.Schema {
fields := metadataFields(objectName)
fields["generate_name"] = &schema.Schema{
Type: schema.TypeString,
Description: "Prefix, used by the server, to generate a unique name ONLY IF the `name` field has not been provided. This value will also be combined with a unique suffix. Read more: https://github.com/kubernetes/community/blob/master/contributors/devel/api-conventions.md#idempotency",
Optional: true,
ForceNew: true,
ValidateFunc: validateGenerateName,
ConflictsWith: []string{"metadata.name"},
if generatableName {
fields["generate_name"] = &schema.Schema{
Type: schema.TypeString,
Description: "Prefix, used by the server, to generate a unique name ONLY IF the `name` field has not been provided. This value will also be combined with a unique suffix. Read more: https://github.com/kubernetes/community/blob/master/contributors/devel/api-conventions.md#idempotency",
Optional: true,
ForceNew: true,
ValidateFunc: validateGenerateName,
ConflictsWith: []string{"metadata.name"},
}
fields["name"].ConflictsWith = []string{"metadata.generate_name"}
}
return &schema.Schema{
@ -92,6 +95,7 @@ func namespacedMetadataSchema(objectName string, generatableName bool) *schema.S
ValidateFunc: validateGenerateName,
ConflictsWith: []string{"metadata.name"},
}
fields["name"].ConflictsWith = []string{"metadata.generate_name"}
}
return &schema.Schema{

View File

@ -68,7 +68,9 @@ func expandStringMap(m map[string]interface{}) map[string]string {
func flattenMetadata(meta api.ObjectMeta) []map[string]interface{} {
m := make(map[string]interface{})
m["annotations"] = filterAnnotations(meta.Annotations)
m["generate_name"] = meta.GenerateName
if meta.GenerateName != "" {
m["generate_name"] = meta.GenerateName
}
m["labels"] = meta.Labels
m["name"] = meta.Name
m["resource_version"] = meta.ResourceVersion