mirror of
https://code.forgejo.org/actions/ovh-dns-update.git
synced 2024-11-24 22:00:59 -05:00
59 lines
1.5 KiB
Go
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)
|
|
}
|