-
Notifications
You must be signed in to change notification settings - Fork 1
/
alias_record.go
113 lines (96 loc) · 2.96 KB
/
alias_record.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package infoblox
import (
"fmt"
"net/http"
)
const (
aliasRecordBasePath = "record:alias"
aliasRecordReturnFields = "name,target_name,target_type,dns_name,dns_target_name,disable,view,dns_name,comment,zone,extattrs"
)
// GetAliasRecordByRef gets alias record by reference
func (c *Client) GetAliasRecordByRef(ref string, queryParams map[string]string) (AliasRecord, error) {
var ret AliasRecord
if queryParams == nil {
queryParams = map[string]string{
"_return_fields": aliasRecordReturnFields,
}
} else {
queryParams["_return_fields"] = aliasRecordReturnFields
}
queryParamString := c.BuildQuery(queryParams)
request, err := c.CreateJSONRequest(http.MethodGet, fmt.Sprintf("%s?%s", ref, queryParamString), nil)
if err != nil {
return ret, err
}
response := c.Call(request, &ret)
if response != nil {
return ret, fmt.Errorf(response.ErrorMessage)
}
return ret, nil
}
// GetAliasRecordByQuery gets alias records by query parameters
func (c *Client) GetAliasRecordByQuery(queryParams map[string]string) ([]AliasRecord, error) {
var ret AliasRecordQueryResult
queryParams["_return_fields"] = aliasRecordReturnFields
queryParams["_return_as_object"] = "1"
queryParams["_paging"] = "0"
queryParams["_max_results"] = "2"
queryParamString := c.BuildQuery(queryParams)
request, err := c.CreateJSONRequest(http.MethodGet, fmt.Sprintf("%s?%s", aliasRecordBasePath, queryParamString), nil)
if err != nil {
return nil, err
}
response := c.Call(request, &ret)
if response != nil {
return nil, fmt.Errorf(response.ErrorMessage)
}
return ret.Results, nil
}
// CreateAliasRecord creates alias record
func (c *Client) CreateAliasRecord(record *AliasRecord) error {
queryParams := map[string]string{
"_return_fields": aliasRecordReturnFields,
}
queryParamString := c.BuildQuery(queryParams)
request, err := c.CreateJSONRequest(http.MethodPost, fmt.Sprintf("%s?%s", aliasRecordBasePath, queryParamString), record)
if err != nil {
return err
}
response := c.Call(request, &record)
if response != nil {
return fmt.Errorf(response.ErrorMessage)
}
return nil
}
// UpdateAliasRecord creates alias record
func (c *Client) UpdateAliasRecord(ref string, network AliasRecord) (AliasRecord, error) {
var ret AliasRecord
queryParams := map[string]string{
"_return_fields": aliasRecordReturnFields,
}
queryParamString := c.BuildQuery(queryParams)
request, err := c.CreateJSONRequest(http.MethodPut, fmt.Sprintf("%s?%s", ref, queryParamString), network)
if err != nil {
return ret, err
}
response := c.Call(request, &ret)
if response != nil {
return ret, fmt.Errorf(response.ErrorMessage)
}
return ret, nil
}
// DeleteAliasRecord creates alias record
func (c *Client) DeleteAliasRecord(ref string) error {
request, err := c.CreateJSONRequest(http.MethodDelete, fmt.Sprintf("%s", ref), nil)
if err != nil {
return err
}
response := c.Call(request, nil)
if response != nil {
if response.StatusCode == 404 {
return nil
}
return err
}
return nil
}