|
| 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 = ®ion |
| 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 = ®ion |
| 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 = ®ion |
| 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 = ®ion |
| 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 | +} |