github.com/teknogeek/dnscontrol/v2@v2.10.1-0.20200227202244-ae299b55ba42/models/t_caa.go (about)

     1  package models
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"strings"
     7  )
     8  
     9  // SetTargetCAA sets the CAA fields.
    10  func (rc *RecordConfig) SetTargetCAA(flag uint8, tag string, target string) error {
    11  	rc.CaaTag = tag
    12  	rc.CaaFlag = flag
    13  	rc.SetTarget(target)
    14  	if rc.Type == "" {
    15  		rc.Type = "CAA"
    16  	}
    17  	if rc.Type != "CAA" {
    18  		panic("assertion failed: SetTargetCAA called when .Type is not CAA")
    19  	}
    20  
    21  	if tag != "issue" && tag != "issuewild" && tag != "iodef" {
    22  		return fmt.Errorf("CAA tag (%v) is not one of issue/issuewild/iodef", tag)
    23  	}
    24  
    25  	return nil
    26  }
    27  
    28  // SetTargetCAAStrings is like SetTargetCAA but accepts strings.
    29  func (rc *RecordConfig) SetTargetCAAStrings(flag, tag, target string) error {
    30  	i64flag, err := strconv.ParseUint(flag, 10, 8)
    31  	if err != nil {
    32  		return fmt.Errorf("CAA flag does not fit in 8 bits: %w", err)
    33  	}
    34  	return rc.SetTargetCAA(uint8(i64flag), tag, target)
    35  }
    36  
    37  // SetTargetCAAString is like SetTargetCAA but accepts one big string.
    38  // Ex: `0 issue "letsencrypt.org"`
    39  func (rc *RecordConfig) SetTargetCAAString(s string) error {
    40  	part := strings.Fields(s)
    41  	if len(part) != 3 {
    42  		return fmt.Errorf("CAA value does not contain 3 fields: (%#v)", s)
    43  	}
    44  	return rc.SetTargetCAAStrings(part[0], part[1], StripQuotes(part[2]))
    45  }