Skip to content

Commit

Permalink
Assorted improvements and uniformization (#14)
Browse files Browse the repository at this point in the history
* Rename postgresql offerings to offering

* Rename postgresql credentials-id flag to credential-id

* Improve postgresql instance create example

* Rename all credentials IDs variables to singular

* Show username and password on credential creation

* Add flag to hide password

* Disallow updated not trying to set any fields
  • Loading branch information
joaopalet authored Nov 21, 2023
1 parent 6e2ef60 commit c753d09
Show file tree
Hide file tree
Showing 16 changed files with 153 additions and 98 deletions.
23 changes: 17 additions & 6 deletions internal/cmd/dns/record-set/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,25 @@ func parseFlags(cmd *cobra.Command) (*flagModel, error) {
return nil, fmt.Errorf("project ID not set")
}

zoneId := utils.FlagToStringValue(cmd, zoneIdFlag)
recordSetId := utils.FlagToStringValue(cmd, recordSetIdFlag)
comment := utils.FlagToStringPointer(cmd, commentFlag)
name := utils.FlagToStringPointer(cmd, nameFlag)
records := utils.FlagToStringSlicePointer(cmd, recordFlag)
ttl := utils.FlagToInt64Pointer(cmd, ttlFlag)

if comment == nil && name == nil && records == nil && ttl == nil {
return nil, fmt.Errorf("please specify at least one field to update")
}

return &flagModel{
ProjectId: projectId,
ZoneId: utils.FlagToStringValue(cmd, zoneIdFlag),
RecordSetId: utils.FlagToStringValue(cmd, recordSetIdFlag),
Comment: utils.FlagToStringPointer(cmd, commentFlag),
Name: utils.FlagToStringPointer(cmd, nameFlag),
Records: utils.FlagToStringSlicePointer(cmd, recordFlag),
TTL: utils.FlagToInt64Pointer(cmd, ttlFlag),
ZoneId: zoneId,
RecordSetId: recordSetId,
Comment: comment,
Name: name,
Records: records,
TTL: ttl,
}, nil
}

Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/dns/record-set/update/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ func TestParseFlags(t *testing.T) {
isValid: false,
},
{
description: "required fields only",
description: "required flags only (no values to update)",
flagValues: map[string]string{
projectIdFlag: testProjectId,
zoneIdFlag: testZoneId,
recordSetIdFlag: testRecordSetId,
},
isValid: true,
isValid: false,
expectedModel: &flagModel{
ProjectId: testProjectId,
ZoneId: testZoneId,
Expand Down
41 changes: 30 additions & 11 deletions internal/cmd/dns/zone/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,38 @@ func parseFlags(cmd *cobra.Command) (*flagModel, error) {
return nil, fmt.Errorf("project ID not set")
}

zoneId := utils.FlagToStringValue(cmd, zoneIdFlag)
name := utils.FlagToStringPointer(cmd, nameFlag)
defaultTTL := utils.FlagToInt64Pointer(cmd, defaultTTLFlag)
primaries := utils.FlagToStringSlicePointer(cmd, primaryFlag)
acl := utils.FlagToStringPointer(cmd, aclFlag)
retryTime := utils.FlagToInt64Pointer(cmd, retryTimeFlag)
refreshTime := utils.FlagToInt64Pointer(cmd, refreshTimeFlag)
negativeCache := utils.FlagToInt64Pointer(cmd, negativeCacheFlag)
expireTime := utils.FlagToInt64Pointer(cmd, expireTimeFlag)
description := utils.FlagToStringPointer(cmd, descriptionFlag)
contactEmail := utils.FlagToStringPointer(cmd, contactEmailFlag)

if name == nil && defaultTTL == nil && primaries == nil &&
acl == nil && retryTime == nil && refreshTime == nil &&
negativeCache == nil && expireTime == nil && description == nil &&
contactEmail == nil {
return nil, fmt.Errorf("please specify at least one field to update")
}

return &flagModel{
ProjectId: projectId,
ZoneId: utils.FlagToStringValue(cmd, zoneIdFlag),
Name: utils.FlagToStringPointer(cmd, nameFlag),
DefaultTTL: utils.FlagToInt64Pointer(cmd, defaultTTLFlag),
Primaries: utils.FlagToStringSlicePointer(cmd, primaryFlag),
Acl: utils.FlagToStringPointer(cmd, aclFlag),
RetryTime: utils.FlagToInt64Pointer(cmd, retryTimeFlag),
RefreshTime: utils.FlagToInt64Pointer(cmd, refreshTimeFlag),
NegativeCache: utils.FlagToInt64Pointer(cmd, negativeCacheFlag),
ExpireTime: utils.FlagToInt64Pointer(cmd, expireTimeFlag),
Description: utils.FlagToStringPointer(cmd, descriptionFlag),
ContactEmail: utils.FlagToStringPointer(cmd, contactEmailFlag),
ZoneId: zoneId,
Name: name,
DefaultTTL: defaultTTL,
Primaries: primaries,
Acl: acl,
RetryTime: retryTime,
RefreshTime: refreshTime,
NegativeCache: negativeCache,
ExpireTime: expireTime,
Description: description,
ContactEmail: contactEmail,
}, nil
}

Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/dns/zone/update/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ func TestParseFlags(t *testing.T) {
isValid: false,
},
{
description: "required fields only",
description: "required flags only (no values to update)",
flagValues: map[string]string{
projectIdFlag: testProjectId,
zoneIdFlag: testZoneId,
},
isValid: true,
isValid: false,
expectedModel: &flagModel{
ProjectId: testProjectId,
ZoneId: testZoneId,
Expand Down
25 changes: 17 additions & 8 deletions internal/cmd/postgresql/credential/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ import (
)

const (
projectIdFlag = "project-id"
instanceIdFlag = "instance-id"
projectIdFlag = "project-id"
instanceIdFlag = "instance-id"
hidePasswordFlag = "hide-password"
)

type flagModel struct {
ProjectId string
InstanceId string
ProjectId string
InstanceId string
HidePassword bool
}

func NewCmd() *cobra.Command {
Expand All @@ -47,10 +49,15 @@ func NewCmd() *cobra.Command {
req := buildRequest(ctx, model, apiClient)
resp, err := req.Execute()
if err != nil {
return fmt.Errorf("create PostgreSQL credentials: %w", err)
return fmt.Errorf("create PostgreSQL credential: %w", err)
}

cmd.Printf("Created credentials with ID %s\n", *resp.Id)
cmd.Printf("Created credential with ID %s\n\nUsername: %s\n", *resp.Id, *resp.Raw.Credentials.Username)
if model.HidePassword {
cmd.Printf("Password: <hidden>\n")
} else {
cmd.Printf("Password: %s\n", *resp.Raw.Credentials.Password)
}
return nil
},
}
Expand All @@ -60,6 +67,7 @@ func NewCmd() *cobra.Command {

func configureFlags(cmd *cobra.Command) {
cmd.Flags().Var(flags.UUIDFlag(), instanceIdFlag, "Instance ID")
cmd.Flags().Bool(hidePasswordFlag, false, "Hide password in output")

err := utils.MarkFlagsRequired(cmd, instanceIdFlag)
cobra.CheckErr(err)
Expand All @@ -72,8 +80,9 @@ func parseFlags(cmd *cobra.Command) (*flagModel, error) {
}

return &flagModel{
ProjectId: projectId,
InstanceId: utils.FlagToStringValue(cmd, instanceIdFlag),
ProjectId: projectId,
InstanceId: utils.FlagToStringValue(cmd, instanceIdFlag),
HidePassword: utils.FlagToBoolValue(cmd, hidePasswordFlag),
}, nil
}

Expand Down
30 changes: 15 additions & 15 deletions internal/cmd/postgresql/credential/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@ import (
)

const (
projectIdFlag = "project-id"
instanceIdFlag = "instance-id"
credentialsIdFlag = "credentials-id"
projectIdFlag = "project-id"
instanceIdFlag = "instance-id"
credentialIdFlag = "credential-id" //nolint:gosec // linter false positive
)

type flagModel struct {
ProjectId string
InstanceId string
CredentialsId string
ProjectId string
InstanceId string
CredentialId string
}

func NewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "delete",
Short: "Delete a PostgreSQL instance credential",
Long: "Delete a PostgreSQL instance credential",
Example: `$ stackit postgresql credential delete --project-id xxx --instance-id xxx --credentials-id xxx`,
Example: `$ stackit postgresql credential delete --project-id xxx --instance-id xxx --credential-id xxx`,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
model, err := parseFlags(cmd)
Expand All @@ -49,10 +49,10 @@ func NewCmd() *cobra.Command {
req := buildRequest(ctx, model, apiClient)
err = req.Execute()
if err != nil {
return fmt.Errorf("delete PostgreSQL credentials: %w", err)
return fmt.Errorf("delete PostgreSQL credential: %w", err)
}

cmd.Printf("Deleted credentials with ID %s\n", model.CredentialsId)
cmd.Printf("Deleted credential with ID %s\n", model.CredentialId)
return nil
},
}
Expand All @@ -62,11 +62,11 @@ func NewCmd() *cobra.Command {

func configureFlags(cmd *cobra.Command) {
cmd.Flags().Var(flags.UUIDFlag(), instanceIdFlag, "Instance ID")
cmd.Flags().Var(flags.UUIDFlag(), credentialsIdFlag, "Credentials ID")
cmd.Flags().Var(flags.UUIDFlag(), credentialIdFlag, "Credentials ID")

err := utils.MarkFlagsRequired(cmd, instanceIdFlag)
cobra.CheckErr(err)
err = utils.MarkFlagsRequired(cmd, credentialsIdFlag)
err = utils.MarkFlagsRequired(cmd, credentialIdFlag)
cobra.CheckErr(err)
}

Expand All @@ -77,13 +77,13 @@ func parseFlags(cmd *cobra.Command) (*flagModel, error) {
}

return &flagModel{
ProjectId: projectId,
InstanceId: utils.FlagToStringValue(cmd, instanceIdFlag),
CredentialsId: utils.FlagToStringValue(cmd, credentialsIdFlag),
ProjectId: projectId,
InstanceId: utils.FlagToStringValue(cmd, instanceIdFlag),
CredentialId: utils.FlagToStringValue(cmd, credentialIdFlag),
}, nil
}

func buildRequest(ctx context.Context, model *flagModel, apiClient *postgresql.APIClient) postgresql.ApiDeleteCredentialsRequest {
req := apiClient.DeleteCredentials(ctx, model.ProjectId, model.InstanceId, model.CredentialsId)
req := apiClient.DeleteCredentials(ctx, model.ProjectId, model.InstanceId, model.CredentialId)
return req
}
22 changes: 11 additions & 11 deletions internal/cmd/postgresql/credential/delete/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ var testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo")
var testClient = &postgresql.APIClient{}
var testProjectId = uuid.NewString()
var testInstanceId = uuid.NewString()
var testCredentialsId = uuid.NewString()
var testCredentialId = uuid.NewString()

func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string {
flagValues := map[string]string{
projectIdFlag: testProjectId,
instanceIdFlag: testInstanceId,
credentialsIdFlag: testCredentialsId,
projectIdFlag: testProjectId,
instanceIdFlag: testInstanceId,
credentialIdFlag: testCredentialId,
}
for _, mod := range mods {
mod(flagValues)
Expand All @@ -36,9 +36,9 @@ func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]st

func fixtureFlagModel(mods ...func(model *flagModel)) *flagModel {
model := &flagModel{
ProjectId: testProjectId,
InstanceId: testInstanceId,
CredentialsId: testCredentialsId,
ProjectId: testProjectId,
InstanceId: testInstanceId,
CredentialId: testCredentialId,
}
for _, mod := range mods {
mod(model)
Expand All @@ -47,7 +47,7 @@ func fixtureFlagModel(mods ...func(model *flagModel)) *flagModel {
}

func fixtureRequest(mods ...func(request *postgresql.ApiDeleteCredentialsRequest)) postgresql.ApiDeleteCredentialsRequest {
request := testClient.DeleteCredentials(testCtx, testProjectId, testInstanceId, testCredentialsId)
request := testClient.DeleteCredentials(testCtx, testProjectId, testInstanceId, testCredentialId)
for _, mod := range mods {
mod(&request)
}
Expand Down Expand Up @@ -117,21 +117,21 @@ func TestParseFlags(t *testing.T) {
{
description: "credentials id missing",
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
delete(flagValues, credentialsIdFlag)
delete(flagValues, credentialIdFlag)
}),
isValid: false,
},
{
description: "credentials id invalid 1",
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
flagValues[credentialsIdFlag] = ""
flagValues[credentialIdFlag] = ""
}),
isValid: false,
},
{
description: "credentials id invalid 2",
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
flagValues[credentialsIdFlag] = "invalid-uuid"
flagValues[credentialIdFlag] = "invalid-uuid"
}),
isValid: false,
},
Expand Down
30 changes: 15 additions & 15 deletions internal/cmd/postgresql/credential/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ import (
)

const (
projectIdFlag = "project-id"
instanceIdFlag = "instance-id"
credentialsIdFlag = "credentials-id"
projectIdFlag = "project-id"
instanceIdFlag = "instance-id"
credentialIdFlag = "credential-id" //nolint:gosec // linter false positive
)

type flagModel struct {
ProjectId string
InstanceId string
CredentialsId string
ProjectId string
InstanceId string
CredentialId string
}

func NewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "describe",
Short: "Get details of a PostgreSQL instance credential",
Long: "Get details of a PostgreSQL instance credential",
Example: `$ stackit postgresql credential describe --project-id xxx --instance-id xxx --credentials-id xxx`,
Example: `$ stackit postgresql credential describe --project-id xxx --instance-id xxx --credential-id xxx`,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
model, err := parseFlags(cmd)
Expand All @@ -50,13 +50,13 @@ func NewCmd() *cobra.Command {
req := buildRequest(ctx, model, apiClient)
resp, err := req.Execute()
if err != nil {
return fmt.Errorf("describe PostgreSQL credentials: %w", err)
return fmt.Errorf("describe PostgreSQL credential: %w", err)
}

// Show details
details, err := json.MarshalIndent(resp, "", " ")
if err != nil {
return fmt.Errorf("marshal PostgreSQL credentials: %w", err)
return fmt.Errorf("marshal PostgreSQL credential: %w", err)
}
cmd.Println(string(details))

Expand All @@ -69,11 +69,11 @@ func NewCmd() *cobra.Command {

func configureFlags(cmd *cobra.Command) {
cmd.Flags().Var(flags.UUIDFlag(), instanceIdFlag, "Instance ID")
cmd.Flags().Var(flags.UUIDFlag(), credentialsIdFlag, "Credentials ID")
cmd.Flags().Var(flags.UUIDFlag(), credentialIdFlag, "Credentials ID")

err := utils.MarkFlagsRequired(cmd, instanceIdFlag)
cobra.CheckErr(err)
err = utils.MarkFlagsRequired(cmd, credentialsIdFlag)
err = utils.MarkFlagsRequired(cmd, credentialIdFlag)
cobra.CheckErr(err)
}

Expand All @@ -84,13 +84,13 @@ func parseFlags(cmd *cobra.Command) (*flagModel, error) {
}

return &flagModel{
ProjectId: projectId,
InstanceId: utils.FlagToStringValue(cmd, instanceIdFlag),
CredentialsId: utils.FlagToStringValue(cmd, credentialsIdFlag),
ProjectId: projectId,
InstanceId: utils.FlagToStringValue(cmd, instanceIdFlag),
CredentialId: utils.FlagToStringValue(cmd, credentialIdFlag),
}, nil
}

func buildRequest(ctx context.Context, model *flagModel, apiClient *postgresql.APIClient) postgresql.ApiGetCredentialsRequest {
req := apiClient.GetCredentials(ctx, model.ProjectId, model.InstanceId, model.CredentialsId)
req := apiClient.GetCredentials(ctx, model.ProjectId, model.InstanceId, model.CredentialId)
return req
}
Loading

0 comments on commit c753d09

Please sign in to comment.