From 9fb370e0a069b8c953671e70181f9ed7cbc951c1 Mon Sep 17 00:00:00 2001 From: 030 Date: Sun, 22 Oct 2023 16:03:37 +0200 Subject: [PATCH] fix: [#384] Create groups. --- cmd/n3dr/configRepository.go | 29 ++++- .../app/n3dr/config/repository/repository.go | 107 ++++++++++++++++++ 2 files changed, 135 insertions(+), 1 deletion(-) diff --git a/cmd/n3dr/configRepository.go b/cmd/n3dr/configRepository.go index d8e78d4e..7a78fcb3 100644 --- a/cmd/n3dr/configRepository.go +++ b/cmd/n3dr/configRepository.go @@ -13,6 +13,7 @@ var ( configRepoDockerPortSecure, configRepoDelete, snapshot, strictContentTypeValidation bool configRepoDockerPort int32 configRepoName, configRepoRecipe, configRepoType, configRepoProxyURL string + configRepoGroupMemberNames []string ) // configRepositoryCmd represents the configRepository command. @@ -40,6 +41,15 @@ Examples: # Create a Rubygems repository: n3dr configRepository -u admin -p some-pass -n localhost:9000 --https=false --configRepoName 3rdparty-rubygems --configRepoType gem + + # Create a Maven2 proxy: + n3dr configRepository --configRepoType maven2 --configRepoName 3rdparty-maven --configRepoRecipe proxy --configRepoProxyURL https://repo.maven.apache.org/maven2/ + + # Create a NPM proxy: + n3dr configRepository --configRepoType npm --configRepoName 3rdparty-npm --configRepoRecipe proxy --configRepoProxyURL https://registry.npmjs.org/ + + # Create a group: + n3dr configRepository --configRepoType maven2 --configRepoRecipe group --configRepoName some-group --configRepoGroupMemberNames releases,snapshots `, Run: func(cmd *cobra.Command, args []string) { n := connection.Nexus3{ @@ -89,16 +99,32 @@ Examples: } } case "maven2": - if configRepoRecipe == "hosted" { + if configRepoRecipe == "group" { + if err := r.CreateMavenGroup(configRepoGroupMemberNames, configRepoName); err != nil { + log.Fatal(err) + } + } else if configRepoRecipe == "hosted" { if err := r.CreateMavenHosted(configRepoName, snapshot); err != nil { log.Fatal(err) } + } else if configRepoRecipe == "proxy" { + if err := r.CreateMavenProxied(configRepoName); err != nil { + log.Fatal(err) + } + } else { + log.Fatalf("configRepoRecipe: '%s' not supported in conjunction with configRepoType: '%s'", configRepoRecipe, configRepoType) } case "npm": if configRepoRecipe == "hosted" { if err := r.CreateNpmHosted(configRepoName, snapshot); err != nil { log.Fatal(err) } + } else if configRepoRecipe == "proxy" { + if err := r.CreateNpmProxied(configRepoName); err != nil { + log.Fatal(err) + } + } else { + log.Fatalf("configRepoRecipe: '%s' not supported in conjunction with configRepoType: '%s'", configRepoRecipe, configRepoType) } case "raw": if configRepoRecipe == "hosted" { @@ -140,4 +166,5 @@ func init() { configRepositoryCmd.Flags().Int32Var(&configRepoDockerPort, "configRepoDockerPort", 8082, "The docker connector port, e.g. 8082") configRepositoryCmd.Flags().BoolVar(&configRepoDockerPortSecure, "configRepoDockerPortSecure", false, "Whether the docker connector port should be secure") configRepositoryCmd.Flags().BoolVar(&strictContentTypeValidation, "strictContentTypeValidation", true, "whether strictContentTypeValidation should be enabled") + configRepositoryCmd.Flags().StringSliceVar(&configRepoGroupMemberNames, "configRepoGroupMemberNames", []string{}, "The repository type, e.g.: 'apt', 'raw'") } diff --git a/internal/app/n3dr/config/repository/repository.go b/internal/app/n3dr/config/repository/repository.go index f2867971..f2aebf6f 100644 --- a/internal/app/n3dr/config/repository/repository.go +++ b/internal/app/n3dr/config/repository/repository.go @@ -31,6 +31,40 @@ func created(name string, err error) error { return fmt.Errorf("could not create repository: '%v', err: '%w'", name, err) } +func (r *Repository) CreateMavenGroup(memberNames []string, name string) error { + log.Infof("creating maven group: '%s'...", name) + client, err := r.Nexus3.Client() + if err != nil { + return err + } + if name == "" { + return fmt.Errorf("repo name should not be empty") + } + if len(memberNames) == 0 { + return fmt.Errorf("memberNames should not be empty") + } + + online := true + mhsa := models.StorageAttributes{BlobStoreName: "default", StrictContentTypeValidation: &r.StrictContentTypeValidation} + group := models.GroupAttributes{MemberNames: memberNames} + body := models.MavenGroupRepositoryAPIRequest{ + Group: &group, + Name: &name, + Online: &online, + Storage: &mhsa, + } + createMavenGroup := repository_management.CreateRepositoryParams{Body: &body} + createMavenGroup.WithTimeout(time.Second * 30) + if _, err := client.RepositoryManagement.CreateRepository(&createMavenGroup); err != nil { + if err := created(name, err); err != nil { + return err + } + } + log.Infof("created the following maven group: '%v'", name) + + return nil +} + func (r *Repository) CreateAptProxied(name string) error { log.Infof("Creating proxied apt repository: '%s'...", name) client, err := r.Nexus3.Client() @@ -68,6 +102,42 @@ func (r *Repository) CreateAptProxied(name string) error { return nil } +func (r *Repository) CreateNpmProxied(name string) error { + log.Infof("Creating npm proxy: '%s'...", name) + client, err := r.Nexus3.Client() + if err != nil { + return err + } + if name == "" { + return fmt.Errorf("repo name should not be empty") + } + + httpClientBlocked := false + httpClientAutoBlocked := true + httpClient := models.HTTPClientAttributes{AutoBlock: &httpClientAutoBlocked, Blocked: &httpClientBlocked} + negativeCacheEnabled := true + var negativeCacheTimeToLive int32 = 1440 + negativeCache := models.NegativeCacheAttributes{Enabled: &negativeCacheEnabled, TimeToLive: &negativeCacheTimeToLive} + var contentMaxAge int32 = 1440 + var metadataMaxAge int32 = 1440 + remoteURL := r.ProxyRemoteURL + proxy := models.ProxyAttributes{ContentMaxAge: &contentMaxAge, MetadataMaxAge: &metadataMaxAge, RemoteURL: remoteURL} + online := true + npm := models.NpmAttributes{} + mhsa := models.StorageAttributes{BlobStoreName: "default", StrictContentTypeValidation: &r.StrictContentTypeValidation} + ma := models.NpmProxyRepositoryAPIRequest{Npm: &npm, Name: &name, Online: &online, Storage: &mhsa, Proxy: &proxy, NegativeCache: &negativeCache, HTTPClient: &httpClient} + createNpmProxy := repository_management.CreateRepository10Params{Body: &ma} + createNpmProxy.WithTimeout(time.Second * 30) + if _, err := client.RepositoryManagement.CreateRepository10(&createNpmProxy); err != nil { + if err := created(name, err); err != nil { + return err + } + } + log.Infof("created the following repository: '%v'", name) + + return nil +} + func (r *Repository) CreateYumProxied(name string) error { log.Infof("Creating proxied yum repository: '%s'...", name) client, err := r.Nexus3.Client() @@ -103,6 +173,43 @@ func (r *Repository) CreateYumProxied(name string) error { return nil } +func (r *Repository) CreateMavenProxied(name string) error { + log.Infof("creating the following maven proxy: '%s'...", name) + client, err := r.Nexus3.Client() + if err != nil { + return err + } + if name == "" { + return fmt.Errorf("repo name should not be empty") + } + + httpClientBlocked := false + httpClientAutoBlocked := true + httpClient := models.HTTPClientAttributesWithPreemptiveAuth{AutoBlock: &httpClientAutoBlocked, Blocked: &httpClientBlocked} + negativeCacheEnabled := true + var negativeCacheTimeToLive int32 = 1440 + negativeCache := models.NegativeCacheAttributes{Enabled: &negativeCacheEnabled, TimeToLive: &negativeCacheTimeToLive} + var contentMaxAge int32 = 1440 + var metadataMaxAge int32 = 1440 + remoteURL := r.ProxyRemoteURL + log.Infof("remoteURL: '%s'", remoteURL) + proxy := models.ProxyAttributes{ContentMaxAge: &contentMaxAge, MetadataMaxAge: &metadataMaxAge, RemoteURL: remoteURL} + online := true + maven := models.MavenAttributes{LayoutPolicy: "STRICT", VersionPolicy: "MIXED"} + mhsa := models.StorageAttributes{BlobStoreName: "default", StrictContentTypeValidation: &r.StrictContentTypeValidation} + ma := models.MavenProxyRepositoryAPIRequest{Maven: &maven, Name: &name, Online: &online, Storage: &mhsa, Proxy: &proxy, NegativeCache: &negativeCache, HTTPClient: &httpClient} + createMavenProxy := repository_management.CreateRepository2Params{Body: &ma} + createMavenProxy.WithTimeout(time.Second * 30) + if _, err := client.RepositoryManagement.CreateRepository2(&createMavenProxy); err != nil { + if err := created(name, err); err != nil { + return err + } + } + log.Infof("created the following maven proxy: '%v'", name) + + return nil +} + func (r *Repository) CreateDockerHosted(secure bool, port int32, name string) error { log.Infof("Creating docker hosted repository: '%s'...", name) client, err := r.Nexus3.Client()