helper.go raw
1 package rrset
2
3 import (
4 "encoding/json"
5 "fmt"
6 "net/url"
7
8 "github.com/ultradns/ultradns-go-sdk/pkg/helper"
9 "github.com/ultradns/ultradns-go-sdk/pkg/record/dirpool"
10 "github.com/ultradns/ultradns-go-sdk/pkg/record/rdpool"
11 "github.com/ultradns/ultradns-go-sdk/pkg/record/sbpool"
12 "github.com/ultradns/ultradns-go-sdk/pkg/record/sfpool"
13 "github.com/ultradns/ultradns-go-sdk/pkg/record/slbpool"
14 "github.com/ultradns/ultradns-go-sdk/pkg/record/tcpool"
15 )
16
17 func (r RRSetKey) RecordURI() string {
18 r.Owner = url.PathEscape(r.Owner)
19 r.Zone = url.PathEscape(r.Zone)
20
21 if r.RecordType == "" {
22 r.RecordType = "ANY"
23 }
24
25 return fmt.Sprintf("zones/%s/rrsets/%s/%s", r.Zone, r.RecordType, r.Owner)
26 }
27
28 func (r RRSetKey) ProbeURI() string {
29 return fmt.Sprintf("%s/probes/%s", r.RecordURI(), r.ID)
30 }
31
32 func (r RRSetKey) ProbeListURI(query string) string {
33 return fmt.Sprintf("%s/probes?q=%s", r.RecordURI(), query)
34 }
35
36 func (r RRSetKey) RecordID() string {
37 r.Owner = helper.GetOwnerFQDN(r.Owner, r.Zone)
38 r.Zone = helper.GetZoneFQDN(r.Zone)
39 r.RecordType = helper.GetRecordTypeFullString(r.RecordType)
40
41 if r.RecordType == "" {
42 r.RecordType = "ANY"
43 }
44
45 return fmt.Sprintf("%s:%s:%s", r.Owner, r.Zone, r.RecordType)
46 }
47
48 // PID stands for Probe Id
49 func (r RRSetKey) PID() string {
50 return fmt.Sprintf("%s:%s", r.RecordID(), r.ID)
51 }
52
53 func (r *RRSet) UnmarshalJSON(data []byte) error {
54 var m map[string]interface{}
55
56 if err := json.Unmarshal(data, &m); err != nil {
57 return err
58 }
59
60 if val, ok := m["ownerName"].(string); ok {
61 r.OwnerName = val
62 }
63
64 if val, ok := m["rrtype"].(string); ok {
65 r.RRType = val
66 }
67
68 if val, ok := m["ttl"].(float64); ok {
69 r.TTL = int(val)
70 }
71
72 if val, ok := m["rdata"].([]interface{}); ok {
73 r.RData = make([]string, len(val))
74
75 for i, v := range val {
76 r.RData[i] = v.(string)
77 }
78 }
79
80 if val, ok := m["profile"].(map[string]interface{}); ok {
81 return r.setProfile(val)
82 }
83
84 return nil
85 }
86
87 func (r *RRSet) setProfile(m map[string]interface{}) error {
88
89 profileJson, err := json.Marshal(m)
90
91 if err != nil {
92 return err
93 }
94
95 var context string
96
97 if val, ok := m["@context"].(string); ok {
98 context = val
99 }
100
101 profile := getPoolProfile(context)
102
103 err = json.Unmarshal(profileJson, profile)
104
105 if err != nil {
106 return err
107 }
108
109 r.Profile = profile
110 return nil
111 }
112
113 func getPoolProfile(context string) RawProfile {
114 switch context {
115 case rdpool.Schema:
116 return &rdpool.Profile{}
117 case sfpool.Schema:
118 return &sfpool.Profile{}
119 case slbpool.Schema:
120 return &slbpool.Profile{}
121 case sbpool.Schema:
122 return &sbpool.Profile{}
123 case tcpool.Schema:
124 return &tcpool.Profile{}
125 case dirpool.Schema:
126 return &dirpool.Profile{}
127 }
128
129 return nil
130 }
131