ovh-dns-update/main.go
2023-08-18 11:40:26 +02:00

59 lines
1.5 KiB
Go

// SPDX-FileCopyrightText: 2023 Olivier Charvin
// SPDX-License-Identifier: CC0-1.0
package main
import (
"errors"
"fmt"
"strconv"
"github.com/ovh/go-ovh/ovh"
"github.com/sethvargo/go-githubactions"
)
func main() {
if err := run(githubactions.New()); err != nil {
githubactions.Fatalf("%v", err)
}
}
func run(action *githubactions.Action) error {
var inputErr error
getRequiredInput := func(name string) string {
v := action.GetInput(name)
if v == "" {
inputErr = errors.Join(inputErr, fmt.Errorf("input %q is required", name))
}
return v
}
subDomain := getRequiredInput("subdomain")
zoneName := getRequiredInput("domain")
recordID := getRequiredInput("record-id")
value := getRequiredInput("value")
if inputErr != nil {
return fmt.Errorf("Missing required inputs:\n%v", inputErr)
}
uri := fmt.Sprintf("/domain/zone/%s/record/%s", zoneName, recordID)
endpoint := getRequiredInput("ovh-endpoint")
ak := getRequiredInput("ovh-app-key")
as := getRequiredInput("ovh-app-secret")
ck := getRequiredInput("ovh-consumer-key")
if inputErr != nil {
return fmt.Errorf("Missing required OVH API credentials. Visit https://www.ovh.com/auth/api/createToken?PUT=%s to generate them:\n%v", uri, inputErr)
}
client, err := ovh.NewClient(endpoint, ak, as, ck)
if err != nil {
return err
}
return client.Put(uri, struct {
SubDomain string `json:"subDomain"`
Target string `json:"target"`
}{
SubDomain: subDomain,
Target: strconv.Quote(value),
}, nil)
}