sigs.k8s.io/external-dns@v0.14.1/provider/gandi/client.go (about)

     1  /*
     2  Copyright 2021 The Kubernetes Authors.
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6      http://www.apache.org/licenses/LICENSE-2.0
     7  Unless required by applicable law or agreed to in writing, software
     8  distributed under the License is distributed on an "AS IS" BASIS,
     9  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10  See the License for the specific language governing permissions and
    11  limitations under the License.
    12  */
    13  
    14  package gandi
    15  
    16  import (
    17  	"github.com/go-gandi/go-gandi/domain"
    18  	"github.com/go-gandi/go-gandi/livedns"
    19  )
    20  
    21  type DomainClientAdapter interface {
    22  	ListDomains() (domains []domain.ListResponse, err error)
    23  }
    24  
    25  type domainClient struct {
    26  	Client *domain.Domain
    27  }
    28  
    29  func (p *domainClient) ListDomains() (domains []domain.ListResponse, err error) {
    30  	return p.Client.ListDomains()
    31  }
    32  
    33  func NewDomainClient(client *domain.Domain) DomainClientAdapter {
    34  	return &domainClient{client}
    35  }
    36  
    37  // standardResponse copied from go-gandi/internal/gandi.go
    38  type standardResponse struct {
    39  	Code    int             `json:"code,omitempty"`
    40  	Message string          `json:"message,omitempty"`
    41  	UUID    string          `json:"uuid,omitempty"`
    42  	Object  string          `json:"object,omitempty"`
    43  	Cause   string          `json:"cause,omitempty"`
    44  	Status  string          `json:"status,omitempty"`
    45  	Errors  []standardError `json:"errors,omitempty"`
    46  }
    47  
    48  // standardError copied from go-gandi/internal/gandi.go
    49  type standardError struct {
    50  	Location    string `json:"location"`
    51  	Name        string `json:"name"`
    52  	Description string `json:"description"`
    53  }
    54  
    55  type LiveDNSClientAdapter interface {
    56  	GetDomainRecords(fqdn string) (records []livedns.DomainRecord, err error)
    57  	CreateDomainRecord(fqdn, name, recordtype string, ttl int, values []string) (response standardResponse, err error)
    58  	DeleteDomainRecord(fqdn, name, recordtype string) (err error)
    59  	UpdateDomainRecordByNameAndType(fqdn, name, recordtype string, ttl int, values []string) (response standardResponse, err error)
    60  }
    61  
    62  type LiveDNSClient struct {
    63  	Client *livedns.LiveDNS
    64  }
    65  
    66  func NewLiveDNSClient(client *livedns.LiveDNS) LiveDNSClientAdapter {
    67  	return &LiveDNSClient{client}
    68  }
    69  
    70  func (p *LiveDNSClient) GetDomainRecords(fqdn string) (records []livedns.DomainRecord, err error) {
    71  	return p.Client.GetDomainRecords(fqdn)
    72  }
    73  
    74  func (p *LiveDNSClient) CreateDomainRecord(fqdn, name, recordtype string, ttl int, values []string) (response standardResponse, err error) {
    75  	res, err := p.Client.CreateDomainRecord(fqdn, name, recordtype, ttl, values)
    76  	if err != nil {
    77  		return standardResponse{}, err
    78  	}
    79  
    80  	// response needs to be copied as the Standard* structs are internal
    81  	var errors []standardError
    82  	for _, e := range res.Errors {
    83  		errors = append(errors, standardError(e))
    84  	}
    85  	return standardResponse{
    86  		Code:    res.Code,
    87  		Message: res.Message,
    88  		UUID:    res.UUID,
    89  		Object:  res.Object,
    90  		Cause:   res.Cause,
    91  		Status:  res.Status,
    92  		Errors:  errors,
    93  	}, err
    94  }
    95  
    96  func (p *LiveDNSClient) DeleteDomainRecord(fqdn, name, recordtype string) (err error) {
    97  	return p.Client.DeleteDomainRecord(fqdn, name, recordtype)
    98  }
    99  
   100  func (p *LiveDNSClient) UpdateDomainRecordByNameAndType(fqdn, name, recordtype string, ttl int, values []string) (response standardResponse, err error) {
   101  	res, err := p.Client.UpdateDomainRecordByNameAndType(fqdn, name, recordtype, ttl, values)
   102  	if err != nil {
   103  		return standardResponse{}, err
   104  	}
   105  
   106  	// response needs to be copied as the Standard* structs are internal
   107  	var errors []standardError
   108  	for _, e := range res.Errors {
   109  		errors = append(errors, standardError(e))
   110  	}
   111  	return standardResponse{
   112  		Code:    res.Code,
   113  		Message: res.Message,
   114  		UUID:    res.UUID,
   115  		Object:  res.Object,
   116  		Cause:   res.Cause,
   117  		Status:  res.Status,
   118  		Errors:  errors,
   119  	}, err
   120  }