Skip to content

Commit

Permalink
Adding GetProviderReverseAliases API call
Browse files Browse the repository at this point in the history
Signed-off-by: AbstractionFactory <[email protected]>
  • Loading branch information
abstractionfactory committed Sep 2, 2024
1 parent 0245542 commit d33be2c
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
3 changes: 3 additions & 0 deletions metadata/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ type ProviderDataAPI interface {
// non-recursive, returning a *ProviderNotFoundError if the target provider does not exist.
GetProviderCanonicalAddr(ctx context.Context, addr provider.Addr) (provider.Addr, error)

// GetProviderReverseAliases returns the aliases to which the current address is the canonical address.
GetProviderReverseAliases(ctx context.Context, addr provider.Addr) ([]provider.Addr, error)

// PutProvider queues up writing the specified provider metadata.
PutProvider(ctx context.Context, addr provider.Addr, metadata provider.Metadata) error
// DeleteProvider queues up deleting the specified provider.
Expand Down
37 changes: 37 additions & 0 deletions metadata/provider_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,40 @@ func (r registryDataAPI) getProviderCanonical(ctx context.Context, providerAddr
ProviderAddr: providerAddr,
}
}

func (r registryDataAPI) GetProviderReverseAliases(ctx context.Context, addr provider.Addr) ([]provider.Addr, error) {
providerAliases, err := r.ListProviderAliases(ctx)
if err != nil {
return nil, err
}
namespaceAliases, err := r.ListProviderNamespaceAliases(ctx)
if err != nil {
return nil, err
}

var results []provider.Addr
for alias, target := range providerAliases {
if target.Equals(addr) {
results = append(results, alias)
// Look up the namespace alias of the provider alias to include it in the list.
for namespace, targetNamespace := range namespaceAliases {
if provider.NormalizeNamespace(targetNamespace) == provider.NormalizeNamespace(alias.Namespace) {
results = append(results, provider.Addr{
Namespace: namespace,
Name: target.Name,
})
}
}

}
}
for namespace, targetNamespace := range namespaceAliases {
if provider.NormalizeNamespace(targetNamespace) == provider.NormalizeNamespace(addr.Namespace) {
results = append(results, provider.Addr{
Namespace: namespace,
Name: addr.Name,
})
}
}
return results, nil
}
62 changes: 62 additions & 0 deletions metadata/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,65 @@ func TestProviderIndividualAliases(t *testing.T) {
}
}
}

// TestProviderReverseAliases tests looking up the reverse aliases.
func TestProviderReverseAliases(t *testing.T) {
// TODO: this test relies on the hard-coded list of aliases. This should be changed to creating aliases dynamically.
canonicalAddr := provider.Addr{
Namespace: "integrations",
Name: "github",
}
aliasedAddr1 := provider.Addr{
Namespace: "opentofu",
Name: "github",
}
aliasedAddr2 := provider.Addr{
Namespace: "hashicorp",
Name: "github",
}

providerVersion := provider.Version{
Version: "v1.0.0",
Protocols: []string{"5.0"},
SHASumsURL: "https://localhost/" + canonicalAddr.Namespace + "/" + canonicalAddr.Name + "/releases/download/v1.0.0/" + canonicalAddr.String() + "_SHA256SUMS",
SHASumsSignatureURL: "https://localhost/" + canonicalAddr.Namespace + "/" + canonicalAddr.Name + "/releases/download/v1.0.0/" + canonicalAddr.String() + "_SHA256SUMS.sig",
Targets: []provider.Target{
{
OS: "linux",
Arch: "amd64",
Filename: canonicalAddr.String() + "_linux_amd64.zip",
DownloadURL: "https://localhost/" + canonicalAddr.Namespace + "/" + canonicalAddr.Name + "/releases/download/v1.0.0/" + canonicalAddr.String() + "_linux_amd64.zip",
SHASum: "c0535e4be2b79ffd93291305436bf889314e4a3faec05ecffcbb7df31ad9e51a",
},
},
}

providerMetadata := provider.Metadata{
Versions: []provider.Version{
providerVersion,
},
}

storage := memory.New()
api, err := metadata.New(storage)
if err != nil {
t.Fatalf("Failed to initialize API (%v)", err)
}

ctx := context.Background()

if err := api.PutProvider(ctx, canonicalAddr, providerMetadata); err != nil {
t.Fatalf("Failed to put provider (%v)", err)
}

reverseAliases, err := api.GetProviderReverseAliases(ctx, canonicalAddr)
if err != nil {
t.Fatalf("Failed to get provider reverse aliases (%v)", err)
}
if len(reverseAliases) != 2 {
t.Fatalf("Incorrect number of reverse aliases returned (%d).", len(reverseAliases))
}
if !((reverseAliases[0].Equals(aliasedAddr1) && reverseAliases[1].Equals(aliasedAddr2)) || (reverseAliases[0].Equals(aliasedAddr2) && reverseAliases[1].Equals(aliasedAddr1))) {
t.Fatalf("Incorrect reverse aliases returned.")
}
}

0 comments on commit d33be2c

Please sign in to comment.