@@ -15,6 +15,7 @@ package instance
1515
1616import (
1717 "fmt"
18+ "strings"
1819
1920 "github.com/goharbor/harbor-cli/pkg/api"
2021 "github.com/goharbor/harbor-cli/pkg/utils"
@@ -24,54 +25,87 @@ import (
2425
2526func CreateInstanceCommand () * cobra.Command {
2627 var opts create.CreateView
28+ var authUsername , authPassword , authToken string
2729
2830 cmd := & cobra.Command {
2931 Use : "create" ,
3032 Short : "Create a new preheat provider instance in Harbor" ,
3133 Long : `Create a new preheat provider instance within Harbor for distributing container images.
3234The instance can be an external service such as Dragonfly, Kraken, or any custom provider.
3335You will need to provide the instance's name, vendor, endpoint, and optionally other details such as authentication and security options.` ,
34- Example : ` harbor-cli instance create --name my-instance --provider Dragonfly --url http://dragonfly.local --description "My preheat provider instance" --enable=true` ,
36+ Example : ` harbor-cli instance create --name my-instance --provider dragonfly --url http://dragonfly.local --description "My preheat provider instance" --enable=true` ,
3537 Args : cobra .NoArgs ,
3638 RunE : func (cmd * cobra.Command , args []string ) error {
3739 var err error
38- createView := & create.CreateView {
39- Name : opts .Name ,
40- Vendor : opts .Vendor ,
41- Description : opts .Description ,
42- Endpoint : opts .Endpoint ,
43- Insecure : opts .Insecure ,
44- Enabled : opts .Enabled ,
45- AuthMode : opts .AuthMode ,
46- AuthInfo : opts .AuthInfo ,
47- }
40+ var instanceName string
4841
4942 if opts .Name != "" && opts .Vendor != "" && opts .Endpoint != "" {
5043 formattedEndpoint := utils .FormatUrl (opts .Endpoint )
5144 if err := utils .ValidateURL (formattedEndpoint ); err != nil {
5245 return err
5346 }
5447 opts .Endpoint = formattedEndpoint
48+
49+ opts .AuthMode = strings .ToUpper (strings .TrimSpace (opts .AuthMode ))
50+
51+ switch opts .AuthMode {
52+ case "BASIC" :
53+ if authUsername == "" || authPassword == "" {
54+ return fmt .Errorf ("username and password are required when authmode is BASIC. Use --auth-username and --auth-password flags" )
55+ }
56+ opts .AuthInfo = map [string ]string {
57+ "username" : authUsername ,
58+ "password" : authPassword ,
59+ }
60+ case "OAUTH" :
61+ if authToken == "" {
62+ return fmt .Errorf ("token is required when authmode is OAUTH. Use --auth-token flag" )
63+ }
64+ opts .AuthInfo = map [string ]string {
65+ "token" : authToken ,
66+ }
67+ case "NONE" :
68+ // Auth credentials are ignored when authmode is NONE
69+ default :
70+ return fmt .Errorf ("invalid authmode '%s'. Valid options: NONE, BASIC, OAUTH" , opts .AuthMode )
71+ }
72+
5573 err = api .CreateInstance (opts )
74+ instanceName = opts .Name
5675 } else {
76+ createView := & create.CreateView {
77+ Name : opts .Name ,
78+ Vendor : opts .Vendor ,
79+ Description : opts .Description ,
80+ Endpoint : opts .Endpoint ,
81+ Insecure : opts .Insecure ,
82+ Enabled : opts .Enabled ,
83+ AuthMode : opts .AuthMode ,
84+ }
5785 err = createInstanceView (createView )
86+ instanceName = createView .Name
5887 }
5988
6089 if err != nil {
61- return fmt .Errorf ("failed to create instance: %v" , err )
90+ return fmt .Errorf ("failed to create instance: %v" , utils . ParseHarborErrorMsg ( err ) )
6291 }
92+
93+ fmt .Printf ("Instance '%s' created successfully\n " , instanceName )
6394 return nil
6495 },
6596 }
6697
6798 flags := cmd .Flags ()
6899 flags .StringVarP (& opts .Name , "name" , "n" , "" , "Name of the instance" )
69- flags .StringVarP (& opts .Vendor , "provider" , "p" , "" , "Provider for the instance" )
70- flags .StringVarP (& opts .Endpoint , "url" , "u" , "" , "URL for the instance" )
71- flags .StringVarP (& opts .Description , "description" , "" , "" , "Description of the instance" )
72- flags .BoolVarP (& opts .Insecure , "insecure" , "i" , true , "Whether or not the certificate will be verified when Harbor tries to access the server" )
73- flags .BoolVarP (& opts .Enabled , "enable" , "" , true , "Whether it is enabled or not" )
74- flags .StringVarP (& opts .AuthMode , "authmode" , "a" , "NONE" , "Choosing different types of authentication method" )
100+ flags .StringVarP (& opts .Vendor , "provider" , "p" , "" , "Provider for the instance (e.g. dragonfly, kraken)" )
101+ flags .StringVarP (& opts .Endpoint , "url" , "u" , "" , "Endpoint URL for the instance" )
102+ flags .StringVarP (& opts .Description , "description" , "d" , "" , "Description of the instance" )
103+ flags .BoolVarP (& opts .Insecure , "insecure" , "i" , false , "Whether or not the certificate will be verified when Harbor tries to access the server" )
104+ flags .BoolVarP (& opts .Enabled , "enable" , "" , true , "Whether the instance is enabled or not" )
105+ flags .StringVarP (& opts .AuthMode , "authmode" , "a" , "NONE" , "Authentication mode (NONE, BASIC, OAUTH)" )
106+ flags .StringVar (& authUsername , "auth-username" , "" , "Username for BASIC authentication" )
107+ flags .StringVar (& authPassword , "auth-password" , "" , "Password for BASIC authentication" )
108+ flags .StringVar (& authToken , "auth-token" , "" , "Token for OAUTH authentication" )
75109
76110 return cmd
77111}
@@ -81,6 +115,8 @@ func createInstanceView(createView *create.CreateView) error {
81115 createView = & create.CreateView {}
82116 }
83117
84- create .CreateInstanceView (createView )
118+ if err := create .CreateInstanceView (createView ); err != nil {
119+ return err
120+ }
85121 return api .CreateInstance (* createView )
86122}
0 commit comments