Skip to content

Commit

Permalink
Validate if object to delete has any back references
Browse files Browse the repository at this point in the history
Change-Id: I3498b49f055c7c7785e2e4c0334c0b6d51609fbb
Closes-Bug: #1784601
  • Loading branch information
dmarkiewicz authored and IridiumOxide committed Aug 6, 2018
1 parent 91afbf6 commit a502472
Show file tree
Hide file tree
Showing 14 changed files with 176 additions and 96 deletions.
Expand Up @@ -45,6 +45,7 @@ val listTagTypes = "listTagTypes"
val listLabelTags = "listLabelTags"
val matchesSecurityScope = "matchesSecurityScope"
val defaultConnection = "defaultConnection"
val hasBackrefs = "hasBackrefs"

val portOfVCVirtualMachine = "portOfVCVirtualMachine"
val networkOfVCPortGroup = "networkOfVCPortGroup"
Expand Up @@ -9,6 +9,7 @@ import com.vmware.o11n.sdk.modeldrivengen.model.FormalParameter
import com.vmware.o11n.sdk.modeldrivengen.model.ManagedConstructor
import com.vmware.o11n.sdk.modeldrivengen.model.ManagedMethod
import com.vmware.o11n.sdk.modeldrivengen.model.ManagedType
import net.juniper.contrail.vro.config.backReferencePattern
import net.juniper.contrail.vro.config.constants.apiTypesPackageName
import net.juniper.contrail.vro.config.isApiObjectClass
import net.juniper.contrail.vro.config.isNodeClass
Expand Down Expand Up @@ -134,6 +135,11 @@ class CustomManagedType(private val delegate: ManagedType) : ManagedType() {
.toList()
} ?: emptyList()

val backrefs: List<CustomReference> = delegate.modelClass?.run {
references.asSequence()
.filter { it.methodName.matches(backReferencePattern) }.toList()
} ?: emptyList()

val referenceProperties: List<CustomReferenceProperty> = delegate.modelClass?.run {
if (isApiObjectClass)
methods.asSequence()
Expand Down
Expand Up @@ -235,6 +235,15 @@ public class ${className}
}

</#list>

public Integer backrefCount() {
int count = 0;
<#list backrefs as bref>
count += ${bref.pluginMethodName}().size();
</#list>
return count;
}

<#list referenceProperties as prop>
public String ${prop.wrapperMethodName}() {
return formatter.format(this, __getTarget().${prop.methodName}(), "${prop.refObjectPluginType}");
Expand Down
Expand Up @@ -45,6 +45,7 @@ import net.juniper.contrail.vro.schema.Schema
import net.juniper.contrail.vro.schema.crudStatus
import net.juniper.contrail.vro.schema.propertyDescription
import net.juniper.contrail.vro.schema.simpleTypeConstraints
import net.juniper.contrail.vro.workflows.custom.hasBackrefs

fun Element.elementInfoPropertiesFor(categoryPath: String) = createElementInfoProperties(
categoryPath = categoryPath,
Expand Down Expand Up @@ -74,6 +75,7 @@ fun deleteWorkflow(className: String, scriptBody: String) =
description = "${className.allCapitalized} to delete"
mandatory = true
showInInventory = true
validWhen = hasBackrefs()
}
}

Expand Down
Expand Up @@ -6,7 +6,7 @@ package net.juniper.contrail.vro.tests.actions

import static net.juniper.contrail.vro.config.Actions.isValidAllocactionPool

class AllocationPoolValidationSpec extends ActionSpec {
class AllocationPoolValidationSpec extends ActionSpec implements ValidationAsserts{
def validatePool = actionFromScript(isValidAllocactionPool)
def allocationValidationMessage = "e.g. 192.168.2.3-192.168.2.10 <enter>... and IPs should be from CIDR"

Expand All @@ -18,8 +18,8 @@ class AllocationPoolValidationSpec extends ActionSpec {
when: "executing validating script"
def result = engine.invokeFunction(validatePool, pools, cidr)

then: "it returns null"
result == null
then: "it succeeds"
validationSuccess(result)
}

def "validating allocation pool with pools not defined should pass" () {
Expand All @@ -30,8 +30,8 @@ class AllocationPoolValidationSpec extends ActionSpec {
when: "executing validating script"
def result = engine.invokeFunction(validatePool, pools, cidr)

then: "it returns null"
result == null
then: "it succeeds"
validationSuccess(result)
}

def "validating allocation pool with cidr not defined should pass" () {
Expand All @@ -42,8 +42,8 @@ class AllocationPoolValidationSpec extends ActionSpec {
when: "executing validating script"
def result = engine.invokeFunction(validatePool, pools, cidr)

then: "it returns null"
result == null
then: "it succeeds"
validationSuccess(result)
}

def "validating allocation pool with cidr not defined and pools with length 0 should pass" () {
Expand All @@ -54,8 +54,8 @@ class AllocationPoolValidationSpec extends ActionSpec {
when: "executing validating script"
def result = engine.invokeFunction(validatePool, pools, cidr)

then: "it returns null"
result == null
then: "it succeeds"
validationSuccess(result)
}

def "validating allocation pool with valid IPv4 pools and cidr should pass" () {
Expand All @@ -66,8 +66,8 @@ class AllocationPoolValidationSpec extends ActionSpec {
when: "executing validating script"
def result = engine.invokeFunction(validatePool, pools, cidr)

then: "it returns null"
result == null
then: "it succeeds"
validationSuccess(result)
}

def "validating pools with valid IPv4 pools and cidr with preceding and trailing whitespaces should pass" () {
Expand All @@ -78,8 +78,8 @@ class AllocationPoolValidationSpec extends ActionSpec {
when: "executing validating script"
def result = engine.invokeFunction(validatePool, pools, cidr)

then: "it returns null"
result == null
then: "it succeeds"
validationSuccess(result)
}

def "validating allocation pool with valid IPv6 pools and cidr should pass" () {
Expand All @@ -90,8 +90,8 @@ class AllocationPoolValidationSpec extends ActionSpec {
when: "executing validating script"
def result = engine.invokeFunction(validatePool, pools, cidr)

then: "it returns null"
result == null
then: "it succeeds"
validationSuccess(result)
}

def "validating allocation pool with not valid cidr format should not pass" () {
Expand All @@ -102,8 +102,8 @@ class AllocationPoolValidationSpec extends ActionSpec {
when: "executing validating script"
def result = engine.invokeFunction(validatePool, pools, cidr)

then: "it returns error message"
result == allocationValidationMessage
then: "it fails with the correct message"
validationFailureWith(result, allocationValidationMessage)
}

def "validating allocation pool with overlapping pools should not pass" () {
Expand All @@ -114,8 +114,8 @@ class AllocationPoolValidationSpec extends ActionSpec {
when: "executing validating script"
def result = engine.invokeFunction(validatePool, pools, cidr)

then: "it returns error message"
result == allocationValidationMessage
then: "it fails with the correct message"
validationFailureWith(result, allocationValidationMessage)
}

def "validating allocation pool with pools no in cidr should not pass" () {
Expand All @@ -126,7 +126,7 @@ class AllocationPoolValidationSpec extends ActionSpec {
when: "executing validating script"
def result = engine.invokeFunction(validatePool, pools, cidr)

then: "it returns error message"
result == allocationValidationMessage
then: "it fails with the correct message"
validationFailureWith(result, allocationValidationMessage)
}
}
Expand Up @@ -6,7 +6,7 @@ package net.juniper.contrail.vro.tests.actions

import static net.juniper.contrail.vro.config.Actions.isValidSubnet

class CidrValidationSpec extends ActionSpec {
class CidrValidationSpec extends ActionSpec implements ValidationAsserts{
def validateCidr = actionFromScript(isValidSubnet)
def cidrValidationMessage = "Enter valid IPv4 or IPv6 Subnet/Mask"

Expand All @@ -17,8 +17,8 @@ class CidrValidationSpec extends ActionSpec {
when: "executing validating script"
def result = engine.invokeFunction(validateCidr, cidr)

then: "it returns null"
result == null
then: "it succeeds"
validationSuccess(result)
}

def "validating empty cidr should not pass" () {
Expand All @@ -29,7 +29,7 @@ class CidrValidationSpec extends ActionSpec {
def result = engine.invokeFunction(validateCidr, cidr)

then: "it returns error message"
result == cidrValidationMessage
validationFailureWith(result, cidrValidationMessage)
}

def "validating IPv4 cidr should pass" () {
Expand All @@ -39,8 +39,8 @@ class CidrValidationSpec extends ActionSpec {
when: "executing validating script"
def result = engine.invokeFunction(validateCidr, cidr)

then: "it returns null"
result == null
then: "it succeeds"
validationSuccess(result)
}

def "validating IPv6 cidr should pass" () {
Expand All @@ -50,8 +50,8 @@ class CidrValidationSpec extends ActionSpec {
when: "executing validating script"
def result = engine.invokeFunction(validateCidr, cidr)

then: "it returns null"
result == null
then: "it succeeds"
validationSuccess(result)
}

def "validating valid IPv4 cidr with too big mask should not pass" () {
Expand All @@ -61,8 +61,8 @@ class CidrValidationSpec extends ActionSpec {
when: "executing validating script"
def result = engine.invokeFunction(validateCidr, cidr)

then: "it returns error message"
result == cidrValidationMessage
then: "it fails with the correct message"
validationFailureWith(result, cidrValidationMessage)
}

def "validating valid IPv6 cidr with too big mask should not pass" () {
Expand All @@ -72,8 +72,8 @@ class CidrValidationSpec extends ActionSpec {
when: "executing validating script"
def result = engine.invokeFunction(validateCidr, cidr)

then: "it returns error message"
result == cidrValidationMessage
then: "it fails with the correct message"
validationFailureWith(result, cidrValidationMessage)
}

def "validating valid IPv4 cidr with biggest valid mask should pass" () {
Expand All @@ -83,8 +83,8 @@ class CidrValidationSpec extends ActionSpec {
when: "executing validating script"
def result = engine.invokeFunction(validateCidr, cidr)

then: "it returns null"
result == null
then: "it succeeds"
validationSuccess(result)
}

def "validating valid IPv6 cidr with biggest valid mask should pass" () {
Expand All @@ -94,8 +94,8 @@ class CidrValidationSpec extends ActionSpec {
when: "executing validating script"
def result = engine.invokeFunction(validateCidr, cidr)

then: "it returns null"
result == null
then: "it succeeds"
validationSuccess(result)
}

def "validating valid IPv4 cidr with smallest valid mask should pass" () {
Expand All @@ -105,8 +105,8 @@ class CidrValidationSpec extends ActionSpec {
when: "executing validating script"
def result = engine.invokeFunction(validateCidr, cidr)

then: "it returns null"
result == null
then: "it succeeds"
validationSuccess(result)
}

def "validating valid IPv6 cidr with smallest valid mask should pass" () {
Expand All @@ -116,8 +116,8 @@ class CidrValidationSpec extends ActionSpec {
when: "executing validating script"
def result = engine.invokeFunction(validateCidr, cidr)

then: "it returns null"
result == null
then: "it succeeds"
validationSuccess(result)
}

def "validating cidr with trailing and preceding whitespaces should pass" () {
Expand All @@ -127,8 +127,8 @@ class CidrValidationSpec extends ActionSpec {
when: "executing validating script"
def result = engine.invokeFunction(validateCidr, cidr)

then: "it returns null"
result == null
then: "it succeeds"
validationSuccess(result)
}

def "validating cidr with whitespaces inside should not pass" () {
Expand All @@ -138,7 +138,7 @@ class CidrValidationSpec extends ActionSpec {
when: "executing validating script"
def result = engine.invokeFunction(validateCidr, cidr)

then: "it returns error message"
result == cidrValidationMessage
then: "it fails with the correct message"
validationFailureWith(result, cidrValidationMessage)
}
}

0 comments on commit a502472

Please sign in to comment.