Skip to content

Commit

Permalink
Fix resource monitor alter validations in the SDK (#2298)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-asawicki authored Dec 21, 2023
1 parent 70edd3e commit 74d19d0
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
4 changes: 2 additions & 2 deletions pkg/resources/resource_monitor_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func TestAcc_ResourceMonitor_issue2167(t *testing.T) {
name := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
configNoUsers, err := resourceMonitorNotifyUsersConfig(name, []string{})
require.NoError(t, err)
config, err := resourceMonitorNotifyUsersConfig(name, []string{"does not matter"})
config, err := resourceMonitorNotifyUsersConfig(name, []string{"non_existing_user"})
require.NoError(t, err)

resource.Test(t, resource.TestCase{
Expand All @@ -241,7 +241,7 @@ func TestAcc_ResourceMonitor_issue2167(t *testing.T) {
},
{
Config: config,
ExpectError: regexp.MustCompile(`.*exactly one of AlterResourceMonitorOptions fields \[Set NotifyUsers Triggers] must be set.*`),
ExpectError: regexp.MustCompile(`.*090268 \(22023\): User non_existing_user does not exist.*`),
},
},
})
Expand Down
6 changes: 3 additions & 3 deletions pkg/sdk/resource_monitors.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,14 +300,14 @@ func (opts *AlterResourceMonitorOptions) validate() error {
if !ValidObjectIdentifier(opts.name) {
errs = append(errs, ErrInvalidObjectIdentifier)
}
if everyValueNil(opts.Set, opts.NotifyUsers, opts.Triggers) {
errs = append(errs, errAtLeastOneOf("AlterResourceMonitorOptions", "Set", "NotifyUsers", "Triggers"))
}
if valueSet(opts.Set) {
if (opts.Set.Frequency != nil && opts.Set.StartTimestamp == nil) || (opts.Set.Frequency == nil && opts.Set.StartTimestamp != nil) {
errs = append(errs, errors.New("must specify frequency and start time together"))
}
}
if !exactlyOneValueSet(opts.Set, opts.NotifyUsers) && opts.Triggers == nil {
errs = append(errs, errExactlyOneOf("AlterResourceMonitorOptions", "Set", "NotifyUsers", "Triggers"))
}
return errors.Join(errs...)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/sdk/resource_monitors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestResourceMonitorAlter(t *testing.T) {
opts := &AlterResourceMonitorOptions{
name: id,
}
assertOptsInvalidJoinedErrors(t, opts, errExactlyOneOf("AlterResourceMonitorOptions", "Set", "NotifyUsers", "Triggers"))
assertOptsInvalidJoinedErrors(t, opts, errAtLeastOneOf("AlterResourceMonitorOptions", "Set", "NotifyUsers", "Triggers"))
})

t.Run("with a single set", func(t *testing.T) {
Expand Down
35 changes: 35 additions & 0 deletions pkg/sdk/testint/resource_monitors_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ func TestInt_ResourceMonitorAlter(t *testing.T) {
resourceMonitor = &resourceMonitors[0]
assert.Equal(t, creditQuota, int(resourceMonitor.CreditQuota))
})

t.Run("when changing scheduling info", func(t *testing.T) {
resourceMonitor, resourceMonitorCleanup := createResourceMonitor(t, client)
t.Cleanup(resourceMonitorCleanup)
Expand Down Expand Up @@ -242,6 +243,40 @@ func TestInt_ResourceMonitorAlter(t *testing.T) {
assert.Equal(t, startTimeStamp, startTime)
assert.Equal(t, endTimeStamp, endTime)
})

t.Run("all options together", func(t *testing.T) {
resourceMonitor, resourceMonitorCleanup := createResourceMonitor(t, client)
t.Cleanup(resourceMonitorCleanup)

newTriggers := make([]sdk.TriggerDefinition, 0)
newTriggers = append(newTriggers, sdk.TriggerDefinition{Threshold: 30, TriggerAction: sdk.TriggerActionNotify})

creditQuota := 100
alterOptions := &sdk.AlterResourceMonitorOptions{
Set: &sdk.ResourceMonitorSet{
CreditQuota: &creditQuota,
},
Triggers: newTriggers,
NotifyUsers: &sdk.NotifyUsers{
Users: []sdk.NotifiedUser{{Name: "ARTUR_SAWICKI"}},
},
}
err := client.ResourceMonitors.Alter(ctx, resourceMonitor.ID(), alterOptions)
require.NoError(t, err)
resourceMonitors, err := client.ResourceMonitors.Show(ctx, &sdk.ShowResourceMonitorOptions{
Like: &sdk.Like{
Pattern: sdk.String(resourceMonitor.Name),
},
})
require.NoError(t, err)
assert.Equal(t, 1, len(resourceMonitors))
resourceMonitor = &resourceMonitors[0]
assert.Equal(t, creditQuota, int(resourceMonitor.CreditQuota))
assert.Len(t, resourceMonitor.NotifyUsers, 1)
assert.Equal(t, "ARTUR_SAWICKI", resourceMonitor.NotifyUsers[0])
assert.Len(t, resourceMonitor.NotifyTriggers, 1)
assert.Equal(t, 30, resourceMonitor.NotifyTriggers[0])
})
}

func TestInt_ResourceMonitorDrop(t *testing.T) {
Expand Down

0 comments on commit 74d19d0

Please sign in to comment.