github.com/sacloud/iaas-api-go@v1.12.0/customize_dns_record.go (about) 1 // Copyright 2022-2023 The sacloud/iaas-api-go Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package iaas 16 17 import ( 18 "fmt" 19 "strings" 20 21 "github.com/sacloud/iaas-api-go/types" 22 ) 23 24 // Equal 名前/タイプ/値が同じレコードの場合trueを返す 25 func (o *DNSRecord) Equal(r *DNSRecord) bool { 26 return o.Name == r.Name && o.Type == r.Type && o.RData == r.RData 27 } 28 29 // NewDNSRecord レコードを生成して返す 30 func NewDNSRecord(t types.EDNSRecordType, name, rdata string, ttl int) *DNSRecord { 31 switch t { 32 case 33 types.DNSRecordTypes.NS, 34 types.DNSRecordTypes.CNAME, 35 types.DNSRecordTypes.MX, 36 types.DNSRecordTypes.ALIAS, 37 types.DNSRecordTypes.PTR: 38 if rdata != "" && !strings.HasSuffix(rdata, ".") { 39 rdata += "." 40 } 41 } 42 43 return &DNSRecord{ 44 Name: name, 45 Type: t, 46 RData: rdata, 47 TTL: ttl, 48 } 49 } 50 51 // MXRecord MXレコード型 52 type MXRecord struct { 53 Name string 54 RData string 55 TTL int 56 Priority int 57 } 58 59 // Type レコードタイプ 60 func (r *MXRecord) Type() types.EDNSRecordType { 61 return types.DNSRecordTypes.MX 62 } 63 64 // ToRecord *DNSRecord型へ変換 65 func (r *MXRecord) ToRecord() *DNSRecord { 66 rdata := r.RData 67 if rdata != "" && !strings.HasSuffix(rdata, ".") { 68 rdata += "." 69 } 70 return &DNSRecord{ 71 Name: r.Name, 72 Type: r.Type(), 73 RData: fmt.Sprintf("%d %s", r.Priority, rdata), 74 TTL: r.TTL, 75 } 76 } 77 78 // NewMXRecord MXレコードを生成して返す 79 func NewMXRecord(name, rdata string, ttl, priority int) *DNSRecord { 80 return (&MXRecord{ 81 Name: name, 82 RData: rdata, 83 Priority: priority, 84 TTL: ttl, 85 }).ToRecord() 86 } 87 88 // SRVRecord SRVレコード型 89 type SRVRecord struct { 90 Name string 91 RData string 92 TTL int 93 Priority int 94 Weight int 95 Port int 96 } 97 98 // Type レコードタイプ 99 func (r *SRVRecord) Type() types.EDNSRecordType { 100 return types.DNSRecordTypes.SRV 101 } 102 103 // ToRecord *DNSRecordに変換 104 func (r *SRVRecord) ToRecord() *DNSRecord { 105 return &DNSRecord{ 106 Name: r.Name, 107 Type: r.Type(), 108 RData: fmt.Sprintf("%d %d %d %s", r.Priority, r.Weight, r.Port, r.RData), 109 TTL: r.TTL, 110 } 111 } 112 113 // NewSRVRecord SRVレコードを生成して返す 114 func NewSRVRecord(name, rdata string, ttl, priority, weight, port int) *DNSRecord { 115 return (&SRVRecord{ 116 Name: name, 117 RData: rdata, 118 TTL: ttl, 119 Priority: priority, 120 Weight: weight, 121 Port: port, 122 }).ToRecord() 123 }