Skip to content

Commit f26a054

Browse files
committed
functionality to check format of namespaces
Signed-off-by: Fiona Ampofo <[email protected]>
1 parent f092c0e commit f26a054

9 files changed

+282
-92
lines changed

pkg/errors/errorMessage.go

+3
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,9 @@ var (
222222
GALASA_ERROR_RESET_RUN_RESPONSE_PARSING = NewMessageType("GAL1134E: The runs reset operation failed. Unable to process the error information returned from the server.", 1134, STACK_TRACE_WANTED)
223223
GALASA_ERROR_CANCEL_RUN_FAILED = NewMessageType("GAL1135E: Error cancelling run '%v'. Reason: '%s'", 1135, STACK_TRACE_WANTED)
224224
GALASA_ERROR_CANCEL_RUN_RESPONSE_PARSING = NewMessageType("GAL1136E: The runs cancel operation failed. Unable to process the error information returned from the server.", 1136, STACK_TRACE_WANTED)
225+
GALASA_ERROR_FAILED_TO_COMPILE_NAMESPACE_REGEX = NewMessageType("GAL1137E: Unable to compile the regex pattern for Galasa Property field 'namespace'. Reason: '%s'", 1137, STACK_TRACE_NOT_WANTED)
226+
GALASA_ERROR_INVALID_PROPERTY_NAMESPACE_FORMAT = NewMessageType("GAL1138E: The namespace, '%s', provided does not match formatting requirements. " +
227+
"The namespace must start with a character in the 'a-z' range, followed by characters in the 'a'-'z' or '0'-'9' ranges only.", 1138, STACK_TRACE_NOT_WANTED)
225228

226229
// Warnings...
227230
GALASA_WARNING_MAVEN_NO_GALASA_OBR_REPO = NewMessageType("GAL2000W: Warning: Maven configuration file settings.xml should contain a reference to a Galasa repository so that the galasa OBR can be resolved. The official release repository is '%s', and 'pre-release' repository is '%s'", 2000, STACK_TRACE_WANTED)

pkg/properties/properties.go

+21
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@
77
package properties
88

99
import (
10+
"regexp"
1011
"strings"
1112

1213
galasaErrors "github.com/galasa-dev/cli/pkg/errors"
1314
)
1415

16+
const (
17+
PROPERTY_NAMESPACE_PATTERN = "^[a-z][a-z0-9]+$"
18+
)
19+
1520
func validateInputsAreNotEmpty(namespace string, name string) error {
1621
var err error
1722
if len(strings.TrimSpace(name)) == 0 {
@@ -23,3 +28,19 @@ func validateInputsAreNotEmpty(namespace string, name string) error {
2328
}
2429
return err
2530
}
31+
32+
func validateNamespaceFormat(namespace string) error {
33+
var err error
34+
35+
validNamespaceFormat, err := regexp.Compile(PROPERTY_NAMESPACE_PATTERN)
36+
if err != nil {
37+
err = galasaErrors.NewGalasaError(galasaErrors.GALASA_ERROR_FAILED_TO_COMPILE_NAMESPACE_REGEX, err.Error())
38+
} else {
39+
//check if the namespace format matches
40+
if !validNamespaceFormat.MatchString(namespace) {
41+
err = galasaErrors.NewGalasaError(galasaErrors.GALASA_ERROR_INVALID_PROPERTY_NAMESPACE_FORMAT, namespace)
42+
}
43+
}
44+
45+
return err
46+
}

pkg/properties/propertiesDelete.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,13 @@ func DeleteProperty(
2727
var err error
2828

2929
err = validateInputsAreNotEmpty(namespace, name)
30-
if err == nil {
31-
log.Printf("DeleteProperty - Galasa Property field values are valid")
32-
err = deleteCpsProperty(namespace, name, apiClient)
30+
if err == nil{
31+
err = validateNamespaceFormat(namespace)
32+
33+
if err == nil {
34+
log.Printf("DeleteProperty - Galasa Property field values are valid")
35+
err = deleteCpsProperty(namespace, name, apiClient)
36+
}
3337
}
3438
return err
3539
}

pkg/properties/propertiesDelete_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ func mockDeletePropertiesServlet(t *testing.T, w http.ResponseWriter, r *http.Re
4545
statusCode, namespaceProperties = CheckNamespace(namespace)
4646
if len(splitUrl) == 5 {
4747

48-
if namespace == "invalidNamespace" {
48+
if namespace == "invalidnamespace" {
4949
statusCode = 404
5050
namespaceProperties = `{
5151
"error_code": 5017,
5252
"error_message": "GAL5017E: Error occured when trying to access namespace 'invalidNamespace'. The Namespace provided is invalid."
5353
}`
54-
} else if namespace == "validNamespace" {
54+
} else if namespace == "validnamespace" {
5555
propertyName := splitUrl[4]
5656
if propertyName == "invalidName" {
5757
statusCode = 404
@@ -71,7 +71,7 @@ func mockDeletePropertiesServlet(t *testing.T, w http.ResponseWriter, r *http.Re
7171

7272
func TestDeletePropertyValueReturnsOk(t *testing.T) {
7373
//Given...
74-
namespace := "validNamespace"
74+
namespace := "validnamespace"
7575
name := "validName"
7676

7777
server := newDeletePropertiesServletMock(t)
@@ -86,7 +86,7 @@ func TestDeletePropertyValueReturnsOk(t *testing.T) {
8686
}
8787

8888
// invalid OR empty namespace, valid propertyname
89-
func TestDeletePropertyWithInvalidNamesapceReturnsError(t *testing.T) {
89+
func TestDeletePropertyWithInvalidNamesapceFormatReturnsError(t *testing.T) {
9090
//Given...
9191
namespace := "invalidNamespace"
9292
name := "validName"
@@ -100,13 +100,13 @@ func TestDeletePropertyWithInvalidNamesapceReturnsError(t *testing.T) {
100100

101101
//Then
102102
assert.NotNil(t, err)
103-
assert.Contains(t, err.Error(), "GAL1099E", "Error occured when trying to access namespace 'invalidNamespace'.")
103+
assert.Contains(t, err.Error(), "GAL1138E", "Error occured when trying to access namespace 'invalidNamespace'.")
104104
}
105105

106106
// validnamespace , invalid propertyname
107107
func TestValidNamespaceAndDeleteInvalidNameNameReturnsError(t *testing.T) {
108108
//Given...
109-
namespace := "validNamespace"
109+
namespace := "validnamespace"
110110
name := "invalidName"
111111

112112
server := newDeletePropertiesServletMock(t)

pkg/properties/propertiesGet.go

+16-11
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,31 @@ func GetProperties(
4141

4242
err = checkNameNotUsedWithPrefixSuffixInfix(name, prefix, suffix, infix)
4343
if err == nil {
44-
var chosenFormatter propertiesformatter.PropertyFormatter
4544

46-
chosenFormatter, err = validateOutputFormatFlagValue(propertiesOutputFormat, validPropertyFormatters)
45+
err = validateNamespaceFormat(namespace)
4746
if err == nil {
48-
var cpsProperty []galasaapi.GalasaProperty
49-
cpsProperty, err = getCpsPropertiesFromRestApi(namespace, name, prefix, suffix, infix, apiClient, console)
5047

51-
log.Printf("GetProperties - Galasa Properties collected: %s", getCpsPropertyArrayAsString(cpsProperty))
52-
if err == nil {
53-
var outputText string
48+
var chosenFormatter propertiesformatter.PropertyFormatter
5449

55-
outputText, err = chosenFormatter.FormatProperties(cpsProperty)
50+
chosenFormatter, err = validateOutputFormatFlagValue(propertiesOutputFormat, validPropertyFormatters)
51+
if err == nil {
52+
var cpsProperty []galasaapi.GalasaProperty
53+
cpsProperty, err = getCpsPropertiesFromRestApi(namespace, name, prefix, suffix, infix, apiClient, console)
5654

55+
log.Printf("GetProperties - Galasa Properties collected: %s", getCpsPropertyArrayAsString(cpsProperty))
5756
if err == nil {
58-
console.WriteString(outputText)
59-
}
57+
var outputText string
58+
59+
outputText, err = chosenFormatter.FormatProperties(cpsProperty)
6060

61+
if err == nil {
62+
console.WriteString(outputText)
63+
}
64+
65+
}
6166
}
6267
}
68+
6369
}
6470
return err
6571
}
@@ -105,7 +111,6 @@ func getCpsPropertiesFromRestApi(
105111
apicall := apiClient.ConfigurationPropertyStoreAPIApi.GetCpsProperty(context, namespace, name).ClientApiVersion(restApiVersion)
106112
cpsProperties, _, err = apicall.Execute()
107113
}
108-
109114

110115
if err != nil {
111116
err = galasaErrors.NewGalasaError(galasaErrors.GALASA_ERROR_QUERY_NAMESPACE_FAILED, err.Error())

0 commit comments

Comments
 (0)