terraform-provider-woodpecker/internal/provider.go

146 lines
3.9 KiB
Go
Raw Normal View History

2023-08-25 05:41:13 +00:00
package internal
import (
"context"
"os"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/provider/schema"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/woodpecker-ci/woodpecker/woodpecker-go/woodpecker"
"golang.org/x/oauth2"
)
type woodpeckerProvider struct {
version string
}
2023-08-26 14:42:23 +00:00
var _ provider.Provider = (*woodpeckerProvider)(nil)
2023-08-25 05:41:13 +00:00
func NewProvider(version string) func() provider.Provider {
return func() provider.Provider {
return &woodpeckerProvider{
version: version,
}
}
}
func (p *woodpeckerProvider) Metadata(_ context.Context, _ provider.MetadataRequest, resp *provider.MetadataResponse) {
resp.TypeName = "woodpecker"
resp.Version = p.version
}
func (p *woodpeckerProvider) Schema(_ context.Context, _ provider.SchemaRequest, resp *provider.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "A Terraform provider used to interact with [Woodpecker CI](https://woodpecker-ci.org/) resources.",
Attributes: map[string]schema.Attribute{
"server": schema.StringAttribute{
Optional: true,
Description: `This is the target Woodpecker CI base API endpoint. It must be provided, but
can also be sourced from the WOODPECKER_SERVER environment
variable.`,
},
"token": schema.StringAttribute{
Optional: true,
Description: `A Woodpecker CI Personal Access Token. It must be provided, but
can also be sourced from the WOODPECKER_TOKEN environment
variable.`,
},
},
}
}
func (p *woodpeckerProvider) DataSources(_ context.Context) []func() datasource.DataSource {
2023-08-26 14:42:23 +00:00
return []func() datasource.DataSource{
newUserDataSource,
2023-08-28 06:38:02 +00:00
newSecretDataSource,
2023-08-26 14:42:23 +00:00
}
2023-08-25 05:41:13 +00:00
}
func (p *woodpeckerProvider) Resources(_ context.Context) []func() resource.Resource {
2023-08-27 06:17:27 +00:00
return []func() resource.Resource{
newUserResource,
2023-08-28 05:59:47 +00:00
newSecretResource,
2023-08-27 06:17:27 +00:00
}
2023-08-25 05:41:13 +00:00
}
func (p *woodpeckerProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
cfg := newProviderConfig(ctx, req, resp)
2023-08-25 05:41:13 +00:00
if resp.Diagnostics.HasError() {
return
}
2023-08-27 06:17:27 +00:00
client := newClient(ctx, cfg, resp)
2023-08-25 05:41:13 +00:00
2023-08-27 06:17:27 +00:00
resp.DataSourceData = client
resp.ResourceData = client
2023-08-25 05:41:13 +00:00
}
type providerConfig struct {
Server types.String `tfsdk:"server"`
Token types.String `tfsdk:"token"`
}
func newProviderConfig(
2023-08-25 05:41:13 +00:00
ctx context.Context,
req provider.ConfigureRequest,
resp *provider.ConfigureResponse,
) providerConfig {
var config providerConfig
resp.Diagnostics.Append(req.Config.Get(ctx, &config)...)
if resp.Diagnostics.HasError() {
return config
}
2023-08-26 14:42:23 +00:00
if config.Server.ValueString() == "" {
2023-08-25 05:41:13 +00:00
config.Server = types.StringValue(os.Getenv("WOODPECKER_SERVER"))
}
if config.Server.ValueString() == "" {
resp.Diagnostics.AddError(
"Missing Server URL Configuration",
"While configuring the provider, the server URL was not found in "+
"the WOODPECKER_SERVER environment variable or provider "+
"configuration block server attribute.",
)
}
2023-08-26 14:42:23 +00:00
if config.Token.ValueString() == "" {
2023-08-25 05:41:13 +00:00
config.Token = types.StringValue(os.Getenv("WOODPECKER_TOKEN"))
}
if config.Token.ValueString() == "" {
resp.Diagnostics.AddError(
"Missing API Token Configuration",
"While configuring the provider, the API token was not found in "+
"the WOODPECKER_TOKEN environment variable or provider "+
"configuration block token attribute.",
)
}
return config
}
func newClient(
2023-08-25 05:41:13 +00:00
ctx context.Context,
config providerConfig,
resp *provider.ConfigureResponse,
2023-08-26 14:42:23 +00:00
) woodpecker.Client {
2023-08-25 05:41:13 +00:00
client := woodpecker.NewClient(
config.Server.ValueString(),
(&oauth2.Config{}).Client(ctx, &oauth2.Token{
AccessToken: config.Token.ValueString(),
}),
)
2023-08-26 14:42:23 +00:00
_, err := client.Self()
2023-08-25 05:41:13 +00:00
if err != nil {
resp.Diagnostics.AddError("Unable to login", err.Error())
2023-08-26 14:42:23 +00:00
return nil
2023-08-25 05:41:13 +00:00
}
2023-08-26 14:42:23 +00:00
return client
2023-08-25 05:41:13 +00:00
}