|
| 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