Files
unkinben ad50a06b33 feat: initial terraform provider for artifactapi v0.0.1
Resources:
- artifactapi_remote: CRUD for remote proxy repositories
- artifactapi_virtual: CRUD for virtual (merged) repositories

Data sources:
- data.artifactapi_remote: read remote config
- data.artifactapi_virtual: read virtual config

Supports all 10 package types (generic, docker, helm, pypi, npm,
rpm, alpine, puppet, terraform, goproxy), allowlist/blocklist,
tag banning, quarantine, and terraform import.
2026-06-07 14:30:20 +10:00

81 lines
2.6 KiB
Go

package provider
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)
var _ datasource.DataSource = &virtualDataSource{}
type virtualDataSource struct {
client *apiClient
}
func NewVirtualDataSource() datasource.DataSource {
return &virtualDataSource{}
}
func (d *virtualDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_virtual"
}
func (d *virtualDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Read an existing ArtifactAPI virtual repository.",
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{Required: true, Description: "Virtual repository name."},
"package_type": schema.StringAttribute{Computed: true},
"description": schema.StringAttribute{Computed: true},
"members": schema.ListAttribute{Computed: true, ElementType: types.StringType},
"managed_by": schema.StringAttribute{Computed: true},
},
}
}
type virtualDataSourceModel struct {
Name types.String `tfsdk:"name"`
PackageType types.String `tfsdk:"package_type"`
Description types.String `tfsdk:"description"`
Members types.List `tfsdk:"members"`
ManagedBy types.String `tfsdk:"managed_by"`
}
func (d *virtualDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(*apiClient)
if !ok {
resp.Diagnostics.AddError("unexpected provider data type", fmt.Sprintf("got %T", req.ProviderData))
return
}
d.client = client
}
func (d *virtualDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var config virtualDataSourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &config)...)
if resp.Diagnostics.HasError() {
return
}
var virt virtualAPI
if err := d.client.get(ctx, "/api/v2/virtuals/"+config.Name.ValueString(), &virt); err != nil {
resp.Diagnostics.AddError("read virtual failed", err.Error())
return
}
state := virtualDataSourceModel{
Name: types.StringValue(virt.Name),
PackageType: types.StringValue(virt.PackageType),
Description: types.StringValue(virt.Description),
Members: stringsToList(ctx, virt.Members),
ManagedBy: types.StringValue(virt.ManagedBy),
}
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
}