Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0879921398 | |||
| 719b0dd502 | |||
| 8d2e0ef27d | |||
| cb1110c723 | |||
| b8c18690ef | |||
| 25b72fd1ac | |||
| 5ee3a1d3d6 | |||
| b21892217a | |||
| 2d8584f103 | |||
| b26e651d45 |
@@ -58,10 +58,14 @@ 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`
|
||||
- `artifactapi_remote_github_alpine`
|
||||
|
||||
#### Common Attributes
|
||||
|
||||
@@ -93,11 +97,47 @@ 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 / github_alpine-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`/`github_alpine` 302-redirect `.rpm`/`.deb`/`.apk` 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`.
|
||||
|
||||
#### github_alpine-specific notes
|
||||
|
||||
`artifactapi_remote_github_alpine` is the Alpine/apk analog of `github_rpm`: it
|
||||
exposes a GitHub repo's release `.apk` assets as an `apk` repository, synthesizing
|
||||
the apk index (`APKINDEX.tar.gz`) 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 `.apk` bytes; `.apk` 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_alpine/main.tf`.
|
||||
|
||||
#### Example
|
||||
|
||||
@@ -128,6 +168,8 @@ 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_alpine`
|
||||
- `artifactapi_local_terraform`
|
||||
|
||||
Each takes just `name` (required, forces replacement) and an optional
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
resource "artifactapi_local_alpine" "internal" {
|
||||
name = "alpine-internal"
|
||||
description = "Internal Alpine package repository"
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
resource "artifactapi_local_deb" "internal" {
|
||||
name = "deb-internal"
|
||||
description = "Internal Deb 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 .apk 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/.*\\.apk$",
|
||||
]
|
||||
}
|
||||
|
||||
# Metadata-only remote: scans goodtune/ghp releases for .apk assets and serves a
|
||||
# synthesized apk repo (APKINDEX). Packages are never precached; downloads
|
||||
# 302-redirect to the "github" remote above.
|
||||
resource "artifactapi_remote_github_alpine" "goodtune_ghp" {
|
||||
name = "goodtune-ghp"
|
||||
base_url = "https://api.github.com/repos/goodtune/ghp"
|
||||
description = "goodtune/ghp GitHub releases as an apk repo"
|
||||
releases_remote = artifactapi_remote_generic.github.name
|
||||
|
||||
mutable_ttl = 3600
|
||||
|
||||
# Optional: restrict which release assets become packages.
|
||||
patterns = [
|
||||
".*_x86_64\\.apk$",
|
||||
".*_noarch\\.apk$",
|
||||
]
|
||||
|
||||
# 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 .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
|
||||
}
|
||||
@@ -63,14 +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"),
|
||||
newRemoteResource("github_alpine"),
|
||||
NewVirtualResource,
|
||||
NewLocalTerraformResource,
|
||||
NewLocalPyPIResource,
|
||||
NewLocalRPMResource,
|
||||
NewLocalDebResource,
|
||||
NewLocalAlpineResource,
|
||||
NewLocalDockerResource,
|
||||
NewLocalGenericResource,
|
||||
}
|
||||
|
||||
@@ -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 + local_terraform/pypi/rpm/docker/generic = 16
|
||||
expectedCount := 16
|
||||
// 13 remote resource types + 1 virtual + local_terraform/pypi/rpm/deb/alpine/docker/generic = 21
|
||||
expectedCount := 22
|
||||
if len(resources) != expectedCount {
|
||||
t.Fatalf("expected %d resources, got %d", expectedCount, len(resources))
|
||||
}
|
||||
@@ -102,14 +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_remote_github_alpine",
|
||||
"artifactapi_virtual",
|
||||
"artifactapi_local_terraform",
|
||||
"artifactapi_local_pypi",
|
||||
"artifactapi_local_rpm",
|
||||
"artifactapi_local_deb",
|
||||
"artifactapi_local_alpine",
|
||||
"artifactapi_local_docker",
|
||||
"artifactapi_local_generic",
|
||||
}
|
||||
|
||||
@@ -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 = &localAlpineResource{}
|
||||
_ resource.ResourceWithImportState = &localAlpineResource{}
|
||||
)
|
||||
|
||||
type localAlpineResource struct {
|
||||
client *apiClient
|
||||
}
|
||||
|
||||
type localAlpineResourceModel struct {
|
||||
Name types.String `tfsdk:"name"`
|
||||
Description types.String `tfsdk:"description"`
|
||||
}
|
||||
|
||||
func NewLocalAlpineResource() resource.Resource {
|
||||
return &localAlpineResource{}
|
||||
}
|
||||
|
||||
func (r *localAlpineResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
|
||||
resp.TypeName = req.ProviderTypeName + "_local_alpine"
|
||||
}
|
||||
|
||||
func (r *localAlpineResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
|
||||
resp.Schema = schema.Schema{
|
||||
Description: "Manages a local ArtifactAPI Alpine repository for hosting Alpine/apk packages directly.",
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"name": schema.StringAttribute{
|
||||
Description: "Unique name of the local Alpine repository.",
|
||||
Required: true,
|
||||
PlanModifiers: []planmodifier.String{
|
||||
stringplanmodifier.RequiresReplace(),
|
||||
},
|
||||
},
|
||||
"description": schema.StringAttribute{
|
||||
Description: "Human-readable description.",
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
Default: stringdefault.StaticString(""),
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (r *localAlpineResource) 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 *localAlpineResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
|
||||
var plan localAlpineResourceModel
|
||||
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
api := localAlpineModelToAPI(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 alpine failed", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
state := localAlpineAPIToModel(created)
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
|
||||
}
|
||||
|
||||
func (r *localAlpineResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
|
||||
var state localAlpineResourceModel
|
||||
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 alpine failed", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
newState := localAlpineAPIToModel(remote)
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, newState)...)
|
||||
}
|
||||
|
||||
func (r *localAlpineResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
|
||||
var plan localAlpineResourceModel
|
||||
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
api := localAlpineModelToAPI(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 alpine failed", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
state := localAlpineAPIToModel(updated)
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
|
||||
}
|
||||
|
||||
func (r *localAlpineResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
|
||||
var state localAlpineResourceModel
|
||||
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 alpine failed", err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (r *localAlpineResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
|
||||
resource.ImportStatePassthroughID(ctx, path.Root("name"), req, resp)
|
||||
}
|
||||
|
||||
func localAlpineModelToAPI(m localAlpineResourceModel) remoteAPI {
|
||||
return remoteAPI{
|
||||
Name: m.Name.ValueString(),
|
||||
PackageType: "alpine",
|
||||
RepoType: "local",
|
||||
Description: m.Description.ValueString(),
|
||||
}
|
||||
}
|
||||
|
||||
func localAlpineAPIToModel(api remoteAPI) localAlpineResourceModel {
|
||||
return localAlpineResourceModel{
|
||||
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 TestLocalAlpineModelToAPI(t *testing.T) {
|
||||
model := localAlpineResourceModel{
|
||||
Name: types.StringValue("alpine-internal"),
|
||||
Description: types.StringValue("Internal Alpine repository"),
|
||||
}
|
||||
|
||||
api := localAlpineModelToAPI(model)
|
||||
|
||||
if api.Name != "alpine-internal" {
|
||||
t.Errorf("Name: expected alpine-internal, got %s", api.Name)
|
||||
}
|
||||
if api.PackageType != "alpine" {
|
||||
t.Errorf("PackageType: expected alpine, got %s", api.PackageType)
|
||||
}
|
||||
if api.RepoType != "local" {
|
||||
t.Errorf("RepoType: expected local, got %s", api.RepoType)
|
||||
}
|
||||
if api.Description != "Internal Alpine repository" {
|
||||
t.Errorf("Description: expected 'Internal Alpine repository', got %s", api.Description)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocalAlpineModelToAPI_EmptyDescription(t *testing.T) {
|
||||
model := localAlpineResourceModel{
|
||||
Name: types.StringValue("alpine-empty"),
|
||||
Description: types.StringValue(""),
|
||||
}
|
||||
|
||||
api := localAlpineModelToAPI(model)
|
||||
|
||||
if api.Name != "alpine-empty" {
|
||||
t.Errorf("Name: expected alpine-empty, got %s", api.Name)
|
||||
}
|
||||
if api.Description != "" {
|
||||
t.Errorf("Description: expected empty string, got %s", api.Description)
|
||||
}
|
||||
if api.PackageType != "alpine" {
|
||||
t.Errorf("PackageType: expected alpine, got %s", api.PackageType)
|
||||
}
|
||||
if api.RepoType != "local" {
|
||||
t.Errorf("RepoType: expected local, got %s", api.RepoType)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocalAlpineAPIToModel(t *testing.T) {
|
||||
api := remoteAPI{
|
||||
Name: "alpine-internal",
|
||||
PackageType: "alpine",
|
||||
RepoType: "local",
|
||||
Description: "Internal Alpine repository",
|
||||
ManagedBy: "terraform",
|
||||
}
|
||||
|
||||
model := localAlpineAPIToModel(api)
|
||||
|
||||
if model.Name.ValueString() != "alpine-internal" {
|
||||
t.Errorf("Name: expected alpine-internal, got %s", model.Name.ValueString())
|
||||
}
|
||||
if model.Description.ValueString() != "Internal Alpine repository" {
|
||||
t.Errorf("Description: expected 'Internal Alpine repository', got %s", model.Description.ValueString())
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocalAlpineRoundTrip(t *testing.T) {
|
||||
original := localAlpineResourceModel{
|
||||
Name: types.StringValue("roundtrip-alpine"),
|
||||
Description: types.StringValue("Round trip test"),
|
||||
}
|
||||
|
||||
api := localAlpineModelToAPI(original)
|
||||
result := localAlpineAPIToModel(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 TestLocalAlpineResource_Metadata(t *testing.T) {
|
||||
r := NewLocalAlpineResource()
|
||||
req := resource.MetadataRequest{ProviderTypeName: "artifactapi"}
|
||||
var resp resource.MetadataResponse
|
||||
r.Metadata(context.Background(), req, &resp)
|
||||
if resp.TypeName != "artifactapi_local_alpine" {
|
||||
t.Errorf("expected artifactapi_local_alpine, got %s", resp.TypeName)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocalAlpineResource_Schema(t *testing.T) {
|
||||
r := NewLocalAlpineResource()
|
||||
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 TestNewLocalAlpineResource_Type(t *testing.T) {
|
||||
r := NewLocalAlpineResource()
|
||||
_, ok := r.(*localAlpineResource)
|
||||
if !ok {
|
||||
t.Error("expected *localAlpineResource")
|
||||
}
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
@@ -62,10 +62,16 @@ 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 NewRemoteGitHubAlpine() resource.Resource {
|
||||
return &remoteResource{packageType: "github_alpine"}
|
||||
}
|
||||
|
||||
func (r *remoteResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
|
||||
resp.TypeName = req.ProviderTypeName + "_remote_" + r.packageType
|
||||
@@ -145,7 +151,7 @@ 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/github_alpine remotes (302 redirect of .rpm/.deb/.apk downloads to a generic github.com remote).",
|
||||
Optional: true, Computed: true, Default: stringdefault.StaticString(""),
|
||||
},
|
||||
"upstream_dial_timeout": schema.Int64Attribute{
|
||||
|
||||
@@ -409,6 +409,9 @@ 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"},
|
||||
{"github_alpine", "artifactapi_remote_github_alpine"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
@@ -424,6 +427,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{}
|
||||
@@ -463,6 +522,9 @@ 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"},
|
||||
{"github_alpine", func() resource.Resource { return NewRemoteGitHubAlpine() }, "github_alpine"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
|
||||
Reference in New Issue
Block a user