|
| 1 | +/* |
| 2 | +Copyright © 2022 VMware, Inc. All Rights Reserved. |
| 3 | +SPDX-License-Identifier: MPL-2.0 |
| 4 | +*/ |
| 5 | + |
| 6 | +package policykindimage |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "fmt" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 14 | + |
| 15 | + policyrecipeimagemodel "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/policy/recipe/image" |
| 16 | + "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/policy" |
| 17 | + reciperesource "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/policy/kind/image/recipe" |
| 18 | +) |
| 19 | + |
| 20 | +var ( |
| 21 | + inputSchema = &schema.Schema{ |
| 22 | + Type: schema.TypeList, |
| 23 | + Description: "Input for the image registry policy, having one of the valid recipes: allowed-name-tag, custom, block-latest-tag or require-digest.", |
| 24 | + Required: true, |
| 25 | + MaxItems: 1, |
| 26 | + MinItems: 1, |
| 27 | + ForceNew: true, |
| 28 | + Elem: &schema.Resource{ |
| 29 | + Schema: map[string]*schema.Schema{ |
| 30 | + reciperesource.AllowedNameTagKey: reciperesource.AllowedNameTag, |
| 31 | + reciperesource.CustomKey: reciperesource.Custom, |
| 32 | + reciperesource.BlockLatestTagKey: reciperesource.BlockLatestTag, |
| 33 | + reciperesource.RequireDigestKey: reciperesource.RequireDigest, |
| 34 | + }, |
| 35 | + }, |
| 36 | + } |
| 37 | + RecipesAllowed = [...]string{reciperesource.AllowedNameTagKey, reciperesource.CustomKey, reciperesource.BlockLatestTagKey, reciperesource.RequireDigestKey} |
| 38 | +) |
| 39 | + |
| 40 | +type ( |
| 41 | + Recipe string |
| 42 | + // InputRecipe is a struct for all types of image registry policy inputs. |
| 43 | + inputRecipe struct { |
| 44 | + recipe Recipe |
| 45 | + inputAllowedNameTag *policyrecipeimagemodel.VmwareTanzuManageV1alpha1CommonPolicySpecImageV1AllowedNameTag |
| 46 | + inputCustom *policyrecipeimagemodel.VmwareTanzuManageV1alpha1CommonPolicySpecImageV1Custom |
| 47 | + inputBlockLatestTag *policyrecipeimagemodel.VmwareTanzuManageV1alpha1CommonPolicySpecImageV1CommonRecipe |
| 48 | + inputRequireDigest *policyrecipeimagemodel.VmwareTanzuManageV1alpha1CommonPolicySpecImageV1CommonRecipe |
| 49 | + } |
| 50 | +) |
| 51 | + |
| 52 | +func constructInput(data []interface{}) (inputRecipeData *inputRecipe) { |
| 53 | + if len(data) == 0 || data[0] == nil { |
| 54 | + return inputRecipeData |
| 55 | + } |
| 56 | + |
| 57 | + inputData, _ := data[0].(map[string]interface{}) |
| 58 | + |
| 59 | + if v, ok := inputData[reciperesource.AllowedNameTagKey]; ok { |
| 60 | + if v1, ok := v.([]interface{}); ok && len(v1) != 0 { |
| 61 | + inputRecipeData = &inputRecipe{ |
| 62 | + recipe: AllowedNameTagRecipe, |
| 63 | + inputAllowedNameTag: reciperesource.ConstructAllowedNameTag(v1), |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + if v, ok := inputData[reciperesource.CustomKey]; ok { |
| 69 | + if v1, ok := v.([]interface{}); ok && len(v1) != 0 { |
| 70 | + inputRecipeData = &inputRecipe{ |
| 71 | + recipe: CustomRecipe, |
| 72 | + inputCustom: reciperesource.ConstructCustom(v1), |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + if v, ok := inputData[reciperesource.BlockLatestTagKey]; ok { |
| 78 | + if v1, ok := v.([]interface{}); ok && len(v1) != 0 { |
| 79 | + inputRecipeData = &inputRecipe{ |
| 80 | + recipe: BlockLatestTagRecipe, |
| 81 | + inputBlockLatestTag: reciperesource.ConstructCommonRecipe(v1), |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + if v, ok := inputData[reciperesource.RequireDigestKey]; ok { |
| 87 | + if v1, ok := v.([]interface{}); ok && len(v1) != 0 { |
| 88 | + inputRecipeData = &inputRecipe{ |
| 89 | + recipe: RequireDigestRecipe, |
| 90 | + inputRequireDigest: reciperesource.ConstructCommonRecipe(v1), |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return inputRecipeData |
| 96 | +} |
| 97 | + |
| 98 | +func flattenInput(inputRecipeData *inputRecipe) (data []interface{}) { |
| 99 | + if inputRecipeData == nil { |
| 100 | + return data |
| 101 | + } |
| 102 | + |
| 103 | + flattenInputData := make(map[string]interface{}) |
| 104 | + |
| 105 | + switch inputRecipeData.recipe { |
| 106 | + case AllowedNameTagRecipe: |
| 107 | + flattenInputData[reciperesource.AllowedNameTagKey] = reciperesource.FlattenAllowedNameTag(inputRecipeData.inputAllowedNameTag) |
| 108 | + case CustomRecipe: |
| 109 | + flattenInputData[reciperesource.CustomKey] = reciperesource.FlattenCustom(inputRecipeData.inputCustom) |
| 110 | + case BlockLatestTagRecipe: |
| 111 | + flattenInputData[reciperesource.BlockLatestTagKey] = reciperesource.FlattenCommonRecipe(inputRecipeData.inputBlockLatestTag) |
| 112 | + case RequireDigestRecipe: |
| 113 | + flattenInputData[reciperesource.RequireDigestKey] = reciperesource.FlattenCommonRecipe(inputRecipeData.inputRequireDigest) |
| 114 | + case UnknownRecipe: |
| 115 | + fmt.Printf("[ERROR]: No valid input recipe block found: minimum one valid input recipe block is required among: %v. Please check the schema.", strings.Join(RecipesAllowed[:], `, `)) |
| 116 | + } |
| 117 | + |
| 118 | + return []interface{}{flattenInputData} |
| 119 | +} |
| 120 | + |
| 121 | +func ValidateInput(ctx context.Context, diff *schema.ResourceDiff, i interface{}) error { |
| 122 | + value, ok := diff.GetOk(policy.SpecKey) |
| 123 | + if !ok { |
| 124 | + return fmt.Errorf("spec: %v is not valid: minimum one valid spec block is required", value) |
| 125 | + } |
| 126 | + |
| 127 | + data, _ := value.([]interface{}) |
| 128 | + |
| 129 | + if len(data) == 0 || data[0] == nil { |
| 130 | + return fmt.Errorf("spec data: %v is not valid: minimum one valid spec block is required among: %v", data, strings.Join(RecipesAllowed[:], `, `)) |
| 131 | + } |
| 132 | + |
| 133 | + specData := data[0].(map[string]interface{}) |
| 134 | + |
| 135 | + v, ok := specData[policy.InputKey] |
| 136 | + if !ok { |
| 137 | + return fmt.Errorf("input: %v is not valid: minimum one valid input block is required", v) |
| 138 | + } |
| 139 | + |
| 140 | + v1, ok := v.([]interface{}) |
| 141 | + if !ok { |
| 142 | + return fmt.Errorf("type of input block data: %v is not valid", v1) |
| 143 | + } |
| 144 | + |
| 145 | + if len(v1) == 0 || v1[0] == nil { |
| 146 | + return fmt.Errorf("input data: %v is not valid: minimum one valid input block is required", v1) |
| 147 | + } |
| 148 | + |
| 149 | + inputData, _ := v1[0].(map[string]interface{}) |
| 150 | + recipesFound := make([]string, 0) |
| 151 | + |
| 152 | + if v, ok := inputData[reciperesource.AllowedNameTagKey]; ok { |
| 153 | + if v1, ok := v.([]interface{}); ok && len(v1) != 0 { |
| 154 | + recipesFound = append(recipesFound, reciperesource.AllowedNameTagKey) |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + if v, ok := inputData[reciperesource.CustomKey]; ok { |
| 159 | + if v1, ok := v.([]interface{}); ok && len(v1) != 0 { |
| 160 | + recipesFound = append(recipesFound, reciperesource.CustomKey) |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + if v, ok := inputData[reciperesource.BlockLatestTagKey]; ok { |
| 165 | + if v1, ok := v.([]interface{}); ok && len(v1) != 0 { |
| 166 | + recipesFound = append(recipesFound, reciperesource.BlockLatestTagKey) |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + if v, ok := inputData[reciperesource.RequireDigestKey]; ok { |
| 171 | + if v1, ok := v.([]interface{}); ok && len(v1) != 0 { |
| 172 | + recipesFound = append(recipesFound, reciperesource.RequireDigestKey) |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + if len(recipesFound) == 0 { |
| 177 | + return fmt.Errorf("no valid input recipe block found: minimum one valid input recipe block is required among: %v", strings.Join(RecipesAllowed[:], `, `)) |
| 178 | + } else if len(recipesFound) > 1 { |
| 179 | + return fmt.Errorf("found input recipes: %v are not valid: maximum one valid input recipe block is allowed", strings.Join(recipesFound, `, `)) |
| 180 | + } |
| 181 | + |
| 182 | + return nil |
| 183 | +} |
0 commit comments