...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/tf/resource/helpers.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/pkg/tf/resource

     1  // Copyright 2022 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package resource
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/text"
    22  	tfschema "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
    23  	"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
    24  )
    25  
    26  func GetTFSchemaForField(tfResource *tfschema.Resource, field string) (*tfschema.Schema, error) {
    27  	return getTFSchemaForNestedField(tfResource, strings.Split(field, ".")...)
    28  }
    29  
    30  func getTFSchemaForNestedField(tfResource *tfschema.Resource, fields ...string) (*tfschema.Schema, error) {
    31  	if len(fields) == 0 {
    32  		return nil, fmt.Errorf("no field specified")
    33  	}
    34  	schema, ok := tfResource.Schema[fields[0]]
    35  	if !ok || schema == nil {
    36  		return nil, fmt.Errorf("no schema found for field '%v'", fields[0])
    37  	}
    38  	if len(fields) == 1 {
    39  		return schema, nil
    40  	}
    41  	resource, ok := schema.Elem.(*tfschema.Resource)
    42  	if !ok {
    43  		return nil, fmt.Errorf("expected field '%v' to be a resource, but it was not", fields[0])
    44  	}
    45  	return getTFSchemaForNestedField(resource, fields[1:]...)
    46  }
    47  
    48  func TFResourceHasField(tfResource *tfschema.Resource, field string) bool {
    49  	return tfResourceHasNestedField(tfResource, strings.Split(field, ".")...)
    50  }
    51  
    52  func tfResourceHasNestedField(tfResource *tfschema.Resource, fields ...string) bool {
    53  	if len(fields) == 0 {
    54  		return false
    55  	}
    56  	schema, ok := tfResource.Schema[fields[0]]
    57  	if !ok || schema == nil {
    58  		return false
    59  	}
    60  	if len(fields) == 1 {
    61  		return true
    62  	}
    63  	resource, ok := schema.Elem.(*tfschema.Resource)
    64  	if !ok {
    65  		return false
    66  	}
    67  	return tfResourceHasNestedField(resource, fields[1:]...)
    68  }
    69  
    70  func IsFieldNestedInList(tfResource *tfschema.Resource, field string) bool {
    71  	return isNestedFieldNestedInList(tfResource, strings.Split(field, ".")...)
    72  }
    73  
    74  func isNestedFieldNestedInList(tfResource *tfschema.Resource, fields ...string) bool {
    75  	if len(fields) <= 1 {
    76  		return false
    77  	}
    78  	schema, ok := tfResource.Schema[fields[0]]
    79  	if !ok || schema == nil {
    80  		return false
    81  	}
    82  	resource, ok := schema.Elem.(*tfschema.Resource)
    83  	if !ok {
    84  		return false
    85  	}
    86  	if schema.MaxItems != 1 {
    87  		// Field can take more than 1 object => field is a list/set of objects
    88  		return true
    89  	}
    90  	return isNestedFieldNestedInList(resource, fields[1:]...)
    91  }
    92  
    93  func TFResourceHasSensitiveFields(tfResource *tfschema.Resource) bool {
    94  	for _, schema := range tfResource.Schema {
    95  		if IsSensitiveConfigurableField(schema) {
    96  			return true
    97  		}
    98  		resource, ok := schema.Elem.(*tfschema.Resource)
    99  		if ok && TFResourceHasSensitiveFields(resource) {
   100  			return true
   101  		}
   102  	}
   103  	return false
   104  }
   105  
   106  func IsSensitiveConfigurableField(tfSchema *tfschema.Schema) bool {
   107  	return tfSchema.Sensitive && IsConfigurableField(tfSchema)
   108  }
   109  
   110  func IsConfigurableField(tfSchema *tfschema.Schema) bool {
   111  	return tfSchema.Required || tfSchema.Optional
   112  }
   113  
   114  func IsObjectField(tfSchema *tfschema.Schema) bool {
   115  	_, ok := tfSchema.Elem.(*tfschema.Resource)
   116  	return tfSchema.Type == tfschema.TypeList && tfSchema.MaxItems == 1 && ok
   117  }
   118  
   119  func ImmutableFieldsFromDiff(diff *terraform.InstanceDiff) []string {
   120  	fields := make([]string, 0)
   121  	for field, rd := range diff.Attributes {
   122  		if rd != nil && rd.RequiresNew {
   123  			// TODO(kcc-eng): more deeply KRM-ify the fields coming back (use reference keys, remove ".0." from
   124  			//  MaxItems==1 lists)
   125  			fieldDiff := fmt.Sprintf("Field Name: %s, Got: %s, Wanted: %s", text.SnakeCaseToLowerCamelCase(field), rd.New, rd.Old)
   126  			fields = append(fields, fieldDiff)
   127  		}
   128  	}
   129  	return fields
   130  }
   131  

View as plain text