diff --git a/sdks/go/pkg/beam/core/runtime/xlangx/expansionx/download.go b/sdks/go/pkg/beam/core/runtime/xlangx/expansionx/download.go index 0f794668d87e..59a1d575248b 100644 --- a/sdks/go/pkg/beam/core/runtime/xlangx/expansionx/download.go +++ b/sdks/go/pkg/beam/core/runtime/xlangx/expansionx/download.go @@ -50,6 +50,24 @@ func newJarGetter() *jarGetter { return &jarGetter{repository: apacheRepository, groupID: beamGroupID, jarCache: cacheDir} } +// GetRepositoryURL returns the current target URL for the defaultJarGetter, +// indicating what repository will be connected to when getting a Beam JAR. +func GetDefaultRepositoryURL() string { + return defaultJarGetter.getRepositoryURL() +} + +// SetRepositoryURL updates the target URL for the defaultJarGetter, changing +// which Maven repository will be connected to when getting a Beam JAR. Also +// validates that it has been passed a URL and returns an error if not. +// +// When changing the target repository, make sure that the value is the prefix +// up to "org/apache/beam" and that the organization of the repository matches +// that of the default from that point on to ensure that the conversion of the +// Gradle target to the JAR name is correct. +func SetDefaultRepositoryURL(repoURL string) error { + return defaultJarGetter.setRepositoryURL(repoURL) +} + // GetBeamJar checks a temporary directory for the desired Beam JAR, downloads the // appropriate JAR from Maven if not present, then returns the file path to the // JAR. @@ -57,6 +75,18 @@ func GetBeamJar(gradleTarget, version string) (string, error) { return defaultJarGetter.getJar(gradleTarget, version) } +func (j *jarGetter) getRepositoryURL() string { + return string(j.repository) +} + +func (j *jarGetter) setRepositoryURL(repoURL string) error { + if !strings.HasPrefix(repoURL, "http") { + return fmt.Errorf("repo URL %v does not have an http or https prefix", repoURL) + } + j.repository = url(strings.TrimSuffix(repoURL, "/")) + return nil +} + func (j *jarGetter) getJar(gradleTarget, version string) (string, error) { strippedTarget := dropEndOfGradleTarget(gradleTarget) fullURL, jarName := j.getURLForBeamJar(strippedTarget, version) @@ -84,7 +114,7 @@ func (j *jarGetter) getJar(gradleTarget, version string) (string, error) { defer resp.Body.Close() if resp.StatusCode != 200 { - return "", fmt.Errorf("received non 200 response code, got %v", resp.StatusCode) + return "", fmt.Errorf("failed to connect to %v: received non 200 response code, got %v", fullURL, resp.StatusCode) } file, err := os.Create(jarPath) diff --git a/sdks/go/pkg/beam/core/runtime/xlangx/expansionx/download_test.go b/sdks/go/pkg/beam/core/runtime/xlangx/expansionx/download_test.go index 1d13faca6bbf..b20977b5e704 100644 --- a/sdks/go/pkg/beam/core/runtime/xlangx/expansionx/download_test.go +++ b/sdks/go/pkg/beam/core/runtime/xlangx/expansionx/download_test.go @@ -23,6 +23,72 @@ import ( "testing" ) +func TestGetAndSetRepositoryURL(t *testing.T) { + tests := []struct { + name string + newRepo string + expRepo string + }{ + { + "correct URL", + "http://new.repo.org", + "http://new.repo.org", + }, + { + "correct URL https", + "https://new.repo.org", + "https://new.repo.org", + }, + { + "correct URL with trailing backslash", + "http://new.repo.org/", + "http://new.repo.org", + }, + { + "correct URL https with trailing backslash", + "https://new.repo.org/", + "https://new.repo.org", + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + j := newJarGetter() + err := j.setRepositoryURL(test.newRepo) + if err != nil { + t.Errorf("failed to set repository URL, got %v", err) + } + if got, want := j.getRepositoryURL(), test.expRepo; got != want { + t.Errorf("getRepositoryURL() got %v, want %v", got, want) + } + }) + } +} + +func TestGetAndSetRepositoryURL_bad(t *testing.T) { + tests := []struct { + name string + newRepo string + }{ + { + "no http", + "new.maven.repo.com", + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + j := newJarGetter() + err := j.setRepositoryURL(test.newRepo) + if err == nil { + t.Errorf("setRepositoryURL(%v) succeeded when it should have failed", test.newRepo) + } + // Check that the failed Set call did not change the URL. + if got, want := j.getRepositoryURL(), string(apacheRepository); got != want { + t.Errorf("getRepositoryURL() got %v, want %v", got, want) + } + }) + } +} + func TestGetURLForBeamJar(t *testing.T) { tests := []struct { name string