github.com/StackExchange/DNSControl@v0.2.8/models/t_srv.go (about)

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