mirror of
https://github.com/SynologyOpenSource/synology-csi.git
synced 2026-02-13 21:00:03 +00:00
69 lines
1.2 KiB
Go
69 lines
1.2 KiB
Go
// Copyright 2021 Synology Inc.
|
|
|
|
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"strings"
|
|
)
|
|
|
|
type AuthType string
|
|
|
|
const (
|
|
UNIT_GB = 1024 * 1024 * 1024
|
|
UNIT_MB = 1024 * 1024
|
|
|
|
ProtocolSmb = "smb"
|
|
ProtocolIscsi = "iscsi"
|
|
ProtocolDefault = ProtocolIscsi
|
|
|
|
AuthTypeReadWrite AuthType = "rw"
|
|
AuthTypeReadOnly AuthType = "ro"
|
|
AuthTypeNoAccess AuthType = "no"
|
|
)
|
|
|
|
func SliceContains(items []string, s string) bool {
|
|
for _, item := range items {
|
|
if s == item {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func MBToBytes(size int64) int64 {
|
|
return size * UNIT_MB
|
|
}
|
|
|
|
func BytesToMB(size int64) int64 {
|
|
return size / UNIT_MB
|
|
}
|
|
|
|
// Ceiling
|
|
func BytesToMBCeil(size int64) int64 {
|
|
return (size + UNIT_MB - 1) / UNIT_MB
|
|
}
|
|
|
|
func StringToBoolean(value string) bool {
|
|
value = strings.ToLower(value)
|
|
return value == "yes" || value == "true" || value == "1"
|
|
}
|
|
|
|
// Haven't supported IPv6 yet.
|
|
func LookupIPv4(name string) ([]string, error) {
|
|
ips, _ := net.LookupIP(name)
|
|
|
|
retIps := []string{}
|
|
for _, ip := range ips {
|
|
if ipv4 := ip.To4(); ipv4 != nil {
|
|
retIps = append(retIps, ipv4.String())
|
|
}
|
|
}
|
|
if len(retIps) > 0 {
|
|
return retIps, nil
|
|
}
|
|
|
|
return nil, fmt.Errorf("Failed to LookupIPv4 by local resolver for: %s", name)
|
|
}
|