Skip to content

Commit

Permalink
Implement review
Browse files Browse the repository at this point in the history
  • Loading branch information
simcod committed Nov 8, 2024
1 parent 14d08b3 commit b5236c3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 26 deletions.
55 changes: 33 additions & 22 deletions cmd/metal-api/internal/service/machine-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -1152,44 +1152,29 @@ func createMachineAllocationSpec(ds *datastore.RethinkStore, machineRequest v1.M
return nil, fmt.Errorf("partition:%s not found err:%w", partitionID, err)
}
var (
dnsServers metal.DNSServers
ntpServers metal.NTPServers
dnsServers = partition.DNSServers
ntpServers = partition.NTPServers
)
if len(machineRequest.DNSServers) != 0 {
if len(machineRequest.DNSServers) > 3 {
return nil, errors.New("please specify a maximum of three dns servers")
}
dnsServers = machineRequest.DNSServers
} else {
dnsServers = partition.DNSServers
}
for _, dnsServer := range dnsServers {
_, err := netip.ParseAddr(dnsServer.IP)
if err != nil {
return nil, fmt.Errorf("ip: %s for dns server not correct err: %w", dnsServer, err)
}

if err := ValidateDNSServers(dnsServers); err != nil {
return nil, err
}

if len(machineRequest.NTPServers) != 0 {
if len(machineRequest.NTPServers) < 3 || len(machineRequest.NTPServers) > 5 {
return nil, errors.New("please specify a minimum of 3 and a maximum of 5 ntp servers")
}
ntpServers = machineRequest.NTPServers
} else {
ntpServers = partition.NTPServers
}

for _, ntpserver := range ntpServers {
if net.ParseIP(ntpserver.Address) != nil {
_, err := netip.ParseAddr(ntpserver.Address)
if err != nil {
return nil, fmt.Errorf("ip: %s for ntp server not correct err: %w", ntpserver, err)
}
} else {
if !govalidator.IsDNSName(ntpserver.Address) {
return nil, fmt.Errorf("dns name: %s for ntp server not correct err: %w", ntpserver, err)
}
}
if err := ValidateNTPServers(ntpServers); err != nil {
return nil, err
}

return &machineAllocationSpec{
Expand Down Expand Up @@ -2527,3 +2512,29 @@ func (s machineAllocationSpec) noautoNetworkN() int {
func (s machineAllocationSpec) autoNetworkN() int {
return len(s.Networks) - s.noautoNetworkN()
}

func ValidateDNSServers(d metal.DNSServers) error {
for _, dnsServer := range d {
_, err := netip.ParseAddr(dnsServer.IP)
if err != nil {
return fmt.Errorf("ip: %s for dns server not correct err: %w", dnsServer, err)
}
}
return nil
}

func ValidateNTPServers(dnsServers metal.NTPServers) error {
for _, ntpserver := range dnsServers {
if net.ParseIP(ntpserver.Address) != nil {
_, err := netip.ParseAddr(ntpserver.Address)
if err != nil {
return fmt.Errorf("ip: %s for ntp server not correct err: %w", ntpserver, err)
}
} else {
if !govalidator.IsDNSName(ntpserver.Address) {
return fmt.Errorf("dns name: %s for ntp server not correct", ntpserver)
}
}
}
return nil
}
18 changes: 14 additions & 4 deletions cmd/metal-api/internal/service/partition-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,23 @@ func (r *partitionResource) createPartition(request *restful.Request, response *
}

var dnsServers metal.DNSServers
if len(requestPayload.DNSServers) > 0 {
dnsServers = requestPayload.DNSServers
reqDNSServers := requestPayload.DNSServers
if len(reqDNSServers) > 0 {
if err := ValidateDNSServers(reqDNSServers); err != nil {
r.sendError(request, response, httperrors.BadRequest(err))
return
}

dnsServers = reqDNSServers
}

var ntpServers metal.NTPServers
if len(requestPayload.NTPServers) > 0 {
ntpServers = requestPayload.NTPServers
reqNtpServers := requestPayload.NTPServers
if len(ntpServers) > 0 {
if err := ValidateNTPServers(reqNtpServers); err != nil {
r.sendError(request, response, httperrors.BadRequest(err))
}
ntpServers = reqNtpServers
}

p := &metal.Partition{
Expand Down

0 comments on commit b5236c3

Please sign in to comment.