-
Notifications
You must be signed in to change notification settings - Fork 3
implement plc attach domain #315
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 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
abda034
implemnet
shiyuhang0 57b7283
fix license
shiyuhang0 2bd6960
doc
shiyuhang0 d06f225
update swagger
shiyuhang0 7608021
mock
shiyuhang0 c621407
add server name
shiyuhang0 219b77c
Merge branch 'main' into plc_attach_domain_cli
shiyuhang0 c8348c2
opt with review
shiyuhang0 d14d27e
update proto
shiyuhang0 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
41 changes: 41 additions & 0 deletions
41
docs/generate_doc/ticloud_serverless_private-link-connection_attach.md
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,41 @@ | ||
| ## ticloud serverless private-link-connection attach | ||
|
|
||
| Attach domains to a private link connection | ||
|
|
||
| ``` | ||
| ticloud serverless private-link-connection attach [flags] | ||
| ``` | ||
|
|
||
| ### Examples | ||
|
|
||
| ``` | ||
| Attach domain (interactive): | ||
| $ ticloud serverless private-link-connection attach | ||
|
|
||
| Attach domain (non-interactive): | ||
| $ ticloud serverless private-link-connection attach -c <cluster-id> --private-link-connection-id <plc-id> --type <type> --unique-name <unique-name> | ||
| ``` | ||
|
|
||
| ### Options | ||
|
|
||
| ``` | ||
| -c, --cluster-id string The cluster ID. | ||
| --dry-run set dry run mode to only show generated domains without attaching them. | ||
| -h, --help help for attach | ||
| --private-link-connection-id string The private link connection ID. | ||
| --type string The type of domain to attach, one of: [TIDBCLOUD_MANAGED CONFLUENT] | ||
| --unique-name string The unique name of the domain to attach, you can use --dry-run to generate the unique name when attaching a TiDB Cloud managed domain. | ||
| ``` | ||
|
|
||
| ### Options inherited from parent commands | ||
|
|
||
| ``` | ||
| -D, --debug Enable debug mode | ||
| --no-color Disable color output | ||
| -P, --profile string Profile to use from your configuration file | ||
| ``` | ||
|
|
||
| ### SEE ALSO | ||
|
|
||
| * [ticloud serverless private-link-connection](ticloud_serverless_private-link-connection.md) - Manage private link connections for dataflow | ||
|
|
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
39 changes: 39 additions & 0 deletions
39
docs/generate_doc/ticloud_serverless_private-link-connection_detach.md
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,39 @@ | ||
| ## ticloud serverless private-link-connection detach | ||
|
|
||
| Detach domains from a private link connection | ||
|
|
||
| ``` | ||
| ticloud serverless private-link-connection detach [flags] | ||
| ``` | ||
|
|
||
| ### Examples | ||
|
|
||
| ``` | ||
| Detach domains (interactive): | ||
| $ ticloud serverless private-link-connection detach | ||
|
|
||
| Detach domains (non-interactive): | ||
| $ ticloud serverless private-link-connection detach -c <cluster-id> --private-link-connection-id <plc-id> --plc-attach-domain-id <attach-id> | ||
| ``` | ||
|
|
||
| ### Options | ||
|
|
||
| ``` | ||
| -c, --cluster-id string The cluster ID. | ||
| -h, --help help for detach | ||
| --plc-attach-domain-id string The private link connection attach domain ID. | ||
| --private-link-connection-id string The private link connection ID. | ||
| ``` | ||
|
|
||
| ### Options inherited from parent commands | ||
|
|
||
| ``` | ||
| -D, --debug Enable debug mode | ||
| --no-color Disable color output | ||
| -P, --profile string Profile to use from your configuration file | ||
| ``` | ||
|
|
||
| ### SEE ALSO | ||
|
|
||
| * [ticloud serverless private-link-connection](ticloud_serverless_private-link-connection.md) - Manage private link connections for dataflow | ||
|
|
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,199 @@ | ||
| // Copyright 2025 PingCAP, 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 privatelink | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/AlecAivazis/survey/v2" | ||
| "github.com/fatih/color" | ||
| "github.com/juju/errors" | ||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/tidbcloud/tidbcloud-cli/internal" | ||
| "github.com/tidbcloud/tidbcloud-cli/internal/config" | ||
| "github.com/tidbcloud/tidbcloud-cli/internal/flag" | ||
| "github.com/tidbcloud/tidbcloud-cli/internal/output" | ||
| "github.com/tidbcloud/tidbcloud-cli/internal/service/cloud" | ||
| plapi "github.com/tidbcloud/tidbcloud-cli/pkg/tidbcloud/v1beta1/serverless/privatelink" | ||
| ) | ||
|
|
||
| type AttachDomainOpts struct { | ||
| interactive bool | ||
| } | ||
|
|
||
| func (o AttachDomainOpts) NonInteractiveFlags() []string { | ||
| return []string{ | ||
| flag.ClusterID, | ||
| flag.PrivateLinkConnectionID, | ||
| flag.PLCAttachDomainType, | ||
| flag.PLCAttachDomainUniqueName, | ||
| } | ||
| } | ||
|
|
||
| func (o AttachDomainOpts) RequiredFlags() []string { | ||
| return []string{ | ||
| flag.ClusterID, | ||
| flag.PrivateLinkConnectionID, | ||
| flag.PLCAttachDomainType, | ||
| } | ||
| } | ||
|
|
||
| func (o *AttachDomainOpts) MarkInteractive(cmd *cobra.Command) error { | ||
| o.interactive = true | ||
| for _, fn := range o.NonInteractiveFlags() { | ||
| if f := cmd.Flags().Lookup(fn); f != nil && f.Changed { | ||
| o.interactive = false | ||
| } | ||
| } | ||
| if !o.interactive { | ||
| for _, fn := range o.RequiredFlags() { | ||
| if err := cmd.MarkFlagRequired(fn); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func AttachDomainCmd(h *internal.Helper) *cobra.Command { | ||
| opts := &AttachDomainOpts{interactive: true} | ||
| cmd := &cobra.Command{ | ||
| Use: "attach", | ||
shiyuhang0 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Short: "Attach domains to a private link connection", | ||
| Args: cobra.NoArgs, | ||
| Example: fmt.Sprintf(` Attach domain (interactive): | ||
| $ %[1]s serverless private-link-connection attach | ||
|
|
||
| Attach domain (non-interactive): | ||
| $ %[1]s serverless private-link-connection attach -c <cluster-id> --private-link-connection-id <plc-id> --type <type> --unique-name <unique-name>`, config.CliName), | ||
| PreRunE: func(cmd *cobra.Command, args []string) error { | ||
| return opts.MarkInteractive(cmd) | ||
| }, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| d, err := h.Client() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| ctx := cmd.Context() | ||
|
|
||
| var clusterID, plcID string | ||
| var domainType plapi.PrivateLinkConnectionDomainTypeEnum | ||
| var uniqueName string | ||
| var dryRun bool | ||
| dryRun, err = cmd.Flags().GetBool(flag.PLCAttachDomainDryRun) | ||
| if err != nil { | ||
| return errors.Trace(err) | ||
| } | ||
| if opts.interactive { | ||
| if !h.IOStreams.CanPrompt { | ||
| return errors.New("The terminal doesn't support interactive mode, please use non-interactive mode") | ||
| } | ||
|
|
||
| project, err := cloud.GetSelectedProject(ctx, h.QueryPageSize, d) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| cluster, err := cloud.GetSelectedCluster(ctx, project.ID, h.QueryPageSize, d) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| clusterID = cluster.ID | ||
|
|
||
| privatelink, err := cloud.GetSelectedPrivateLinkConnection(ctx, cluster.ID, int32(h.QueryPageSize), d) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| plcID = privatelink.ID | ||
|
|
||
| domainType, err = GetSelectedPLCAttachDomainType() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| switch domainType { | ||
| case plapi.PRIVATELINKCONNECTIONDOMAINTYPEENUM_CONFLUENT: | ||
| if err := survey.AskOne(&survey.Input{Message: "Domain unique name:"}, &uniqueName); err != nil { | ||
| return err | ||
| } | ||
| case plapi.PRIVATELINKCONNECTIONDOMAINTYPEENUM_TIDBCLOUD_MANAGED: | ||
| if !dryRun { | ||
| if err := survey.AskOne(&survey.Input{Message: "Domain unique name:"}, &uniqueName); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| default: | ||
| return errors.New("invalid domain type") | ||
| } | ||
| } else { | ||
| var err error | ||
| clusterID, err = cmd.Flags().GetString(flag.ClusterID) | ||
| if err != nil { | ||
| return errors.Trace(err) | ||
| } | ||
| plcID, err = cmd.Flags().GetString(flag.PrivateLinkConnectionID) | ||
| if err != nil { | ||
| return errors.Trace(err) | ||
| } | ||
| domainTypeStr, err := cmd.Flags().GetString(flag.PLCAttachDomainType) | ||
| if err != nil { | ||
| return errors.Trace(err) | ||
| } | ||
| domainType = plapi.PrivateLinkConnectionDomainTypeEnum(domainTypeStr) | ||
| uniqueName, err = cmd.Flags().GetString(flag.PLCAttachDomainUniqueName) | ||
| if err != nil { | ||
| return errors.Trace(err) | ||
| } | ||
| } | ||
|
|
||
| if domainType == "" { | ||
shiyuhang0 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return errors.New("domain type is required") | ||
| } | ||
|
|
||
| body := &plapi.PrivateLinkConnectionServiceAttachDomainsBody{ | ||
| AttachDomain: plapi.AttachDomain{ | ||
| Type: domainType, | ||
| UniqueName: &uniqueName, | ||
| }, | ||
| DryRun: &dryRun, | ||
| } | ||
|
|
||
| resp, err := d.AttachPrivateLinkDomains(ctx, clusterID, plcID, body) | ||
| if err != nil { | ||
| return errors.Trace(err) | ||
| } | ||
| if !dryRun { | ||
| return output.PrintJson(h.IOStreams.Out, resp) | ||
| } | ||
| domains := make([]string, 0, len(resp.Domains)) | ||
| for _, domain := range resp.Domains { | ||
| domains = append(domains, *domain.Name) | ||
| } | ||
| fmt.Fprintf(h.IOStreams.Out, "unique name %s:\n%s\n", | ||
| color.BlueString("%v", *resp.UniqueName), | ||
| color.GreenString("%v", strings.Join(domains, "\n"))) | ||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().StringP(flag.ClusterID, flag.ClusterIDShort, "", "The cluster ID.") | ||
| cmd.Flags().String(flag.PrivateLinkConnectionID, "", "The private link connection ID.") | ||
| cmd.Flags().String(flag.PLCAttachDomainType, "", fmt.Sprintf("The type of domain to attach, one of: %v", plapi.AllowedPrivateLinkConnectionDomainTypeEnumEnumValues)) | ||
| cmd.Flags().String(flag.PLCAttachDomainUniqueName, "", "The unique name of the domain to attach, you can use --dry-run to generate the unique name when attaching a TiDB Cloud managed domain.") | ||
| cmd.Flags().Bool(flag.PLCAttachDomainDryRun, false, "set dry run mode to only show generated domains without attaching them.") | ||
shiyuhang0 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return cmd | ||
| } | ||
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
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.