From 318bdf94f996e65ed83ccc512c442aa893be87e8 Mon Sep 17 00:00:00 2001 From: Sasha Bauer Date: Tue, 31 Dec 2024 13:55:32 -0500 Subject: [PATCH] refactor(oncall): reduce duplication in the Shift API --- grafanaplane/oncall/shift.libsonnet | 94 ++++++++--------------------- 1 file changed, 25 insertions(+), 69 deletions(-) diff --git a/grafanaplane/oncall/shift.libsonnet b/grafanaplane/oncall/shift.libsonnet index 0602ee2..471458f 100644 --- a/grafanaplane/oncall/shift.libsonnet +++ b/grafanaplane/oncall/shift.libsonnet @@ -4,92 +4,48 @@ local xtd = import 'github.com/jsonnet-libs/xtd/main.libsonnet'; local raw = import '../zz/main.libsonnet'; local shift = raw.oncall.v1alpha1.onCallShift; +// Add the forProvider functions here for convenience. +shift.spec.parameters.forProvider { local base = self, '#new': d.func.new( ||| `new` creates an OnCallShift. The `name` is a display-friendly string, - and `resourceId` defaults to a slug-ified version of it. `type` is the - type of Shift. `start` is the start time of the shift in - `yyyy-MM-dd'T'HH:mm:ss` format. `providerName` is the resource name - (`myprovider.metadata.name`) of the Provider. + and `id` defaults to a slug-ified version of it. |||, [ d.argument.new('name', d.T.string), - d.argument.new('type', d.T.string), - d.argument.new('start', d.T.string), - d.argument.new('providerName', d.T.string), d.argument.new('resourceId', d.T.string, default='rfc1123(name)'), ] ), - new(name, type, start, providerName, resourceId=null):: - local id = if resourceId != null then resourceId else xtd.ascii.stringToRFC1123(name); + new(name, id=xtd.ascii.stringToRFC1123(name)):: shift.new(id) - + shift.spec.parameters.providerConfigRef.withName(providerName) - + shift.spec.parameters.forProvider.withName(name) - + shift.spec.parameters.forProvider.withType(type) - + shift.spec.parameters.forProvider.withStart(start), + + shift.spec.parameters.forProvider.withName(name), + + '#withStart':: d.func.new( + ||| + `withStart` sets the start time of the shift in `yyyy-MM-dd'T'HH:mm:ss` + format. + |||, + [d.argument.new('start', d.T.string)] + ), + withStart(start):: shift.spec.parameters.forProvider.withStart(start), - '#newRollingUsers': d.func.new( + '#withRollingUsers': d.func.new( ||| - `newRollingUsers` creates an OnCallShift of `rolling_users` type. The - `name` is a display-friendly string, and `resourceId` defaults to a - slug-ified version of it. `rollingUsers` is the list of users as an array - of strings. `start` is the start time of the shift in - `yyyy-MM-dd'T'HH:mm:ss` format. `duration` is the length of each shift in - seconds. `interval` is the interval at which the recurrence rule repeats. - `providerName` is the resource name (`myprovider.metadata.name`) of the - Provider. `byDay` is a list of days in iCal format on which the shift - takes place. `byMonth` is a list of months in which the shift takes - place. `byMonthDay` is a list of month days on which the shift takes - place. If any of `byDay`, `byMonth`, or `byMonthDay` is `null` (default), - it is omitted. + `withRollingUsers` sets an OnCallShift to type `rolling_users` and + configures required fields. `users` must be an array *of arrays* + of strings referring to users by email address. `frequency` is required + for this shift type. |||, [ - d.argument.new('name', d.T.string), - d.argument.new('rollingUsers', d.T.array), - d.argument.new('start', d.T.string), - d.argument.new('duration', d.T.number), - d.argument.new('interval', d.T.number), - d.argument.new('providerName', d.T.string), - d.argument.new('byDay', d.T.array, default='null'), - d.argument.new('byMonth', d.T.array, default='null'), - d.argument.new('byMonthDay', d.T.array, default='null'), - d.argument.new('resourceId', d.T.string, default='rfc1123(name)'), + d.argument.new('frequency', d.T.string, enums=['hourly', 'daily', 'weekly', 'monthly']), + d.argument.new('users', d.T.array), ] ), - newRollingUsers( - name, - start, - duration, - interval, - rollingUsers, - providerName, - byDay=null, - byMonth=null, - byMonthDay=null, - resourceId=null, - ):: - base.new(name, 'rolling_users', start, providerName, resourceId) - + shift.spec.parameters.forProvider.withRollingUsers(rollingUsers) - + shift.spec.parameters.forProvider.withDuration(duration) - + shift.spec.parameters.forProvider.withInterval(interval) - + ( - if byDay != null - then shift.spec.parameters.forProvider.withByDay(byDay) - else {} - ) - + ( - if byMonth != null - then shift.spec.parameters.forProvider.withByMonth(byMonth) - else {} - ) - + ( - if byMonthDay != null - then shift.spec.parameters.forProvider.withByMonthDay(byMonthDay) - else {} - ), + withRollingUsers(frequency, users):: + shift.spec.parameters.forProvider.withType('rolling_users') + + shift.spec.parameters.forProvider.withRollingUsers(users) + + shift.spec.parameters.forProvider.withFrequency(frequency), } -// Add the forProvider functions here for convenience. -+ shift.spec.parameters.forProvider