Files
libdns-plesk/provider.go
2025-01-07 12:19:11 +10:00

94 lines
3.0 KiB
Go

package plesk
import (
"context"
"errors"
"net/http"
"strings"
"time"
"github.com/libdns/libdns"
)
// Provider implements the libdns interfaces for Plesk.
type Provider struct {
// BaseURL is the root of your Plesk API, e.g. "https://my-plesk-server:8443/api/v2"
BaseURL string `json:"base_url,omitempty"`
// Authentication: could be a secret token or user/pass
// For example, if using "API secret keys" in Plesk:
SecretToken string `json:"secret_token,omitempty"`
// If you need additional fields or custom config, include them here
}
// These ensure Provider satisfies the libdns interfaces you need.
var (
_ libdns.RecordGetter = &Provider{}
_ libdns.RecordAppender = &Provider{}
_ libdns.RecordDeleter = &Provider{}
// If you need updating, also do: _ libdns.RecordSetter = &Provider{}
)
// internal helper: make a request to Plesk
func (p *Provider) doRequest(ctx context.Context, method, path string, body []byte) ([]byte, error) {
url := strings.TrimSuffix(p.BaseURL, "/") + "/" + strings.TrimPrefix(path, "/")
req, err := http.NewRequestWithContext(ctx, method, url, nil)
if err != nil {
return nil, err
}
// If there's a body (for POST/PUT requests), attach it
if body != nil {
req.Body = http.NoBody // replace with actual body if needed
// This is just a placeholder
}
// Authorization: Bearer <token>
req.Header.Set("Authorization", "Bearer "+p.SecretToken)
// Possibly set content-type to JSON or XML depending on your Plesk API
// req.Header.Set("Content-Type", "application/json") // or "application/xml"
client := &http.Client{Timeout: 10 * time.Second}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// read resp.Body
// ...
// if resp.StatusCode != 200, handle error
// parse JSON or XML accordingly
// placeholder
return nil, errors.New("doRequest not fully implemented")
}
// GetRecords lists DNS records for the given zone in Plesk
func (p *Provider) GetRecords(ctx context.Context, zone string) ([]libdns.Record, error) {
// Typically you'd do something like:
// GET /dns/<zone> or /zones/<zone>/records
// parse JSON/XML
// transform into []libdns.Record
// ...
return []libdns.Record{}, nil
}
// AppendRecords creates the given records in the specified DNS zone.
func (p *Provider) AppendRecords(ctx context.Context, zone string, recs []libdns.Record) ([]libdns.Record, error) {
// For each record, make a request to Plesk to create the DNS entry
// e.g. POST /dns/<zone> with JSON/XML specifying a record type, name, content
// Then parse the result. Return the created records in libdns format.
return recs, nil
}
// DeleteRecords removes the given records from the specified DNS zone.
func (p *Provider) DeleteRecords(ctx context.Context, zone string, recs []libdns.Record) ([]libdns.Record, error) {
// For each record, figure out the record ID or how Plesk identifies it
// Then DELETE /dns/<zone>/<recordID> (or some Plesk-specific path)
// Return records that were successfully removed
return recs, nil
}