-
Notifications
You must be signed in to change notification settings - Fork 61
OSAC-240: Add describe networkclass CLI command and update #701
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
Open
SiddarthR56
wants to merge
1
commit into
osac-project:main
Choose a base branch
from
SiddarthR56:osac-240
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
177 changes: 177 additions & 0 deletions
177
internal/cmd/cli/describe/networkclass/describe_networkclass_cmd.go
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,177 @@ | ||
| /* | ||
| Copyright (c) 2025 Red Hat Inc. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the | ||
| License. You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an | ||
| "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific | ||
| language governing permissions and limitations under the License. | ||
| */ | ||
|
|
||
| package networkclass | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| "log/slog" | ||
| "strings" | ||
| "text/tabwriter" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "google.golang.org/protobuf/proto" | ||
|
|
||
| publicv1 "github.com/osac-project/fulfillment-service/internal/api/osac/public/v1" | ||
| "github.com/osac-project/fulfillment-service/internal/cmd/cli/lookup" | ||
| "github.com/osac-project/fulfillment-service/internal/config" | ||
| "github.com/osac-project/fulfillment-service/internal/logging" | ||
| "github.com/osac-project/fulfillment-service/internal/terminal" | ||
| ) | ||
|
|
||
| func Cmd() *cobra.Command { | ||
| runner := &runnerContext{} | ||
| result := &cobra.Command{ | ||
| Use: "networkclass [FLAG...] ID|NAME", | ||
| Aliases: []string{"networkclasses"}, | ||
| Short: shortHelp, | ||
| Long: longHelp, | ||
| DisableFlagsInUseLine: true, | ||
| Args: cobra.ExactArgs(1), | ||
| RunE: runner.run, | ||
| } | ||
| result.Flags().BoolVar( | ||
| &runner.includeDeleted, | ||
| "include-deleted", | ||
| false, | ||
| "Include soft-deleted objects in resolution.", | ||
| ) | ||
| return result | ||
| } | ||
|
|
||
| type runnerContext struct { | ||
| logger *slog.Logger | ||
| console *terminal.Console | ||
| includeDeleted bool | ||
| } | ||
|
|
||
| func (c *runnerContext) run(cmd *cobra.Command, args []string) error { | ||
| ref := args[0] | ||
|
|
||
| ctx := cmd.Context() | ||
|
|
||
| c.logger = logging.LoggerFromContext(ctx) | ||
| c.console = terminal.ConsoleFromContext(ctx) | ||
|
|
||
| cfg := config.SettingsFromContext(ctx) | ||
| if !cfg.Armed() { | ||
| return fmt.Errorf("there is no configuration, run the 'login' command") | ||
| } | ||
|
|
||
| conn, err := cfg.Connect(ctx, cmd.Flags()) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create gRPC connection: %w", err) | ||
| } | ||
| defer conn.Close() | ||
|
|
||
| client := publicv1.NewNetworkClassesClient(conn) | ||
|
|
||
| matched, err := lookup.Find(ref, "network class", lookup.FindOptions{IncludeDeleted: c.includeDeleted}, func(filter string, limit int32) ([]*publicv1.NetworkClass, error) { | ||
| resp, err := client.List(ctx, publicv1.NetworkClassesListRequest_builder{ | ||
| Filter: proto.String(filter), | ||
| Limit: proto.Int32(limit), | ||
| }.Build()) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to describe network class: %w", err) | ||
| } | ||
| return resp.GetItems(), nil | ||
| }) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| RenderNetworkClass(c.console, matched) | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func boolYesNo(v bool) string { | ||
| if v { | ||
| return "yes" | ||
| } | ||
| return "no" | ||
| } | ||
|
|
||
| // RenderNetworkClass writes a formatted description of nc to w. | ||
| func RenderNetworkClass(w io.Writer, nc *publicv1.NetworkClass) { | ||
| writer := tabwriter.NewWriter(w, 0, 0, 2, ' ', 0) | ||
|
|
||
| name := "-" | ||
| if v := nc.GetMetadata().GetName(); v != "" { | ||
| name = v | ||
| } | ||
|
|
||
| title := "-" | ||
| if v := nc.GetTitle(); v != "" { | ||
| title = v | ||
| } | ||
|
|
||
| description := "-" | ||
| if v := nc.GetDescription(); v != "" { | ||
| description = v | ||
| } | ||
|
|
||
| supportsIPv4 := "-" | ||
| supportsIPv6 := "-" | ||
| supportsDualStack := "-" | ||
| if caps := nc.GetCapabilities(); caps != nil { | ||
| supportsIPv4 = boolYesNo(caps.GetSupportsIpv4()) | ||
| supportsIPv6 = boolYesNo(caps.GetSupportsIpv6()) | ||
| supportsDualStack = boolYesNo(caps.GetSupportsDualStack()) | ||
| } | ||
|
|
||
| isDefault := "-" | ||
| if nc.HasIsDefault() { | ||
| isDefault = boolYesNo(nc.GetIsDefault()) | ||
| } | ||
|
|
||
| state := "-" | ||
| message := "-" | ||
| if nc.GetStatus() != nil { | ||
| state = strings.TrimPrefix(nc.GetStatus().GetState().String(), "NETWORK_CLASS_STATE_") | ||
| if v := nc.GetStatus().GetMessage(); v != "" { | ||
| message = v | ||
| } | ||
| } | ||
|
|
||
| fmt.Fprintf(writer, "ID:\t%s\n", nc.GetId()) | ||
| fmt.Fprintf(writer, "Name:\t%s\n", name) | ||
| fmt.Fprintf(writer, "Title:\t%s\n", title) | ||
| fmt.Fprintf(writer, "Description:\t%s\n", description) | ||
| fmt.Fprintf(writer, "Supports IPv4:\t%s\n", supportsIPv4) | ||
| fmt.Fprintf(writer, "Supports IPv6:\t%s\n", supportsIPv6) | ||
| fmt.Fprintf(writer, "Supports Dual Stack:\t%s\n", supportsDualStack) | ||
| fmt.Fprintf(writer, "Default:\t%s\n", isDefault) | ||
| fmt.Fprintf(writer, "State:\t%s\n", state) | ||
| fmt.Fprintf(writer, "Message:\t%s\n", message) | ||
| writer.Flush() | ||
| } | ||
|
|
||
| const shortHelp = "Describe a network class" | ||
|
|
||
| const longHelp = ` | ||
| Display detailed information about a network class, referenced by identifier or name. | ||
|
|
||
| Examples: | ||
|
|
||
| {{ bt 3 }}shell | ||
| # Describe a network class by identifier: | ||
| {{ binary }} describe networkclass 019e5ff0-6266-7310-acf3-94e99a3786c9 | ||
| {{ bt 3 }} | ||
|
|
||
| {{ bt 3 }}shell | ||
| # Describe a network class by name: | ||
| {{ binary }} describe networkclass udn-net | ||
| {{ bt 3 }} | ||
| ` | ||
26 changes: 26 additions & 0 deletions
26
internal/cmd/cli/describe/networkclass/describe_networkclass_suite_test.go
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,26 @@ | ||
| /* | ||
| Copyright (c) 2025 Red Hat Inc. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the | ||
| License. You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an | ||
| "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific | ||
| language governing permissions and limitations under the License. | ||
| */ | ||
|
|
||
| package networkclass | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| . "github.com/onsi/ginkgo/v2/dsl/core" | ||
| . "github.com/onsi/gomega" | ||
| ) | ||
|
|
||
| func TestDescribeNetworkClass(t *testing.T) { | ||
| RegisterFailHandler(Fail) | ||
| RunSpecs(t, "Describe NetworkClass Suite") | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know we mostly don't care about readability of code, but this is awfully long and difficult to read. Consider making it a bit better.