aboutsummaryrefslogtreecommitdiff
path: root/provider/config.go
blob: 956cbef7648d8ef1a932bcb788f3565d223ca7af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package provider

import (
	"context"
	"fmt"

	"github.com/pulumi/pulumi-go-provider/infer"

	"git.sr.ht/~rbdr/pulumi-porkbun/internal/porkbun"
)

type Config struct {
	APIKey       string `pulumi:"apiKey,optional" provider:"secret"`
	SecretAPIKey string `pulumi:"secretApiKey,optional" provider:"secret"`
	BaseURL      string `pulumi:"baseUrl,optional"`

	client *porkbun.Client
}

func (c *Config) Annotate(a infer.Annotator) {
	a.Describe(&c, "Configuration for the Porkbun provider.")
	a.Describe(&c.APIKey, "The Porkbun API key. May also be set via the `PORKBUN_API_KEY` environment variable.")
	a.Describe(&c.SecretAPIKey, "The Porkbun secret API key. May also be set via the `PORKBUN_SECRET_API_KEY` environment variable.")
	a.Describe(&c.BaseURL, "The base URL of the Porkbun API. Provided for testing purpouses; defaults to the public endpoint.")
	a.SetDefault(&c.APIKey, "", "PORKBUN_API_KEY")
	a.SetDefault(&c.SecretAPIKey, "", "PORKBUN_SECRET_API_KEY")
	a.SetDefault(&c.BaseURL, porkbun.DefaultBaseURL)
}

func (c *Config) Configure(ctx context.Context) error {
	if c.APIKey == "" || c.SecretAPIKey == "" {
		return fmt.Errorf("porkbun API credentials are missing: set the porkbun:apiKey and porkbun:secretApiKey " +
			"config values (e.g. `pulumi config set porkbun:apiKey --secret`) or the PORKBUN_API_KEY and " +
			"PORKBUN_SECRET_API_KEY environment variables")
	}
	opts := []porkbun.Option{}
	if c.BaseURL != "" {
		opts = append(opts, porkbun.WithBaseURL(c.BaseURL))
	}
	c.client = porkbun.NewClient(c.APIKey, c.SecretAPIKey, opts...)
	return nil
}

func (c Config) Client() *porkbun.Client {
	return c.client
}