add OVH provider

This commit is contained in:
DataHearth 2021-03-14 16:35:40 +01:00
parent 2b27941b8d
commit d3bf027eef
No known key found for this signature in database
GPG Key ID: E88FD356ACC5F3C4
3 changed files with 78 additions and 0 deletions

6
pkg/providers/main.go Normal file
View File

@ -0,0 +1,6 @@
package providers
// Provider is the default interface for all providers
type Provider interface {
UpdateIP(subdomain, ip string) error
}

60
pkg/providers/ovh/main.go Normal file
View File

@ -0,0 +1,60 @@
package ovh
import (
"net/http"
"strings"
"github.com/datahearth/ddnsclient/pkg/providers"
"github.com/datahearth/ddnsclient/pkg/utils"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
type ovh struct {
ovhConfig config
logger logrus.FieldLogger
}
// NewOVH returns a new instance of the OVH provider
func NewOVH(logger logrus.FieldLogger) (providers.Provider, error) {
var ovhConfig config
if c, ok := viper.GetStringMap("provider")["ovh"].(config); ok {
ovhConfig = c
} else {
return nil, ErrNilOvhConfig
}
if logger == nil {
return nil, utils.ErrNilLogger
}
return &ovh{
ovhConfig: ovhConfig,
logger: logger,
}, nil
}
func (ovh *ovh) UpdateIP(subdomain, ip string) error {
newURL := strings.ReplaceAll(ovh.ovhConfig["url"].(string), "SUBDOMAIN", subdomain)
newURL = strings.ReplaceAll(newURL, "NEWIP", ip)
// * create GET request
req, err := http.NewRequest("GET", newURL, nil)
if err != nil {
return err
}
req.SetBasicAuth(ovh.ovhConfig["username"].(string), ovh.ovhConfig["password"].(string))
// * perform GET request
c := new(http.Client)
resp, err := c.Do(req)
if err != nil {
return err
}
if resp.StatusCode != 200 {
return utils.ErrWrongStatusCode
}
return nil
}

View File

@ -0,0 +1,12 @@
package ovh
import "errors"
type (
config map[string]interface{}
)
var (
// ErrNilOvhConfig is thrown when OVH configuration is empty
ErrNilOvhConfig = errors.New("OVH config is mandatory")
)