github.com/teknogeek/dnscontrol/v2@v2.10.1-0.20200227202244-ae299b55ba42/models/t_srv.go (about) 1 package models 2 3 import ( 4 "fmt" 5 "strconv" 6 "strings" 7 ) 8 9 // SetTargetSRV sets the SRV fields. 10 func (rc *RecordConfig) SetTargetSRV(priority, weight, port uint16, target string) error { 11 rc.SrvPriority = priority 12 rc.SrvWeight = weight 13 rc.SrvPort = port 14 rc.SetTarget(target) 15 if rc.Type == "" { 16 rc.Type = "SRV" 17 } 18 if rc.Type != "SRV" { 19 panic("assertion failed: SetTargetSRV called when .Type is not SRV") 20 } 21 return nil 22 } 23 24 // setTargetSRVIntAndStrings is like SetTargetSRV but accepts priority as an int, the other parameters as strings. 25 func (rc *RecordConfig) setTargetSRVIntAndStrings(priority uint16, weight, port, target string) (err error) { 26 var i64weight, i64port uint64 27 if i64weight, err = strconv.ParseUint(weight, 10, 16); err == nil { 28 if i64port, err = strconv.ParseUint(port, 10, 16); err == nil { 29 return rc.SetTargetSRV(priority, uint16(i64weight), uint16(i64port), target) 30 } 31 } 32 return fmt.Errorf("SRV value too big for uint16: %w", err) 33 } 34 35 // SetTargetSRVStrings is like SetTargetSRV but accepts all parameters as strings. 36 func (rc *RecordConfig) SetTargetSRVStrings(priority, weight, port, target string) (err error) { 37 var i64priority uint64 38 if i64priority, err = strconv.ParseUint(priority, 10, 16); err == nil { 39 return rc.setTargetSRVIntAndStrings(uint16(i64priority), weight, port, target) 40 } 41 return fmt.Errorf("SRV value too big for uint16: %w", err) 42 } 43 44 // SetTargetSRVPriorityString is like SetTargetSRV but accepts priority as an 45 // uint16 and the rest of the values joined in a string that needs to be parsed. 46 // This is a helper function that comes in handy when a provider re-uses the MX preference 47 // field as the SRV priority. 48 func (rc *RecordConfig) SetTargetSRVPriorityString(priority uint16, s string) error { 49 part := strings.Fields(s) 50 switch len(part) { 51 case 3: 52 return rc.setTargetSRVIntAndStrings(priority, part[0], part[1], part[2]) 53 case 2: 54 return rc.setTargetSRVIntAndStrings(priority, part[0], part[1], ".") 55 default: 56 return fmt.Errorf("SRV value does not contain 3 fields: (%#v)", s) 57 } 58 } 59 60 // SetTargetSRVString is like SetTargetSRV but accepts one big string to be parsed. 61 func (rc *RecordConfig) SetTargetSRVString(s string) error { 62 part := strings.Fields(s) 63 if len(part) != 4 { 64 return fmt.Errorf("SRV value does not contain 4 fields: (%#v)", s) 65 } 66 return rc.SetTargetSRVStrings(part[0], part[1], part[2], part[3]) 67 }