165 lines
4.9 KiB
Go
165 lines
4.9 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/hashicorp/terraform-plugin-framework/path"
|
|
"github.com/hashicorp/terraform-plugin-framework/resource"
|
|
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
|
|
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
|
|
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
|
|
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
|
|
"github.com/hashicorp/terraform-plugin-framework/types"
|
|
)
|
|
|
|
var (
|
|
_ resource.Resource = &localPyPIResource{}
|
|
_ resource.ResourceWithImportState = &localPyPIResource{}
|
|
)
|
|
|
|
type localPyPIResource struct {
|
|
client *apiClient
|
|
}
|
|
|
|
type localPyPIResourceModel struct {
|
|
Name types.String `tfsdk:"name"`
|
|
Description types.String `tfsdk:"description"`
|
|
}
|
|
|
|
func NewLocalPyPIResource() resource.Resource {
|
|
return &localPyPIResource{}
|
|
}
|
|
|
|
func (r *localPyPIResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
|
|
resp.TypeName = req.ProviderTypeName + "_local_pypi"
|
|
}
|
|
|
|
func (r *localPyPIResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
|
|
resp.Schema = schema.Schema{
|
|
Description: "Manages a local ArtifactAPI PyPI repository for hosting Python packages directly.",
|
|
Attributes: map[string]schema.Attribute{
|
|
"name": schema.StringAttribute{
|
|
Description: "Unique name of the local PyPI repository.",
|
|
Required: true,
|
|
PlanModifiers: []planmodifier.String{
|
|
stringplanmodifier.RequiresReplace(),
|
|
},
|
|
},
|
|
"description": schema.StringAttribute{
|
|
Description: "Human-readable description.",
|
|
Optional: true,
|
|
Computed: true,
|
|
Default: stringdefault.StaticString(""),
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func (r *localPyPIResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.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
|
|
}
|
|
r.client = client
|
|
}
|
|
|
|
func (r *localPyPIResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
|
|
var plan localPyPIResourceModel
|
|
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
api := localPyPIModelToAPI(plan)
|
|
api.ManagedBy = "terraform"
|
|
|
|
var created remoteAPI
|
|
if err := r.client.post(ctx, "/api/v2/remotes", api, &created); err != nil {
|
|
resp.Diagnostics.AddError("create local pypi failed", err.Error())
|
|
return
|
|
}
|
|
|
|
state := localPyPIAPIToModel(created)
|
|
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
|
|
}
|
|
|
|
func (r *localPyPIResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
|
|
var state localPyPIResourceModel
|
|
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
var remote remoteAPI
|
|
err := r.client.get(ctx, "/api/v2/remotes/"+state.Name.ValueString(), &remote)
|
|
if err != nil {
|
|
if isNotFound(err) {
|
|
resp.State.RemoveResource(ctx)
|
|
return
|
|
}
|
|
resp.Diagnostics.AddError("read local pypi failed", err.Error())
|
|
return
|
|
}
|
|
|
|
newState := localPyPIAPIToModel(remote)
|
|
resp.Diagnostics.Append(resp.State.Set(ctx, newState)...)
|
|
}
|
|
|
|
func (r *localPyPIResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
|
|
var plan localPyPIResourceModel
|
|
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
api := localPyPIModelToAPI(plan)
|
|
api.ManagedBy = "terraform"
|
|
|
|
var updated remoteAPI
|
|
if err := r.client.put(ctx, "/api/v2/remotes/"+plan.Name.ValueString(), api, &updated); err != nil {
|
|
resp.Diagnostics.AddError("update local pypi failed", err.Error())
|
|
return
|
|
}
|
|
|
|
state := localPyPIAPIToModel(updated)
|
|
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
|
|
}
|
|
|
|
func (r *localPyPIResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
|
|
var state localPyPIResourceModel
|
|
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
if err := r.client.del(ctx, "/api/v2/remotes/"+state.Name.ValueString()); err != nil {
|
|
resp.Diagnostics.AddError("delete local pypi failed", err.Error())
|
|
return
|
|
}
|
|
}
|
|
|
|
func (r *localPyPIResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
|
|
resource.ImportStatePassthroughID(ctx, path.Root("name"), req, resp)
|
|
}
|
|
|
|
func localPyPIModelToAPI(m localPyPIResourceModel) remoteAPI {
|
|
return remoteAPI{
|
|
Name: m.Name.ValueString(),
|
|
PackageType: "pypi",
|
|
RepoType: "local",
|
|
Description: m.Description.ValueString(),
|
|
}
|
|
}
|
|
|
|
func localPyPIAPIToModel(api remoteAPI) localPyPIResourceModel {
|
|
return localPyPIResourceModel{
|
|
Name: types.StringValue(api.Name),
|
|
Description: types.StringValue(api.Description),
|
|
}
|
|
}
|