Skip to content

Commit 8efd905

Browse files
add workspace scope for image policy
1 parent 5148a4a commit 8efd905

18 files changed

+322
-184
lines changed

internal/resources/policy/constants.go

-12
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,9 @@ const (
1111
KeyKey = "key"
1212
OperatorKey = "operator"
1313
ValuesKey = "values"
14-
ScopeKey = "scope"
15-
clusterKey = "cluster"
16-
clusterGroupKey = "cluster_group"
17-
organizationKey = "organization"
1814
SpecKey = "spec"
1915
NameKey = "name"
2016
InputKey = "input"
2117
RecipeVersionDefaultValue = "v1"
2218
UnknownRecipe = ""
2319
)
24-
25-
// Allowed scopes.
26-
const (
27-
UnknownScope Scope = iota
28-
ClusterScope
29-
ClusterGroupScope
30-
OrganizationScope
31-
)

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/policy"
1414
policykindcustom "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/policy/kind/custom"
1515
policyoperations "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/policy/operations"
16+
"github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/policy/scope"
1617
)
1718

1819
func ResourceCustomPolicy() *schema.Resource {
@@ -23,7 +24,7 @@ func ResourceCustomPolicy() *schema.Resource {
2324
DeleteContext: schema.DeleteContextFunc(policyoperations.ResourceOperation(policyoperations.WithResourceName(policykindcustom.ResourceName), policyoperations.WithOperationType(policyoperations.Delete))),
2425
Schema: customPolicySchema,
2526
CustomizeDiff: customdiff.All(
26-
policy.ValidateScope,
27+
scope.ValidateScope,
2728
policykindcustom.ValidateInput,
2829
policy.ValidateSpecLabelSelectorRequirement,
2930
),
@@ -37,7 +38,7 @@ var customPolicySchema = map[string]*schema.Schema{
3738
Required: true,
3839
ForceNew: true,
3940
},
40-
policy.ScopeKey: policy.ScopeSchema,
41-
common.MetaKey: common.Meta,
42-
policy.SpecKey: policykindcustom.SpecSchema,
41+
scope.ScopeKey: scope.ScopeSchema,
42+
common.MetaKey: common.Meta,
43+
policy.SpecKey: policykindcustom.SpecSchema,
4344
}

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

+55-55
Large diffs are not rendered by default.

internal/resources/policy/kind/image/recipe/allowed_name_tag_schema.go

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ var AllowedNameTag = &schema.Schema{
3737
Type: schema.TypeString,
3838
Description: "Allowed image names, wildcards are supported(for example: fooservice/*). Empty field is equivalent to *.",
3939
Optional: true,
40+
Default: "",
4041
},
4142
TagKey: tag,
4243
},

internal/resources/policy/kind/image/recipe/custom_schema.go

+3
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,19 @@ var Custom = &schema.Schema{
3737
Type: schema.TypeString,
3838
Description: "Allowed image hostnames, wildcards are supported(for example: *.mycompany.com). Empty field is equivalent to *.",
3939
Optional: true,
40+
Default: "",
4041
},
4142
ImageNameKey: {
4243
Type: schema.TypeString,
4344
Description: "Allowed image names, wildcards are supported(for example: fooservice/*). Empty field is equivalent to *.",
4445
Optional: true,
46+
Default: "",
4547
},
4648
PortKey: {
4749
Type: schema.TypeString,
4850
Description: "Allowed port(if presented) of the image hostname, must associate with valid hostname. Wildcards are supported.",
4951
Optional: true,
52+
Default: "",
5053
},
5154
RequireKey: {
5255
Type: schema.TypeBool,

internal/resources/policy/kind/image/recipe/tag_schema.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ var tag = &schema.Schema{
2121
NegateKey: {
2222
Type: schema.TypeBool,
2323
Description: "The negate flag used to exclude certain tag patterns.",
24-
Required: true,
24+
Optional: true,
25+
Default: false,
2526
},
2627
ValueKey: {
2728
Type: schema.TypeString,
2829
Description: "The value (support wildcard) is used to validate against the tag of the image.",
2930
Optional: true,
31+
Default: "",
3032
},
3133
},
3234
},

internal/resources/policy/kind/image/resource/resource_image_registry_policy.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ import (
1212
"github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/common"
1313
"github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/policy"
1414
policykindimage "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/policy/kind/image"
15+
"github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/policy/scope"
1516
)
1617

1718
func ResourceImageRegistryPolicy() *schema.Resource {
1819
return &schema.Resource{
1920
Schema: imageRegistryPolicySchema,
2021
CustomizeDiff: customdiff.All(
21-
policy.ValidateScope,
22+
scope.ValidateScope,
2223
policykindimage.ValidateInput,
2324
policy.ValidateSpecLabelSelectorRequirement,
2425
),
@@ -32,7 +33,7 @@ var imageRegistryPolicySchema = map[string]*schema.Schema{
3233
Required: true,
3334
ForceNew: true,
3435
},
35-
policy.ScopeKey: policy.ScopeSchema,
36-
common.MetaKey: common.Meta,
37-
policy.SpecKey: policykindimage.SpecSchema,
36+
scope.ScopeKey: scope.ScopeSchema,
37+
common.MetaKey: common.Meta,
38+
policy.SpecKey: policykindimage.SpecSchema,
3839
}

internal/resources/policy/kind/security/resource/resource_security_policy.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/policy"
1414
policykindsecurity "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/policy/kind/security"
1515
policyoperations "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/policy/operations"
16+
"github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/policy/scope"
1617
)
1718

1819
func ResourceSecurityPolicy() *schema.Resource {
@@ -23,7 +24,7 @@ func ResourceSecurityPolicy() *schema.Resource {
2324
DeleteContext: schema.DeleteContextFunc(policyoperations.ResourceOperation(policyoperations.WithResourceName(policykindsecurity.ResourceName), policyoperations.WithOperationType(policyoperations.Delete))),
2425
Schema: securityPolicySchema,
2526
CustomizeDiff: customdiff.All(
26-
policy.ValidateScope,
27+
scope.ValidateScope,
2728
policykindsecurity.ValidateInput,
2829
policy.ValidateSpecLabelSelectorRequirement,
2930
),
@@ -37,7 +38,7 @@ var securityPolicySchema = map[string]*schema.Schema{
3738
Required: true,
3839
ForceNew: true,
3940
},
40-
policy.ScopeKey: policy.ScopeSchema,
41-
common.MetaKey: common.Meta,
42-
policy.SpecKey: policykindsecurity.SpecSchema,
41+
scope.ScopeKey: scope.ScopeSchema,
42+
common.MetaKey: common.Meta,
43+
policy.SpecKey: policykindsecurity.SpecSchema,
4344
}

internal/resources/policy/kind/security/resource/resource_security_policy_test.go

+37-37
Original file line numberDiff line numberDiff line change
@@ -26,7 +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
policykindsecurity "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/policy/kind/security"
29-
scoperesource "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/policy/scope"
29+
"github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/policy/scope"
3030
testhelper "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/testing"
3131
)
3232

@@ -73,21 +73,21 @@ func TestAcceptanceForSecurityPolicyResource(t *testing.T) {
7373
t.Skip("KUBECONFIG env var is not set for cluster scoped security policy acceptance test")
7474
}
7575
},
76-
Config: testConfig.getTestSecurityPolicyResourceBasicConfigValue(policy.ClusterScope, policykindsecurity.BaselineRecipe),
77-
Check: testConfig.checkSecurityPolicyResourceAttributes(policy.ClusterScope),
76+
Config: testConfig.getTestSecurityPolicyResourceBasicConfigValue(scope.ClusterScope, policykindsecurity.BaselineRecipe),
77+
Check: testConfig.checkSecurityPolicyResourceAttributes(scope.ClusterScope),
7878
},
7979
{
80-
Config: testConfig.getTestSecurityPolicyResourceBasicConfigValue(policy.ClusterGroupScope, policykindsecurity.BaselineRecipe),
81-
Check: testConfig.checkSecurityPolicyResourceAttributes(policy.ClusterGroupScope),
80+
Config: testConfig.getTestSecurityPolicyResourceBasicConfigValue(scope.ClusterGroupScope, policykindsecurity.BaselineRecipe),
81+
Check: testConfig.checkSecurityPolicyResourceAttributes(scope.ClusterGroupScope),
8282
},
8383
{
8484
PreConfig: func() {
8585
if testConfig.ScopeHelperResources.OrgID == "" {
8686
t.Skip("ORG_ID env var is not set for organization scoped security policy acceptance test")
8787
}
8888
},
89-
Config: testConfig.getTestSecurityPolicyResourceBasicConfigValue(policy.OrganizationScope, policykindsecurity.BaselineRecipe),
90-
Check: testConfig.checkSecurityPolicyResourceAttributes(policy.OrganizationScope),
89+
Config: testConfig.getTestSecurityPolicyResourceBasicConfigValue(scope.OrganizationScope, policykindsecurity.BaselineRecipe),
90+
Check: testConfig.checkSecurityPolicyResourceAttributes(scope.OrganizationScope),
9191
},
9292
},
9393
},
@@ -108,21 +108,21 @@ func TestAcceptanceForSecurityPolicyResource(t *testing.T) {
108108
t.Skip("KUBECONFIG env var is not set for cluster scoped security policy acceptance test")
109109
}
110110
},
111-
Config: testConfig.getTestSecurityPolicyResourceBasicConfigValue(policy.ClusterScope, policykindsecurity.CustomRecipe),
112-
Check: testConfig.checkSecurityPolicyResourceAttributes(policy.ClusterScope),
111+
Config: testConfig.getTestSecurityPolicyResourceBasicConfigValue(scope.ClusterScope, policykindsecurity.CustomRecipe),
112+
Check: testConfig.checkSecurityPolicyResourceAttributes(scope.ClusterScope),
113113
},
114114
{
115-
Config: testConfig.getTestSecurityPolicyResourceBasicConfigValue(policy.ClusterGroupScope, policykindsecurity.CustomRecipe),
116-
Check: testConfig.checkSecurityPolicyResourceAttributes(policy.ClusterGroupScope),
115+
Config: testConfig.getTestSecurityPolicyResourceBasicConfigValue(scope.ClusterGroupScope, policykindsecurity.CustomRecipe),
116+
Check: testConfig.checkSecurityPolicyResourceAttributes(scope.ClusterGroupScope),
117117
},
118118
{
119119
PreConfig: func() {
120120
if testConfig.ScopeHelperResources.OrgID == "" {
121121
t.Skip("ORG_ID env var is not set for organization scoped security policy acceptance test")
122122
}
123123
},
124-
Config: testConfig.getTestSecurityPolicyResourceBasicConfigValue(policy.OrganizationScope, policykindsecurity.CustomRecipe),
125-
Check: testConfig.checkSecurityPolicyResourceAttributes(policy.OrganizationScope),
124+
Config: testConfig.getTestSecurityPolicyResourceBasicConfigValue(scope.OrganizationScope, policykindsecurity.CustomRecipe),
125+
Check: testConfig.checkSecurityPolicyResourceAttributes(scope.OrganizationScope),
126126
},
127127
},
128128
},
@@ -143,21 +143,21 @@ func TestAcceptanceForSecurityPolicyResource(t *testing.T) {
143143
t.Skip("KUBECONFIG env var is not set for cluster scoped security policy acceptance test")
144144
}
145145
},
146-
Config: testConfig.getTestSecurityPolicyResourceBasicConfigValue(policy.ClusterScope, policykindsecurity.StrictRecipe),
147-
Check: testConfig.checkSecurityPolicyResourceAttributes(policy.ClusterScope),
146+
Config: testConfig.getTestSecurityPolicyResourceBasicConfigValue(scope.ClusterScope, policykindsecurity.StrictRecipe),
147+
Check: testConfig.checkSecurityPolicyResourceAttributes(scope.ClusterScope),
148148
},
149149
{
150-
Config: testConfig.getTestSecurityPolicyResourceBasicConfigValue(policy.ClusterGroupScope, policykindsecurity.StrictRecipe),
151-
Check: testConfig.checkSecurityPolicyResourceAttributes(policy.ClusterGroupScope),
150+
Config: testConfig.getTestSecurityPolicyResourceBasicConfigValue(scope.ClusterGroupScope, policykindsecurity.StrictRecipe),
151+
Check: testConfig.checkSecurityPolicyResourceAttributes(scope.ClusterGroupScope),
152152
},
153153
{
154154
PreConfig: func() {
155155
if testConfig.ScopeHelperResources.OrgID == "" {
156156
t.Skip("ORG_ID env var is not set for organization scoped security policy acceptance test")
157157
}
158158
},
159-
Config: testConfig.getTestSecurityPolicyResourceBasicConfigValue(policy.OrganizationScope, policykindsecurity.StrictRecipe),
160-
Check: testConfig.checkSecurityPolicyResourceAttributes(policy.OrganizationScope),
159+
Config: testConfig.getTestSecurityPolicyResourceBasicConfigValue(scope.OrganizationScope, policykindsecurity.StrictRecipe),
160+
Check: testConfig.checkSecurityPolicyResourceAttributes(scope.OrganizationScope),
161161
},
162162
},
163163
},
@@ -167,7 +167,7 @@ func TestAcceptanceForSecurityPolicyResource(t *testing.T) {
167167
t.Log("all security policy resource acceptance tests complete!")
168168
}
169169

170-
func (testConfig *testAcceptanceConfig) getTestSecurityPolicyResourceBasicConfigValue(scope policy.Scope, recipe policykindsecurity.Recipe) string {
170+
func (testConfig *testAcceptanceConfig) getTestSecurityPolicyResourceBasicConfigValue(scope scope.Scope, recipe policykindsecurity.Recipe) string {
171171
helperBlock, scopeBlock := testConfig.ScopeHelperResources.GetTestPolicyResourceHelperAndScope(scope)
172172
inputBlock := testConfig.getTestSecurityPolicyResourceInput(recipe)
173173

@@ -357,29 +357,29 @@ func (testConfig *testAcceptanceConfig) getTestSecurityPolicyResourceInput(recip
357357
}
358358

359359
// checkSecurityPolicyResourceAttributes checks for security policy creation along with meta attributes.
360-
func (testConfig *testAcceptanceConfig) checkSecurityPolicyResourceAttributes(scope policy.Scope) resource.TestCheckFunc {
360+
func (testConfig *testAcceptanceConfig) checkSecurityPolicyResourceAttributes(scopeType scope.Scope) resource.TestCheckFunc {
361361
var check = []resource.TestCheckFunc{
362-
testConfig.verifySecurityPolicyResourceCreation(scope),
362+
testConfig.verifySecurityPolicyResourceCreation(scopeType),
363363
resource.TestCheckResourceAttr(testConfig.SecurityPolicyResourceName, "name", testConfig.SecurityPolicyName),
364364
}
365365

366-
switch scope {
367-
case policy.ClusterScope:
366+
switch scopeType {
367+
case scope.ClusterScope:
368368
check = append(check, resource.TestCheckResourceAttr(testConfig.SecurityPolicyResourceName, "scope.0.cluster.0.name", testConfig.ScopeHelperResources.Cluster.Name))
369-
case policy.ClusterGroupScope:
369+
case scope.ClusterGroupScope:
370370
check = append(check, resource.TestCheckResourceAttr(testConfig.SecurityPolicyResourceName, "scope.0.cluster_group.0.cluster_group", testConfig.ScopeHelperResources.ClusterGroup.Name))
371-
case policy.OrganizationScope:
371+
case scope.OrganizationScope:
372372
check = append(check, resource.TestCheckResourceAttr(testConfig.SecurityPolicyResourceName, "scope.0.organization.0.organization", testConfig.ScopeHelperResources.OrgID))
373-
case policy.UnknownScope:
374-
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(policy.ScopesAllowed[:], `, `))
373+
case scope.UnknownScope:
374+
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[:], `, `))
375375
}
376376

377377
check = append(check, policy.MetaResourceAttributeCheck(testConfig.SecurityPolicyResourceName)...)
378378

379379
return resource.ComposeTestCheckFunc(check...)
380380
}
381381

382-
func (testConfig *testAcceptanceConfig) verifySecurityPolicyResourceCreation(scope policy.Scope) resource.TestCheckFunc {
382+
func (testConfig *testAcceptanceConfig) verifySecurityPolicyResourceCreation(scopeType scope.Scope) resource.TestCheckFunc {
383383
return func(s *terraform.State) error {
384384
if testConfig.Provider == nil {
385385
return fmt.Errorf("provider not initialised")
@@ -406,13 +406,13 @@ func (testConfig *testAcceptanceConfig) verifySecurityPolicyResourceCreation(sco
406406
return errors.Wrap(err, "unable to set the context")
407407
}
408408

409-
switch scope {
410-
case policy.ClusterScope:
409+
switch scopeType {
410+
case scope.ClusterScope:
411411
fn := &policyclustermodel.VmwareTanzuManageV1alpha1ClusterPolicyFullName{
412412
ClusterName: testConfig.ScopeHelperResources.Cluster.Name,
413-
ManagementClusterName: scoperesource.AttachedValue,
413+
ManagementClusterName: scope.AttachedValue,
414414
Name: testConfig.SecurityPolicyName,
415-
ProvisionerName: scoperesource.AttachedValue,
415+
ProvisionerName: scope.AttachedValue,
416416
}
417417

418418
resp, err := config.TMCConnection.ClusterPolicyResourceService.ManageV1alpha1ClusterPolicyResourceServiceGet(fn)
@@ -423,7 +423,7 @@ func (testConfig *testAcceptanceConfig) verifySecurityPolicyResourceCreation(sco
423423
if resp == nil {
424424
return errors.Wrapf(err, "cluster scoped security policy resource is empty, resource: %s", testConfig.SecurityPolicyResourceName)
425425
}
426-
case policy.ClusterGroupScope:
426+
case scope.ClusterGroupScope:
427427
fn := &policyclustergroupmodel.VmwareTanzuManageV1alpha1ClustergroupPolicyFullName{
428428
ClusterGroupName: testConfig.ScopeHelperResources.ClusterGroup.Name,
429429
Name: testConfig.SecurityPolicyName,
@@ -437,7 +437,7 @@ func (testConfig *testAcceptanceConfig) verifySecurityPolicyResourceCreation(sco
437437
if resp == nil {
438438
return errors.Wrapf(err, "cluster group scoped security policy resource is empty, resource: %s", testConfig.SecurityPolicyResourceName)
439439
}
440-
case policy.OrganizationScope:
440+
case scope.OrganizationScope:
441441
fn := &policyorganizationmodel.VmwareTanzuManageV1alpha1OrganizationPolicyFullName{
442442
OrgID: testConfig.ScopeHelperResources.OrgID,
443443
Name: testConfig.SecurityPolicyName,
@@ -451,8 +451,8 @@ func (testConfig *testAcceptanceConfig) verifySecurityPolicyResourceCreation(sco
451451
if resp == nil {
452452
return errors.Wrapf(err, "organization scoped security policy resource is empty, resource: %s", testConfig.SecurityPolicyResourceName)
453453
}
454-
case policy.UnknownScope:
455-
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(policy.ScopesAllowed[:], `, `))
454+
case scope.UnknownScope:
455+
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[:], `, `))
456456
}
457457

458458
return nil

0 commit comments

Comments
 (0)