Skip to content

Commit a4fa7c3

Browse files
committed
add system config update command
Signed-off-by: bupd <[email protected]>
1 parent 0ae8800 commit a4fa7c3

File tree

5 files changed

+158
-14
lines changed

5 files changed

+158
-14
lines changed

Diff for: cmd/harbor/root/cmd.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ harbor help
101101
root.AddCommand(
102102
versionCommand(),
103103
LoginCommand(),
104-
ConfigCommand(),
104+
ConfigCommand(),
105105
project.Project(),
106106
registry.Registry(),
107107
repositry.Repository(),

Diff for: cmd/harbor/root/config.go

+26-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
package root
22

33
import (
4-
"context"
5-
"fmt"
6-
"log"
7-
8-
"github.com/goharbor/go-client/pkg/harbor"
9-
"github.com/goharbor/go-client/pkg/sdk/v2.0/client/user"
104
"github.com/goharbor/harbor-cli/pkg/api"
115
"github.com/goharbor/harbor-cli/pkg/utils"
12-
"github.com/goharbor/harbor-cli/pkg/views/login"
6+
log "github.com/sirupsen/logrus"
137
"github.com/spf13/cobra"
148
)
159

@@ -23,6 +17,7 @@ func ConfigCommand() *cobra.Command {
2317

2418
cmd.AddCommand(
2519
GetConfigCmd(),
20+
UpdateConfigCmd(),
2621
)
2722

2823
return cmd
@@ -41,7 +36,7 @@ func GetConfigCmd() *cobra.Command {
4136
}
4237

4338
if err = utils.AddConfigToConfigFile(res.Payload, utils.DefaultConfigPath); err != nil {
44-
log.Fatalf("failed to store the credential: %v", err)
39+
log.Fatalf("failed to store the configuration: %v", err)
4540
}
4641

4742
utils.PrintPayloadInJSONFormat(res.Payload)
@@ -50,3 +45,26 @@ func GetConfigCmd() *cobra.Command {
5045

5146
return cmd
5247
}
48+
49+
func UpdateConfigCmd() *cobra.Command {
50+
cmd := &cobra.Command{
51+
Use: "update",
52+
Short: "update system configurations",
53+
Example: `harbor config update`,
54+
Run: func(cmd *cobra.Command, args []string) {
55+
config, err := utils.GetConfigurations()
56+
if err != nil {
57+
log.Fatalf("failed to get config from file: %v", err)
58+
}
59+
60+
err = api.UpdateConfiguration(config)
61+
if err != nil {
62+
log.Fatalf("failed to update config: %v", err)
63+
}
64+
65+
log.Infof("Configuration updated successfully.")
66+
},
67+
}
68+
69+
return cmd
70+
}

Diff for: pkg/api/config_handler.go

+19
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package api
22

33
import (
44
"github.com/goharbor/go-client/pkg/sdk/v2.0/client/configure"
5+
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
56
"github.com/goharbor/harbor-cli/pkg/utils"
67
)
78

@@ -22,3 +23,21 @@ func GetConfiguration() (*configure.GetConfigurationsOK, error) {
2223

2324
return response, nil
2425
}
26+
27+
// Update Configuration of the system
28+
func UpdateConfiguration(config *models.Configurations) error {
29+
ctx, client, err := utils.ContextWithClient()
30+
if err != nil {
31+
return err
32+
}
33+
34+
_, err = client.Configure.UpdateConfigurations(
35+
ctx,
36+
&configure.UpdateConfigurationsParams{Configurations: config},
37+
)
38+
if err != nil {
39+
return err
40+
}
41+
42+
return nil
43+
}

Diff for: pkg/utils/config.go

+23-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"path/filepath"
77

88
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
9+
"github.com/goharbor/harbor-cli/pkg/views/config/update"
910
"github.com/spf13/viper"
1011
)
1112

@@ -17,9 +18,9 @@ type Credential struct {
1718
}
1819

1920
type HarborConfig struct {
20-
CurrentCredentialName string `yaml:"current-credential-name"`
21-
Credentials []Credential `yaml:"credentials"`
22-
Configurations models.ConfigurationsResponse `yaml:"config"`
21+
CurrentCredentialName string `yaml:"current-credential-name"`
22+
Credentials []Credential `yaml:"credentials"`
23+
Configurations models.Configurations `yaml:"config"`
2324
}
2425

2526
var (
@@ -82,7 +83,6 @@ func AddCredentialsToConfigFile(credential Credential, configPath string) error
8283
return nil
8384
}
8485

85-
// Adds system configuration to the config file.
8686
func AddConfigToConfigFile(config *models.ConfigurationsResponse, configPath string) error {
8787
if _, err := os.Stat(configPath); os.IsNotExist(err) {
8888
return err
@@ -100,7 +100,10 @@ func AddConfigToConfigFile(config *models.ConfigurationsResponse, configPath str
100100
return err
101101
}
102102

103-
c.Configurations = *config
103+
// convert ConfigurationsResponse to Configurations
104+
configurations := update.ConvertToConfigurations(config, "", "")
105+
106+
c.Configurations = *configurations
104107

105108
viper.Set("config", c.Configurations)
106109
err = viper.WriteConfig()
@@ -111,6 +114,21 @@ func AddConfigToConfigFile(config *models.ConfigurationsResponse, configPath str
111114
return nil
112115
}
113116

117+
func GetConfigurations() (*models.Configurations, error) {
118+
err := viper.ReadInConfig()
119+
if err != nil {
120+
return &models.Configurations{}, err
121+
}
122+
123+
c := HarborConfig{}
124+
err = viper.Unmarshal(&c)
125+
if err != nil {
126+
return &models.Configurations{}, err
127+
}
128+
129+
return &c.Configurations, nil
130+
}
131+
114132
func GetCredentials(credentialName string) (Credential, error) {
115133
err := viper.ReadInConfig()
116134
if err != nil {

Diff for: pkg/views/config/update/view.go

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package update
2+
3+
import "github.com/goharbor/go-client/pkg/sdk/v2.0/models"
4+
5+
// Function to convert ConfigurationsResponse to Configurations
6+
func ConvertToConfigurations(
7+
resp *models.ConfigurationsResponse,
8+
ldapSearchPassword string,
9+
oidcClientSecret string,
10+
) *models.Configurations {
11+
return &models.Configurations{
12+
AuditLogForwardEndpoint: getStringValue(resp.AuditLogForwardEndpoint),
13+
AuthMode: getStringValue(resp.AuthMode),
14+
BannerMessage: getStringValue(resp.BannerMessage),
15+
HTTPAuthproxyAdminGroups: getStringValue(resp.HTTPAuthproxyAdminGroups),
16+
HTTPAuthproxyAdminUsernames: getStringValue(resp.HTTPAuthproxyAdminUsernames),
17+
HTTPAuthproxyEndpoint: getStringValue(resp.HTTPAuthproxyEndpoint),
18+
HTTPAuthproxyServerCertificate: getStringValue(resp.HTTPAuthproxyServerCertificate),
19+
HTTPAuthproxySkipSearch: getBoolValue(resp.HTTPAuthproxySkipSearch),
20+
HTTPAuthproxyTokenreviewEndpoint: getStringValue(resp.HTTPAuthproxyTokenreviewEndpoint),
21+
HTTPAuthproxyVerifyCert: getBoolValue(resp.HTTPAuthproxyVerifyCert),
22+
LdapBaseDn: getStringValue(resp.LdapBaseDn),
23+
LdapFilter: getStringValue(resp.LdapFilter),
24+
LdapGroupAdminDn: getStringValue(resp.LdapGroupAdminDn),
25+
LdapGroupAttributeName: getStringValue(resp.LdapGroupAttributeName),
26+
LdapGroupBaseDn: getStringValue(resp.LdapGroupBaseDn),
27+
LdapGroupMembershipAttribute: getStringValue(resp.LdapGroupMembershipAttribute),
28+
LdapGroupSearchFilter: getStringValue(resp.LdapGroupSearchFilter),
29+
LdapGroupSearchScope: getInt64Value(resp.LdapGroupSearchScope),
30+
LdapScope: getInt64Value(resp.LdapScope),
31+
LdapSearchDn: getStringValue(resp.LdapSearchDn),
32+
LdapSearchPassword: &ldapSearchPassword,
33+
LdapTimeout: getInt64Value(resp.LdapTimeout),
34+
LdapUID: getStringValue(resp.LdapUID),
35+
LdapURL: getStringValue(resp.LdapURL),
36+
LdapVerifyCert: getBoolValue(resp.LdapVerifyCert),
37+
NotificationEnable: getBoolValue(resp.NotificationEnable),
38+
OIDCAdminGroup: getStringValue(resp.OIDCAdminGroup),
39+
OIDCAutoOnboard: getBoolValue(resp.OIDCAutoOnboard),
40+
OIDCClientID: getStringValue(resp.OIDCClientID),
41+
OIDCClientSecret: &oidcClientSecret,
42+
OIDCEndpoint: getStringValue(resp.OIDCEndpoint),
43+
OIDCExtraRedirectParms: getStringValue(resp.OIDCExtraRedirectParms),
44+
OIDCGroupFilter: getStringValue(resp.OIDCGroupFilter),
45+
OIDCGroupsClaim: getStringValue(resp.OIDCGroupsClaim),
46+
OIDCName: getStringValue(resp.OIDCName),
47+
OIDCScope: getStringValue(resp.OIDCScope),
48+
OIDCUserClaim: getStringValue(resp.OIDCUserClaim),
49+
OIDCVerifyCert: getBoolValue(resp.OIDCVerifyCert),
50+
PrimaryAuthMode: getBoolValue(resp.PrimaryAuthMode),
51+
ProjectCreationRestriction: getStringValue(resp.ProjectCreationRestriction),
52+
QuotaPerProjectEnable: getBoolValue(resp.QuotaPerProjectEnable),
53+
ReadOnly: getBoolValue(resp.ReadOnly),
54+
RobotNamePrefix: getStringValue(resp.RobotNamePrefix),
55+
RobotTokenDuration: getInt64Value(resp.RobotTokenDuration),
56+
ScannerSkipUpdatePulltime: getBoolValue(resp.ScannerSkipUpdatePulltime),
57+
SelfRegistration: getBoolValue(resp.SelfRegistration),
58+
SessionTimeout: getInt64Value(resp.SessionTimeout),
59+
SkipAuditLogDatabase: getBoolValue(resp.SkipAuditLogDatabase),
60+
StoragePerProject: getInt64Value(resp.StoragePerProject),
61+
TokenExpiration: getInt64Value(resp.TokenExpiration),
62+
UaaClientID: getStringValue(resp.UaaClientID),
63+
UaaClientSecret: getStringValue(resp.UaaClientSecret),
64+
UaaEndpoint: getStringValue(resp.UaaEndpoint),
65+
UaaVerifyCert: getBoolValue(resp.UaaVerifyCert),
66+
}
67+
}
68+
69+
// Helper functions to extract values from configuration items
70+
func getStringValue(item *models.StringConfigItem) *string {
71+
if item != nil {
72+
return &item.Value
73+
}
74+
return nil
75+
}
76+
77+
func getBoolValue(item *models.BoolConfigItem) *bool {
78+
if item != nil {
79+
return &item.Value
80+
}
81+
return nil
82+
}
83+
84+
func getInt64Value(item *models.IntegerConfigItem) *int64 {
85+
if item != nil {
86+
return &item.Value
87+
}
88+
return nil
89+
}

0 commit comments

Comments
 (0)