GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Merge pull request #112 from jinfz12/feat/udb-mongodb · ucloud/ucloud-cli@d0ac3d2

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • cmd
    • products.gen.go
  • products/umongodb
    • internal/umongodb
      • api.go
      • cmd.go
      • completion.go
      • const.go
      • create_replset.go
      • create_sharded.go
      • delete.go
      • delete_replset.go
      • delete_sharded.go
      • describe.go
      • list.go
      • list_machine_specs.go
      • list_templates.go
      • list_versions.go
      • poll.go
      • restart.go
      • rows.go
      • start.go
      • status.go
      • stop.go
    • product.go
    • product.yaml
    • testdata
      • cmdtree.golden
      • completion.golden
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎cmd/products.gen.go‎

Copy file name to clipboardExpand all lines: cmd/products.gen.go
+2Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Collapse file

‎products/umongodb/internal/umongodb/api.go‎

Copy file name to clipboard
+39Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package umongodb
2+
3+
import (
4+
"fmt"
5+
"time"
6+
7+
"github.com/ucloud/ucloud-sdk-go/services/uaccount"
8+
9+
"github.com/ucloud/ucloud-cli/pkg/cli"
10+
)
11+
12+
// defaultCallTimeout is the HTTP timeout for GenericInvoke calls.
13+
// MongoDB creation APIs are slow; 5 minutes avoids premature timeouts.
14+
const defaultCallTimeout = 5 * time.Minute
15+
16+
// genericCall invokes a UMongoDB API via GenericInvoke and returns the
17+
// raw payload map. All MongoDB commands use this instead of typed SDK methods.
18+
func genericCall(ctx *cli.Context, action string, params map[string]interface{}) (map[string]interface{}, error) {
19+
client := cli.NewServiceClient(ctx, uaccount.NewClient)
20+
req := client.NewGenericRequest()
21+
if err := req.SetPayload(params); err != nil {
22+
return nil, fmt.Errorf("set payload for %s: %w", action, err)
23+
}
24+
req.WithTimeout(defaultCallTimeout)
25+
resp, err := client.GenericInvoke(req)
26+
if err != nil {
27+
return nil, err
28+
}
29+
payload := resp.GetPayload()
30+
retCode, _ := payload["RetCode"].(float64)
31+
if retCode != 0 {
32+
msg, _ := payload["Message"].(string)
33+
if msg == "" {
34+
msg = fmt.Sprintf("API %s returned RetCode %v", action, retCode)
35+
}
36+
return nil, fmt.Errorf("%s: %s", action, msg)
37+
}
38+
return payload, nil
39+
}
Collapse file

‎products/umongodb/internal/umongodb/cmd.go‎

Copy file name to clipboard
+31Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package umongodb
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
6+
"github.com/ucloud/ucloud-cli/pkg/cli"
7+
)
8+
9+
// NewCommand assembles the umongodb command tree.
10+
func NewCommand(ctx *cli.Context) *cobra.Command {
11+
cmd := &cobra.Command{
12+
Use: productName,
13+
Short: "Manipulate MongoDB on UCloud platform",
14+
Long: "Manipulate MongoDB on UCloud platform",
15+
}
16+
17+
cmd.AddCommand(newList(ctx))
18+
cmd.AddCommand(newListVersions(ctx))
19+
cmd.AddCommand(newListTemplates(ctx))
20+
cmd.AddCommand(newListMachineSpecs(ctx))
21+
cmd.AddCommand(newDescribe(ctx))
22+
cmd.AddCommand(newCreateReplset(ctx))
23+
cmd.AddCommand(newCreateSharded(ctx))
24+
cmd.AddCommand(newStart(ctx))
25+
cmd.AddCommand(newStop(ctx))
26+
cmd.AddCommand(newRestart(ctx))
27+
cmd.AddCommand(newDeleteReplset(ctx))
28+
cmd.AddCommand(newDeleteSharded(ctx))
29+
30+
return cmd
31+
}
Collapse file

‎products/umongodb/internal/umongodb/completion.go‎

Copy file name to clipboard
+289Lines changed: 289 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,289 @@
1+
package umongodb
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/ucloud/ucloud-sdk-go/services/vpc"
8+
"github.com/ucloud/ucloud-sdk-go/services/umongodb"
9+
10+
"github.com/ucloud/ucloud-cli/pkg/cli"
11+
)
12+
13+
// ---------------------------------------------------------------------------
14+
// Generic helpers
15+
// ---------------------------------------------------------------------------
16+
17+
// derefStr safely dereferences a *string bound by a flag, returning "" for nil.
18+
func derefStr(p *string) string {
19+
if p == nil {
20+
return ""
21+
}
22+
return *p
23+
}
24+
25+
// stateAllowed reports whether state passes the optional allow-list. A nil
26+
// allow-list means "any state".
27+
func stateAllowed(state string, states []string) bool {
28+
if states == nil {
29+
return true
30+
}
31+
for _, s := range states {
32+
if s == state {
33+
return true
34+
}
35+
}
36+
return false
37+
}
38+
39+
// ---------------------------------------------------------------------------
40+
// MongoDB instance ID completion (ListUMongoDBInstances) — GenericInvoke
41+
// SDK has no typed method for ListUMongoDBInstances.
42+
// ---------------------------------------------------------------------------
43+
44+
// getMongoDBIDList returns the resource-id completion candidates for the
45+
// --umongodb-id flag, in the conventional "id/name" form. states restricts
46+
// candidates to those whose State is in the set (nil = any state).
47+
func getMongoDBIDList(ctx *cli.Context, states []string, region, zone, projectID string) []string {
48+
params := map[string]interface{}{
49+
"Action": "ListUMongoDBInstances",
50+
"Region": region,
51+
}
52+
if zone != "" {
53+
params["Zone"] = zone
54+
}
55+
if projectID != "" {
56+
params["ProjectId"] = projectID
57+
}
58+
payload, err := genericCall(ctx, "ListUMongoDBInstances", params)
59+
if err != nil {
60+
return nil
61+
}
62+
dataSet, ok := payload["DataSet"].([]interface{})
63+
if !ok {
64+
return nil
65+
}
66+
candidates := make([]string, 0, len(dataSet))
67+
for _, item := range dataSet {
68+
ins, ok := item.(map[string]interface{})
69+
if !ok {
70+
continue
71+
}
72+
if states != nil {
73+
// ListUMongoDBInstances has no state filter param;
74+
// client-side filter if states provided.
75+
st, _ := ins["State"].(string)
76+
if !stateAllowed(st, states) {
77+
continue
78+
}
79+
}
80+
id, _ := ins["ClusterId"].(string)
81+
name, _ := ins["Name"].(string)
82+
candidates = append(candidates, fmt.Sprintf("%s/%s", id, name))
83+
}
84+
return candidates
85+
}
86+
87+
// ---------------------------------------------------------------------------
88+
// MongoDB version completion (ListUMongoDBVersion) — GenericInvoke
89+
// SDK typed response lacks EngineType and DefaultDBVersion fields.
90+
// ---------------------------------------------------------------------------
91+
92+
// getMongoDBVersionList returns available MongoDB version strings via
93+
// ListUMongoDBVersion.
94+
func getMongoDBVersionList(ctx *cli.Context, region, zone string) []string {
95+
params := map[string]interface{}{
96+
"Action": "ListUMongoDBVersion",
97+
"Region": region,
98+
"Zone": zone,
99+
}
100+
payload, err := genericCall(ctx, "ListUMongoDBVersion", params)
101+
if err != nil {
102+
return nil
103+
}
104+
dataSet, ok := payload["DataSet"].([]interface{})
105+
if !ok {
106+
return nil
107+
}
108+
list := make([]string, 0, len(dataSet))
109+
for _, item := range dataSet {
110+
v, ok := item.(map[string]interface{})
111+
if !ok {
112+
continue
113+
}
114+
ver, _ := v["DBVersion"].(string)
115+
if ver != "" {
116+
list = append(list, ver)
117+
}
118+
}
119+
return list
120+
}
121+
122+
// ---------------------------------------------------------------------------
123+
// Config template completion and auto-default (ListUMongoDBConfigTemplate)
124+
// Migrated to typed SDK.
125+
// ---------------------------------------------------------------------------
126+
127+
// getDefaultTemplateID fetches the default config template ID for a given
128+
// MongoDB version and cluster type via typed ListUMongoDBConfigTemplate.
129+
func getDefaultTemplateID(ctx *cli.Context, dbVersion, clusterType, project, region string) (string, error) {
130+
client := cli.NewServiceClient(ctx, umongodb.NewClient)
131+
req := client.NewListUMongoDBConfigTemplateRequest()
132+
req.Region = &region
133+
if project != "" {
134+
req.ProjectId = &project
135+
}
136+
resp, err := client.ListUMongoDBConfigTemplate(req)
137+
if err != nil {
138+
return "", err
139+
}
140+
if len(resp.DataSet) == 0 {
141+
return "", fmt.Errorf("no config template found for version %s / type %s in %s", dbVersion, clusterType, region)
142+
}
143+
for _, t := range resp.DataSet {
144+
if t.MongodbVersion == dbVersion && t.ClusterType == clusterType && t.TemplateType == "DefaultTemplate" {
145+
if t.TemplateId != "" {
146+
return t.TemplateId, nil
147+
}
148+
}
149+
}
150+
return "", fmt.Errorf("no default config template found for version %s / type %s in %s", dbVersion, clusterType, region)
151+
}
152+
153+
// getMongoDBTemplateList returns config template candidates as "id/name" strings
154+
// via typed ListUMongoDBConfigTemplate.
155+
func getMongoDBTemplateList(ctx *cli.Context, dbVersion, project, region string) []string {
156+
client := cli.NewServiceClient(ctx, umongodb.NewClient)
157+
req := client.NewListUMongoDBConfigTemplateRequest()
158+
req.Region = &region
159+
if project != "" {
160+
req.ProjectId = &project
161+
}
162+
resp, err := client.ListUMongoDBConfigTemplate(req)
163+
if err != nil {
164+
return nil
165+
}
166+
var list []string
167+
for _, t := range resp.DataSet {
168+
// Filter by version if specified
169+
if dbVersion != "" {
170+
if !strings.EqualFold(t.MongodbVersion, dbVersion) {
171+
continue
172+
}
173+
}
174+
if t.TemplateId != "" {
175+
list = append(list, fmt.Sprintf("%s/%s", t.TemplateId, t.TemplateName))
176+
}
177+
}
178+
return list
179+
}
180+
181+
// ---------------------------------------------------------------------------
182+
// Machine spec completion (ListUMongoDBMachineSpec) — GenericInvoke
183+
// SDK has no typed method for ListUMongoDBMachineSpec.
184+
// ---------------------------------------------------------------------------
185+
186+
// getMongoDBMachineSpecList returns machine type candidates from
187+
// ListUMongoDBMachineSpec, flattened from nested ComputeType arrays.
188+
func getMongoDBMachineSpecList(ctx *cli.Context, region, zone string) []string {
189+
params := map[string]interface{}{
190+
"Action": "ListUMongoDBMachineSpec",
191+
"Region": region,
192+
"Zone": zone,
193+
}
194+
payload, err := genericCall(ctx, "ListUMongoDBMachineSpec", params)
195+
if err != nil {
196+
return nil
197+
}
198+
dataSet, ok := payload["DataSet"].([]interface{})
199+
if !ok {
200+
return nil
201+
}
202+
var list []string
203+
for _, item := range dataSet {
204+
spec, ok := item.(map[string]interface{})
205+
if !ok {
206+
continue
207+
}
208+
ct, ok := spec["ComputeType"].([]interface{})
209+
if !ok {
210+
continue
211+
}
212+
for _, c := range ct {
213+
m, ok := c.(map[string]interface{})
214+
if !ok {
215+
continue
216+
}
217+
id, _ := m["MachineTypeId"].(string)
218+
desc, _ := m["Description"].(string)
219+
if id != "" {
220+
list = append(list, fmt.Sprintf("%s/%s", id, desc))
221+
}
222+
}
223+
}
224+
return list
225+
}
226+
227+
// ---------------------------------------------------------------------------
228+
// VPC / Subnet completion (copied per boundary rule, products must be self-contained)
229+
// ---------------------------------------------------------------------------
230+
231+
func getAllVPCIns(ctx *cli.Context, project, region string) ([]vpc.VPCInfo, error) {
232+
client := cli.NewServiceClient(ctx, vpc.NewClient)
233+
req := client.NewDescribeVPCRequest()
234+
req.ProjectId = &project
235+
req.Region = &region
236+
resp, err := client.DescribeVPC(req)
237+
if err != nil {
238+
return nil, err
239+
}
240+
return resp.DataSet, nil
241+
}
242+
243+
func getAllVPCIdNames(ctx *cli.Context, project, region string) []string {
244+
vpcInsList, err := getAllVPCIns(ctx, project, region)
245+
if err != nil {
246+
return nil
247+
}
248+
list := make([]string, 0, len(vpcInsList))
249+
for _, v := range vpcInsList {
250+
list = append(list, fmt.Sprintf("%s/%s", v.VPCId, v.Name))
251+
}
252+
return list
253+
}
254+
255+
func getAllSubnets(ctx *cli.Context, vpcID, project, region string) ([]vpc.SubnetInfo, error) {
256+
client := cli.NewServiceClient(ctx, vpc.NewClient)
257+
req := client.NewDescribeSubnetRequest()
258+
req.ProjectId = &project
259+
req.Region = &region
260+
if vpcID != "" {
261+
req.VPCId = &vpcID
262+
}
263+
subnets := []vpc.SubnetInfo{}
264+
for limit, offset := 50, 0; ; offset += limit {
265+
req.Limit = &limit
266+
req.Offset = &offset
267+
resp, err := client.DescribeSubnet(req)
268+
if err != nil {
269+
return nil, err
270+
}
271+
subnets = append(subnets, resp.DataSet...)
272+
if limit+offset >= resp.TotalCount {
273+
break
274+
}
275+
}
276+
return subnets, nil
277+
}
278+
279+
func getAllSubnetIDNames(ctx *cli.Context, vpcID, project, region string) []string {
280+
subnets, err := getAllSubnets(ctx, vpcID, project, region)
281+
if err != nil {
282+
return nil
283+
}
284+
list := make([]string, 0, len(subnets))
285+
for _, s := range subnets {
286+
list = append(list, fmt.Sprintf("%s/%s", s.SubnetId, s.SubnetName))
287+
}
288+
return list
289+
}
Collapse file

‎products/umongodb/internal/umongodb/const.go‎

Copy file name to clipboard
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package umongodb
2+
3+
// productName is the single source of truth for the umongodb command name
4+
// and its resource-id flag (--umongodb-id).
5+
const productName = "umongodb"
6+
7+
// resourceIDFlag is the resource-id flag, named after the product per the
8+
// onboarding contract.
9+
const resourceIDFlag = productName + "-id" // "umongodb-id"

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.