Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2d8584f103 | |||
| b26e651d45 | |||
| c7a2416518 | |||
| 14b7c4bdcd | |||
| a232a26bf5 | |||
| 30b414141a |
@@ -62,6 +62,7 @@ Available resource types:
|
||||
- `artifactapi_remote_puppet`
|
||||
- `artifactapi_remote_terraform`
|
||||
- `artifactapi_remote_goproxy`
|
||||
- `artifactapi_remote_github_rpm`
|
||||
|
||||
#### Common Attributes
|
||||
|
||||
@@ -93,11 +94,21 @@ Available resource types:
|
||||
| `ban_tags_enabled` | `false` | Enable tag banning |
|
||||
| `ban_tags` | | List of tags to ban |
|
||||
|
||||
#### Terraform-specific Attributes
|
||||
#### Terraform / github_rpm-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` 302-redirects `.rpm` 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`.
|
||||
|
||||
#### Example
|
||||
|
||||
@@ -118,6 +129,31 @@ 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_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_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,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
|
||||
}
|
||||
@@ -67,10 +67,13 @@ func (p *ArtifactAPIProvider) Resources(_ context.Context) []func() resource.Res
|
||||
newRemoteResource("puppet"),
|
||||
newRemoteResource("terraform"),
|
||||
newRemoteResource("goproxy"),
|
||||
newRemoteResource("github_rpm"),
|
||||
NewVirtualResource,
|
||||
NewLocalTerraformResource,
|
||||
NewLocalPyPIResource,
|
||||
NewLocalRPMResource,
|
||||
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 + 1 local_terraform + 1 local_pypi + 1 local_rpm = 14
|
||||
expectedCount := 14
|
||||
// 11 remote resource types + 1 virtual + local_terraform/pypi/rpm/docker/generic = 17
|
||||
expectedCount := 17
|
||||
if len(resources) != expectedCount {
|
||||
t.Fatalf("expected %d resources, got %d", expectedCount, len(resources))
|
||||
}
|
||||
@@ -106,10 +106,13 @@ func TestProvider_Resources_ContainsExpectedTypes(t *testing.T) {
|
||||
"artifactapi_remote_puppet",
|
||||
"artifactapi_remote_terraform",
|
||||
"artifactapi_remote_goproxy",
|
||||
"artifactapi_remote_github_rpm",
|
||||
"artifactapi_virtual",
|
||||
"artifactapi_local_terraform",
|
||||
"artifactapi_local_pypi",
|
||||
"artifactapi_local_rpm",
|
||||
"artifactapi_local_docker",
|
||||
"artifactapi_local_generic",
|
||||
}
|
||||
|
||||
for _, name := range expected {
|
||||
|
||||
@@ -0,0 +1,164 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/path"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
)
|
||||
|
||||
var (
|
||||
_ resource.Resource = &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")
|
||||
}
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
@@ -66,6 +66,7 @@ func NewRemoteAlpine() resource.Resource { return &remoteResource{packageType
|
||||
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 (r *remoteResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
|
||||
resp.TypeName = req.ProviderTypeName + "_remote_" + r.packageType
|
||||
@@ -145,7 +146,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 remote (302 redirect of .rpm downloads to a generic github.com remote).",
|
||||
Optional: true, Computed: true, Default: stringdefault.StaticString(""),
|
||||
},
|
||||
"upstream_dial_timeout": schema.Int64Attribute{
|
||||
|
||||
@@ -409,6 +409,7 @@ func TestRemoteResource_Metadata(t *testing.T) {
|
||||
{"helm", "artifactapi_remote_helm"},
|
||||
{"pypi", "artifactapi_remote_pypi"},
|
||||
{"npm", "artifactapi_remote_npm"},
|
||||
{"github_rpm", "artifactapi_remote_github_rpm"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
@@ -424,6 +425,34 @@ 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 TestRemoteResource_Schema(t *testing.T) {
|
||||
r := &remoteResource{packageType: "docker"}
|
||||
req := resource.SchemaRequest{}
|
||||
@@ -463,6 +492,7 @@ 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"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
|
||||
Reference in New Issue
Block a user