Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add private registry provider platform API #406

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions errors.go
Expand Up @@ -155,6 +155,20 @@ var (
ErrInvalidCommentID = errors.New("invalid value for comment ID")

ErrInvalidCommentBody = errors.New("invalid value for comment body")

ErrInvalidNamespace = errors.New("invalid value for namespace")

ErrInvalidPrivateProviderNamespaceDoesntMatchOrganization = errors.New("invalid namespace must match organization name for private providers")

ErrInvalidRegistryName = errors.New("invalid value for registry-name")

ErrInvalidRegistryNameType = errors.New("invalid type for registry-name. Please use 'RegistryName'")

ErrInvalidKeyID = errors.New("invalid value for key-id")

ErrInvalidOS = errors.New("invalid value for OS")

ErrInvalidArch = errors.New("invalid value for arch")
)

// Missing values for required field/option
Expand Down Expand Up @@ -268,4 +282,14 @@ var (
ErrEmptyTeamName = errors.New("team name can not be empty")

ErrInvalidEmail = errors.New("email is invalid")

ErrRequiredPrivateRegistry = errors.New("only private registry is allowed")

ErrRequiredOS = errors.New("OS is required")

ErrRequiredArch = errors.New("arch is required")

ErrRequiredShasum = errors.New("shasum is required")

ErrRequiredFilename = errors.New("filename is required")
)
3 changes: 3 additions & 0 deletions generate_mocks.sh
Expand Up @@ -35,6 +35,9 @@ mockgen -source=policy_set.go -destination=mocks/policy_set_mocks.go -package=mo
mockgen -source=policy_set_parameter.go -destination=mocks/policy_set_parameter_mocks.go -package=mocks
mockgen -source=policy_set_version.go -destination=mocks/policy_set_version_mocks.go -package=mocks
mockgen -source=registry_module.go -destination=mocks/registry_module_mocks.go -package=mocks
mockgen -source=registry_provider.go -destination=mocks/registry_provider_mocks.go -package=mocks
mockgen -source=registry_provider_platform.go -destination=mocks/registry_provider_platform_mocks.go -package=mocks
mockgen -source=registry_provider_version.go -destination=mocks/registry_provider_version_mocks.go -package=mocks
mockgen -source=run.go -destination=mocks/run_mocks.go -package=mocks
mockgen -source=run_task.go -destination=mocks/run_tasks.go -package=mocks
mockgen -source=run_trigger.go -destination=mocks/run_trigger_mocks.go -package=mocks
Expand Down
213 changes: 207 additions & 6 deletions helper_test.go
Expand Up @@ -10,6 +10,7 @@ import (
"errors"
"fmt"
"io/ioutil"
"math/rand"
"os"
"sync"
"testing"
Expand Down Expand Up @@ -391,7 +392,7 @@ func createOAuthToken(t *testing.T, client *Client, org *Organization) (*OAuthTo

func createOrganization(t *testing.T, client *Client) (*Organization, func()) {
return createOrganizationWithOptions(t, client, OrganizationCreateOptions{
Name: String("tst-" + randomString(t)),
Name: String("org-" + randomString(t)), // Max of 40 chars
Email: String(fmt.Sprintf("%s@tfe.local", randomString(t))),
})
}
Expand Down Expand Up @@ -717,23 +718,24 @@ func createRegistryModuleWithVersion(t *testing.T, client *Client, org *Organiza
}

func createRunTask(t *testing.T, client *Client, org *Organization) (*RunTask, func()) {
runTaskURL := os.Getenv("TFC_RUN_TASK_URL")
if runTaskURL == "" {
t.Error("Cannot create a run task with an empty URL. You must set TFC_RUN_TASK_URL for run task related tests.")
}

var orgCleanup func()

if org == nil {
org, orgCleanup = createOrganization(t, client)
}

runTaskURL := os.Getenv("TFC_RUN_TASK_URL")
if runTaskURL == "" {
t.Error("Cannot create a run task with an empty URL. You must set TFC_RUN_TASK_URL for run task related tests.")
}

ctx := context.Background()
r, err := client.RunTasks.Create(ctx, org.Name, RunTaskCreateOptions{
Name: "tst-" + randomString(t),
URL: runTaskURL,
Category: "task",
})

if err != nil {
t.Fatal(err)
}
Expand All @@ -751,6 +753,201 @@ func createRunTask(t *testing.T, client *Client, org *Organization) (*RunTask, f
}
}

func createPrivateRegistryProvider(t *testing.T, client *Client, org *Organization) (*RegistryProvider, func()) {
var orgCleanup func()

if org == nil {
org, orgCleanup = createOrganization(t, client)
}

ctx := context.Background()

options := RegistryProviderCreateOptions{
Name: "test-registry-provider-" + randomString(t),
Namespace: org.Name,
RegistryName: PrivateRegistry,
}

prv, err := client.RegistryProviders.Create(ctx, org.Name, options)

if err != nil {
t.Fatal(err)
}

prv.Organization = org

return prv, func() {
id := RegistryProviderID{
OrganizationName: org.Name,
RegistryName: prv.RegistryName,
Namespace: prv.Namespace,
Name: prv.Name,
}

if err := client.RegistryProviders.Delete(ctx, id); err != nil {
t.Errorf("Error destroying registry provider! WARNING: Dangling resources\n"+
"may exist! The full error is shown below.\n\n"+
"Registry Provider: %s/%s\nError: %s", prv.Namespace, prv.Name, err)
}

if orgCleanup != nil {
orgCleanup()
}
}
}

func createPublicRegistryProvider(t *testing.T, client *Client, org *Organization) (*RegistryProvider, func()) {
var orgCleanup func()

if org == nil {
org, orgCleanup = createOrganization(t, client)
}

ctx := context.Background()

options := RegistryProviderCreateOptions{
Name: "test-name-" + randomString(t),
Namespace: "test-namespace-" + randomString(t),
RegistryName: PublicRegistry,
}

prv, err := client.RegistryProviders.Create(ctx, org.Name, options)

if err != nil {
t.Fatal(err)
}

prv.Organization = org

return prv, func() {
id := RegistryProviderID{
OrganizationName: org.Name,
RegistryName: prv.RegistryName,
Namespace: prv.Namespace,
Name: prv.Name,
}

if err := client.RegistryProviders.Delete(ctx, id); err != nil {
t.Errorf("Error destroying registry provider! WARNING: Dangling resources\n"+
"may exist! The full error is shown below.\n\n"+
"Registry Provider: %s/%s\nError: %s", prv.Namespace, prv.Name, err)
}

if orgCleanup != nil {
orgCleanup()
}
}
}

func createRegistryProviderPlatform(t *testing.T, client *Client, provider *RegistryProvider, version *RegistryProviderVersion) (*RegistryProviderPlatform, func()) {
var providerCleanup func()
var versionCleanup func()

if provider == nil {
provider, providerCleanup = createPrivateRegistryProvider(t, client, nil)
}

providerID := RegistryProviderID{
OrganizationName: provider.Organization.Name,
RegistryName: provider.RegistryName,
Namespace: provider.Namespace,
Name: provider.Name,
}

if version == nil {
version, versionCleanup = createRegistryProviderVersion(t, client, provider)
}

versionID := RegistryProviderVersionID{
RegistryProviderID: providerID,
Version: version.Version,
}

ctx := context.Background()

options := RegistryProviderPlatformCreateOptions{
OS: randomString(t),
Arch: randomString(t),
Shasum: genSha(t, "secret", "data"),
Filename: randomString(t),
}

rpp, err := client.RegistryProviderPlatforms.Create(ctx, versionID, options)

if err != nil {
t.Fatal(err)
}

return rpp, func() {
platformID := RegistryProviderPlatformID{
RegistryProviderVersionID: versionID,
OS: rpp.OS,
Arch: rpp.Arch,
}

if err := client.RegistryProviderPlatforms.Delete(ctx, platformID); err != nil {
t.Errorf("Error destroying registry provider platform! WARNING: Dangling resources\n"+
"may exist! The full error is shown below.\n\n"+
"Registry Provider Version: %s/%s/%s/%s\nError: %s", rpp.RegistryProviderVersion.RegistryProvider.Namespace, rpp.RegistryProviderVersion.RegistryProvider.Name, rpp.OS, rpp.Arch, err)
}

if versionCleanup != nil {
versionCleanup()
}
if providerCleanup != nil {
providerCleanup()
}
}
}

func createRegistryProviderVersion(t *testing.T, client *Client, provider *RegistryProvider) (*RegistryProviderVersion, func()) {
var providerCleanup func()

if provider == nil {
provider, providerCleanup = createPrivateRegistryProvider(t, client, nil)
}

providerID := RegistryProviderID{
OrganizationName: provider.Organization.Name,
RegistryName: provider.RegistryName,
Namespace: provider.Namespace,
Name: provider.Name,
}

ctx := context.Background()

options := RegistryProviderVersionCreateOptions{
Version: randomSemver(t),
KeyID: randomString(t),
Protocols: []string{"4.0", "5.0", "6.0"},
}

prvv, err := client.RegistryProviderVersions.Create(ctx, providerID, options)

if err != nil {
t.Fatal(err)
}

prvv.RegistryProvider = provider

return prvv, func() {
id := RegistryProviderVersionID{
Version: options.Version,
RegistryProviderID: providerID,
}

if err := client.RegistryProviderVersions.Delete(ctx, id); err != nil {
t.Errorf("Error destroying registry provider version! WARNING: Dangling resources\n"+
"may exist! The full error is shown below.\n\n"+
"Registry Provider Version: %s/%s/%s\nError: %s", prvv.RegistryProvider.Namespace, prvv.RegistryProvider.Name, prvv.Version, err)
}

if providerCleanup != nil {
providerCleanup()
}
}
}

func createSSHKey(t *testing.T, client *Client, org *Organization) (*SSHKey, func()) {
var orgCleanup func()

Expand Down Expand Up @@ -1271,6 +1468,10 @@ func randomString(t *testing.T) string {
return v
}

func randomSemver(t *testing.T) string {
return fmt.Sprintf("%d.%d.%d", rand.Intn(99)+3, rand.Intn(99)+1, rand.Intn(99)+1)
}

// skips a test if the environment is for Terraform Cloud.
func skipIfCloud(t *testing.T) {
if !enterpriseEnabled() {
Expand Down