Compare commits

...

23 Commits

Author SHA1 Message Date
benvin b8c18690ef Merge pull request 'Add artifactapi_remote_github_deb resource' (#15) from benvin/deb-github-metadataonly into main
ci/woodpecker/tag/release Pipeline was successful
Reviewed-on: #15
2026-08-11 23:52:45 +10:00
unkin-agent 25b72fd1ac Add artifactapi_remote_github_deb resource
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
Add the metadata-only Debian/apt analog of artifactapi_remote_github_rpm,
exposing a GitHub repo's release .deb assets as a synthesized apt repository
with downloads redirected to a backing releases_remote.

- Register newRemoteResource("github_deb") and add NewRemoteGitHubDeb ctor
- Bump provider resource count 19 -> 20 and add the type name to test lists
- Add modelToAPI/metadata/constructor test coverage for github_deb
- Document github_deb in README and add an examples/ main.tf (goodtune/ghp)
2026-08-11 23:43:31 +10:00
benvin 5ee3a1d3d6 Merge pull request 'Add artifactapi_local_deb and artifactapi_remote_deb resources' (#14) from benvin/deb-local-remote into main
Reviewed-on: #14
2026-08-11 23:41:33 +10:00
unkin-agent b21892217a Add artifactapi_local_deb and artifactapi_remote_deb resources
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
2026-08-11 21:50:20 +10:00
benvin 2d8584f103 Merge pull request 'feat: add artifactapi_remote_github_rpm resource' (#13) from benvin/github-rpm-remote into main
ci/woodpecker/tag/release Pipeline was successful
Reviewed-on: #13
2026-08-10 21:08:50 +10:00
unkinben b26e651d45 feat: add artifactapi_remote_github_rpm resource
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
The artifactapi server gains a github_rpm remote type that exposes a GitHub
repo's releases as a metadata-only yum repository (synthesized repodata, no
precache, downloads redirected to a backend remote). Surface it through the
provider so these remotes are declarable as code alongside the other types.

- Register the github_rpm remote resource (artifactapi_remote_github_rpm),
  reusing the shared remoteResource plumbing; add the NewRemoteGitHubRPM
  constructor to match its siblings.
- Generalize the releases_remote attribute description: it now names the
  backend remote that serves download bytes for both the terraform remote
  (URL rewriting) and the github_rpm remote (302 redirect of .rpm downloads).
- Document the type and add an example wiring a github_rpm remote to a generic
  github.com releases_remote.

Tests cover the resource metadata type name, the constructor, and mapping of
package_type/base_url/releases_remote/patterns through the API model; the
resource-count and expected-types assertions are updated.

Depends on the github_rpm API surface in artifactapi (separate PR). Cut a
minor release (make minor) once merged.
2026-08-10 20:57:26 +10:00
benvin c7a2416518 Merge pull request 'Add artifactapi_local_generic resource' (#12) from benvin/local-generic into main
ci/woodpecker/tag/release Pipeline was successful
Reviewed-on: #12
2026-07-29 23:52:46 +10:00
unkinben 14b7c4bdcd Add artifactapi_local_generic resource
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
Adds a local generic (raw-file) repository resource so arbitrary binaries -- the
bootapi node rootfs tarballs -- can be hosted on artifactapi and managed in
Terraform. The backend already serves generic locals; only the provider surface
was missing.

- resource_local_generic.go: local repo with package_type=generic (mirrors
  local_rpm), registered in the provider.
- Tests + example.

Claude-Session: https://claude.ai/code/session_015ur3i7D2azsMAWTSVABApv
2026-07-29 21:48:47 +10:00
benvin a232a26bf5 Merge pull request 'feat: add artifactapi_local_docker resource' (#11) from benvin/local-docker into main
ci/woodpecker/tag/release Pipeline was successful
Reviewed-on: #11
2026-07-05 16:56:47 +10:00
unkinben 30b414141a feat: add artifactapi_local_docker resource
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
The artifactapi server now serves local docker repos as real container
registries, but the provider had no resource to declare one — only remote
docker proxies and local terraform/pypi/rpm repos.

- Add the artifactapi_local_docker resource (package_type=docker,
  repo_type=local), mirroring the other local resources: name + description,
  managed via /api/v2/remotes.
- Register it in the provider and update the resource-count/type tests.
- Add unit tests, an example, and a Local Resources section to the README.
2026-07-04 22:37:10 +10:00
benvin b149e1bf9d Merge pull request 'Align release pipeline with the standard mechanism' (#10) from benvin/release-backend-options into main
ci/woodpecker/tag/release Pipeline was successful
Reviewed-on: #10
2026-07-03 15:13:07 +10:00
unkinben f82a9900a9 Add kubernetes backend options to build and test steps
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
Give the build/lint/test CI steps the same serviceAccount + resource
requests/limits as the pre-commit and release steps, so every step on the k8s
woodpecker backend is scheduled with bounded resources.
2026-07-03 15:05:00 +10:00
unkinben 7d7cbde0bf Align release pipeline with the standard mechanism
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
Match terraform-provider-litellmvaultsecret's release flow so both providers
publish the same way.

- Point the upload at the reachable artifactapi host
  (artifactapi.k8s.syd1.au.unkin.net) instead of the unresolvable artifactapi3
- Add kubernetes backend options (serviceAccount + resource requests/limits) to
  the package and upload steps
- Make the upload step explicitly depend_on the package step
2026-07-03 15:01:10 +10:00
benvin 0561bae10e Merge pull request 'feat: support per-remote upstream timeouts' (#9) from benvin/remote-upstream-timeouts into main
ci/woodpecker/tag/release Pipeline failed
Reviewed-on: #9
2026-07-02 22:23:04 +10:00
unkinben 4dd290518d feat: support per-remote upstream timeouts
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
Add upstream_dial_timeout, upstream_tls_timeout and
upstream_response_header_timeout (seconds; 0 = server default) to the
remote resource and data source, matching the artifactapi server. Wire
them through the API model, schema, create/read/update mapping, docs and
unit tests.
2026-07-02 22:19:39 +10:00
benvin 0b4db409b0 Merge pull request 'feat: add artifactapi_local_pypi and artifactapi_local_rpm resources' (#8) from benvin/add-local-pypi-rpm-resources into main
ci/woodpecker/tag/release Pipeline was successful
Reviewed-on: #8
2026-06-23 23:21:56 +10:00
unkinben 1bddc48e5a feat: add artifactapi_local_pypi and artifactapi_local_rpm resource types
ci/woodpecker/pr/pre-commit Pipeline was successful
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
2026-06-23 23:16:17 +10:00
benvin 4dcb3ac71a Merge pull request 'fix: escape shell variables in release pipeline' (#7) from benvin/fix-release-vars into main
ci/woodpecker/tag/release Pipeline was successful
Reviewed-on: #7
2026-06-23 00:42:40 +10:00
unkinben eeb4a60639 fix: escape shell variables in release pipeline
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
Woodpecker interpolates ${VERSION} as a CI variable (empty) before the
shell sees it. Use $$ escaping so the shell assignment and references
survive YAML interpolation.
2026-06-23 00:39:21 +10:00
benvin 41ef83ba34 Merge pull request 'fix: use python3 zipfile instead of zip binary' (#6) from benvin/fix-zip-packaging into main
ci/woodpecker/tag/release Pipeline failed
Reviewed-on: #6
2026-06-23 00:35:50 +10:00
unkinben 3d5154a12a fix: use python3 zipfile instead of zip binary for packaging
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
The almalinux9-gobuilder image doesn't have the zip binary and dnf
install is unreliable in CI. python3 is always available.
2026-06-23 00:31:53 +10:00
benvin ec9f7d410f Merge pull request 'feat: add zip packaging target and woodpecker release pipeline' (#5) from benvin/makefile-ci-upload into main
ci/woodpecker/tag/release Pipeline failed
Reviewed-on: #5
2026-06-23 00:21:30 +10:00
unkinben 03b3344d8f feat: add zip packaging target and woodpecker release pipeline
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
2026-06-23 00:18:06 +10:00
29 changed files with 1866 additions and 7 deletions
+10
View File
@@ -6,3 +6,13 @@ steps:
image: golang:1.25
commands:
- make build
backend_options:
kubernetes:
serviceAccountName: default
resources:
requests:
memory: 512Mi
cpu: 1
limits:
memory: 2Gi
cpu: 2
+40
View File
@@ -0,0 +1,40 @@
when:
- event: tag
steps:
- name: package
image: git.unkin.net/unkin/almalinux9-gobuilder:20260606
commands:
- make package VERSION=${CI_COMMIT_TAG}
backend_options:
kubernetes:
serviceAccountName: default
resources:
requests:
memory: 512Mi
cpu: 1
limits:
memory: 2Gi
cpu: 2
- 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://artifactapi.k8s.syd1.au.unkin.net/api/v2/remotes/terraform-unkin/files/unkin/artifactapi/$${FILE}" \
-H "Content-Type: application/zip" \
--data-binary @"$${FILE}"
depends_on: [package]
backend_options:
kubernetes:
serviceAccountName: default
resources:
requests:
memory: 128Mi
cpu: 100m
limits:
memory: 512Mi
cpu: 500m
+20
View File
@@ -6,8 +6,28 @@ steps:
image: golang:1.25
commands:
- make lint
backend_options:
kubernetes:
serviceAccountName: default
resources:
requests:
memory: 512Mi
cpu: 1
limits:
memory: 2Gi
cpu: 2
- name: test
image: golang:1.25
commands:
- make test
backend_options:
kubernetes:
serviceAccountName: default
resources:
requests:
memory: 512Mi
cpu: 1
limits:
memory: 2Gi
cpu: 2
+8 -2
View File
@@ -1,10 +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_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/')
@@ -30,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
+57 -2
View File
@@ -58,10 +58,13 @@ Available resource types:
- `artifactapi_remote_pypi`
- `artifactapi_remote_npm`
- `artifactapi_remote_rpm`
- `artifactapi_remote_deb`
- `artifactapi_remote_alpine`
- `artifactapi_remote_puppet`
- `artifactapi_remote_terraform`
- `artifactapi_remote_goproxy`
- `artifactapi_remote_github_rpm`
- `artifactapi_remote_github_deb`
#### Common Attributes
@@ -82,6 +85,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
@@ -90,11 +96,34 @@ Available resource types:
| `ban_tags_enabled` | `false` | Enable tag banning |
| `ban_tags` | | List of tags to ban |
#### Terraform-specific Attributes
#### Terraform / github_rpm / github_deb-specific Attributes
| Attribute | Default | Description |
|-------------------|---------|----------------------------------------------------------|
| `releases_remote` | `""` | Name of a generic remote for download URL rewriting |
| `releases_remote` | `""` | Name of a backend remote that serves the download bytes. Terraform uses it for download URL rewriting; `github_rpm`/`github_deb` 302-redirect `.rpm`/`.deb` downloads to it. |
#### github_rpm-specific notes
`artifactapi_remote_github_rpm` exposes a GitHub repo's releases as a `dnf`/`yum`
repository without precaching packages. Set `base_url` to the releases API root
(`https://api.github.com/repos/{owner}/{repo}`) and `releases_remote` to a
generic `github.com` remote that streams the `.rpm` bytes. `patterns` filters
which release assets become packages (regex on asset filename). `password` may
hold a token for private repos or higher API rate limits. See
`examples/resources/artifactapi_remote_github_rpm/main.tf`.
#### github_deb-specific notes
`artifactapi_remote_github_deb` is the Debian/apt analog of `github_rpm`: it
exposes a GitHub repo's release `.deb` assets as an `apt` repository, synthesizing
the apt index (`Packages`/`Release`) from release metadata without precaching
packages. Set `base_url` to the releases API root
(`https://api.github.com/repos/{owner}/{repo}`) and `releases_remote` to a
generic `github.com` remote that streams the `.deb` bytes; `.deb` downloads
302-redirect there. `patterns` filters which release assets become packages
(regex on asset filename). `password` may hold a token for private repos or
higher API rate limits. See
`examples/resources/artifactapi_remote_github_deb/main.tf`.
#### Example
@@ -115,6 +144,32 @@ resource "artifactapi_remote_docker" "dockerhub" {
}
```
### Local Resources
Local resources manage repositories that ArtifactAPI hosts directly (rather than
proxying an upstream) — each is a real registry for its package type.
Available resource types:
- `artifactapi_local_docker` — a container registry (Docker Registry HTTP API V2, push and pull)
- `artifactapi_local_pypi`
- `artifactapi_local_rpm`
- `artifactapi_local_deb`
- `artifactapi_local_terraform`
Each takes just `name` (required, forces replacement) and an optional
`description`.
```hcl
resource "artifactapi_local_docker" "internal" {
name = "docker-internal"
description = "Internal container image registry"
}
```
Images push and pull against `<endpoint>/<name>/<image>:<tag>`, e.g.
`docker push artifactapi.example.com/docker-internal/myapp:latest`.
### Virtual Resources
Virtual repositories merge multiple remotes of the same package type into a single endpoint.
@@ -0,0 +1,4 @@
resource "artifactapi_local_deb" "internal" {
name = "deb-internal"
description = "Internal Deb package repository"
}
@@ -0,0 +1,4 @@
resource "artifactapi_local_docker" "internal" {
name = "docker-internal"
description = "Internal container image registry"
}
@@ -0,0 +1,4 @@
resource "artifactapi_local_generic" "rootfs_images" {
name = "rootfs-images"
description = "Prebuilt node rootfs tarballs (bootapi image provisioning)"
}
@@ -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,23 @@
terraform {
required_providers {
artifactapi = {
source = "git.unkin.net/unkin/artifactapi"
version = "0.0.1"
}
}
}
provider "artifactapi" {
endpoint = "https://artifactapi.example.com"
}
# Deb remote proxies a Debian/apt package repository.
# The provider knows the index files (Release, Packages) are mutable; .deb packages are immutable.
resource "artifactapi_remote_deb" "debian" {
name = "debian"
base_url = "http://deb.debian.org/debian"
description = "Debian apt package repository"
immutable_ttl = 0
mutable_ttl = 7200
}
@@ -0,0 +1,44 @@
terraform {
required_providers {
artifactapi = {
source = "git.unkin.net/unkin/artifactapi"
version = "0.0.1"
}
}
}
provider "artifactapi" {
endpoint = "https://artifactapi.example.com"
}
# Backend generic remote that serves the actual .deb bytes from github.com.
# Its patterns must allowlist the target repo's release-download assets.
resource "artifactapi_remote_generic" "github" {
name = "github"
base_url = "https://github.com"
patterns = [
"goodtune/ghp/releases/download/.*\\.deb$",
]
}
# Metadata-only remote: scans goodtune/ghp releases for .deb assets and serves a
# synthesized apt repo. Packages are never precached; downloads 302-redirect to
# the "github" remote above.
resource "artifactapi_remote_github_deb" "goodtune_ghp" {
name = "goodtune-ghp"
base_url = "https://api.github.com/repos/goodtune/ghp"
description = "goodtune/ghp GitHub releases as an apt repo"
releases_remote = artifactapi_remote_generic.github.name
mutable_ttl = 3600
# Optional: restrict which release assets become packages.
patterns = [
".*_amd64\\.deb$",
".*_all\\.deb$",
]
# Optional: token for private repos / higher GitHub API rate limits.
# password = var.github_token
}
@@ -0,0 +1,44 @@
terraform {
required_providers {
artifactapi = {
source = "git.unkin.net/unkin/artifactapi"
version = "0.0.1"
}
}
}
provider "artifactapi" {
endpoint = "https://artifactapi.example.com"
}
# Backend generic remote that serves the actual .rpm bytes from github.com.
# Its patterns must allowlist the target repo's release-download assets.
resource "artifactapi_remote_generic" "github" {
name = "github"
base_url = "https://github.com"
patterns = [
"acme/tools/releases/download/.*\\.rpm$",
]
}
# Metadata-only remote: scans acme/tools releases for .rpm assets and serves a
# synthesized yum repo. Packages are never precached; downloads 302-redirect to
# the "github" remote above.
resource "artifactapi_remote_github_rpm" "acme_tools" {
name = "acme-tools"
base_url = "https://api.github.com/repos/acme/tools"
description = "acme/tools GitHub releases as a yum repo"
releases_remote = artifactapi_remote_generic.github.name
mutable_ttl = 3600
# Optional: restrict which release assets become packages.
patterns = [
".*\\.x86_64\\.rpm$",
".*\\.noarch\\.rpm$",
]
# Optional: token for private repos / higher GitHub API rate limits.
# password = var.github_token
}
+12
View File
@@ -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)...)
}
+4
View File
@@ -22,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 {
+8
View File
@@ -63,12 +63,20 @@ func (p *ArtifactAPIProvider) Resources(_ context.Context) []func() resource.Res
newRemoteResource("pypi"),
newRemoteResource("npm"),
newRemoteResource("rpm"),
newRemoteResource("deb"),
newRemoteResource("alpine"),
newRemoteResource("puppet"),
newRemoteResource("terraform"),
newRemoteResource("goproxy"),
newRemoteResource("github_rpm"),
newRemoteResource("github_deb"),
NewVirtualResource,
NewLocalTerraformResource,
NewLocalPyPIResource,
NewLocalRPMResource,
NewLocalDebResource,
NewLocalDockerResource,
NewLocalGenericResource,
}
}
+10 -2
View File
@@ -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 + 1 local_terraform = 12
expectedCount := 12
// 13 remote resource types + 1 virtual + local_terraform/pypi/rpm/deb/docker/generic = 20
expectedCount := 20
if len(resources) != expectedCount {
t.Fatalf("expected %d resources, got %d", expectedCount, len(resources))
}
@@ -102,12 +102,20 @@ func TestProvider_Resources_ContainsExpectedTypes(t *testing.T) {
"artifactapi_remote_pypi",
"artifactapi_remote_npm",
"artifactapi_remote_rpm",
"artifactapi_remote_deb",
"artifactapi_remote_alpine",
"artifactapi_remote_puppet",
"artifactapi_remote_terraform",
"artifactapi_remote_goproxy",
"artifactapi_remote_github_rpm",
"artifactapi_remote_github_deb",
"artifactapi_virtual",
"artifactapi_local_terraform",
"artifactapi_local_pypi",
"artifactapi_local_rpm",
"artifactapi_local_deb",
"artifactapi_local_docker",
"artifactapi_local_generic",
}
for _, name := range expected {
+164
View File
@@ -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 = &localDebResource{}
_ resource.ResourceWithImportState = &localDebResource{}
)
type localDebResource struct {
client *apiClient
}
type localDebResourceModel struct {
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
}
func NewLocalDebResource() resource.Resource {
return &localDebResource{}
}
func (r *localDebResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_local_deb"
}
func (r *localDebResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Manages a local ArtifactAPI Deb repository for hosting Debian/apt packages directly.",
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Description: "Unique name of the local Deb repository.",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"description": schema.StringAttribute{
Description: "Human-readable description.",
Optional: true,
Computed: true,
Default: stringdefault.StaticString(""),
},
},
}
}
func (r *localDebResource) 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 *localDebResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var plan localDebResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
api := localDebModelToAPI(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 deb failed", err.Error())
return
}
state := localDebAPIToModel(created)
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
}
func (r *localDebResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var state localDebResourceModel
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 deb failed", err.Error())
return
}
newState := localDebAPIToModel(remote)
resp.Diagnostics.Append(resp.State.Set(ctx, newState)...)
}
func (r *localDebResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var plan localDebResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
api := localDebModelToAPI(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 deb failed", err.Error())
return
}
state := localDebAPIToModel(updated)
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
}
func (r *localDebResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var state localDebResourceModel
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 deb failed", err.Error())
return
}
}
func (r *localDebResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("name"), req, resp)
}
func localDebModelToAPI(m localDebResourceModel) remoteAPI {
return remoteAPI{
Name: m.Name.ValueString(),
PackageType: "deb",
RepoType: "local",
Description: m.Description.ValueString(),
}
}
func localDebAPIToModel(api remoteAPI) localDebResourceModel {
return localDebResourceModel{
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 TestLocalDebModelToAPI(t *testing.T) {
model := localDebResourceModel{
Name: types.StringValue("deb-internal"),
Description: types.StringValue("Internal Deb repository"),
}
api := localDebModelToAPI(model)
if api.Name != "deb-internal" {
t.Errorf("Name: expected deb-internal, got %s", api.Name)
}
if api.PackageType != "deb" {
t.Errorf("PackageType: expected deb, got %s", api.PackageType)
}
if api.RepoType != "local" {
t.Errorf("RepoType: expected local, got %s", api.RepoType)
}
if api.Description != "Internal Deb repository" {
t.Errorf("Description: expected 'Internal Deb repository', got %s", api.Description)
}
}
func TestLocalDebModelToAPI_EmptyDescription(t *testing.T) {
model := localDebResourceModel{
Name: types.StringValue("deb-empty"),
Description: types.StringValue(""),
}
api := localDebModelToAPI(model)
if api.Name != "deb-empty" {
t.Errorf("Name: expected deb-empty, got %s", api.Name)
}
if api.Description != "" {
t.Errorf("Description: expected empty string, got %s", api.Description)
}
if api.PackageType != "deb" {
t.Errorf("PackageType: expected deb, got %s", api.PackageType)
}
if api.RepoType != "local" {
t.Errorf("RepoType: expected local, got %s", api.RepoType)
}
}
func TestLocalDebAPIToModel(t *testing.T) {
api := remoteAPI{
Name: "deb-internal",
PackageType: "deb",
RepoType: "local",
Description: "Internal Deb repository",
ManagedBy: "terraform",
}
model := localDebAPIToModel(api)
if model.Name.ValueString() != "deb-internal" {
t.Errorf("Name: expected deb-internal, got %s", model.Name.ValueString())
}
if model.Description.ValueString() != "Internal Deb repository" {
t.Errorf("Description: expected 'Internal Deb repository', got %s", model.Description.ValueString())
}
}
func TestLocalDebRoundTrip(t *testing.T) {
original := localDebResourceModel{
Name: types.StringValue("roundtrip-deb"),
Description: types.StringValue("Round trip test"),
}
api := localDebModelToAPI(original)
result := localDebAPIToModel(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 TestLocalDebResource_Metadata(t *testing.T) {
r := NewLocalDebResource()
req := resource.MetadataRequest{ProviderTypeName: "artifactapi"}
var resp resource.MetadataResponse
r.Metadata(context.Background(), req, &resp)
if resp.TypeName != "artifactapi_local_deb" {
t.Errorf("expected artifactapi_local_deb, got %s", resp.TypeName)
}
}
func TestLocalDebResource_Schema(t *testing.T) {
r := NewLocalDebResource()
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 TestNewLocalDebResource_Type(t *testing.T) {
r := NewLocalDebResource()
_, ok := r.(*localDebResource)
if !ok {
t.Error("expected *localDebResource")
}
}
+164
View File
@@ -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 = &localDockerResource{}
_ resource.ResourceWithImportState = &localDockerResource{}
)
type localDockerResource struct {
client *apiClient
}
type localDockerResourceModel struct {
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
}
func NewLocalDockerResource() resource.Resource {
return &localDockerResource{}
}
func (r *localDockerResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_local_docker"
}
func (r *localDockerResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Manages a local ArtifactAPI Docker repository — a real container registry serving the Docker Registry HTTP API V2 for push and pull.",
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Description: "Unique name of the local Docker repository. Becomes the first path segment of pushed image references (e.g. <endpoint>/<name>/<image>:<tag>).",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"description": schema.StringAttribute{
Description: "Human-readable description.",
Optional: true,
Computed: true,
Default: stringdefault.StaticString(""),
},
},
}
}
func (r *localDockerResource) 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 *localDockerResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var plan localDockerResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
api := localDockerModelToAPI(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 docker failed", err.Error())
return
}
state := localDockerAPIToModel(created)
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
}
func (r *localDockerResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var state localDockerResourceModel
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 docker failed", err.Error())
return
}
newState := localDockerAPIToModel(remote)
resp.Diagnostics.Append(resp.State.Set(ctx, newState)...)
}
func (r *localDockerResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var plan localDockerResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
api := localDockerModelToAPI(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 docker failed", err.Error())
return
}
state := localDockerAPIToModel(updated)
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
}
func (r *localDockerResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var state localDockerResourceModel
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 docker failed", err.Error())
return
}
}
func (r *localDockerResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("name"), req, resp)
}
func localDockerModelToAPI(m localDockerResourceModel) remoteAPI {
return remoteAPI{
Name: m.Name.ValueString(),
PackageType: "docker",
RepoType: "local",
Description: m.Description.ValueString(),
}
}
func localDockerAPIToModel(api remoteAPI) localDockerResourceModel {
return localDockerResourceModel{
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 TestLocalDockerModelToAPI(t *testing.T) {
model := localDockerResourceModel{
Name: types.StringValue("docker-internal"),
Description: types.StringValue("Internal container registry"),
}
api := localDockerModelToAPI(model)
if api.Name != "docker-internal" {
t.Errorf("Name: expected docker-internal, got %s", api.Name)
}
if api.PackageType != "docker" {
t.Errorf("PackageType: expected docker, got %s", api.PackageType)
}
if api.RepoType != "local" {
t.Errorf("RepoType: expected local, got %s", api.RepoType)
}
if api.Description != "Internal container registry" {
t.Errorf("Description: expected 'Internal container registry', got %s", api.Description)
}
}
func TestLocalDockerModelToAPI_EmptyDescription(t *testing.T) {
model := localDockerResourceModel{
Name: types.StringValue("docker-empty"),
Description: types.StringValue(""),
}
api := localDockerModelToAPI(model)
if api.Name != "docker-empty" {
t.Errorf("Name: expected docker-empty, got %s", api.Name)
}
if api.Description != "" {
t.Errorf("Description: expected empty string, got %s", api.Description)
}
if api.PackageType != "docker" {
t.Errorf("PackageType: expected docker, got %s", api.PackageType)
}
if api.RepoType != "local" {
t.Errorf("RepoType: expected local, got %s", api.RepoType)
}
}
func TestLocalDockerAPIToModel(t *testing.T) {
api := remoteAPI{
Name: "docker-internal",
PackageType: "docker",
RepoType: "local",
Description: "Internal container registry",
ManagedBy: "terraform",
}
model := localDockerAPIToModel(api)
if model.Name.ValueString() != "docker-internal" {
t.Errorf("Name: expected docker-internal, got %s", model.Name.ValueString())
}
if model.Description.ValueString() != "Internal container registry" {
t.Errorf("Description: expected 'Internal container registry', got %s", model.Description.ValueString())
}
}
func TestLocalDockerRoundTrip(t *testing.T) {
original := localDockerResourceModel{
Name: types.StringValue("roundtrip-docker"),
Description: types.StringValue("Round trip test"),
}
api := localDockerModelToAPI(original)
result := localDockerAPIToModel(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 TestLocalDockerResource_Metadata(t *testing.T) {
r := NewLocalDockerResource()
req := resource.MetadataRequest{ProviderTypeName: "artifactapi"}
var resp resource.MetadataResponse
r.Metadata(context.Background(), req, &resp)
if resp.TypeName != "artifactapi_local_docker" {
t.Errorf("expected artifactapi_local_docker, got %s", resp.TypeName)
}
}
func TestLocalDockerResource_Schema(t *testing.T) {
r := NewLocalDockerResource()
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 TestNewLocalDockerResource_Type(t *testing.T) {
r := NewLocalDockerResource()
_, ok := r.(*localDockerResource)
if !ok {
t.Error("expected *localDockerResource")
}
}
+164
View File
@@ -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 = &localGenericResource{}
_ resource.ResourceWithImportState = &localGenericResource{}
)
type localGenericResource struct {
client *apiClient
}
type localGenericResourceModel struct {
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
}
func NewLocalGenericResource() resource.Resource {
return &localGenericResource{}
}
func (r *localGenericResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_local_generic"
}
func (r *localGenericResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Manages a local ArtifactAPI generic repository for hosting arbitrary files (e.g. rootfs tarballs).",
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Description: "Unique name of the local Generic repository.",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"description": schema.StringAttribute{
Description: "Human-readable description.",
Optional: true,
Computed: true,
Default: stringdefault.StaticString(""),
},
},
}
}
func (r *localGenericResource) 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 *localGenericResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var plan localGenericResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
api := localGenericModelToAPI(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 := localGenericAPIToModel(created)
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
}
func (r *localGenericResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var state localGenericResourceModel
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 := localGenericAPIToModel(remote)
resp.Diagnostics.Append(resp.State.Set(ctx, newState)...)
}
func (r *localGenericResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var plan localGenericResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
api := localGenericModelToAPI(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 := localGenericAPIToModel(updated)
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
}
func (r *localGenericResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var state localGenericResourceModel
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 *localGenericResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("name"), req, resp)
}
func localGenericModelToAPI(m localGenericResourceModel) remoteAPI {
return remoteAPI{
Name: m.Name.ValueString(),
PackageType: "generic",
RepoType: "local",
Description: m.Description.ValueString(),
}
}
func localGenericAPIToModel(api remoteAPI) localGenericResourceModel {
return localGenericResourceModel{
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 TestLocalGenericModelToAPI(t *testing.T) {
model := localGenericResourceModel{
Name: types.StringValue("rootfs-images"),
Description: types.StringValue("Internal Generic repository"),
}
api := localGenericModelToAPI(model)
if api.Name != "rootfs-images" {
t.Errorf("Name: expected rootfs-images, got %s", api.Name)
}
if api.PackageType != "generic" {
t.Errorf("PackageType: expected generic, got %s", api.PackageType)
}
if api.RepoType != "local" {
t.Errorf("RepoType: expected local, got %s", api.RepoType)
}
if api.Description != "Internal Generic repository" {
t.Errorf("Description: expected 'Internal Generic repository', got %s", api.Description)
}
}
func TestLocalGenericModelToAPI_EmptyDescription(t *testing.T) {
model := localGenericResourceModel{
Name: types.StringValue("generic-empty"),
Description: types.StringValue(""),
}
api := localGenericModelToAPI(model)
if api.Name != "generic-empty" {
t.Errorf("Name: expected generic-empty, got %s", api.Name)
}
if api.Description != "" {
t.Errorf("Description: expected empty string, got %s", api.Description)
}
if api.PackageType != "generic" {
t.Errorf("PackageType: expected generic, got %s", api.PackageType)
}
if api.RepoType != "local" {
t.Errorf("RepoType: expected local, got %s", api.RepoType)
}
}
func TestLocalGenericAPIToModel(t *testing.T) {
api := remoteAPI{
Name: "rootfs-images",
PackageType: "generic",
RepoType: "local",
Description: "Internal Generic repository",
ManagedBy: "terraform",
}
model := localGenericAPIToModel(api)
if model.Name.ValueString() != "rootfs-images" {
t.Errorf("Name: expected rootfs-images, got %s", model.Name.ValueString())
}
if model.Description.ValueString() != "Internal Generic repository" {
t.Errorf("Description: expected 'Internal Generic repository', got %s", model.Description.ValueString())
}
}
func TestLocalGenericRoundTrip(t *testing.T) {
original := localGenericResourceModel{
Name: types.StringValue("roundtrip-generic"),
Description: types.StringValue("Round trip test"),
}
api := localGenericModelToAPI(original)
result := localGenericAPIToModel(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 TestLocalGenericResource_Metadata(t *testing.T) {
r := NewLocalGenericResource()
req := resource.MetadataRequest{ProviderTypeName: "artifactapi"}
var resp resource.MetadataResponse
r.Metadata(context.Background(), req, &resp)
if resp.TypeName != "artifactapi_local_generic" {
t.Errorf("expected artifactapi_local_generic, got %s", resp.TypeName)
}
}
func TestLocalGenericResource_Schema(t *testing.T) {
r := NewLocalGenericResource()
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 TestNewLocalGenericResource_Type(t *testing.T) {
r := NewLocalGenericResource()
_, ok := r.(*localGenericResource)
if !ok {
t.Error("expected *localGenericResource")
}
}
+164
View File
@@ -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")
}
}
+164
View File
@@ -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")
}
}
+28 -1
View File
@@ -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 {
@@ -58,10 +62,13 @@ func NewRemoteHelm() resource.Resource { return &remoteResource{packageType
func NewRemotePyPI() resource.Resource { return &remoteResource{packageType: "pypi"} }
func NewRemoteNPM() resource.Resource { return &remoteResource{packageType: "npm"} }
func NewRemoteRPM() resource.Resource { return &remoteResource{packageType: "rpm"} }
func NewRemoteDeb() resource.Resource { return &remoteResource{packageType: "deb"} }
func NewRemoteAlpine() resource.Resource { return &remoteResource{packageType: "alpine"} }
func NewRemotePuppet() resource.Resource { return &remoteResource{packageType: "puppet"} }
func NewRemoteTerraform() resource.Resource { return &remoteResource{packageType: "terraform"} }
func NewRemoteGoProxy() resource.Resource { return &remoteResource{packageType: "goproxy"} }
func NewRemoteGitHubRPM() resource.Resource { return &remoteResource{packageType: "github_rpm"} }
func NewRemoteGitHubDeb() resource.Resource { return &remoteResource{packageType: "github_deb"} }
func (r *remoteResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_remote_" + r.packageType
@@ -141,9 +148,21 @@ func (r *remoteResource) Schema(_ context.Context, _ resource.SchemaRequest, res
ElementType: types.StringType,
},
"releases_remote": schema.StringAttribute{
Description: "Name of the CDN remote for download URL rewriting (terraform only).",
Description: "Name of a backend remote that serves the actual download bytes. Used by the terraform remote (download URL rewriting) and the github_rpm/github_deb remotes (302 redirect of .rpm/.deb downloads to a generic github.com remote).",
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 +287,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 +322,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
}
+93
View File
@@ -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)
}
@@ -377,6 +409,8 @@ func TestRemoteResource_Metadata(t *testing.T) {
{"helm", "artifactapi_remote_helm"},
{"pypi", "artifactapi_remote_pypi"},
{"npm", "artifactapi_remote_npm"},
{"github_rpm", "artifactapi_remote_github_rpm"},
{"github_deb", "artifactapi_remote_github_deb"},
}
for _, tt := range tests {
@@ -392,6 +426,62 @@ func TestRemoteResource_Metadata(t *testing.T) {
}
}
func TestModelToAPI_GitHubRPM(t *testing.T) {
ctx := context.Background()
r := &remoteResource{packageType: "github_rpm"}
model := remoteResourceModel{
Name: types.StringValue("acme-tools"),
BaseURL: types.StringValue("https://api.github.com/repos/acme/tools"),
ReleasesRemote: types.StringValue("github"),
MutableTTL: types.Int64Value(3600),
Patterns: stringsToList(ctx, []string{`.*\.x86_64\.rpm$`}),
}
api := r.modelToAPI(ctx, model)
if api.PackageType != "github_rpm" {
t.Errorf("PackageType: expected github_rpm, got %s", api.PackageType)
}
if api.BaseURL != "https://api.github.com/repos/acme/tools" {
t.Errorf("BaseURL: got %s", api.BaseURL)
}
if api.ReleasesRemote != "github" {
t.Errorf("ReleasesRemote: expected github, got %s", api.ReleasesRemote)
}
if len(api.Patterns) != 1 || api.Patterns[0] != `.*\.x86_64\.rpm$` {
t.Errorf("Patterns: got %v", api.Patterns)
}
}
func TestModelToAPI_GitHubDeb(t *testing.T) {
ctx := context.Background()
r := &remoteResource{packageType: "github_deb"}
model := remoteResourceModel{
Name: types.StringValue("acme-tools"),
BaseURL: types.StringValue("https://api.github.com/repos/acme/tools"),
ReleasesRemote: types.StringValue("github"),
MutableTTL: types.Int64Value(3600),
Patterns: stringsToList(ctx, []string{`.*_amd64\.deb$`}),
}
api := r.modelToAPI(ctx, model)
if api.PackageType != "github_deb" {
t.Errorf("PackageType: expected github_deb, got %s", api.PackageType)
}
if api.BaseURL != "https://api.github.com/repos/acme/tools" {
t.Errorf("BaseURL: got %s", api.BaseURL)
}
if api.ReleasesRemote != "github" {
t.Errorf("ReleasesRemote: expected github, got %s", api.ReleasesRemote)
}
if len(api.Patterns) != 1 || api.Patterns[0] != `.*_amd64\.deb$` {
t.Errorf("Patterns: got %v", api.Patterns)
}
}
func TestRemoteResource_Schema(t *testing.T) {
r := &remoteResource{packageType: "docker"}
req := resource.SchemaRequest{}
@@ -405,6 +495,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 {
@@ -430,6 +521,8 @@ func TestNewRemoteResource_Constructors(t *testing.T) {
{"puppet", func() resource.Resource { return NewRemotePuppet() }, "puppet"},
{"terraform", func() resource.Resource { return NewRemoteTerraform() }, "terraform"},
{"goproxy", func() resource.Resource { return NewRemoteGoProxy() }, "goproxy"},
{"github_rpm", func() resource.Resource { return NewRemoteGitHubRPM() }, "github_rpm"},
{"github_deb", func() resource.Resource { return NewRemoteGitHubDeb() }, "github_deb"},
}
for _, tt := range tests {