Skip to content

Commit

Permalink
adding support and validation for providing FQDN (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
aggarwalta authored Jan 3, 2022
1 parent 595198f commit 4adbee3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
6 changes: 3 additions & 3 deletions vdoctl/cmd/drivers.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ var driversCmd = &cobra.Command{
isCPIRequired := utils.PromptGetInput("Do you want to configure CloudProvider? (Y/N)", errors.New("invalid input"), utils.IsString)

if strings.EqualFold(isCPIRequired, "Y") {
fmt.Printf("Please provide the vcenter IP for configuring CloudProvider \n")
fmt.Printf("Please provide the vcenter IP/FQDN for configuring CloudProvider \n")

multivcloop:
for {
Expand Down Expand Up @@ -217,7 +217,7 @@ var driversCmd = &cobra.Command{
csi.insecure = cpi.insecure
csi.thumbprint = cpi.thumbprint
} else {
fmt.Printf("Please provide the vcenter IP for configuring StorageProvider \n")
fmt.Printf("Please provide the vcenter IP/FQDN for configuring StorageProvider \n")
fetchVCIP(&csi, labels)
if !csi.insecure {
fetchThumbprint(&csi, labels)
Expand Down Expand Up @@ -422,7 +422,7 @@ func createVDOConfig(cl client.Client, ctx context.Context, cpi credentials, csi
}

func fetchVCIP(cred *credentials, labels credentials) {
vcIp := utils.PromptGetInput(labels.vcIp, errors.New("unable to get the IP Address - Invalid input"), utils.IsIP)
vcIp := utils.PromptGetInput(labels.vcIp, errors.New("unable to get the IP Address/ FQDN - Invalid input"), utils.IsIPorFQDN)
cred.vcIp = vcIp

res := utils.PromptGetInput("Do you want to establish a secure connection? (Y/N)", errors.New("invalid input"), utils.IsString)
Expand Down
19 changes: 13 additions & 6 deletions vdoctl/pkg/utils/promptutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ import (
"net"
"net/url"
"os"
"regexp"
"strings"
)

type ValidationFlags string

const (
IsPwd ValidationFlags = "isPwd"
IsURL ValidationFlags = "isURL"
IsIP ValidationFlags = "isIP"
IsString ValidationFlags = "isString"
IsPwd ValidationFlags = "isPwd"
IsURL ValidationFlags = "isURL"
IsIPorFQDN ValidationFlags = "isIPorFQDN"
IsString ValidationFlags = "isString"
)

func CheckIfUrl(str string) bool {
Expand All @@ -39,6 +40,12 @@ func checkIPAddress(ip string) bool {
return net.ParseIP(ip) != nil
}

func checkIfFQDN(domain string) bool {
RegExp := regexp.MustCompile(`^([a-zA-Z0-9_]{1}[a-zA-Z0-9_-]{0,62}){1}(\.[a-zA-Z0-9_]{1}[a-zA-Z0-9_-]{0,62})+[\._]?[^\.]$`)
return RegExp.MatchString(domain)

}

func PromptGetInput(label string, err error, flag ValidationFlags) string {
validate := func(input string) error {
if len(input) <= 0 {
Expand All @@ -50,8 +57,8 @@ func PromptGetInput(label string, err error, flag ValidationFlags) string {

}

if flag == IsIP && !checkIPAddress(input) {
return errors.New("Please provide a valid IP address")
if flag == IsIPorFQDN && !(checkIPAddress(input) || checkIfFQDN(input)) {
return errors.New("Please provide a valid IP address/ FQDN")
}
return nil
}
Expand Down

0 comments on commit 4adbee3

Please sign in to comment.