Return peer's FQDN via API (#567)

Added a temp method to retrieve the dns domain
This commit is contained in:
Maycon Santos 2022-11-21 11:14:42 +01:00 committed by GitHub
parent 9cb66bdb5d
commit d63a9ce4a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 5 deletions

View File

@ -91,6 +91,7 @@ type AccountManager interface {
UpdateNameServerGroup(accountID, nsGroupID string, operations []NameServerGroupUpdateOperation) (*nbdns.NameServerGroup, error)
DeleteNameServerGroup(accountID, nsGroupID string) error
ListNameServerGroups(accountID string) ([]*nbdns.NameServerGroup, error)
GetDNSDomain() string
}
type DefaultAccountManager struct {
@ -901,6 +902,11 @@ func (am *DefaultAccountManager) AccountExists(accountID string) (*bool, error)
return &res, nil
}
// GetDNSDomain returns the configured dnsDomain
func (am *DefaultAccountManager) GetDNSDomain() string {
return am.dnsDomain
}
// addAllGroup to account object if it doesn't exists
func addAllGroup(account *Account) {
if len(account.Groups) == 0 {

View File

@ -41,7 +41,8 @@ func (h *Peers) updatePeer(account *server.Account, peer *server.Peer, w http.Re
util.WriteError(err, w)
return
}
util.WriteJSONObject(w, toPeerResponse(peer, account))
dnsDomain := h.accountManager.GetDNSDomain()
util.WriteJSONObject(w, toPeerResponse(peer, account, dnsDomain))
}
func (h *Peers) deletePeer(accountId string, peer *server.Peer, w http.ResponseWriter, r *http.Request) {
@ -73,6 +74,8 @@ func (h *Peers) HandlePeer(w http.ResponseWriter, r *http.Request) {
return
}
dnsDomain := h.accountManager.GetDNSDomain()
switch r.Method {
case http.MethodDelete:
h.deletePeer(account.Id, peer, w, r)
@ -81,7 +84,7 @@ func (h *Peers) HandlePeer(w http.ResponseWriter, r *http.Request) {
h.updatePeer(account, peer, w, r)
return
case http.MethodGet:
util.WriteJSONObject(w, toPeerResponse(peer, account))
util.WriteJSONObject(w, toPeerResponse(peer, account, dnsDomain))
return
default:
@ -106,9 +109,11 @@ func (h *Peers) GetPeers(w http.ResponseWriter, r *http.Request) {
return
}
dnsDomain := h.accountManager.GetDNSDomain()
respBody := []*api.Peer{}
for _, peer := range peers {
respBody = append(respBody, toPeerResponse(peer, account))
respBody = append(respBody, toPeerResponse(peer, account, dnsDomain))
}
util.WriteJSONObject(w, respBody)
return
@ -117,7 +122,7 @@ func (h *Peers) GetPeers(w http.ResponseWriter, r *http.Request) {
}
}
func toPeerResponse(peer *server.Peer, account *server.Account) *api.Peer {
func toPeerResponse(peer *server.Peer, account *server.Account, dnsDomain string) *api.Peer {
var groupsInfo []api.GroupMinimum
groupsChecked := make(map[string]struct{})
for _, group := range account.Groups {
@ -138,6 +143,10 @@ func toPeerResponse(peer *server.Peer, account *server.Account) *api.Peer {
}
}
}
fqdn := peer.DNSLabel
if dnsDomain != "" {
fqdn = peer.DNSLabel + "." + dnsDomain
}
return &api.Peer{
Id: peer.IP.String(),
Name: peer.Name,
@ -151,6 +160,6 @@ func toPeerResponse(peer *server.Peer, account *server.Account) *api.Peer {
Hostname: peer.Meta.Hostname,
UserId: &peer.UserID,
UiVersion: &peer.Meta.UIVersion,
DnsLabel: peer.DNSLabel,
DnsLabel: fqdn,
}
}

View File

@ -60,6 +60,7 @@ type MockAccountManager struct {
ListNameServerGroupsFunc func(accountID string) ([]*nbdns.NameServerGroup, error)
CreateUserFunc func(accountID string, key *server.UserInfo) (*server.UserInfo, error)
GetAccountFromTokenFunc func(claims jwtclaims.AuthorizationClaims) (*server.Account, *server.User, error)
GetDNSDomainFunc func() string
}
// GetUsersFromAccount mock implementation of GetUsersFromAccount from server.AccountManager interface
@ -477,3 +478,11 @@ func (am *MockAccountManager) GetPeers(accountID, userID string) ([]*server.Peer
}
return nil, status.Errorf(codes.Unimplemented, "method GetPeersFunc is not implemented")
}
// GetDNSDomain mocks GetDNSDomain of the AccountManager interface
func (am *MockAccountManager) GetDNSDomain() string {
if am.GetDNSDomainFunc != nil {
return am.GetDNSDomainFunc()
}
return ""
}