-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.go
173 lines (142 loc) · 4.41 KB
/
client.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Direct implementation of all required netucp DNS API endpoints
package netcup
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
// fixed netcup API URL, may be made variable later
const apiUrl = "https://ccp.netcup.net/run/webservice/servers/endpoint.php?JSON"
const loggingPrefixNetcup = "[netcup]"
// Executes a request to the netcup API with a given request value.
// Returns the response with raw response data, which needs to be unmarshalled depending on the request.
func (p *Provider) doRequest(ctx context.Context, req request) (*response, error) {
requestBody, err := json.Marshal(req)
if err != nil {
return nil, err
}
httpReq, err := http.NewRequestWithContext(ctx, "POST", apiUrl, bytes.NewReader(requestBody))
if err != nil {
return nil, err
}
httpResp, err := http.DefaultClient.Do(httpReq)
if err != nil {
return nil, err
}
defer httpResp.Body.Close()
responseBody, err := ioutil.ReadAll(httpResp.Body)
if err != nil {
return nil, err
}
var response response
if err = json.Unmarshal(responseBody, &response); err != nil {
return nil, err
}
if response.Status != "success" {
return nil, fmt.Errorf("%v %v: %v", loggingPrefixNetcup, response.ShortMessage, response.LongMessage)
}
// possibly useful with a variable to switch on debug logging
// fmt.Printf("%v %v: %v\n", loggingPrefixNetcup, response.ShortMessage, response.LongMessage)
return &response, nil
}
// login starts an API session that lasts for some minutes (see nectup API documentation).
// The session ID is returned, which is needed for all other requests.
func (p *Provider) login(ctx context.Context) (string, error) {
loginRequest := request{
Action: "login",
Param: requestParam{
CustomerNumber: p.CustomerNumber,
APIKey: p.APIKey,
APIPassword: p.APIPassword,
},
}
res, err := p.doRequest(ctx, loginRequest)
if err != nil {
return "", err
}
var asd apiSessionData
if err = json.Unmarshal(res.ResponseData, &asd); err != nil {
return "", err
}
return asd.APISessionId, nil
}
// Stops the session with the given session ID.
func (p *Provider) logout(ctx context.Context, apiSessionID string) {
logoutRequest := request{
Action: "logout",
Param: requestParam{
CustomerNumber: p.CustomerNumber,
APIKey: p.APIKey,
APISessionID: apiSessionID,
},
}
p.doRequest(ctx, logoutRequest)
}
// Provides information about the given zone, especially the TTL
func (p *Provider) infoDNSZone(ctx context.Context, zone string, apiSessionID string) (*dnsZone, error) {
infoDNSZoneRequest := request{
Action: "infoDnsZone",
Param: requestParam{
DomainName: zone,
CustomerNumber: p.CustomerNumber,
APIKey: p.APIKey,
APISessionID: apiSessionID,
},
}
res, err := p.doRequest(ctx, infoDNSZoneRequest)
if err != nil {
return nil, err
}
var dz dnsZone
if err = json.Unmarshal(res.ResponseData, &dz); err != nil {
return nil, err
}
return &dz, nil
}
// Returns a slice of all records found in the given zone.
func (p *Provider) infoDNSRecords(ctx context.Context, zone string, apiSessionID string) (*dnsRecordSet, error) {
infoDNSrecordsRequest := request{
Action: "infoDnsRecords",
Param: requestParam{
DomainName: zone,
CustomerNumber: p.CustomerNumber,
APIKey: p.APIKey,
APISessionID: apiSessionID,
},
}
res, err := p.doRequest(ctx, infoDNSrecordsRequest)
if err != nil {
return nil, err
}
var recordSet dnsRecordSet
if err = json.Unmarshal(res.ResponseData, &recordSet); err != nil {
return nil, err
}
return &recordSet, err
}
// Updates records in the given zone with the values in the dnsRecordSet. Records are appended when no ID is set and updated when
// an ID is set and it exists. Returns all records found in the zone (with the appends and updates applied).
func (p *Provider) updateDNSRecords(ctx context.Context, zone string, updateRecordSet dnsRecordSet, apiSessionID string) (*dnsRecordSet, error) {
updateDNSrecordsRequest := request{
Action: "updateDnsRecords",
Param: requestParam{
DomainName: zone,
CustomerNumber: p.CustomerNumber,
APIKey: p.APIKey,
APISessionID: apiSessionID,
DNSRecordSet: updateRecordSet,
},
}
res, err := p.doRequest(ctx, updateDNSrecordsRequest)
if err != nil {
return nil, err
}
var recordSet dnsRecordSet
if err = json.Unmarshal(res.ResponseData, &recordSet); err != nil {
return nil, err
}
return &recordSet, err
}