Compare commits
17 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0561bae10e | |||
| 4dd290518d | |||
| 0b4db409b0 | |||
| 1bddc48e5a | |||
| 4dcb3ac71a | |||
| eeb4a60639 | |||
| 41ef83ba34 | |||
| 3d5154a12a | |||
| ec9f7d410f | |||
| 03b3344d8f | |||
| 2dfbd1d799 | |||
| ddd9e41679 | |||
| 2653c34f94 | |||
| e6d58ac2ee | |||
| 3d776f9e0f | |||
| d2da94cb52 | |||
| 6446997a12 |
@@ -1,5 +1,5 @@
|
||||
when:
|
||||
- event: [push, pull_request]
|
||||
- event: pull_request
|
||||
|
||||
steps:
|
||||
- name: build
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
when:
|
||||
- event: pull_request
|
||||
|
||||
steps:
|
||||
- name: pre-commit
|
||||
image: git.unkin.net/unkin/almalinux9-gobuilder:20260606
|
||||
commands:
|
||||
- uvx pre-commit run --all-files
|
||||
backend_options:
|
||||
kubernetes:
|
||||
serviceAccountName: default
|
||||
resources:
|
||||
requests:
|
||||
memory: 512Mi
|
||||
cpu: 1
|
||||
limits:
|
||||
memory: 2Gi
|
||||
cpu: 2
|
||||
@@ -0,0 +1,19 @@
|
||||
when:
|
||||
- event: tag
|
||||
|
||||
steps:
|
||||
- name: package
|
||||
image: git.unkin.net/unkin/almalinux9-gobuilder:20260606
|
||||
commands:
|
||||
- make package VERSION=${CI_COMMIT_TAG}
|
||||
|
||||
- name: upload
|
||||
image: git.unkin.net/unkin/almalinux9-base:20260606
|
||||
commands:
|
||||
- |
|
||||
VERSION=$$(echo ${CI_COMMIT_TAG} | sed 's/^v//')
|
||||
FILE="terraform-provider-artifactapi_$${VERSION}_linux_amd64.zip"
|
||||
curl -f -X PUT \
|
||||
"https://artifactapi3.k8s.syd1.au.unkin.net/api/v2/remotes/terraform-unkin/files/unkin/artifactapi/$${FILE}" \
|
||||
-H "Content-Type: application/zip" \
|
||||
--data-binary @"$${FILE}"
|
||||
@@ -1,5 +1,5 @@
|
||||
when:
|
||||
- event: [push, pull_request]
|
||||
- event: pull_request
|
||||
|
||||
steps:
|
||||
- name: lint
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
.PHONY: build install test lint fmt clean tidy patch minor major
|
||||
.PHONY: build install test lint fmt clean tidy patch minor major package
|
||||
|
||||
BINARY := terraform-provider-artifactapi
|
||||
VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo "0.0.0-dev")
|
||||
OS_ARCH := linux_amd64
|
||||
INSTALL_DIR := ~/.terraform.d/plugins/git.unkin.net/unkin/artifactapi/$(VERSION)/$(OS_ARCH)
|
||||
INSTALL_VERSION := $(shell echo $(VERSION) | sed 's/^v//')
|
||||
INSTALL_DIR := ~/.terraform.d/plugins/git.unkin.net/unkin/artifactapi/$(INSTALL_VERSION)/$(OS_ARCH)
|
||||
ZIP := $(BINARY)_$(INSTALL_VERSION)_$(OS_ARCH).zip
|
||||
|
||||
GO_VERSION_REQUIRED := 1.23
|
||||
GO_VERSION_ACTUAL := $(shell go version | sed 's/go version go\([0-9]*\.[0-9]*\).*/\1/')
|
||||
@@ -29,8 +31,13 @@ lint: check-go
|
||||
fmt: check-go
|
||||
gofmt -w .
|
||||
|
||||
package: build
|
||||
cp $(BINARY) $(BINARY)_v$(INSTALL_VERSION)
|
||||
python3 -c "import zipfile,sys; z=zipfile.ZipFile(sys.argv[1],'w',zipfile.ZIP_DEFLATED); z.write(sys.argv[2]); z.close()" $(ZIP) $(BINARY)_v$(INSTALL_VERSION)
|
||||
rm $(BINARY)_v$(INSTALL_VERSION)
|
||||
|
||||
clean:
|
||||
rm -f $(BINARY)
|
||||
rm -f $(BINARY) *.zip
|
||||
|
||||
tidy:
|
||||
go mod tidy
|
||||
|
||||
@@ -82,6 +82,9 @@ Available resource types:
|
||||
| `quarantine_enabled` | No | `false` | Enable quarantine for new artifacts |
|
||||
| `quarantine_days` | No | `3` | Days to quarantine new artifacts |
|
||||
| `stale_on_error` | No | `true` | Serve stale cache when upstream is unreachable |
|
||||
| `upstream_dial_timeout` | No | `0` | Upstream TCP connect timeout in seconds (0 = server default) |
|
||||
| `upstream_tls_timeout` | No | `0` | Upstream TLS handshake timeout in seconds (0 = server default) |
|
||||
| `upstream_response_header_timeout` | No | `0` | Upstream response-header timeout in seconds (0 = server default) |
|
||||
|
||||
#### Docker-specific Attributes
|
||||
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
resource "artifactapi_local_pypi" "internal" {
|
||||
name = "pypi-internal"
|
||||
description = "Internal PyPI package registry"
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
resource "artifactapi_local_rpm" "internal" {
|
||||
name = "rpm-internal"
|
||||
description = "Internal RPM package repository"
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
resource "artifactapi_local_terraform" "internal" {
|
||||
name = "tf-internal"
|
||||
description = "Internal terraform provider registry"
|
||||
}
|
||||
@@ -45,6 +45,10 @@ func (d *remoteDataSource) Schema(_ context.Context, _ datasource.SchemaRequest,
|
||||
"stale_on_error": schema.BoolAttribute{Computed: true},
|
||||
"releases_remote": schema.StringAttribute{Computed: true},
|
||||
"managed_by": schema.StringAttribute{Computed: true},
|
||||
|
||||
"upstream_dial_timeout": schema.Int64Attribute{Computed: true},
|
||||
"upstream_tls_timeout": schema.Int64Attribute{Computed: true},
|
||||
"upstream_response_header_timeout": schema.Int64Attribute{Computed: true},
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -68,6 +72,10 @@ type remoteDataSourceModel struct {
|
||||
StaleOnError types.Bool `tfsdk:"stale_on_error"`
|
||||
ReleasesRemote types.String `tfsdk:"releases_remote"`
|
||||
ManagedBy types.String `tfsdk:"managed_by"`
|
||||
|
||||
UpstreamDialTimeout types.Int64 `tfsdk:"upstream_dial_timeout"`
|
||||
UpstreamTLSTimeout types.Int64 `tfsdk:"upstream_tls_timeout"`
|
||||
UpstreamResponseHeaderTimeout types.Int64 `tfsdk:"upstream_response_header_timeout"`
|
||||
}
|
||||
|
||||
func (d *remoteDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
|
||||
@@ -114,6 +122,10 @@ func (d *remoteDataSource) Read(ctx context.Context, req datasource.ReadRequest,
|
||||
StaleOnError: types.BoolValue(remote.StaleOnError),
|
||||
ReleasesRemote: types.StringValue(remote.ReleasesRemote),
|
||||
ManagedBy: types.StringValue(remote.ManagedBy),
|
||||
|
||||
UpstreamDialTimeout: types.Int64Value(remote.UpstreamDialTimeout),
|
||||
UpstreamTLSTimeout: types.Int64Value(remote.UpstreamTLSTimeout),
|
||||
UpstreamResponseHeaderTimeout: types.Int64Value(remote.UpstreamResponseHeaderTimeout),
|
||||
}
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package provider
|
||||
type remoteAPI struct {
|
||||
Name string `json:"name"`
|
||||
PackageType string `json:"package_type"`
|
||||
RepoType string `json:"repo_type,omitempty"`
|
||||
BaseURL string `json:"base_url"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Username string `json:"username,omitempty"`
|
||||
@@ -21,6 +22,10 @@ type remoteAPI struct {
|
||||
StaleOnError bool `json:"stale_on_error"`
|
||||
ReleasesRemote string `json:"releases_remote,omitempty"`
|
||||
ManagedBy string `json:"managed_by,omitempty"`
|
||||
|
||||
UpstreamDialTimeout int64 `json:"upstream_dial_timeout"`
|
||||
UpstreamTLSTimeout int64 `json:"upstream_tls_timeout"`
|
||||
UpstreamResponseHeaderTimeout int64 `json:"upstream_response_header_timeout"`
|
||||
}
|
||||
|
||||
type virtualAPI struct {
|
||||
|
||||
@@ -68,6 +68,9 @@ func (p *ArtifactAPIProvider) Resources(_ context.Context) []func() resource.Res
|
||||
newRemoteResource("terraform"),
|
||||
newRemoteResource("goproxy"),
|
||||
NewVirtualResource,
|
||||
NewLocalTerraformResource,
|
||||
NewLocalPyPIResource,
|
||||
NewLocalRPMResource,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -66,8 +66,8 @@ func TestProvider_Resources(t *testing.T) {
|
||||
p := &ArtifactAPIProvider{version: "1.0.0"}
|
||||
resources := p.Resources(context.Background())
|
||||
|
||||
// 10 remote resource types + 1 virtual = 11
|
||||
expectedCount := 11
|
||||
// 10 remote resource types + 1 virtual + 1 local_terraform + 1 local_pypi + 1 local_rpm = 14
|
||||
expectedCount := 14
|
||||
if len(resources) != expectedCount {
|
||||
t.Fatalf("expected %d resources, got %d", expectedCount, len(resources))
|
||||
}
|
||||
@@ -107,6 +107,9 @@ func TestProvider_Resources_ContainsExpectedTypes(t *testing.T) {
|
||||
"artifactapi_remote_terraform",
|
||||
"artifactapi_remote_goproxy",
|
||||
"artifactapi_virtual",
|
||||
"artifactapi_local_terraform",
|
||||
"artifactapi_local_pypi",
|
||||
"artifactapi_local_rpm",
|
||||
}
|
||||
|
||||
for _, name := range expected {
|
||||
|
||||
@@ -0,0 +1,164 @@
|
||||
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),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
)
|
||||
|
||||
func TestLocalPyPIModelToAPI(t *testing.T) {
|
||||
model := localPyPIResourceModel{
|
||||
Name: types.StringValue("pypi-internal"),
|
||||
Description: types.StringValue("Internal PyPI registry"),
|
||||
}
|
||||
|
||||
api := localPyPIModelToAPI(model)
|
||||
|
||||
if api.Name != "pypi-internal" {
|
||||
t.Errorf("Name: expected pypi-internal, got %s", api.Name)
|
||||
}
|
||||
if api.PackageType != "pypi" {
|
||||
t.Errorf("PackageType: expected pypi, got %s", api.PackageType)
|
||||
}
|
||||
if api.RepoType != "local" {
|
||||
t.Errorf("RepoType: expected local, got %s", api.RepoType)
|
||||
}
|
||||
if api.Description != "Internal PyPI registry" {
|
||||
t.Errorf("Description: expected 'Internal PyPI registry', got %s", api.Description)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocalPyPIModelToAPI_EmptyDescription(t *testing.T) {
|
||||
model := localPyPIResourceModel{
|
||||
Name: types.StringValue("pypi-empty"),
|
||||
Description: types.StringValue(""),
|
||||
}
|
||||
|
||||
api := localPyPIModelToAPI(model)
|
||||
|
||||
if api.Name != "pypi-empty" {
|
||||
t.Errorf("Name: expected pypi-empty, got %s", api.Name)
|
||||
}
|
||||
if api.Description != "" {
|
||||
t.Errorf("Description: expected empty string, got %s", api.Description)
|
||||
}
|
||||
if api.PackageType != "pypi" {
|
||||
t.Errorf("PackageType: expected pypi, got %s", api.PackageType)
|
||||
}
|
||||
if api.RepoType != "local" {
|
||||
t.Errorf("RepoType: expected local, got %s", api.RepoType)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocalPyPIAPIToModel(t *testing.T) {
|
||||
api := remoteAPI{
|
||||
Name: "pypi-internal",
|
||||
PackageType: "pypi",
|
||||
RepoType: "local",
|
||||
Description: "Internal PyPI registry",
|
||||
ManagedBy: "terraform",
|
||||
}
|
||||
|
||||
model := localPyPIAPIToModel(api)
|
||||
|
||||
if model.Name.ValueString() != "pypi-internal" {
|
||||
t.Errorf("Name: expected pypi-internal, got %s", model.Name.ValueString())
|
||||
}
|
||||
if model.Description.ValueString() != "Internal PyPI registry" {
|
||||
t.Errorf("Description: expected 'Internal PyPI registry', got %s", model.Description.ValueString())
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocalPyPIRoundTrip(t *testing.T) {
|
||||
original := localPyPIResourceModel{
|
||||
Name: types.StringValue("roundtrip-pypi"),
|
||||
Description: types.StringValue("Round trip test"),
|
||||
}
|
||||
|
||||
api := localPyPIModelToAPI(original)
|
||||
result := localPyPIAPIToModel(api)
|
||||
|
||||
if result.Name.ValueString() != original.Name.ValueString() {
|
||||
t.Errorf("Name: expected %s, got %s", original.Name.ValueString(), result.Name.ValueString())
|
||||
}
|
||||
if result.Description.ValueString() != original.Description.ValueString() {
|
||||
t.Errorf("Description: expected %s, got %s", original.Description.ValueString(), result.Description.ValueString())
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocalPyPIResource_Metadata(t *testing.T) {
|
||||
r := NewLocalPyPIResource()
|
||||
req := resource.MetadataRequest{ProviderTypeName: "artifactapi"}
|
||||
var resp resource.MetadataResponse
|
||||
r.Metadata(context.Background(), req, &resp)
|
||||
if resp.TypeName != "artifactapi_local_pypi" {
|
||||
t.Errorf("expected artifactapi_local_pypi, got %s", resp.TypeName)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocalPyPIResource_Schema(t *testing.T) {
|
||||
r := NewLocalPyPIResource()
|
||||
req := resource.SchemaRequest{}
|
||||
var resp resource.SchemaResponse
|
||||
r.Schema(context.Background(), req, &resp)
|
||||
|
||||
expectedAttrs := []string{"name", "description"}
|
||||
for _, attr := range expectedAttrs {
|
||||
if _, ok := resp.Schema.Attributes[attr]; !ok {
|
||||
t.Errorf("missing expected attribute: %s", attr)
|
||||
}
|
||||
}
|
||||
|
||||
if len(resp.Schema.Attributes) != len(expectedAttrs) {
|
||||
t.Errorf("expected %d attributes, got %d", len(expectedAttrs), len(resp.Schema.Attributes))
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewLocalPyPIResource_Type(t *testing.T) {
|
||||
r := NewLocalPyPIResource()
|
||||
_, ok := r.(*localPyPIResource)
|
||||
if !ok {
|
||||
t.Error("expected *localPyPIResource")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
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 = &localRPMResource{}
|
||||
_ resource.ResourceWithImportState = &localRPMResource{}
|
||||
)
|
||||
|
||||
type localRPMResource struct {
|
||||
client *apiClient
|
||||
}
|
||||
|
||||
type localRPMResourceModel struct {
|
||||
Name types.String `tfsdk:"name"`
|
||||
Description types.String `tfsdk:"description"`
|
||||
}
|
||||
|
||||
func NewLocalRPMResource() resource.Resource {
|
||||
return &localRPMResource{}
|
||||
}
|
||||
|
||||
func (r *localRPMResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
|
||||
resp.TypeName = req.ProviderTypeName + "_local_rpm"
|
||||
}
|
||||
|
||||
func (r *localRPMResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
|
||||
resp.Schema = schema.Schema{
|
||||
Description: "Manages a local ArtifactAPI RPM repository for hosting RPM packages directly.",
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"name": schema.StringAttribute{
|
||||
Description: "Unique name of the local RPM repository.",
|
||||
Required: true,
|
||||
PlanModifiers: []planmodifier.String{
|
||||
stringplanmodifier.RequiresReplace(),
|
||||
},
|
||||
},
|
||||
"description": schema.StringAttribute{
|
||||
Description: "Human-readable description.",
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
Default: stringdefault.StaticString(""),
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (r *localRPMResource) 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 *localRPMResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
|
||||
var plan localRPMResourceModel
|
||||
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
api := localRPMModelToAPI(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 rpm failed", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
state := localRPMAPIToModel(created)
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
|
||||
}
|
||||
|
||||
func (r *localRPMResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
|
||||
var state localRPMResourceModel
|
||||
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 rpm failed", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
newState := localRPMAPIToModel(remote)
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, newState)...)
|
||||
}
|
||||
|
||||
func (r *localRPMResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
|
||||
var plan localRPMResourceModel
|
||||
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
api := localRPMModelToAPI(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 rpm failed", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
state := localRPMAPIToModel(updated)
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
|
||||
}
|
||||
|
||||
func (r *localRPMResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
|
||||
var state localRPMResourceModel
|
||||
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 rpm failed", err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (r *localRPMResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
|
||||
resource.ImportStatePassthroughID(ctx, path.Root("name"), req, resp)
|
||||
}
|
||||
|
||||
func localRPMModelToAPI(m localRPMResourceModel) remoteAPI {
|
||||
return remoteAPI{
|
||||
Name: m.Name.ValueString(),
|
||||
PackageType: "rpm",
|
||||
RepoType: "local",
|
||||
Description: m.Description.ValueString(),
|
||||
}
|
||||
}
|
||||
|
||||
func localRPMAPIToModel(api remoteAPI) localRPMResourceModel {
|
||||
return localRPMResourceModel{
|
||||
Name: types.StringValue(api.Name),
|
||||
Description: types.StringValue(api.Description),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
)
|
||||
|
||||
func TestLocalRPMModelToAPI(t *testing.T) {
|
||||
model := localRPMResourceModel{
|
||||
Name: types.StringValue("rpm-internal"),
|
||||
Description: types.StringValue("Internal RPM repository"),
|
||||
}
|
||||
|
||||
api := localRPMModelToAPI(model)
|
||||
|
||||
if api.Name != "rpm-internal" {
|
||||
t.Errorf("Name: expected rpm-internal, got %s", api.Name)
|
||||
}
|
||||
if api.PackageType != "rpm" {
|
||||
t.Errorf("PackageType: expected rpm, got %s", api.PackageType)
|
||||
}
|
||||
if api.RepoType != "local" {
|
||||
t.Errorf("RepoType: expected local, got %s", api.RepoType)
|
||||
}
|
||||
if api.Description != "Internal RPM repository" {
|
||||
t.Errorf("Description: expected 'Internal RPM repository', got %s", api.Description)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocalRPMModelToAPI_EmptyDescription(t *testing.T) {
|
||||
model := localRPMResourceModel{
|
||||
Name: types.StringValue("rpm-empty"),
|
||||
Description: types.StringValue(""),
|
||||
}
|
||||
|
||||
api := localRPMModelToAPI(model)
|
||||
|
||||
if api.Name != "rpm-empty" {
|
||||
t.Errorf("Name: expected rpm-empty, got %s", api.Name)
|
||||
}
|
||||
if api.Description != "" {
|
||||
t.Errorf("Description: expected empty string, got %s", api.Description)
|
||||
}
|
||||
if api.PackageType != "rpm" {
|
||||
t.Errorf("PackageType: expected rpm, got %s", api.PackageType)
|
||||
}
|
||||
if api.RepoType != "local" {
|
||||
t.Errorf("RepoType: expected local, got %s", api.RepoType)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocalRPMAPIToModel(t *testing.T) {
|
||||
api := remoteAPI{
|
||||
Name: "rpm-internal",
|
||||
PackageType: "rpm",
|
||||
RepoType: "local",
|
||||
Description: "Internal RPM repository",
|
||||
ManagedBy: "terraform",
|
||||
}
|
||||
|
||||
model := localRPMAPIToModel(api)
|
||||
|
||||
if model.Name.ValueString() != "rpm-internal" {
|
||||
t.Errorf("Name: expected rpm-internal, got %s", model.Name.ValueString())
|
||||
}
|
||||
if model.Description.ValueString() != "Internal RPM repository" {
|
||||
t.Errorf("Description: expected 'Internal RPM repository', got %s", model.Description.ValueString())
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocalRPMRoundTrip(t *testing.T) {
|
||||
original := localRPMResourceModel{
|
||||
Name: types.StringValue("roundtrip-rpm"),
|
||||
Description: types.StringValue("Round trip test"),
|
||||
}
|
||||
|
||||
api := localRPMModelToAPI(original)
|
||||
result := localRPMAPIToModel(api)
|
||||
|
||||
if result.Name.ValueString() != original.Name.ValueString() {
|
||||
t.Errorf("Name: expected %s, got %s", original.Name.ValueString(), result.Name.ValueString())
|
||||
}
|
||||
if result.Description.ValueString() != original.Description.ValueString() {
|
||||
t.Errorf("Description: expected %s, got %s", original.Description.ValueString(), result.Description.ValueString())
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocalRPMResource_Metadata(t *testing.T) {
|
||||
r := NewLocalRPMResource()
|
||||
req := resource.MetadataRequest{ProviderTypeName: "artifactapi"}
|
||||
var resp resource.MetadataResponse
|
||||
r.Metadata(context.Background(), req, &resp)
|
||||
if resp.TypeName != "artifactapi_local_rpm" {
|
||||
t.Errorf("expected artifactapi_local_rpm, got %s", resp.TypeName)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocalRPMResource_Schema(t *testing.T) {
|
||||
r := NewLocalRPMResource()
|
||||
req := resource.SchemaRequest{}
|
||||
var resp resource.SchemaResponse
|
||||
r.Schema(context.Background(), req, &resp)
|
||||
|
||||
expectedAttrs := []string{"name", "description"}
|
||||
for _, attr := range expectedAttrs {
|
||||
if _, ok := resp.Schema.Attributes[attr]; !ok {
|
||||
t.Errorf("missing expected attribute: %s", attr)
|
||||
}
|
||||
}
|
||||
|
||||
if len(resp.Schema.Attributes) != len(expectedAttrs) {
|
||||
t.Errorf("expected %d attributes, got %d", len(expectedAttrs), len(resp.Schema.Attributes))
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewLocalRPMResource_Type(t *testing.T) {
|
||||
r := NewLocalRPMResource()
|
||||
_, ok := r.(*localRPMResource)
|
||||
if !ok {
|
||||
t.Error("expected *localRPMResource")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
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 = &localTerraformResource{}
|
||||
_ resource.ResourceWithImportState = &localTerraformResource{}
|
||||
)
|
||||
|
||||
type localTerraformResource struct {
|
||||
client *apiClient
|
||||
}
|
||||
|
||||
type localTerraformResourceModel struct {
|
||||
Name types.String `tfsdk:"name"`
|
||||
Description types.String `tfsdk:"description"`
|
||||
}
|
||||
|
||||
func NewLocalTerraformResource() resource.Resource {
|
||||
return &localTerraformResource{}
|
||||
}
|
||||
|
||||
func (r *localTerraformResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
|
||||
resp.TypeName = req.ProviderTypeName + "_local_terraform"
|
||||
}
|
||||
|
||||
func (r *localTerraformResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
|
||||
resp.Schema = schema.Schema{
|
||||
Description: "Manages a local ArtifactAPI terraform registry for hosting providers directly.",
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"name": schema.StringAttribute{
|
||||
Description: "Unique name of the local terraform repository.",
|
||||
Required: true,
|
||||
PlanModifiers: []planmodifier.String{
|
||||
stringplanmodifier.RequiresReplace(),
|
||||
},
|
||||
},
|
||||
"description": schema.StringAttribute{
|
||||
Description: "Human-readable description.",
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
Default: stringdefault.StaticString(""),
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (r *localTerraformResource) 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 *localTerraformResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
|
||||
var plan localTerraformResourceModel
|
||||
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
api := localTerraformModelToAPI(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 terraform failed", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
state := localTerraformAPIToModel(created)
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
|
||||
}
|
||||
|
||||
func (r *localTerraformResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
|
||||
var state localTerraformResourceModel
|
||||
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 terraform failed", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
newState := localTerraformAPIToModel(remote)
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, newState)...)
|
||||
}
|
||||
|
||||
func (r *localTerraformResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
|
||||
var plan localTerraformResourceModel
|
||||
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
api := localTerraformModelToAPI(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 terraform failed", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
state := localTerraformAPIToModel(updated)
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
|
||||
}
|
||||
|
||||
func (r *localTerraformResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
|
||||
var state localTerraformResourceModel
|
||||
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 terraform failed", err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (r *localTerraformResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
|
||||
resource.ImportStatePassthroughID(ctx, path.Root("name"), req, resp)
|
||||
}
|
||||
|
||||
func localTerraformModelToAPI(m localTerraformResourceModel) remoteAPI {
|
||||
return remoteAPI{
|
||||
Name: m.Name.ValueString(),
|
||||
PackageType: "terraform",
|
||||
RepoType: "local",
|
||||
Description: m.Description.ValueString(),
|
||||
}
|
||||
}
|
||||
|
||||
func localTerraformAPIToModel(api remoteAPI) localTerraformResourceModel {
|
||||
return localTerraformResourceModel{
|
||||
Name: types.StringValue(api.Name),
|
||||
Description: types.StringValue(api.Description),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
)
|
||||
|
||||
func TestLocalTerraformModelToAPI(t *testing.T) {
|
||||
model := localTerraformResourceModel{
|
||||
Name: types.StringValue("tf-internal"),
|
||||
Description: types.StringValue("Internal terraform registry"),
|
||||
}
|
||||
|
||||
api := localTerraformModelToAPI(model)
|
||||
|
||||
if api.Name != "tf-internal" {
|
||||
t.Errorf("Name: expected tf-internal, got %s", api.Name)
|
||||
}
|
||||
if api.PackageType != "terraform" {
|
||||
t.Errorf("PackageType: expected terraform, got %s", api.PackageType)
|
||||
}
|
||||
if api.RepoType != "local" {
|
||||
t.Errorf("RepoType: expected local, got %s", api.RepoType)
|
||||
}
|
||||
if api.Description != "Internal terraform registry" {
|
||||
t.Errorf("Description: expected 'Internal terraform registry', got %s", api.Description)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocalTerraformModelToAPI_EmptyDescription(t *testing.T) {
|
||||
model := localTerraformResourceModel{
|
||||
Name: types.StringValue("tf-empty"),
|
||||
Description: types.StringValue(""),
|
||||
}
|
||||
|
||||
api := localTerraformModelToAPI(model)
|
||||
|
||||
if api.Name != "tf-empty" {
|
||||
t.Errorf("Name: expected tf-empty, got %s", api.Name)
|
||||
}
|
||||
if api.Description != "" {
|
||||
t.Errorf("Description: expected empty string, got %s", api.Description)
|
||||
}
|
||||
if api.PackageType != "terraform" {
|
||||
t.Errorf("PackageType: expected terraform, got %s", api.PackageType)
|
||||
}
|
||||
if api.RepoType != "local" {
|
||||
t.Errorf("RepoType: expected local, got %s", api.RepoType)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocalTerraformAPIToModel(t *testing.T) {
|
||||
api := remoteAPI{
|
||||
Name: "tf-internal",
|
||||
PackageType: "terraform",
|
||||
RepoType: "local",
|
||||
Description: "Internal terraform registry",
|
||||
ManagedBy: "terraform",
|
||||
}
|
||||
|
||||
model := localTerraformAPIToModel(api)
|
||||
|
||||
if model.Name.ValueString() != "tf-internal" {
|
||||
t.Errorf("Name: expected tf-internal, got %s", model.Name.ValueString())
|
||||
}
|
||||
if model.Description.ValueString() != "Internal terraform registry" {
|
||||
t.Errorf("Description: expected 'Internal terraform registry', got %s", model.Description.ValueString())
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocalTerraformRoundTrip(t *testing.T) {
|
||||
original := localTerraformResourceModel{
|
||||
Name: types.StringValue("roundtrip-local"),
|
||||
Description: types.StringValue("Round trip test"),
|
||||
}
|
||||
|
||||
api := localTerraformModelToAPI(original)
|
||||
result := localTerraformAPIToModel(api)
|
||||
|
||||
if result.Name.ValueString() != original.Name.ValueString() {
|
||||
t.Errorf("Name: expected %s, got %s", original.Name.ValueString(), result.Name.ValueString())
|
||||
}
|
||||
if result.Description.ValueString() != original.Description.ValueString() {
|
||||
t.Errorf("Description: expected %s, got %s", original.Description.ValueString(), result.Description.ValueString())
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocalTerraformResource_Metadata(t *testing.T) {
|
||||
r := NewLocalTerraformResource()
|
||||
req := resource.MetadataRequest{ProviderTypeName: "artifactapi"}
|
||||
var resp resource.MetadataResponse
|
||||
r.Metadata(context.Background(), req, &resp)
|
||||
if resp.TypeName != "artifactapi_local_terraform" {
|
||||
t.Errorf("expected artifactapi_local_terraform, got %s", resp.TypeName)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocalTerraformResource_Schema(t *testing.T) {
|
||||
r := NewLocalTerraformResource()
|
||||
req := resource.SchemaRequest{}
|
||||
var resp resource.SchemaResponse
|
||||
r.Schema(context.Background(), req, &resp)
|
||||
|
||||
expectedAttrs := []string{"name", "description"}
|
||||
for _, attr := range expectedAttrs {
|
||||
if _, ok := resp.Schema.Attributes[attr]; !ok {
|
||||
t.Errorf("missing expected attribute: %s", attr)
|
||||
}
|
||||
}
|
||||
|
||||
if len(resp.Schema.Attributes) != len(expectedAttrs) {
|
||||
t.Errorf("expected %d attributes, got %d", len(expectedAttrs), len(resp.Schema.Attributes))
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewLocalTerraformResource_Type(t *testing.T) {
|
||||
r := NewLocalTerraformResource()
|
||||
_, ok := r.(*localTerraformResource)
|
||||
if !ok {
|
||||
t.Error("expected *localTerraformResource")
|
||||
}
|
||||
}
|
||||
@@ -44,6 +44,10 @@ type remoteResourceModel struct {
|
||||
QuarantineDays types.Int64 `tfsdk:"quarantine_days"`
|
||||
StaleOnError types.Bool `tfsdk:"stale_on_error"`
|
||||
ReleasesRemote types.String `tfsdk:"releases_remote"`
|
||||
|
||||
UpstreamDialTimeout types.Int64 `tfsdk:"upstream_dial_timeout"`
|
||||
UpstreamTLSTimeout types.Int64 `tfsdk:"upstream_tls_timeout"`
|
||||
UpstreamResponseHeaderTimeout types.Int64 `tfsdk:"upstream_response_header_timeout"`
|
||||
}
|
||||
|
||||
func newRemoteResource(packageType string) func() resource.Resource {
|
||||
@@ -144,6 +148,18 @@ func (r *remoteResource) Schema(_ context.Context, _ resource.SchemaRequest, res
|
||||
Description: "Name of the CDN remote for download URL rewriting (terraform only).",
|
||||
Optional: true, Computed: true, Default: stringdefault.StaticString(""),
|
||||
},
|
||||
"upstream_dial_timeout": schema.Int64Attribute{
|
||||
Description: "Upstream TCP connect timeout in seconds (0 = server default).",
|
||||
Optional: true, Computed: true, Default: int64default.StaticInt64(0),
|
||||
},
|
||||
"upstream_tls_timeout": schema.Int64Attribute{
|
||||
Description: "Upstream TLS handshake timeout in seconds (0 = server default).",
|
||||
Optional: true, Computed: true, Default: int64default.StaticInt64(0),
|
||||
},
|
||||
"upstream_response_header_timeout": schema.Int64Attribute{
|
||||
Description: "Upstream response-header timeout in seconds (0 = server default).",
|
||||
Optional: true, Computed: true, Default: int64default.StaticInt64(0),
|
||||
},
|
||||
}
|
||||
|
||||
resp.Schema = schema.Schema{
|
||||
@@ -268,6 +284,10 @@ func (r *remoteResource) modelToAPI(ctx context.Context, m remoteResourceModel)
|
||||
QuarantineEnabled: m.QuarantineEnabled.ValueBool(),
|
||||
QuarantineDays: m.QuarantineDays.ValueInt64(),
|
||||
StaleOnError: m.StaleOnError.ValueBool(),
|
||||
|
||||
UpstreamDialTimeout: m.UpstreamDialTimeout.ValueInt64(),
|
||||
UpstreamTLSTimeout: m.UpstreamTLSTimeout.ValueInt64(),
|
||||
UpstreamResponseHeaderTimeout: m.UpstreamResponseHeaderTimeout.ValueInt64(),
|
||||
}
|
||||
api.Patterns = listToStrings(ctx, m.Patterns)
|
||||
api.Blocklist = listToStrings(ctx, m.Blocklist)
|
||||
@@ -299,6 +319,10 @@ func (r *remoteResource) apiToModel(ctx context.Context, api remoteAPI) remoteRe
|
||||
BanTagsEnabled: types.BoolValue(api.BanTagsEnabled),
|
||||
BanTags: stringsToList(ctx, api.BanTags),
|
||||
ReleasesRemote: types.StringValue(api.ReleasesRemote),
|
||||
|
||||
UpstreamDialTimeout: types.Int64Value(api.UpstreamDialTimeout),
|
||||
UpstreamTLSTimeout: types.Int64Value(api.UpstreamTLSTimeout),
|
||||
UpstreamResponseHeaderTimeout: types.Int64Value(api.UpstreamResponseHeaderTimeout),
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
@@ -31,10 +31,18 @@ func TestModelToAPI_FullFields(t *testing.T) {
|
||||
QuarantineDays: types.Int64Value(7),
|
||||
StaleOnError: types.BoolValue(false),
|
||||
ReleasesRemote: types.StringValue("cdn-remote"),
|
||||
|
||||
UpstreamDialTimeout: types.Int64Value(3),
|
||||
UpstreamTLSTimeout: types.Int64Value(4),
|
||||
UpstreamResponseHeaderTimeout: types.Int64Value(5),
|
||||
}
|
||||
|
||||
api := r.modelToAPI(ctx, model)
|
||||
|
||||
if api.UpstreamDialTimeout != 3 || api.UpstreamTLSTimeout != 4 || api.UpstreamResponseHeaderTimeout != 5 {
|
||||
t.Errorf("upstream timeouts: got %d/%d/%d, want 3/4/5",
|
||||
api.UpstreamDialTimeout, api.UpstreamTLSTimeout, api.UpstreamResponseHeaderTimeout)
|
||||
}
|
||||
if api.Name != "my-remote" {
|
||||
t.Errorf("Name: expected my-remote, got %s", api.Name)
|
||||
}
|
||||
@@ -211,10 +219,22 @@ func TestAPIToModel_FullFields(t *testing.T) {
|
||||
StaleOnError: false,
|
||||
ReleasesRemote: "cdn-remote",
|
||||
ManagedBy: "terraform",
|
||||
|
||||
UpstreamDialTimeout: 3,
|
||||
UpstreamTLSTimeout: 4,
|
||||
UpstreamResponseHeaderTimeout: 5,
|
||||
}
|
||||
|
||||
model := r.apiToModel(ctx, api)
|
||||
|
||||
if model.UpstreamDialTimeout.ValueInt64() != 3 ||
|
||||
model.UpstreamTLSTimeout.ValueInt64() != 4 ||
|
||||
model.UpstreamResponseHeaderTimeout.ValueInt64() != 5 {
|
||||
t.Errorf("upstream timeouts: got %d/%d/%d, want 3/4/5",
|
||||
model.UpstreamDialTimeout.ValueInt64(),
|
||||
model.UpstreamTLSTimeout.ValueInt64(),
|
||||
model.UpstreamResponseHeaderTimeout.ValueInt64())
|
||||
}
|
||||
if model.Name.ValueString() != "my-remote" {
|
||||
t.Errorf("Name: expected my-remote, got %s", model.Name.ValueString())
|
||||
}
|
||||
@@ -324,12 +344,24 @@ func TestModelToAPI_RoundTrip(t *testing.T) {
|
||||
QuarantineDays: 3,
|
||||
StaleOnError: true,
|
||||
ReleasesRemote: "",
|
||||
|
||||
UpstreamDialTimeout: 5,
|
||||
UpstreamTLSTimeout: 0,
|
||||
UpstreamResponseHeaderTimeout: 45,
|
||||
}
|
||||
|
||||
// API -> Model -> API round-trip
|
||||
model := r.apiToModel(ctx, original)
|
||||
result := r.modelToAPI(ctx, model)
|
||||
|
||||
if result.UpstreamDialTimeout != original.UpstreamDialTimeout ||
|
||||
result.UpstreamTLSTimeout != original.UpstreamTLSTimeout ||
|
||||
result.UpstreamResponseHeaderTimeout != original.UpstreamResponseHeaderTimeout {
|
||||
t.Errorf("upstream timeouts round-trip: got %d/%d/%d, want %d/%d/%d",
|
||||
result.UpstreamDialTimeout, result.UpstreamTLSTimeout, result.UpstreamResponseHeaderTimeout,
|
||||
original.UpstreamDialTimeout, original.UpstreamTLSTimeout, original.UpstreamResponseHeaderTimeout)
|
||||
}
|
||||
|
||||
if result.Name != original.Name {
|
||||
t.Errorf("Name: expected %s, got %s", original.Name, result.Name)
|
||||
}
|
||||
@@ -405,6 +437,7 @@ func TestRemoteResource_Schema(t *testing.T) {
|
||||
"ban_tags_enabled", "ban_tags",
|
||||
"quarantine_enabled", "quarantine_days",
|
||||
"stale_on_error", "releases_remote",
|
||||
"upstream_dial_timeout", "upstream_tls_timeout", "upstream_response_header_timeout",
|
||||
}
|
||||
|
||||
for _, attr := range expectedAttrs {
|
||||
|
||||
Reference in New Issue
Block a user