Skip to content

Commit cd0c4f1

Browse files
committed
add cleanup cronjob for spaces leftover from integration tests
1 parent 9f2c030 commit cd0c4f1

2 files changed

Lines changed: 110 additions & 0 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Leftover spaces cleanup
2+
3+
on:
4+
schedule:
5+
# Every Sunday at 11PM
6+
- cron: '0 23 * * 0'
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
name: Leftover spaces cleanup
12+
steps:
13+
- uses: actions/checkout@v4
14+
15+
- name: Set up Go
16+
uses: actions/setup-go@v4
17+
with:
18+
go-version: '1.24'
19+
20+
- name: Run leftover spaces cleanup
21+
run: go run cmd/test-space-cleaner/main.go
22+
env:
23+
MONDOO_API_TOKEN: ${{ secrets.MONDOO_TEST_ORG_TOKEN }}
24+
MONDOO_ORG_MRN: '//captain.api.mondoo.app/organizations/mondoo-operator-testing'
25+
MONDOO_GQL_ENDPOINT: 'https://api.mondoo.com/query'
26+
27+

cmd/test-space-cleaner/main.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright (c) Mondoo, Inc.
2+
// SPDX-License-Identifier: BUSL-1.1
3+
4+
package main
5+
6+
import (
7+
"context"
8+
"fmt"
9+
10+
"github.com/rs/zerolog/log"
11+
mondoogql "go.mondoo.com/mondoo-go"
12+
"go.mondoo.com/mondoo-operator/tests/framework/nexus"
13+
)
14+
15+
func main() {
16+
nexusClient, err := nexus.NewClient()
17+
if err != nil {
18+
log.Fatal().Err(err).Msg("failed to create nexus client")
19+
}
20+
21+
ctx := context.Background()
22+
spaces, err := ListSpaces(ctx, nexusClient.Client, "//captain.api.mondoo.app/organizations/mondoo-operator-testing")
23+
if err != nil {
24+
log.Fatal().Err(err).Msg("failed to list spaces")
25+
26+
}
27+
for _, space := range spaces.Edges {
28+
if space.Node.Name == "mondoo-operator-tests" {
29+
continue
30+
}
31+
err := Delete(ctx, nexusClient.Client, space.Node.Mrn)
32+
if err != nil {
33+
log.Warn().Err(err).Str("mrn", space.Node.Mrn).Msg("failed to delete space")
34+
continue
35+
}
36+
fmt.Println("Deleted space:", space.Node.Name, "with MRN:", space.Node.Mrn)
37+
log.Info().Str("name", space.Node.Name).Str("mrn", space.Node.Mrn).Msg("deleted space")
38+
}
39+
}
40+
41+
func Delete(ctx context.Context, gqlClient *mondoogql.Client, mrn string) error {
42+
var m struct {
43+
DeleteSpace string `graphql:"deleteSpace(spaceMrn: $input)"`
44+
}
45+
return gqlClient.Mutate(ctx, &m, nil, map[string]interface{}{
46+
"input": mondoogql.ID(mrn),
47+
})
48+
}
49+
50+
type SpaceConnection struct {
51+
Edges []struct {
52+
Node struct {
53+
Name string
54+
Mrn string
55+
}
56+
}
57+
}
58+
59+
type OrgWithListSpacesQuery struct {
60+
Id string
61+
Mrn string
62+
Name string
63+
Description string
64+
// TODO: handle pagination
65+
SpaceList SpaceConnection `graphql:"spacesList"`
66+
}
67+
68+
// TODO: output is not great yet, lets focus on spaces
69+
func ListSpaces(ctx context.Context, gqlClient *mondoogql.Client, orgMrn string) (SpaceConnection, error) {
70+
var q struct {
71+
Organization OrgWithListSpacesQuery `graphql:"organization(mrn: $mrn)"`
72+
}
73+
variables := map[string]interface{}{
74+
"mrn": mondoogql.String(orgMrn),
75+
}
76+
77+
err := gqlClient.Query(ctx, &q, variables)
78+
if err != nil {
79+
return SpaceConnection{}, err
80+
}
81+
82+
return q.Organization.SpaceList, nil
83+
}

0 commit comments

Comments
 (0)