Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/harbor/root/instance/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ These instances represent external services such as Dragonfly or Kraken that hel
CreateInstanceCommand(),
DeleteInstanceCommand(),
ListInstanceCommand(),
PingInstanceCommand(),
UpdateInstanceCommand(),
ViewInstanceCommand(),
)
return cmd
}
69 changes: 51 additions & 18 deletions cmd/harbor/root/instance/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

func CreateInstanceCommand() *cobra.Command {
var opts create.CreateView
var authUsername, authPassword, authToken string

cmd := &cobra.Command{
Use: "create",
Expand All @@ -35,43 +36,73 @@ You will need to provide the instance's name, vendor, endpoint, and optionally o
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
var err error
createView := &create.CreateView{
Name: opts.Name,
Vendor: opts.Vendor,
Description: opts.Description,
Endpoint: opts.Endpoint,
Insecure: opts.Insecure,
Enabled: opts.Enabled,
AuthMode: opts.AuthMode,
AuthInfo: opts.AuthInfo,
}
var instanceName string

if opts.Name != "" && opts.Vendor != "" && opts.Endpoint != "" {
formattedEndpoint := utils.FormatUrl(opts.Endpoint)
if err := utils.ValidateURL(formattedEndpoint); err != nil {
return err
}
opts.Endpoint = formattedEndpoint

switch opts.AuthMode {
case "BASIC":
if authUsername == "" || authPassword == "" {
return fmt.Errorf("username and password are required when authmode is BASIC. Use --auth-username and --auth-password flags")
}
opts.AuthInfo = map[string]string{
"username": authUsername,
"password": authPassword,
}
case "OAUTH":
if authToken == "" {
return fmt.Errorf("token is required when authmode is OAUTH. Use --auth-token flag")
}
opts.AuthInfo = map[string]string{
"token": authToken,
}
case "NONE":
// Auth credentials are ignored when authmode is NONE
default:
return fmt.Errorf("invalid authmode '%s'. Valid options: NONE, BASIC, OAUTH", opts.AuthMode)
}

err = api.CreateInstance(opts)
instanceName = opts.Name
} else {
createView := &create.CreateView{
Name: opts.Name,
Vendor: opts.Vendor,
Description: opts.Description,
Endpoint: opts.Endpoint,
Insecure: opts.Insecure,
Enabled: opts.Enabled,
AuthMode: opts.AuthMode,
}
err = createInstanceView(createView)
instanceName = createView.Name
}

if err != nil {
return fmt.Errorf("failed to create instance: %v", err)
return fmt.Errorf("failed to create instance: %v", utils.ParseHarborErrorMsg(err))
}

fmt.Printf("Instance '%s' created successfully\n", instanceName)
return nil
},
}

flags := cmd.Flags()
flags.StringVarP(&opts.Name, "name", "n", "", "Name of the instance")
flags.StringVarP(&opts.Vendor, "provider", "p", "", "Provider for the instance")
flags.StringVarP(&opts.Endpoint, "url", "u", "", "URL for the instance")
flags.StringVarP(&opts.Description, "description", "", "", "Description of the instance")
flags.BoolVarP(&opts.Insecure, "insecure", "i", true, "Whether or not the certificate will be verified when Harbor tries to access the server")
flags.BoolVarP(&opts.Enabled, "enable", "", true, "Whether it is enabled or not")
flags.StringVarP(&opts.AuthMode, "authmode", "a", "NONE", "Choosing different types of authentication method")
flags.StringVarP(&opts.Vendor, "provider", "p", "", "Provider for the instance (e.g. dragonfly, kraken)")
flags.StringVarP(&opts.Endpoint, "url", "u", "", "Endpoint URL for the instance")
flags.StringVarP(&opts.Description, "description", "d", "", "Description of the instance")
flags.BoolVarP(&opts.Insecure, "insecure", "", false, "Whether or not the certificate will be verified when Harbor tries to access the server")
flags.BoolVarP(&opts.Enabled, "enable", "", true, "Whether the instance is enabled or not")
flags.StringVarP(&opts.AuthMode, "authmode", "a", "NONE", "Authentication mode (NONE, BASIC, OAUTH)")
flags.StringVar(&authUsername, "auth-username", "", "Username for BASIC authentication")
flags.StringVar(&authPassword, "auth-password", "", "Password for BASIC authentication")
flags.StringVar(&authToken, "auth-token", "", "Token for OAUTH authentication")

return cmd
}
Expand All @@ -81,6 +112,8 @@ func createInstanceView(createView *create.CreateView) error {
createView = &create.CreateView{}
}

create.CreateInstanceView(createView)
if err := create.CreateInstanceView(createView); err != nil {
return err
}
return api.CreateInstance(*createView)
}
5 changes: 4 additions & 1 deletion cmd/harbor/root/instance/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ If no argument is provided, you will be prompted to select an instance from a li
} else if len(args) > 0 {
instanceName = args[0]
} else {
instanceName = prompt.GetInstanceFromUser()
instanceName, err = prompt.GetInstanceNameFromUser()
if err != nil {
return fmt.Errorf("%v", err)
}
}
err = api.DeleteInstance(instanceName)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/harbor/root/instance/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ This command provides an easy way to view all instances along with their details
return fmt.Errorf("page size should be less than or equal to 100")
}

instance, err := api.ListInstance(opts)
instance, err := api.ListAllInstance(opts)

if err != nil {
return fmt.Errorf("failed to get instance list: %v", err)
Expand Down
78 changes: 78 additions & 0 deletions cmd/harbor/root/instance/ping.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package instance

import (
"fmt"

"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/prompt"
"github.com/goharbor/harbor-cli/pkg/utils"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func PingInstanceCommand() *cobra.Command {
var useInstanceID bool

cmd := &cobra.Command{
Use: "ping [NAME|ID]",
Short: "Ping preheat provider instance by name or id",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
var err error
var instanceName string

if useInstanceID && len(args) == 0 {
return fmt.Errorf("instance ID must be provided when using --id")
}

if len(args) > 0 {
log.Debugf("Instance name provided: %s", args[0])
instanceName = args[0]
} else {
log.Debug("No instance name provided, prompting user")
instanceName, err = prompt.GetInstanceNameFromUser()
if err != nil {
return fmt.Errorf("failed to get instance name: %v", utils.ParseHarborErrorMsg(err))
}
}

log.Debugf("Pinging instance: %s", instanceName)
response, err := api.PingInstance(instanceName, useInstanceID)
if err != nil {
if utils.ParseHarborErrorCode(err) == "404" {
return fmt.Errorf("instance %s does not exist", instanceName)
}
return fmt.Errorf("failed to ping instance: %v", utils.ParseHarborErrorMsg(err))
}

outputFormat := viper.GetString("output-format")
if outputFormat != "" {
if err := utils.PrintFormat(response, outputFormat); err != nil {
return err
}
} else {
fmt.Printf("Instance '%s' pinged successfully\n", instanceName)
}
return nil
},
}

flags := cmd.Flags()
flags.BoolVar(&useInstanceID, "id", false, "Get instance by id")

return cmd
}
Loading
Loading