e1336c0c87
Resources renamed from artifactapi_remote to per-type: - artifactapi_remote_generic - artifactapi_remote_docker (with ban_tags) - artifactapi_remote_helm - artifactapi_remote_pypi - artifactapi_remote_npm - artifactapi_remote_rpm - artifactapi_remote_alpine - artifactapi_remote_puppet - artifactapi_remote_terraform (with releases_remote) - artifactapi_remote_goproxy Classification simplified: - patterns: paths to proxy (empty = all, acts as allowlist) - blocklist: paths to deny (checked first) - mutable_patterns: override provider auto-classification - immutable_patterns: override provider auto-classification - Provider handles mutability automatically per package type
120 lines
5.1 KiB
Go
120 lines
5.1 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 = &remoteDataSource{}
|
|
|
|
type remoteDataSource struct {
|
|
client *apiClient
|
|
}
|
|
|
|
func NewRemoteDataSource() datasource.DataSource {
|
|
return &remoteDataSource{}
|
|
}
|
|
|
|
func (d *remoteDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
|
|
resp.TypeName = req.ProviderTypeName + "_remote"
|
|
}
|
|
|
|
func (d *remoteDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
|
|
resp.Schema = schema.Schema{
|
|
Description: "Read an existing ArtifactAPI remote.",
|
|
Attributes: map[string]schema.Attribute{
|
|
"name": schema.StringAttribute{Required: true},
|
|
"package_type": schema.StringAttribute{Computed: true},
|
|
"base_url": schema.StringAttribute{Computed: true},
|
|
"description": schema.StringAttribute{Computed: true},
|
|
"immutable_ttl": schema.Int64Attribute{Computed: true},
|
|
"mutable_ttl": schema.Int64Attribute{Computed: true},
|
|
"check_mutable": schema.BoolAttribute{Computed: true},
|
|
"patterns": schema.ListAttribute{Computed: true, ElementType: types.StringType},
|
|
"blocklist": schema.ListAttribute{Computed: true, ElementType: types.StringType},
|
|
"mutable_patterns": schema.ListAttribute{Computed: true, ElementType: types.StringType},
|
|
"immutable_patterns": schema.ListAttribute{Computed: true, ElementType: types.StringType},
|
|
"ban_tags_enabled": schema.BoolAttribute{Computed: true},
|
|
"ban_tags": schema.ListAttribute{Computed: true, ElementType: types.StringType},
|
|
"quarantine_enabled": schema.BoolAttribute{Computed: true},
|
|
"quarantine_days": schema.Int64Attribute{Computed: true},
|
|
"stale_on_error": schema.BoolAttribute{Computed: true},
|
|
"releases_remote": schema.StringAttribute{Computed: true},
|
|
"managed_by": schema.StringAttribute{Computed: true},
|
|
},
|
|
}
|
|
}
|
|
|
|
type remoteDataSourceModel struct {
|
|
Name types.String `tfsdk:"name"`
|
|
PackageType types.String `tfsdk:"package_type"`
|
|
BaseURL types.String `tfsdk:"base_url"`
|
|
Description types.String `tfsdk:"description"`
|
|
ImmutableTTL types.Int64 `tfsdk:"immutable_ttl"`
|
|
MutableTTL types.Int64 `tfsdk:"mutable_ttl"`
|
|
CheckMutable types.Bool `tfsdk:"check_mutable"`
|
|
Patterns types.List `tfsdk:"patterns"`
|
|
Blocklist types.List `tfsdk:"blocklist"`
|
|
MutablePatterns types.List `tfsdk:"mutable_patterns"`
|
|
ImmutablePatterns types.List `tfsdk:"immutable_patterns"`
|
|
BanTagsEnabled types.Bool `tfsdk:"ban_tags_enabled"`
|
|
BanTags types.List `tfsdk:"ban_tags"`
|
|
QuarantineEnabled types.Bool `tfsdk:"quarantine_enabled"`
|
|
QuarantineDays types.Int64 `tfsdk:"quarantine_days"`
|
|
StaleOnError types.Bool `tfsdk:"stale_on_error"`
|
|
ReleasesRemote types.String `tfsdk:"releases_remote"`
|
|
ManagedBy types.String `tfsdk:"managed_by"`
|
|
}
|
|
|
|
func (d *remoteDataSource) 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 *remoteDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
|
|
var config remoteDataSourceModel
|
|
resp.Diagnostics.Append(req.Config.Get(ctx, &config)...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
var remote remoteAPI
|
|
if err := d.client.get(ctx, "/api/v2/remotes/"+config.Name.ValueString(), &remote); err != nil {
|
|
resp.Diagnostics.AddError("read remote failed", err.Error())
|
|
return
|
|
}
|
|
|
|
state := remoteDataSourceModel{
|
|
Name: types.StringValue(remote.Name),
|
|
PackageType: types.StringValue(remote.PackageType),
|
|
BaseURL: types.StringValue(remote.BaseURL),
|
|
Description: types.StringValue(remote.Description),
|
|
ImmutableTTL: types.Int64Value(remote.ImmutableTTL),
|
|
MutableTTL: types.Int64Value(remote.MutableTTL),
|
|
CheckMutable: types.BoolValue(remote.CheckMutable),
|
|
Patterns: stringsToList(ctx, remote.Patterns),
|
|
Blocklist: stringsToList(ctx, remote.Blocklist),
|
|
MutablePatterns: stringsToList(ctx, remote.MutablePatterns),
|
|
ImmutablePatterns: stringsToList(ctx, remote.ImmutablePatterns),
|
|
BanTagsEnabled: types.BoolValue(remote.BanTagsEnabled),
|
|
BanTags: stringsToList(ctx, remote.BanTags),
|
|
QuarantineEnabled: types.BoolValue(remote.QuarantineEnabled),
|
|
QuarantineDays: types.Int64Value(remote.QuarantineDays),
|
|
StaleOnError: types.BoolValue(remote.StaleOnError),
|
|
ReleasesRemote: types.StringValue(remote.ReleasesRemote),
|
|
ManagedBy: types.StringValue(remote.ManagedBy),
|
|
}
|
|
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
|
|
}
|