|
| 1 | +package privatelink |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/AlecAivazis/survey/v2" |
| 8 | + "github.com/fatih/color" |
| 9 | + "github.com/juju/errors" |
| 10 | + "github.com/spf13/cobra" |
| 11 | + |
| 12 | + "github.com/tidbcloud/tidbcloud-cli/internal" |
| 13 | + "github.com/tidbcloud/tidbcloud-cli/internal/config" |
| 14 | + "github.com/tidbcloud/tidbcloud-cli/internal/flag" |
| 15 | + "github.com/tidbcloud/tidbcloud-cli/internal/output" |
| 16 | + "github.com/tidbcloud/tidbcloud-cli/internal/service/cloud" |
| 17 | + plapi "github.com/tidbcloud/tidbcloud-cli/pkg/tidbcloud/v1beta1/serverless/privatelink" |
| 18 | +) |
| 19 | + |
| 20 | +type AttachDomainOpts struct { |
| 21 | + interactive bool |
| 22 | +} |
| 23 | + |
| 24 | +func (o AttachDomainOpts) NonInteractiveFlags() []string { |
| 25 | + return []string{ |
| 26 | + flag.ClusterID, |
| 27 | + flag.PrivateLinkConnectionID, |
| 28 | + flag.PLCAttachDomainType, |
| 29 | + flag.PLCAttachDomainUniqueName, |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func (o AttachDomainOpts) RequiredFlags() []string { |
| 34 | + return []string{ |
| 35 | + flag.ClusterID, |
| 36 | + flag.PrivateLinkConnectionID, |
| 37 | + flag.PLCAttachDomainType, |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func (o *AttachDomainOpts) MarkInteractive(cmd *cobra.Command) error { |
| 42 | + o.interactive = true |
| 43 | + for _, fn := range o.NonInteractiveFlags() { |
| 44 | + if f := cmd.Flags().Lookup(fn); f != nil && f.Changed { |
| 45 | + o.interactive = false |
| 46 | + } |
| 47 | + } |
| 48 | + if !o.interactive { |
| 49 | + for _, fn := range o.RequiredFlags() { |
| 50 | + if err := cmd.MarkFlagRequired(fn); err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + return nil |
| 56 | +} |
| 57 | + |
| 58 | +func AttachDomainCmd(h *internal.Helper) *cobra.Command { |
| 59 | + opts := &AttachDomainOpts{interactive: true} |
| 60 | + cmd := &cobra.Command{ |
| 61 | + Use: "attach", |
| 62 | + Short: "Attach domains to a private link connection", |
| 63 | + Args: cobra.NoArgs, |
| 64 | + Example: fmt.Sprintf(` Attach domain (interactive): |
| 65 | + $ %[1]s serverless private-link-connection attach |
| 66 | +
|
| 67 | + Attach domain (non-interactive): |
| 68 | + $ %[1]s serverless private-link-connection attach -c <cluster-id> --private-link-connection-id <plc-id> --type <type> --unique-name <unique-name>`, config.CliName), |
| 69 | + PreRunE: func(cmd *cobra.Command, args []string) error { |
| 70 | + return opts.MarkInteractive(cmd) |
| 71 | + }, |
| 72 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 73 | + d, err := h.Client() |
| 74 | + if err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + ctx := cmd.Context() |
| 78 | + |
| 79 | + var clusterID, plcID string |
| 80 | + var domainType plapi.PrivateLinkConnectionDomainTypeEnum |
| 81 | + var uniqueName string |
| 82 | + var dryRun bool |
| 83 | + dryRun, err = cmd.Flags().GetBool(flag.PLCAttachDomainDryRun) |
| 84 | + if err != nil { |
| 85 | + return errors.Trace(err) |
| 86 | + } |
| 87 | + if opts.interactive { |
| 88 | + if !h.IOStreams.CanPrompt { |
| 89 | + return errors.New("The terminal doesn't support interactive mode, please use non-interactive mode") |
| 90 | + } |
| 91 | + |
| 92 | + project, err := cloud.GetSelectedProject(ctx, h.QueryPageSize, d) |
| 93 | + if err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + cluster, err := cloud.GetSelectedCluster(ctx, project.ID, h.QueryPageSize, d) |
| 97 | + if err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + clusterID = cluster.ID |
| 101 | + |
| 102 | + privatelink, err := cloud.GetSelectedPrivateLinkConnection(ctx, cluster.ID, int32(h.QueryPageSize), d) |
| 103 | + if err != nil { |
| 104 | + return err |
| 105 | + } |
| 106 | + plcID = privatelink.ID |
| 107 | + |
| 108 | + domainType, err = GetSelectedPLCAttachDomainType() |
| 109 | + if err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + |
| 113 | + switch domainType { |
| 114 | + case plapi.PRIVATELINKCONNECTIONDOMAINTYPEENUM_CONFLUENT: |
| 115 | + if err := survey.AskOne(&survey.Input{Message: "Domain unique name:"}, &uniqueName); err != nil { |
| 116 | + return err |
| 117 | + } |
| 118 | + case plapi.PRIVATELINKCONNECTIONDOMAINTYPEENUM_TIDBCLOUD_MANAGED: |
| 119 | + if !dryRun { |
| 120 | + if err := survey.AskOne(&survey.Input{Message: "Domain unique name:"}, &uniqueName); err != nil { |
| 121 | + return err |
| 122 | + } |
| 123 | + } |
| 124 | + default: |
| 125 | + return errors.New("invalid domain type") |
| 126 | + } |
| 127 | + } else { |
| 128 | + var err error |
| 129 | + clusterID, err = cmd.Flags().GetString(flag.ClusterID) |
| 130 | + if err != nil { |
| 131 | + return errors.Trace(err) |
| 132 | + } |
| 133 | + plcID, err = cmd.Flags().GetString(flag.PrivateLinkConnectionID) |
| 134 | + if err != nil { |
| 135 | + return errors.Trace(err) |
| 136 | + } |
| 137 | + domainTypeStr, err := cmd.Flags().GetString(flag.PLCAttachDomainType) |
| 138 | + if err != nil { |
| 139 | + return errors.Trace(err) |
| 140 | + } |
| 141 | + domainType = plapi.PrivateLinkConnectionDomainTypeEnum(domainTypeStr) |
| 142 | + uniqueName, err = cmd.Flags().GetString(flag.PLCAttachDomainUniqueName) |
| 143 | + if err != nil { |
| 144 | + return errors.Trace(err) |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + if domainType == "" { |
| 149 | + return errors.New("domain type is required") |
| 150 | + } |
| 151 | + |
| 152 | + body := &plapi.PrivateLinkConnectionServiceAttachDomainsBody{ |
| 153 | + AttachDomain: plapi.AttachDomain{ |
| 154 | + Type: domainType, |
| 155 | + UniqueName: &uniqueName, |
| 156 | + }, |
| 157 | + DryRun: &dryRun, |
| 158 | + } |
| 159 | + |
| 160 | + resp, err := d.AttachPrivateLinkDomains(ctx, clusterID, plcID, body) |
| 161 | + if err != nil { |
| 162 | + return errors.Trace(err) |
| 163 | + } |
| 164 | + if !dryRun { |
| 165 | + return output.PrintJson(h.IOStreams.Out, resp) |
| 166 | + } |
| 167 | + domains := make([]string, 0, len(resp.Domains)) |
| 168 | + for _, domain := range resp.Domains { |
| 169 | + domains = append(domains, *domain.Name) |
| 170 | + } |
| 171 | + fmt.Fprintf(h.IOStreams.Out, "unique name %s:\n%s\n", |
| 172 | + color.BlueString("%v", *resp.UniqueName), |
| 173 | + color.GreenString("%v", strings.Join(domains, "\n"))) |
| 174 | + return nil |
| 175 | + }, |
| 176 | + } |
| 177 | + |
| 178 | + cmd.Flags().StringP(flag.ClusterID, flag.ClusterIDShort, "", "The cluster ID.") |
| 179 | + cmd.Flags().String(flag.PrivateLinkConnectionID, "", "The private link connection ID.") |
| 180 | + cmd.Flags().String(flag.PLCAttachDomainType, "", fmt.Sprintf("The type of domain to attach, one of: %v", plapi.AllowedPrivateLinkConnectionDomainTypeEnumEnumValues)) |
| 181 | + 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.") |
| 182 | + cmd.Flags().Bool(flag.PLCAttachDomainDryRun, false, "set dry run mode to only show generated domains without attaching them.") |
| 183 | + |
| 184 | + return cmd |
| 185 | +} |
0 commit comments