nameserver.go raw

   1  package goinwx
   2  
   3  import (
   4  	"errors"
   5  	"fmt"
   6  	"time"
   7  
   8  	"github.com/fatih/structs"
   9  	"github.com/go-viper/mapstructure/v2"
  10  )
  11  
  12  const (
  13  	methodNameserverCheck        = "nameserver.check"
  14  	methodNameserverCreate       = "nameserver.create"
  15  	methodNameserverCreateRecord = "nameserver.createRecord"
  16  	methodNameserverDelete       = "nameserver.delete"
  17  	methodNameserverDeleteRecord = "nameserver.deleteRecord"
  18  	methodNameserverInfo         = "nameserver.info"
  19  	methodNameserverList         = "nameserver.list"
  20  	methodNameserverUpdate       = "nameserver.update"
  21  	methodNameserverUpdateRecord = "nameserver.updateRecord"
  22  )
  23  
  24  // NameserverService API access to Nameservers.
  25  type NameserverService service
  26  
  27  // Check checks a domain on nameservers.
  28  func (s *NameserverService) Check(domain string, nameservers []string) (*NameserverCheckResponse, error) {
  29  	req := s.client.NewRequest(methodNameserverCheck, map[string]any{
  30  		"domain": domain,
  31  		"ns":     nameservers,
  32  	})
  33  
  34  	resp, err := s.client.Do(req)
  35  	if err != nil {
  36  		return nil, err
  37  	}
  38  
  39  	var result NameserverCheckResponse
  40  
  41  	err = mapstructure.Decode(resp, &result)
  42  	if err != nil {
  43  		return nil, err
  44  	}
  45  
  46  	return &result, nil
  47  }
  48  
  49  // Info gets information.
  50  func (s *NameserverService) Info(request *NameserverInfoRequest) (*NameserverInfoResponse, error) {
  51  	req := s.client.NewRequest(methodNameserverInfo, structs.Map(request))
  52  
  53  	resp, err := s.client.Do(req)
  54  	if err != nil {
  55  		return nil, err
  56  	}
  57  
  58  	result := NameserverInfoResponse{}
  59  
  60  	err = mapstructure.Decode(resp, &result)
  61  	if err != nil {
  62  		return nil, err
  63  	}
  64  
  65  	return &result, nil
  66  }
  67  
  68  // List lists nameservers for a domain.
  69  //
  70  // Deprecated: use ListWithParams instead.
  71  func (s *NameserverService) List(domain string) (*NameserverListResponse, error) {
  72  	requestMap := map[string]any{
  73  		"domain": "*",
  74  		"wide":   2,
  75  	}
  76  
  77  	if domain != "" {
  78  		requestMap["domain"] = domain
  79  	}
  80  
  81  	req := s.client.NewRequest(methodNameserverList, requestMap)
  82  
  83  	resp, err := s.client.Do(req)
  84  	if err != nil {
  85  		return nil, err
  86  	}
  87  
  88  	result := NameserverListResponse{}
  89  
  90  	err = mapstructure.Decode(resp, &result)
  91  	if err != nil {
  92  		return nil, err
  93  	}
  94  
  95  	return &result, nil
  96  }
  97  
  98  // ListWithParams lists nameservers for a domain.
  99  func (s *NameserverService) ListWithParams(request *NameserverListRequest) (*NameserverListResponse, error) {
 100  	if request == nil {
 101  		return nil, errors.New("request can't be nil")
 102  	}
 103  
 104  	requestMap := structs.Map(request)
 105  	requestMap["wide"] = "2"
 106  
 107  	req := s.client.NewRequest(methodNameserverList, requestMap)
 108  
 109  	resp, err := s.client.Do(req)
 110  	if err != nil {
 111  		return nil, err
 112  	}
 113  
 114  	result := NameserverListResponse{}
 115  
 116  	err = mapstructure.Decode(resp, &result)
 117  	if err != nil {
 118  		return nil, err
 119  	}
 120  
 121  	return &result, nil
 122  }
 123  
 124  // Create creates a nameserver.
 125  func (s *NameserverService) Create(request *NameserverCreateRequest) (int, error) {
 126  	req := s.client.NewRequest(methodNameserverCreate, structs.Map(request))
 127  
 128  	resp, err := s.client.Do(req)
 129  	if err != nil {
 130  		return 0, err
 131  	}
 132  
 133  	var result map[string]int
 134  
 135  	err = mapstructure.Decode(resp, &result)
 136  	if err != nil {
 137  		return 0, err
 138  	}
 139  
 140  	return result["roId"], nil
 141  }
 142  
 143  // CreateRecord creates a DNS record.
 144  func (s *NameserverService) CreateRecord(request *NameserverRecordRequest) (string, error) {
 145  	req := s.client.NewRequest(methodNameserverCreateRecord, structs.Map(request))
 146  
 147  	resp, err := s.client.Do(req)
 148  	if err != nil {
 149  		return "", err
 150  	}
 151  
 152  	var result map[string]string
 153  
 154  	err = mapstructure.Decode(resp, &result)
 155  	if err != nil {
 156  		return "", err
 157  	}
 158  
 159  	return result["id"], nil
 160  }
 161  
 162  // UpdateRecord updates a DNS record.
 163  func (s *NameserverService) UpdateRecord(recID string, request *NameserverRecordRequest) error {
 164  	if request == nil {
 165  		return errors.New("request can't be nil")
 166  	}
 167  
 168  	requestMap := structs.Map(request)
 169  	requestMap["id"] = recID
 170  
 171  	req := s.client.NewRequest(methodNameserverUpdateRecord, requestMap)
 172  
 173  	_, err := s.client.Do(req)
 174  	if err != nil {
 175  		return err
 176  	}
 177  
 178  	return nil
 179  }
 180  
 181  // DeleteRecord deletes a DNS record.
 182  func (s *NameserverService) DeleteRecord(recID string) error {
 183  	req := s.client.NewRequest(methodNameserverDeleteRecord, map[string]any{
 184  		"id": recID,
 185  	})
 186  
 187  	_, err := s.client.Do(req)
 188  	if err != nil {
 189  		return err
 190  	}
 191  
 192  	return nil
 193  }
 194  
 195  // FindRecordByID search a DNS record by ID.
 196  func (s *NameserverService) FindRecordByID(recID string) (*NameserverRecord, *NameserverDomain, error) {
 197  	listResp, err := s.client.Nameservers.ListWithParams(&NameserverListRequest{})
 198  	if err != nil {
 199  		return nil, nil, err
 200  	}
 201  
 202  	for _, domainItem := range listResp.Domains {
 203  		resp, err := s.client.Nameservers.Info(&NameserverInfoRequest{RoID: domainItem.RoID})
 204  		if err != nil {
 205  			return nil, nil, err
 206  		}
 207  
 208  		for _, record := range resp.Records {
 209  			if record.ID == recID {
 210  				return &record, &domainItem, nil
 211  			}
 212  		}
 213  	}
 214  
 215  	return nil, nil, fmt.Errorf("couldn't find INWX Record for id %s", recID)
 216  }
 217  
 218  // NameserverCheckResponse API model.
 219  type NameserverCheckResponse struct {
 220  	Details []string `mapstructure:"details"`
 221  	Status  string   `mapstructure:"status"`
 222  }
 223  
 224  // NameserverRecordRequest API model.
 225  type NameserverRecordRequest struct {
 226  	RoID                   int    `structs:"roId,omitempty"`
 227  	Domain                 string `structs:"domain,omitempty"`
 228  	Type                   string `structs:"type"`
 229  	Content                string `structs:"content"`
 230  	Name                   string `structs:"name,omitempty"`
 231  	TTL                    int    `structs:"ttl,omitempty"`
 232  	Priority               int    `structs:"prio,omitempty"`
 233  	URLAppend              bool   `structs:"urlAppend,omitempty"`
 234  	URLRedirectType        string `structs:"urlRedirectType,omitempty"`
 235  	URLRedirectTitle       string `structs:"urlRedirectTitle,omitempty"`
 236  	URLRedirectDescription string `structs:"urlRedirectDescription,omitempty"`
 237  	URLRedirectFavIcon     string `structs:"urlRedirectFavIcon,omitempty"`
 238  	URLRedirectKeywords    string `structs:"urlRedirectKeywords,omitempty"`
 239  }
 240  
 241  // NameserverCreateRequest API model.
 242  type NameserverCreateRequest struct {
 243  	Domain                 string   `structs:"domain"`
 244  	Type                   string   `structs:"type"`
 245  	Nameservers            []string `structs:"ns,omitempty"`
 246  	MasterIP               string   `structs:"masterIp,omitempty"`
 247  	Web                    string   `structs:"web,omitempty"`
 248  	Mail                   string   `structs:"mail,omitempty"`
 249  	SoaEmail               string   `structs:"soaEmail,omitempty"`
 250  	URLRedirectType        string   `structs:"urlRedirectType,omitempty"`
 251  	URLRedirectTitle       string   `structs:"urlRedirectTitle,omitempty"`
 252  	URLRedirectDescription string   `structs:"urlRedirectDescription,omitempty"`
 253  	URLRedirectFavIcon     string   `structs:"urlRedirectFavIcon,omitempty"`
 254  	URLRedirectKeywords    string   `structs:"urlRedirectKeywords,omitempty"`
 255  	Testing                bool     `structs:"testing,omitempty"`
 256  }
 257  
 258  // NameserverInfoRequest API model.
 259  type NameserverInfoRequest struct {
 260  	Domain   string `structs:"domain,omitempty"`
 261  	RoID     int    `structs:"roId,omitempty"`
 262  	RecordID string `structs:"recordId,omitempty"`
 263  	Type     string `structs:"type,omitempty"`
 264  	Name     string `structs:"name,omitempty"`
 265  	Content  string `structs:"content,omitempty"`
 266  	TTL      int    `structs:"ttl,omitempty"`
 267  	Priority int    `structs:"prio,omitempty"`
 268  }
 269  
 270  // NamserverInfoResponse API model.
 271  //
 272  // Deprecated: Use NameserverInfoResponse instead.
 273  type NamserverInfoResponse = NameserverInfoResponse
 274  
 275  // NameserverInfoResponse API model.
 276  type NameserverInfoResponse struct {
 277  	RoID          int                `mapstructure:"roId"`
 278  	Domain        string             `mapstructure:"domain"`
 279  	Type          string             `mapstructure:"type"`
 280  	MasterIP      string             `mapstructure:"masterIp"`
 281  	LastZoneCheck time.Time          `mapstructure:"lastZoneCheck"`
 282  	SlaveDNS      []SlaveInfo        `mapstructure:"slaveDns"`
 283  	SOASerial     string             `mapstructure:"SOAserial"`
 284  	Count         int                `mapstructure:"count"`
 285  	Records       []NameserverRecord `mapstructure:"record"`
 286  }
 287  
 288  // SlaveInfo API model.
 289  type SlaveInfo struct {
 290  	Name string `mapstructure:"name"`
 291  	IP   string `mapstructure:"ip"`
 292  }
 293  
 294  // NameserverRecord API model.
 295  type NameserverRecord struct {
 296  	ID                     string `mapstructure:"id"`
 297  	Name                   string `mapstructure:"name"`
 298  	Type                   string `mapstructure:"type"`
 299  	Content                string `mapstructure:"content"`
 300  	TTL                    int    `mapstructure:"TTL"`
 301  	Priority               int    `mapstructure:"prio"`
 302  	URLAppend              bool   `mapstructure:"urlAppend,omitempty"`
 303  	URLRedirectType        string `mapstructure:"urlRedirectType"`
 304  	URLRedirectTitle       string `mapstructure:"urlRedirectTitle"`
 305  	URLRedirectDescription string `mapstructure:"urlRedirectDescription"`
 306  	URLRedirectKeywords    string `mapstructure:"urlRedirectKeywords"`
 307  	URLRedirectFavIcon     string `mapstructure:"urlRedirectFavIcon"`
 308  }
 309  
 310  // NameserverListRequest API model.
 311  type NameserverListRequest struct {
 312  	Domain    string `structs:"domain,omitempty"`
 313  	Wide      int    `structs:"wide,omitempty"`
 314  	Page      int    `structs:"page,omitempty"`
 315  	PageLimit int    `structs:"pagelimit,omitempty"`
 316  }
 317  
 318  // NamserverListResponse API model.
 319  //
 320  // Deprecated: Use NameserverListResponse instead.
 321  type NamserverListResponse = NameserverListResponse
 322  
 323  // NameserverListResponse API model.
 324  type NameserverListResponse struct {
 325  	Count   int                `mapstructure:"count"`
 326  	Domains []NameserverDomain `mapstructure:"domains"`
 327  }
 328  
 329  // NameserverDomain API model.
 330  type NameserverDomain struct {
 331  	RoID     int    `mapstructure:"roId"`
 332  	Domain   string `mapstructure:"domain"`
 333  	Type     string `mapstructure:"type"`
 334  	MasterIP string `mapstructure:"masterIp"`
 335  	Mail     string `mapstructure:"mail"`
 336  	Web      string `mapstructure:"web"`
 337  	URL      string `mapstructure:"url"`
 338  	Ipv4     string `mapstructure:"ipv4"`
 339  	Ipv6     string `mapstructure:"ipv6"`
 340  }
 341