Skip to content

Commit

Permalink
fix: [#384] Create groups.
Browse files Browse the repository at this point in the history
  • Loading branch information
030 committed Oct 22, 2023
1 parent 03a282a commit 9fb370e
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 1 deletion.
29 changes: 28 additions & 1 deletion cmd/n3dr/configRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var (
configRepoDockerPortSecure, configRepoDelete, snapshot, strictContentTypeValidation bool
configRepoDockerPort int32
configRepoName, configRepoRecipe, configRepoType, configRepoProxyURL string
configRepoGroupMemberNames []string
)

// configRepositoryCmd represents the configRepository command.
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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" {
Expand Down Expand Up @@ -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'")
}
107 changes: 107 additions & 0 deletions internal/app/n3dr/config/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 9fb370e

Please sign in to comment.