Skip to content

Commit 8de017e

Browse files
add image registry policy resource with schema and flatten tests
Signed-off-by: Vasundhara Shukla <vasundharas@vmware.com>
1 parent a27da7a commit 8de017e

34 files changed

+798
-127
lines changed

go.mod

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/hashicorp/terraform-plugin-sdk/v2 v2.12.0
1010
github.com/pkg/errors v0.9.1
1111
github.com/stretchr/testify v1.7.0
12+
golang.org/x/exp v0.0.0-20221126150942-6ab00d035af9
1213
k8s.io/apiextensions-apiserver v0.18.2
1314
k8s.io/apimachinery v0.22.0
1415
k8s.io/client-go v0.22.0
@@ -37,7 +38,7 @@ require (
3738
github.com/go-stack/stack v1.8.0 // indirect
3839
github.com/gogo/protobuf v1.3.2 // indirect
3940
github.com/golang/protobuf v1.5.2 // indirect
40-
github.com/google/go-cmp v0.5.7 // indirect
41+
github.com/google/go-cmp v0.5.8 // indirect
4142
github.com/google/gofuzz v1.1.0 // indirect
4243
github.com/googleapis/gnostic v0.5.5 // indirect
4344
github.com/hashicorp/errwrap v1.0.0 // indirect
@@ -81,7 +82,7 @@ require (
8182
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e // indirect
8283
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d // indirect
8384
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d // indirect
84-
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e // indirect
85+
golang.org/x/sys v0.1.0 // indirect
8586
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d // indirect
8687
golang.org/x/text v0.3.6 // indirect
8788
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect

go.sum

+6-3
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,9 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
306306
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
307307
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
308308
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
309-
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
310309
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
310+
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
311+
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
311312
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
312313
github.com/google/gofuzz v1.1.0 h1:Hsa8mG0dQ46ij8Sl2AYJDUv1oA9/d6Vk+3LG99Oe02g=
313314
github.com/google/gofuzz v1.1.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
@@ -651,6 +652,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
651652
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
652653
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
653654
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
655+
golang.org/x/exp v0.0.0-20221126150942-6ab00d035af9 h1:yZNXmy+j/JpX19vZkVktWqAo7Gny4PBWYYK3zskGpx4=
656+
golang.org/x/exp v0.0.0-20221126150942-6ab00d035af9/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
654657
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
655658
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
656659
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
@@ -790,8 +793,8 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w
790793
golang.org/x/sys v0.0.0-20210502180810-71e4cd670f79/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
791794
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
792795
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
793-
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e h1:WUoyKPm6nCo1BnNUvPGnFG3T5DUVem42yDJZZ4CNxMA=
794-
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
796+
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
797+
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
795798
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
796799
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
797800
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d h1:SZxvLBoTP5yHO3Frd4z4vrF+DBX9vMVanchswa69toE=

internal/helper/helper.go

+4
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,7 @@ func SetPrimitiveValue(input, model interface{}, key string) {
118118
}
119119
}
120120
}
121+
122+
func BoolPointer(b bool) *bool {
123+
return &b
124+
}

internal/provider/provider.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func Provider() *schema.Provider {
3838
iampolicy.ResourceName: iampolicy.ResourceIAMPolicy(),
3939
custompolicy.ResourceName: custompolicyresource.ResourceCustomPolicy(),
4040
securitypolicy.ResourceName: securitypolicyresource.ResourceSecurityPolicy(),
41-
imagepolicy.ResourceName: imagepolicyresource.ResourceImageRegistryPolicy(),
41+
imagepolicy.ResourceName: imagepolicyresource.ResourceImagePolicy(),
4242
credential.ResourceName: credential.ResourceCredential(),
4343
integration.ResourceName: integration.ResourceIntegration(),
4444
},

internal/resources/policy/kind/custom/recipe/tmc_block_rolebinding_subjects_flatten_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestFlattenTMCBlockRolebindingSubjects(t *testing.T) {
2929
expected: nil,
3030
},
3131
{
32-
description: "normal scenario with with complete custom policy tmc_block_rolebinding_subjects recipe",
32+
description: "normal scenario with complete custom policy tmc_block_rolebinding_subjects recipe",
3333
input: &policyrecipecustommodel.VmwareTanzuManageV1alpha1CommonPolicySpecCustomV1TMCBlockRoleBindingSubjects{
3434
Audit: true,
3535
Parameters: &policyrecipecustommodel.VmwareTanzuManageV1alpha1CommonPolicySpecCustomV1TMCBlockRoleBindingSubjectsParameters{
@@ -94,7 +94,7 @@ func TestFlattenBlockRoleBindingParameters(t *testing.T) {
9494
expected: nil,
9595
},
9696
{
97-
description: "normal scenario with with complete custom policy tmc_block_rolebinding_subjects parameters",
97+
description: "normal scenario with complete custom policy tmc_block_rolebinding_subjects parameters",
9898
input: &policyrecipecustommodel.VmwareTanzuManageV1alpha1CommonPolicySpecCustomV1TMCBlockRoleBindingSubjectsParameters{
9999
DisallowedSubjects: []*policyrecipecustommodel.VmwareTanzuManageV1alpha1CommonPolicySpecCustomV1TMCBlockRoleBindingSubjectsParametersDisallowedSubjects{
100100
{
@@ -139,7 +139,7 @@ func TestFlattenDisallowedSubjects(t *testing.T) {
139139
expected: nil,
140140
},
141141
{
142-
description: "normal scenario with with complete custom policy tmc_block_rolebinding_subjects parameters disallowed subjects",
142+
description: "normal scenario with complete custom policy tmc_block_rolebinding_subjects parameters disallowed subjects",
143143
input: &policyrecipecustommodel.VmwareTanzuManageV1alpha1CommonPolicySpecCustomV1TMCBlockRoleBindingSubjectsParametersDisallowedSubjects{
144144
Kind: "nodes",
145145
Name: "test",

internal/resources/policy/kind/custom/recipe/tmc_common_recipe_flatten_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
policyrecipecustomcommonmodel "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/policy/recipe/custom/common"
1515
)
1616

17-
func TestFlattenTMCBlockNodeportService(t *testing.T) {
17+
func TestFlattenTMCCommonRecipe(t *testing.T) {
1818
t.Parallel()
1919

2020
cases := []struct {
@@ -23,12 +23,12 @@ func TestFlattenTMCBlockNodeportService(t *testing.T) {
2323
expected []interface{}
2424
}{
2525
{
26-
description: "check for nil custom policy tmc_block_nodeport_service recipe",
26+
description: "check for nil custom policy tmc-block-nodeport-service/ tmc-block-resources/ tmc-https-ingress recipes",
2727
input: nil,
2828
expected: nil,
2929
},
3030
{
31-
description: "normal scenario with with complete custom policy tmc_block_nodeport_service recipe",
31+
description: "normal scenario with complete custom policy tmc-block-nodeport-service/ tmc-block-resources/ tmc-https-ingress recipes",
3232
input: &policyrecipecustommodel.VmwareTanzuManageV1alpha1CommonPolicySpecCustomV1TMCCommonRecipe{
3333
Audit: true,
3434
TargetKubernetesResources: []*policyrecipecustomcommonmodel.VmwareTanzuManageV1alpha1CommonPolicySpecCustomV1TargetKubernetesResources{

internal/resources/policy/kind/custom/recipe/tmc_external_ips_flatten_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestFlattenTMCExternalIPS(t *testing.T) {
2828
expected: nil,
2929
},
3030
{
31-
description: "normal scenario with with complete custom policy tmc_external_ips recipe",
31+
description: "normal scenario with complete custom policy tmc_external_ips recipe",
3232
input: &policyrecipecustommodel.VmwareTanzuManageV1alpha1CommonPolicySpecCustomV1TMCExternalIPS{
3333
Audit: true,
3434
Parameters: &policyrecipecustommodel.VmwareTanzuManageV1alpha1CommonPolicySpecCustomV1TMCExternalIPSParameters{
@@ -83,7 +83,7 @@ func TestFlattenExternalIPSParameters(t *testing.T) {
8383
expected: nil,
8484
},
8585
{
86-
description: "normal scenario with with complete custom policy tmc_external_ips parameters",
86+
description: "normal scenario with complete custom policy tmc_external_ips parameters",
8787
input: &policyrecipecustommodel.VmwareTanzuManageV1alpha1CommonPolicySpecCustomV1TMCExternalIPSParameters{
8888
AllowedIPs: []string{"127.0.0.1"},
8989
},

internal/resources/policy/kind/custom/recipe/tmc_require_labels_flatten_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestFlattenTMCRequireLabels(t *testing.T) {
2929
expected: nil,
3030
},
3131
{
32-
description: "normal scenario with with complete custom policy tmc_require_labels recipe",
32+
description: "normal scenario with complete custom policy tmc_require_labels recipe",
3333
input: &policyrecipecustommodel.VmwareTanzuManageV1alpha1CommonPolicySpecCustomV1TMCRequireLabels{
3434
Audit: true,
3535
Parameters: &policyrecipecustommodel.VmwareTanzuManageV1alpha1CommonPolicySpecCustomV1TMCRequireLabelsParameters{
@@ -94,7 +94,7 @@ func TestFlattenRequiredLabelsParameters(t *testing.T) {
9494
expected: nil,
9595
},
9696
{
97-
description: "normal scenario with with complete custom policy tmc_require_labels parameters",
97+
description: "normal scenario with complete custom policy tmc_require_labels parameters",
9898
input: &policyrecipecustommodel.VmwareTanzuManageV1alpha1CommonPolicySpecCustomV1TMCRequireLabelsParameters{
9999
Labels: []*policyrecipecustommodel.VmwareTanzuManageV1alpha1CommonPolicySpecCustomV1TMCRequireLabelsParametersLabels{
100100
{
@@ -139,7 +139,7 @@ func TestFlattenRequiredLabelsParametersLabels(t *testing.T) {
139139
expected: nil,
140140
},
141141
{
142-
description: "normal scenario with with complete custom policy tmc_require_labels parameters labels",
142+
description: "normal scenario with complete custom policy tmc_require_labels parameters labels",
143143
input: &policyrecipecustommodel.VmwareTanzuManageV1alpha1CommonPolicySpecCustomV1TMCRequireLabelsParametersLabels{
144144
Key: "key-1",
145145
Value: "value-1",

internal/resources/policy/kind/custom/resource/resource_custom_policy.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func ResourceCustomPolicy() *schema.Resource {
2424
DeleteContext: schema.DeleteContextFunc(policyoperations.ResourceOperation(policyoperations.WithResourceName(policykindcustom.ResourceName), policyoperations.WithOperationType(policyoperations.Delete))),
2525
Schema: customPolicySchema,
2626
CustomizeDiff: customdiff.All(
27-
scope.ValidateScope,
27+
schema.CustomizeDiffFunc(scope.ValidateScope(policyoperations.ScopeMap[policykindcustom.ResourceName])),
2828
policykindcustom.ValidateInput,
2929
policy.ValidateSpecLabelSelectorRequirement,
3030
),

internal/resources/policy/kind/custom/resource/resource_custom_policy_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
policyorganizationmodel "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/policy/organization"
2727
"github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/policy"
2828
policykindCustom "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/policy/kind/custom"
29+
policyoperations "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/policy/operations"
2930
"github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/policy/scope"
3031
testhelper "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/testing"
3132
)
@@ -273,7 +274,7 @@ func TestAcceptanceForCustomPolicyResource(t *testing.T) {
273274
}
274275

275276
func (testConfig *testAcceptanceConfig) getTestCustomPolicyResourceBasicConfigValue(scope scope.Scope, recipe policykindCustom.Recipe) string {
276-
helperBlock, scopeBlock := testConfig.ScopeHelperResources.GetTestPolicyResourceHelperAndScope(scope)
277+
helperBlock, scopeBlock := testConfig.ScopeHelperResources.GetTestPolicyResourceHelperAndScope(scope, policyoperations.ScopeMap[testConfig.CustomPolicyResource])
277278
inputBlock := testConfig.getTestCustomPolicyResourceInput(recipe)
278279

279280
return fmt.Sprintf(`
@@ -455,7 +456,7 @@ func (testConfig *testAcceptanceConfig) checkCustomPolicyResourceAttributes(scop
455456
case scope.OrganizationScope:
456457
check = append(check, resource.TestCheckResourceAttr(testConfig.CustomPolicyResourceName, "scope.0.organization.0.organization", testConfig.ScopeHelperResources.OrgID))
457458
case scope.UnknownScope:
458-
log.Printf("[ERROR]: No valid scope type block found: minimum one valid scope type block is required among: %v. Please check the schema.", strings.Join(scope.ScopesAllowed[:], `, `))
459+
log.Printf("[ERROR]: No valid scope type block found: minimum one valid scope type block is required among: %v. Please check the schema.", strings.Join(policyoperations.ScopeMap[testConfig.CustomPolicyResource], `, `))
459460
}
460461

461462
check = append(check, policy.MetaResourceAttributeCheck(testConfig.CustomPolicyResourceName)...)
@@ -536,7 +537,7 @@ func (testConfig *testAcceptanceConfig) verifyCustomPolicyResourceCreation(scope
536537
return errors.Wrapf(err, "organization scoped custom policy resource is empty, resource: %s", testConfig.CustomPolicyResourceName)
537538
}
538539
case scope.UnknownScope:
539-
return errors.Errorf("[ERROR]: No valid scope type block found: minimum one valid scope type block is required among: %v. Please check the schema.", strings.Join(scope.ScopesAllowed[:], `, `))
540+
return errors.Errorf("[ERROR]: No valid scope type block found: minimum one valid scope type block is required among: %v. Please check the schema.", strings.Join(policyoperations.ScopeMap[testConfig.CustomPolicyResource], `, `))
540541
}
541542

542543
return nil

internal/resources/policy/kind/image/constants.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
)
1212

1313
const (
14-
ResourceName = "tanzu-mission-control_image_registry_policy"
15-
typeDefaultValue = "image-registry-policy"
14+
ResourceName = "tanzu-mission-control_image_policy"
15+
typeDefaultValue = "image-policy"
1616
)
1717

1818
// Allowed input recipes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
"testing"
10+
11+
"github.com/stretchr/testify/require"
12+
13+
"github.com/vmware/terraform-provider-tanzu-mission-control/internal/helper"
14+
policyrecipeimagemodel "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/policy/recipe/image"
15+
reciperesource "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/policy/kind/image/recipe"
16+
)
17+
18+
func TestFlattenInput(t *testing.T) {
19+
t.Parallel()
20+
21+
cases := []struct {
22+
description string
23+
input *inputRecipe
24+
expected []interface{}
25+
}{
26+
{
27+
description: "check for nil input",
28+
input: nil,
29+
expected: nil,
30+
},
31+
{
32+
description: "normal scenario with complete input",
33+
input: &inputRecipe{
34+
recipe: BlockLatestTagRecipe,
35+
inputBlockLatestTag: &policyrecipeimagemodel.VmwareTanzuManageV1alpha1CommonPolicySpecImageV1CommonRecipe{
36+
Audit: helper.BoolPointer(true),
37+
},
38+
},
39+
expected: []interface{}{
40+
map[string]interface{}{
41+
reciperesource.BlockLatestTagKey: []interface{}{
42+
map[string]interface{}{
43+
reciperesource.AuditKey: true,
44+
},
45+
},
46+
},
47+
},
48+
},
49+
}
50+
51+
for _, each := range cases {
52+
test := each
53+
t.Run(test.description, func(t *testing.T) {
54+
actual := flattenInput(test.input)
55+
require.Equal(t, test.expected, actual)
56+
})
57+
}
58+
}

internal/resources/policy/kind/image/input_schema.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
var (
2121
inputSchema = &schema.Schema{
2222
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.",
23+
Description: "Input for the image policy, having one of the valid recipes: allowed-name-tag, custom, block-latest-tag or require-digest.",
2424
Required: true,
2525
MaxItems: 1,
2626
MinItems: 1,
@@ -39,7 +39,7 @@ var (
3939

4040
type (
4141
Recipe string
42-
// InputRecipe is a struct for all types of image registry policy inputs.
42+
// InputRecipe is a struct for all types of image policy inputs.
4343
inputRecipe struct {
4444
recipe Recipe
4545
inputAllowedNameTag *policyrecipeimagemodel.VmwareTanzuManageV1alpha1CommonPolicySpecImageV1AllowedNameTag

0 commit comments

Comments
 (0)