diff --git a/internal/cmd/examplegen/main.go b/internal/cmd/examplegen/main.go index db621c1a..68e78897 100644 --- a/internal/cmd/examplegen/main.go +++ b/internal/cmd/examplegen/main.go @@ -80,18 +80,17 @@ func getV1alphaExamplesConfigs() []examplesGeneratorConfig { object.GetKind().ToLower(), ) grouped := groupBy(examples, func(e v1alphaExamples.Example) string { return e.GetVariant() }) - // If we don't have any variants, we can write all examples into examples.yaml file. - if len(grouped) == 1 { - configs = append(configs, examplesGeneratorConfig{ - Examples: examples, - Path: filepath.Join(basePath, "examples.yaml"), - }) - continue - } for variant, examples := range grouped { + var path string + if len(grouped) == 1 { + // If we don't have any variants, we can write all examples into examples.yaml file. + path = filepath.Join(basePath, "examples.yaml") + } else { + path = filepath.Join(basePath, "examples", strings.ReplaceAll(strings.ToLower(variant), " ", "-")+".yaml") + } config := examplesGeneratorConfig{ Examples: examples, - Path: filepath.Join(basePath, "examples", strings.ReplaceAll(strings.ToLower(variant), " ", "-")+".yaml"), + Path: path, Comments: make(yaml.CommentMap), } if len(examples) == 1 { diff --git a/manifest/v1alpha/alertsilence/examples.yaml b/manifest/v1alpha/alertsilence/examples.yaml index ae867eee..7598d43a 100644 --- a/manifest/v1alpha/alertsilence/examples.yaml +++ b/manifest/v1alpha/alertsilence/examples.yaml @@ -1,54 +1,58 @@ +# duration - apiVersion: n9/v1alpha kind: AlertSilence metadata: - name: scheduled-maintenance-2024-05-01 + name: incident-70 project: default spec: - description: Scheduled maintenance alerts silence + description: Alerts silenced for the duration of the active incident 70 slo: api-server-latency alertPolicy: name: fast-burn project: default period: - startTime: 2024-05-01T12:00:00Z - endTime: 2024-05-01T14:00:00Z + duration: 4h +# end time - apiVersion: n9/v1alpha kind: AlertSilence metadata: - name: scheduled-maintenance-2024-05-02 + name: incident-71 project: default spec: - description: Scheduled maintenance alerts silence + description: Alerts silenced until incident 71 is resolved slo: api-server-latency alertPolicy: name: fast-burn project: default period: - startTime: 2024-05-02T12:00:00Z - duration: 2h + endTime: 2024-05-01T20:00:00Z +# start and end time - apiVersion: n9/v1alpha kind: AlertSilence metadata: - name: incident-70 + name: scheduled-maintenance-2024-05-01 project: default spec: - description: Alerts silenced for the duration of the active incident 70 + description: Scheduled maintenance alerts silence slo: api-server-latency alertPolicy: name: fast-burn project: default period: - duration: 4h + startTime: 2024-05-01T12:00:00Z + endTime: 2024-05-01T14:00:00Z +# start time and duration - apiVersion: n9/v1alpha kind: AlertSilence metadata: - name: incident-71 + name: scheduled-maintenance-2024-05-02 project: default spec: - description: Alerts silenced until incident 71 is resolved + description: Scheduled maintenance alerts silence slo: api-server-latency alertPolicy: name: fast-burn project: default period: - endTime: 2024-05-01T20:00:00Z + startTime: 2024-05-02T12:00:00Z + duration: 2h diff --git a/manifest/v1alpha/dataexport/examples.yaml b/manifest/v1alpha/dataexport/examples.yaml index 9f5deb11..0a47508b 100644 --- a/manifest/v1alpha/dataexport/examples.yaml +++ b/manifest/v1alpha/dataexport/examples.yaml @@ -1,3 +1,4 @@ +# gcs - apiVersion: n9/v1alpha kind: DataExport metadata: @@ -9,6 +10,7 @@ spec: bucketName: prod-data-export-bucket status: null +# s3 - apiVersion: n9/v1alpha kind: DataExport metadata: diff --git a/manifest/v1alpha/rolebinding/examples.yaml b/manifest/v1alpha/rolebinding/examples.yaml index b6418d3c..a467875e 100644 --- a/manifest/v1alpha/rolebinding/examples.yaml +++ b/manifest/v1alpha/rolebinding/examples.yaml @@ -1,11 +1,4 @@ -- apiVersion: n9/v1alpha - kind: RoleBinding - metadata: - name: default-project-binding - spec: - user: 00u2y4e4atkzaYkXP4x8 - roleRef: project-viewer - projectRef: default +# organization binding - apiVersion: n9/v1alpha kind: RoleBinding metadata: @@ -13,6 +6,7 @@ spec: user: 00u2y4e4atkzaYkXP4x8 roleRef: organization-admin +# organization group binding - apiVersion: n9/v1alpha kind: RoleBinding metadata: @@ -20,6 +14,16 @@ spec: groupRef: group-Q72HorLyjjCc roleRef: organization-admin +# project binding +- apiVersion: n9/v1alpha + kind: RoleBinding + metadata: + name: default-project-binding + spec: + user: 00u2y4e4atkzaYkXP4x8 + roleRef: project-viewer + projectRef: default +# project group binding - apiVersion: n9/v1alpha kind: RoleBinding metadata: diff --git a/tests/helpers_test.go b/tests/helpers_test.go index 9d33495e..fd880208 100644 --- a/tests/helpers_test.go +++ b/tests/helpers_test.go @@ -105,3 +105,15 @@ func deepCopyObject[T any](t *testing.T, object T) T { require.NoError(t, json.Unmarshal(data, &copied)) return copied } + +func filterSlice[T any](s []T, filter func(T) bool) []T { + result := make([]T, 0, len(s)) + for i := range s { + if filter(s[i]) { + result = append(result, s[i]) + } + } + return result +} + +func ptr[T any](v T) *T { return &v } diff --git a/tests/v1alpha_alertmethod_test.go b/tests/v1alpha_alertmethod_test.go index d2e79e9d..8126ea4b 100644 --- a/tests/v1alpha_alertmethod_test.go +++ b/tests/v1alpha_alertmethod_test.go @@ -51,7 +51,7 @@ func Test_Objects_V1_V1alpha_AlertMethod(t *testing.T) { }{ "all": { request: objectsV1.GetAlertMethodsRequest{Project: sdk.ProjectsWildcard}, - expected: manifest.FilterByKind[v1alphaAlertMethod.AlertMethod](allObjects), + expected: inputs, returnsAll: true, }, "default project": { diff --git a/tests/v1alpha_rolebinding_test.go b/tests/v1alpha_rolebinding_test.go new file mode 100644 index 00000000..90477413 --- /dev/null +++ b/tests/v1alpha_rolebinding_test.go @@ -0,0 +1,134 @@ +//go:build e2e_test + +package tests + +import ( + "context" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/nobl9/nobl9-go/manifest" + v1alphaRoleBinding "github.com/nobl9/nobl9-go/manifest/v1alpha/rolebinding" + "github.com/nobl9/nobl9-go/sdk" + objectsV1 "github.com/nobl9/nobl9-go/sdk/endpoints/objects/v1" +) + +func Test_Objects_V1_V1alpha_RoleBinding(t *testing.T) { + t.Parallel() + ctx := context.Background() + + project := generateV1alphaProject(t) + v1Apply(t, ctx, []manifest.Object{project}) + implicitBindings, err := client.Objects().V1().GetV1alphaRoleBindings(ctx, + objectsV1.GetRoleBindingsRequest{Project: project.GetName()}) + require.NoError(t, err) + require.Len(t, implicitBindings, 1) + implicitProjectBinding := implicitBindings[0] + + inputs := []v1alphaRoleBinding.RoleBinding{ + v1alphaRoleBinding.New( + v1alphaRoleBinding.Metadata{Name: generateName()}, + v1alphaRoleBinding.Spec{ + User: ptr(generateName()), + RoleRef: "organization-blank", + }, + ), + v1alphaRoleBinding.New( + v1alphaRoleBinding.Metadata{Name: generateName()}, + v1alphaRoleBinding.Spec{ + GroupRef: ptr(generateName()), + RoleRef: "organization-blank", + }, + ), + v1alphaRoleBinding.New( + v1alphaRoleBinding.Metadata{Name: generateName()}, + v1alphaRoleBinding.Spec{ + User: ptr(generateName()), + RoleRef: "project-viewer", + ProjectRef: project.GetName(), + }, + ), + v1alphaRoleBinding.New( + v1alphaRoleBinding.Metadata{Name: generateName()}, + v1alphaRoleBinding.Spec{ + GroupRef: ptr(generateName()), + RoleRef: "project-viewer", + ProjectRef: project.GetName(), + }, + ), + v1alphaRoleBinding.New( + v1alphaRoleBinding.Metadata{Name: generateName()}, + v1alphaRoleBinding.Spec{ + User: ptr(generateName()), + RoleRef: "project-viewer", + ProjectRef: defaultProject, + }, + ), + v1alphaRoleBinding.New( + v1alphaRoleBinding.Metadata{Name: generateName()}, + v1alphaRoleBinding.Spec{ + GroupRef: ptr(generateName()), + RoleRef: "project-viewer", + ProjectRef: defaultProject, + }, + ), + } + + v1Apply(t, ctx, inputs) + t.Cleanup(func() { + // Organization role bindings cannot be deleted. + filterOrganizationBindings := func(r v1alphaRoleBinding.RoleBinding) bool { + return !strings.HasPrefix(r.Spec.RoleRef, "organization-") + } + v1Delete(t, ctx, filterSlice(inputs, filterOrganizationBindings)) + }) + + filterTests := map[string]struct { + request objectsV1.GetRoleBindingsRequest + expected []v1alphaRoleBinding.RoleBinding + returnsAll bool + }{ + "all": { + request: objectsV1.GetRoleBindingsRequest{Project: sdk.ProjectsWildcard}, + expected: inputs, + returnsAll: true, + }, + "default project": { + request: objectsV1.GetRoleBindingsRequest{}, + expected: []v1alphaRoleBinding.RoleBinding{inputs[4], inputs[5]}, + returnsAll: true, + }, + "filter by project": { + request: objectsV1.GetRoleBindingsRequest{ + Project: project.GetName(), + }, + expected: []v1alphaRoleBinding.RoleBinding{implicitProjectBinding, inputs[2], inputs[3]}, + }, + "filter by name": { + request: objectsV1.GetRoleBindingsRequest{ + Project: project.GetName(), + Names: []string{inputs[2].Metadata.Name}, + }, + expected: []v1alphaRoleBinding.RoleBinding{inputs[2]}, + }, + } + for name, test := range filterTests { + t.Run(name, func(t *testing.T) { + t.Parallel() + actual, err := client.Objects().V1().GetV1alphaRoleBindings(ctx, test.request) + require.NoError(t, err) + if !test.returnsAll { + require.Len(t, actual, len(test.expected)) + } + assertSubset(t, actual, test.expected, assertV1alphaRoleBindingsAreEqual) + }) + } +} + +func assertV1alphaRoleBindingsAreEqual(t *testing.T, expected, actual v1alphaRoleBinding.RoleBinding) { + t.Helper() + assert.Equal(t, expected, actual) +} diff --git a/tests/v1alpha_service_test.go b/tests/v1alpha_service_test.go index af7cf634..0f77661c 100644 --- a/tests/v1alpha_service_test.go +++ b/tests/v1alpha_service_test.go @@ -70,7 +70,7 @@ func Test_Objects_V1_V1alpha_Service(t *testing.T) { }{ "all": { request: objectsV1.GetServicesRequest{Project: sdk.ProjectsWildcard}, - expected: manifest.FilterByKind[v1alphaService.Service](allObjects), + expected: inputs, returnsAll: true, }, "default project": {