|
| 1 | +package flink |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "sort" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/spf13/cobra" |
| 9 | + |
| 10 | + networkingprivatelinkv1 "github.com/confluentinc/ccloud-sdk-go-v2/networking-privatelink/v1" |
| 11 | + |
| 12 | + pcmd "github.com/confluentinc/cli/v4/pkg/cmd" |
| 13 | + "github.com/confluentinc/cli/v4/pkg/errors" |
| 14 | + "github.com/confluentinc/cli/v4/pkg/examples" |
| 15 | + "github.com/confluentinc/cli/v4/pkg/output" |
| 16 | +) |
| 17 | + |
| 18 | +type CloudRegionKey struct { |
| 19 | + cloud string |
| 20 | + region string |
| 21 | +} |
| 22 | + |
| 23 | +func (c *command) newEndpointListCommand() *cobra.Command { |
| 24 | + cmd := &cobra.Command{ |
| 25 | + Use: "list", |
| 26 | + RunE: c.endpointList, |
| 27 | + Short: "List Flink endpoint.", |
| 28 | + Example: examples.BuildExampleString( |
| 29 | + examples.Example{ |
| 30 | + Text: "List the available Flink endpoints with current cloud provider and region.", |
| 31 | + Code: "confluent flink endpoint list", |
| 32 | + }, |
| 33 | + ), |
| 34 | + } |
| 35 | + |
| 36 | + pcmd.AddContextFlag(cmd, c.CLICommand) |
| 37 | + pcmd.AddOutputFlag(cmd) |
| 38 | + |
| 39 | + return cmd |
| 40 | +} |
| 41 | + |
| 42 | +func (c *command) endpointList(cmd *cobra.Command, _ []string) error { |
| 43 | + // Get the current Flink cloud and region |
| 44 | + cloud := c.Context.GetCurrentFlinkCloudProvider() |
| 45 | + region := c.Context.GetCurrentFlinkRegion() |
| 46 | + if cloud == "" || region == "" { |
| 47 | + return errors.NewErrorWithSuggestions( |
| 48 | + "Current Flink cloud provider or region is empty", |
| 49 | + "Run `confluent flink region use --cloud <cloud> --region <region>` to set the Flink cloud provider and region first.", |
| 50 | + ) |
| 51 | + } |
| 52 | + cloud = strings.ToUpper(cloud) |
| 53 | + |
| 54 | + environmentId, err := c.Context.EnvironmentId() |
| 55 | + if err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + |
| 59 | + list := output.NewList(cmd) |
| 60 | + flinkRegions, err := c.V2Client.ListFlinkRegions(cloud, region) |
| 61 | + if err != nil { |
| 62 | + return fmt.Errorf("unable to list Flink endpoint, failed to list Flink regions: %w", err) |
| 63 | + } |
| 64 | + results := make([]*flinkEndpointOut, 0, len(flinkRegions)*2) |
| 65 | + |
| 66 | + // 1 - List all the public endpoints based optionally on cloud(upper case) and region(lower case) |
| 67 | + for _, flinkRegion := range flinkRegions { |
| 68 | + results = append(results, &flinkEndpointOut{ |
| 69 | + IsCurrent: flinkRegion.GetHttpEndpoint() == c.Context.GetCurrentFlinkEndpoint(), |
| 70 | + Endpoint: flinkRegion.GetHttpEndpoint(), |
| 71 | + Cloud: flinkRegion.GetCloud(), |
| 72 | + Region: flinkRegion.GetRegionName(), |
| 73 | + Type: publicFlinkEndpointType, |
| 74 | + }) |
| 75 | + } |
| 76 | + |
| 77 | + // 2 - List all the private endpoints based on the presence of "READY" PrivateLinkAttachments as filter |
| 78 | + // Note the `cloud` and `region` parameters have to be `nil` instead of empty slice in case of no filter |
| 79 | + platts, err := c.V2Client.ListPrivateLinkAttachments(environmentId, nil, nil, nil, []string{"READY"}) |
| 80 | + if err != nil { |
| 81 | + return fmt.Errorf("unable to list Flink endpoint, failed to list private link attachments: %w", err) |
| 82 | + } |
| 83 | + |
| 84 | + filterKeyMap := buildCloudRegionKeyFilterMapFromPrivateLinkAttachments(platts) |
| 85 | + |
| 86 | + for _, flinkRegion := range flinkRegions { |
| 87 | + key := CloudRegionKey{ |
| 88 | + cloud: flinkRegion.GetCloud(), |
| 89 | + region: flinkRegion.GetRegionName(), |
| 90 | + } |
| 91 | + |
| 92 | + if _, ok := filterKeyMap[key]; ok { |
| 93 | + results = append(results, &flinkEndpointOut{ |
| 94 | + IsCurrent: flinkRegion.GetPrivateHttpEndpoint() == c.Context.GetCurrentFlinkEndpoint(), |
| 95 | + Endpoint: flinkRegion.GetPrivateHttpEndpoint(), |
| 96 | + Cloud: flinkRegion.GetCloud(), |
| 97 | + Region: flinkRegion.GetRegionName(), |
| 98 | + Type: privateFlinkEndpointType, |
| 99 | + }) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // 3 - List all the CCN endpoint with the list of "READY" network domains |
| 104 | + // Note the cloud and region have to be empty slice instead of `nil` in case of no filter |
| 105 | + networks, err := c.V2Client.ListNetworks(environmentId, nil, []string{cloud}, []string{region}, nil, []string{"READY"}, nil) |
| 106 | + if err != nil { |
| 107 | + return fmt.Errorf("unable to list Flink endpoint, failed to list networks: %w", err) |
| 108 | + } |
| 109 | + |
| 110 | + for _, network := range networks { |
| 111 | + suffix := network.Status.GetEndpointSuffix() |
| 112 | + endpoint := fmt.Sprintf("https://flink%s", suffix) |
| 113 | + results = append(results, &flinkEndpointOut{ |
| 114 | + IsCurrent: endpoint == c.Context.GetCurrentFlinkEndpoint(), |
| 115 | + Endpoint: endpoint, |
| 116 | + Cloud: network.Spec.GetCloud(), |
| 117 | + Region: network.Spec.GetRegion(), |
| 118 | + Type: privateFlinkEndpointType, |
| 119 | + }) |
| 120 | + } |
| 121 | + |
| 122 | + // Sort the results order by cloud, region, type and endpoint |
| 123 | + sort.Slice(results, func(i, j int) bool { |
| 124 | + if results[i].Cloud != results[j].Cloud { |
| 125 | + return results[i].Cloud < results[j].Cloud |
| 126 | + } |
| 127 | + if results[i].Region != results[j].Region { |
| 128 | + return results[i].Region < results[j].Region |
| 129 | + } |
| 130 | + if results[i].Type != results[j].Type { |
| 131 | + return results[i].Type < results[j].Type |
| 132 | + } |
| 133 | + return results[i].Endpoint < results[j].Endpoint |
| 134 | + }) |
| 135 | + |
| 136 | + for _, result := range results { |
| 137 | + list.Add(&flinkEndpointOut{ |
| 138 | + IsCurrent: result.IsCurrent, |
| 139 | + Endpoint: result.Endpoint, |
| 140 | + Cloud: result.Cloud, |
| 141 | + Region: result.Region, |
| 142 | + Type: result.Type, |
| 143 | + }) |
| 144 | + } |
| 145 | + |
| 146 | + // Disable the default sort to use the custom sort above |
| 147 | + list.Sort(false) |
| 148 | + return list.Print() |
| 149 | +} |
| 150 | + |
| 151 | +// buildCloudRegionKeyFilterMapFromPrivateLinkAttachments creates a map of unique cloud/region pairs from PrivateLinkAttachments. |
| 152 | +// This function helps deduplicate scenarios where users have multiple private link attachments in the same cloud region. |
| 153 | +// Each unique combination of cloud and region is represented as a CloudRegionKey in the returned map. |
| 154 | +// |
| 155 | +// Parameters: |
| 156 | +// - platts: A slice of NetworkingV1PrivateLinkAttachment objects to process |
| 157 | +// |
| 158 | +// Returns: |
| 159 | +// - A map with CloudRegionKey as keys and boolean 'true' as values for each unique cloud/region combination |
| 160 | +func buildCloudRegionKeyFilterMapFromPrivateLinkAttachments(platts []networkingprivatelinkv1.NetworkingV1PrivateLinkAttachment) map[CloudRegionKey]bool { |
| 161 | + result := make(map[CloudRegionKey]bool, len(platts)) |
| 162 | + for _, platt := range platts { |
| 163 | + if platt.Spec.GetCloud() == "" || platt.Spec.GetRegion() == "" { |
| 164 | + continue |
| 165 | + } |
| 166 | + compositeKey := CloudRegionKey{ |
| 167 | + cloud: platt.Spec.GetCloud(), |
| 168 | + region: platt.Spec.GetRegion(), |
| 169 | + } |
| 170 | + result[compositeKey] = true |
| 171 | + } |
| 172 | + return result |
| 173 | +} |
0 commit comments