-
Notifications
You must be signed in to change notification settings - Fork 4
feat: add config (ConfigMap) CLI commands #678
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
19bdd3a
feat: add configs (ConfigMap) CLI commands
frodesundby ed28203
test: add unit tests for config package
frodesundby 39ad253
chore: update GraphQL schema and regenerate client from local API
frodesundby b8f059e
fix: handle non-deletion case and stabilize time-based test in config
frodesundby 4ad505b
refactor: rename configs command to config and fix activity sort order
frodesundby File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "context" | ||
| "slices" | ||
| "sort" | ||
| "time" | ||
|
|
||
| "github.com/nais/cli/internal/naisapi" | ||
| "github.com/nais/cli/internal/naisapi/gql" | ||
| ) | ||
|
|
||
| type ConfigActivity struct { | ||
| CreatedAt time.Time `heading:"Created" json:"created_at"` | ||
| Actor string `json:"actor"` | ||
| Environment string `json:"environment"` | ||
| Message string `json:"message"` | ||
| } | ||
|
|
||
| type configActivityEntry struct { | ||
| CreatedAt time.Time | ||
| Actor string | ||
| Message string | ||
| EnvironmentName string | ||
| } | ||
|
|
||
| type configActivityResource struct { | ||
| Name string | ||
| DefaultEnvName string | ||
| Entries []configActivityEntry | ||
| } | ||
|
|
||
| func GetActivity(ctx context.Context, team, name string, environments []string, activityTypes []gql.ActivityLogActivityType, limit int) ([]ConfigActivity, bool, error) { | ||
| _ = `# @genqlient | ||
| query GetConfigActivity($team: Slug!, $name: String!, $activityTypes: [ActivityLogActivityType!], $first: Int) { | ||
| team(slug: $team) { | ||
| configs(filter: { name: $name }, first: 1000) { | ||
| nodes { | ||
| name | ||
| teamEnvironment { | ||
| environment { | ||
| name | ||
| } | ||
| } | ||
| activityLog(first: $first, filter: { activityTypes: $activityTypes }) { | ||
| nodes { | ||
| actor | ||
| createdAt | ||
| message | ||
| environmentName | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ` | ||
|
|
||
| client, err := naisapi.GraphqlClient(ctx) | ||
| if err != nil { | ||
| return nil, false, err | ||
| } | ||
|
|
||
| resp, err := gql.GetConfigActivity(ctx, client, team, name, activityTypes, limit) | ||
| if err != nil { | ||
| return nil, false, err | ||
| } | ||
|
|
||
| resources := make([]configActivityResource, 0, len(resp.Team.Configs.Nodes)) | ||
| for _, c := range resp.Team.Configs.Nodes { | ||
| entries := make([]configActivityEntry, 0, len(c.ActivityLog.Nodes)) | ||
| for _, entry := range c.ActivityLog.Nodes { | ||
| entries = append(entries, configActivityEntry{ | ||
| CreatedAt: entry.GetCreatedAt(), | ||
| Actor: entry.GetActor(), | ||
| Message: entry.GetMessage(), | ||
| EnvironmentName: entry.GetEnvironmentName(), | ||
| }) | ||
| } | ||
|
|
||
| resources = append(resources, configActivityResource{ | ||
| Name: c.Name, | ||
| DefaultEnvName: c.TeamEnvironment.Environment.Name, | ||
| Entries: entries, | ||
| }) | ||
| } | ||
|
|
||
| ret, found := buildConfigActivity(resources, name, environments) | ||
| return ret, found, nil | ||
| } | ||
|
|
||
| func buildConfigActivity(resources []configActivityResource, name string, environments []string) ([]ConfigActivity, bool) { | ||
| found := false | ||
| ret := make([]ConfigActivity, 0) | ||
|
|
||
| for _, c := range resources { | ||
| if c.Name != name { | ||
| continue | ||
| } | ||
|
|
||
| defaultEnv := c.DefaultEnvName | ||
| if len(environments) > 0 && !slices.Contains(environments, defaultEnv) { | ||
| continue | ||
| } | ||
|
|
||
| found = true | ||
|
|
||
| for _, entry := range c.Entries { | ||
| env := defaultEnv | ||
| if entry.EnvironmentName != "" { | ||
| env = entry.EnvironmentName | ||
| } | ||
frodesundby marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ret = append(ret, ConfigActivity{ | ||
| CreatedAt: entry.CreatedAt, | ||
| Actor: entry.Actor, | ||
| Environment: env, | ||
| Message: entry.Message, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| sort.SliceStable(ret, func(i, j int) bool { | ||
| return ret[i].CreatedAt.After(ret[j].CreatedAt) | ||
| }) | ||
|
|
||
| return ret, found | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "reflect" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| func TestBuildConfigActivity(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| now := time.Now().UTC() | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| resources []configActivityResource | ||
| configName string | ||
| environments []string | ||
| wantFound bool | ||
| want []ConfigActivity | ||
| }{ | ||
| { | ||
| name: "exact name match with fallback environment", | ||
| configName: "app-settings", | ||
| resources: []configActivityResource{ | ||
| { | ||
| Name: "app-settings", | ||
| DefaultEnvName: "dev-gcp", | ||
| Entries: []configActivityEntry{ | ||
| {CreatedAt: now, Actor: "alice@example.com", Message: "Updated config value", EnvironmentName: ""}, | ||
| }, | ||
| }, | ||
| }, | ||
| wantFound: true, | ||
| want: []ConfigActivity{ | ||
| {CreatedAt: now, Actor: "alice@example.com", Environment: "dev-gcp", Message: "Updated config value"}, | ||
| }, | ||
| }, | ||
| { | ||
| name: "entry with explicit environment overrides default", | ||
| configName: "app-settings", | ||
| resources: []configActivityResource{ | ||
| { | ||
| Name: "app-settings", | ||
| DefaultEnvName: "dev-gcp", | ||
| Entries: []configActivityEntry{ | ||
| {CreatedAt: now, Actor: "bob@example.com", Message: "Created config", EnvironmentName: "prod-gcp"}, | ||
| }, | ||
| }, | ||
| }, | ||
| wantFound: true, | ||
| want: []ConfigActivity{ | ||
| {CreatedAt: now, Actor: "bob@example.com", Environment: "prod-gcp", Message: "Created config"}, | ||
| }, | ||
| }, | ||
| { | ||
| name: "not found in requested environment", | ||
| configName: "app-settings", | ||
| resources: []configActivityResource{ | ||
| { | ||
| Name: "app-settings", | ||
| DefaultEnvName: "dev-gcp", | ||
| Entries: []configActivityEntry{ | ||
| {CreatedAt: now, Actor: "alice@example.com", Message: "Updated config value", EnvironmentName: ""}, | ||
| }, | ||
| }, | ||
| }, | ||
| environments: []string{"prod-gcp"}, | ||
| wantFound: false, | ||
| want: []ConfigActivity{}, | ||
| }, | ||
| { | ||
| name: "not found when only partial name exists", | ||
| configName: "app", | ||
| resources: []configActivityResource{ | ||
| { | ||
| Name: "app-settings", | ||
| DefaultEnvName: "dev-gcp", | ||
| }, | ||
| }, | ||
| wantFound: false, | ||
| want: []ConfigActivity{}, | ||
| }, | ||
| { | ||
| name: "multiple resources same name different envs", | ||
| configName: "db-config", | ||
| resources: []configActivityResource{ | ||
| { | ||
| Name: "db-config", | ||
| DefaultEnvName: "dev-gcp", | ||
| Entries: []configActivityEntry{ | ||
| {CreatedAt: now, Actor: "alice@example.com", Message: "Added key", EnvironmentName: ""}, | ||
| }, | ||
| }, | ||
| { | ||
| Name: "db-config", | ||
| DefaultEnvName: "prod-gcp", | ||
| Entries: []configActivityEntry{ | ||
| {CreatedAt: now, Actor: "bob@example.com", Message: "Updated key", EnvironmentName: ""}, | ||
| }, | ||
| }, | ||
| }, | ||
| environments: []string{"prod-gcp"}, | ||
| wantFound: true, | ||
| want: []ConfigActivity{ | ||
| {CreatedAt: now, Actor: "bob@example.com", Environment: "prod-gcp", Message: "Updated key"}, | ||
| }, | ||
| }, | ||
| { | ||
| name: "empty resources", | ||
| configName: "anything", | ||
| resources: nil, | ||
| wantFound: false, | ||
| want: []ConfigActivity{}, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| got, found := buildConfigActivity(tt.resources, tt.configName, tt.environments) | ||
| if found != tt.wantFound { | ||
| t.Fatalf("buildConfigActivity() found = %v, want %v", found, tt.wantFound) | ||
| } | ||
| if !reflect.DeepEqual(got, tt.want) { | ||
| t.Fatalf("buildConfigActivity() = %#v, want %#v", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package command | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| activityutil "github.com/nais/cli/internal/activity" | ||
| "github.com/nais/cli/internal/config" | ||
| "github.com/nais/cli/internal/config/command/flag" | ||
| "github.com/nais/naistrix" | ||
| "github.com/nais/naistrix/output" | ||
| ) | ||
|
|
||
| func activity(parentFlags *flag.Config) *naistrix.Command { | ||
| f := &flag.Activity{ | ||
| Config: parentFlags, | ||
| Output: "table", | ||
| Limit: 20, | ||
| } | ||
|
|
||
| return &naistrix.Command{ | ||
| Name: "activity", | ||
| Title: "Show activity for a config.", | ||
| Args: defaultArgs, | ||
| Flags: f, | ||
| RunFunc: func(ctx context.Context, args *naistrix.Arguments, out *naistrix.OutputWriter) error { | ||
| if err := validateArgs(args); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| activityTypes, err := activityutil.ParseActivityTypes(f.ActivityType) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| ret, found, err := config.GetActivity(ctx, f.Team, args.Get("name"), f.Environment, activityTypes, f.Limit) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if !found { | ||
| out.Println("Config not found.") | ||
| return nil | ||
| } | ||
|
|
||
| if len(ret) == 0 { | ||
| out.Println("No activity found for config.") | ||
| return nil | ||
| } | ||
|
|
||
| if f.Output == "json" { | ||
| return out.JSON(output.JSONWithPrettyOutput()).Render(ret) | ||
| } | ||
|
|
||
| return out.Table().Render(ret) | ||
| }, | ||
| AutoCompleteFunc: func(ctx context.Context, args *naistrix.Arguments, _ string) ([]string, string) { | ||
| if args.Len() == 0 { | ||
| if f.Team == "" { | ||
| return nil, "Please provide team to auto-complete config names. 'nais defaults set team <team>', or '--team <team>' flag." | ||
| } | ||
| environments := []string(f.Environment) | ||
| if len(environments) == 0 { | ||
| environments = environmentValuesFromCLIArgs() | ||
| } | ||
| return autoCompleteConfigNamesInEnvironments(ctx, f.Team, environments, false) | ||
| } | ||
| return nil, "" | ||
| }, | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.