Skip to content

Commit 08af615

Browse files
authored
Merge pull request #9 from kellysweetie/master
add vpc
2 parents 39eb7d8 + bd11201 commit 08af615

4 files changed

Lines changed: 291 additions & 16 deletions

File tree

README.md

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,46 @@ $ ucloud help
7878

7979
## Example
8080

81-
Taking configure globalssh to uhost instance as an example, which will acceleare the instance SSH management efficiency (TCP 22 as default):
81+
Taking create uhost in Nigeria (region: air-nigeria) and bind a public IP as an example, then configure GlobalSSH to accelerate the SSH efficiency beyond China mainland.
82+
83+
First to create an uhost instance:
84+
85+
```
86+
$ ucloud uhost create --cpu 1 --memory 1 --password mypassword123 --image-id uimage-fya3qr
87+
88+
UHost:[uhost-tr1eau] created successfully!
89+
```
90+
91+
*Note*
92+
93+
Run follow command to get details regarding the parameters to create new uhost instance.
94+
95+
```
96+
$ ucloud uhost create --help
97+
```
98+
99+
And suggest run the command to get the image-id first.
82100

83101
```
84-
$ ucloud gssh create --location Washington --target-ip 128.14.225.161
102+
$ ucloud image list
103+
```
104+
105+
Secondly, we're going to allocate an EIP and bind to the instance created above.
106+
107+
```
108+
$ ucloud eip allocate --line International --bandwidth 1
109+
EIPId:eip-xov13b,IP:152.32.140.92,Line:International
110+
111+
$ ucloud eip bind --eip-id eip-xov13b --resource-id uhost-tr1eau
112+
EIP: [eip-xov13b] bind with uhost:[uhost-tr1eau] successfully
113+
```
114+
115+
Configure the GlobalSSH to the uhost instance and login the instance via GlobalSSH
116+
117+
```
118+
$ ucloud gssh create --location Washington --target-ip 152.32.140.92
119+
ResourceID: uga-pdhxvs
120+
121+
$ ssh root@152.32.140.92.ipssh.net
122+
root@152.32.140.92.ipssh.net's password: password of the uhost instance
85123
```

cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func NewCmdRoot() *cobra.Command {
7474
cmd.AddCommand(NewCmdGssh())
7575
cmd.AddCommand(NewCmdUImage())
7676
cmd.AddCommand(NewCmdSubnet())
77-
cmd.AddCommand(NewCmdVPC())
77+
cmd.AddCommand(NewCmdVpc())
7878
cmd.AddCommand(NewCmdFirewall())
7979
cmd.AddCommand(NewCmdDisk())
8080
return cmd

cmd/unet.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,19 +91,6 @@ func NewCmdSubnetList() *cobra.Command {
9191
return cmd
9292
}
9393

94-
//NewCmdVPC ucloud vpc
95-
func NewCmdVPC() *cobra.Command {
96-
cmd := &cobra.Command{
97-
Use: "vpc",
98-
Short: "List vpc",
99-
Long: `List vpc`,
100-
Args: cobra.NoArgs,
101-
}
102-
cmd.AddCommand(NewCmdVPCList())
103-
104-
return cmd
105-
}
106-
10794
//VPCRow 表格行
10895
type VPCRow struct {
10996
VPCName string

cmd/vpc.go

Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
1+
package cmd
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
. "github.com/ucloud/ucloud-cli/util"
6+
)
7+
8+
//NewCmdVpc ucloud vpc
9+
func NewCmdVpc() *cobra.Command {
10+
cmd := &cobra.Command{
11+
Use: "vpc",
12+
Short: "List vpc",
13+
Long: "List vpc",
14+
Args: cobra.NoArgs,
15+
}
16+
17+
cmd.AddCommand(NewCmdVpcCreate())
18+
cmd.AddCommand(NewCmdVPCList())
19+
cmd.AddCommand(NewCmdVpcDelete())
20+
cmd.AddCommand(NewCmdVpcCreatePeer())
21+
cmd.AddCommand(NewCmdVpcListPeer())
22+
cmd.AddCommand(NewCmdVpcDeletePeer())
23+
cmd.AddCommand(NewCmdSubnetCreate())
24+
return cmd
25+
}
26+
27+
/* type VPCRow struct {
28+
VPCName string
29+
ResourceID string
30+
Group string
31+
NetworkSegment string
32+
SubnetCount int
33+
CreationTime string
34+
}
35+
36+
type SubnetRow struct {
37+
SubnetName string
38+
ResourceID string
39+
Group string
40+
AffiliatedVPC string
41+
NetworkSegment string
42+
CreationTime string
43+
}
44+
*/
45+
46+
type VPCIntercomRow struct {
47+
ProjectId string
48+
Network []string
49+
DstRegion string
50+
Name string
51+
VPCId string
52+
Tag string
53+
}
54+
55+
//NewCreateVPCRequest ucloud vpc create
56+
func NewCmdVpcCreate() *cobra.Command {
57+
var segments *[]string
58+
req := BizClient.NewCreateVPCRequest()
59+
cmd := &cobra.Command{
60+
Use: "create",
61+
Short: "Create vpc network",
62+
Long: "Create vpc network",
63+
Example: "ucloud vpc create --name xxx --segment xxx",
64+
Args: cobra.NoArgs,
65+
Run: func(cmd *cobra.Command, args []string) {
66+
req.Network = *segments
67+
resp, err := BizClient.CreateVPC(req)
68+
if err != nil {
69+
HandleError(err)
70+
return
71+
}
72+
Cxt.Printf("VPC: %v created successfully!\n", resp.VPCId)
73+
},
74+
}
75+
flags := cmd.Flags()
76+
flags.SortFlags = false
77+
78+
req.Name = cmd.Flags().String("name", "", "Required. Name of the vpc network.")
79+
segments = cmd.Flags().StringSlice("segment", nil, "Required. The segment for private network.")
80+
req.Tag = cmd.Flags().String("Group", "Default", "Optional. Business group.")
81+
req.Remark = cmd.Flags().String("Remark", "Default", "Optional. The description of the vpc.")
82+
req.Region = cmd.Flags().String("region", ConfigInstance.Region, "Optional. Assign the region of the VPC")
83+
req.ProjectId = cmd.Flags().String("project-id", ConfigInstance.ProjectID, "Optional. Assign the project-id")
84+
cmd.MarkFlagRequired("name")
85+
cmd.MarkFlagRequired("segment")
86+
87+
return cmd
88+
}
89+
90+
//NewDeleteVPCRequest ucloud vpc delete
91+
func NewCmdVpcDelete() *cobra.Command {
92+
req := BizClient.NewDeleteVPCRequest()
93+
cmd := &cobra.Command{
94+
Use: "delete",
95+
Short: "Delete vpc network",
96+
Long: "Delete vpc network",
97+
Example: "ucloud vpc delete --vpc-id",
98+
Run: func(cmd *cobra.Command, args []string) {
99+
resp, err := BizClient.DeleteVPC(req)
100+
if err != nil {
101+
HandleError(err)
102+
} else {
103+
Cxt.Printf("VPC [%s] was successfully deleted\n ", resp)
104+
}
105+
},
106+
}
107+
108+
cmd.Flags().SortFlags = false
109+
110+
req.VPCId = cmd.Flags().String("vpc-id", "", "Required. The vpc network you want to delete")
111+
req.Region = cmd.Flags().String("region", ConfigInstance.Region, "Optional. Clarify the region of the vpc")
112+
req.ProjectId = cmd.Flags().String("project-id", ConfigInstance.ProjectID, "Optional. The project id of the vpc")
113+
cmd.MarkFlagRequired("vpc-id")
114+
115+
return cmd
116+
}
117+
118+
//NewCreateVPCIntercomRequest ucloud vpc peer
119+
func NewCmdVpcCreatePeer() *cobra.Command {
120+
req := BizClient.NewCreateVPCIntercomRequest()
121+
cmd := &cobra.Command{
122+
Use: "create-intercome",
123+
Short: "create intercome with other vpc",
124+
Long: "create intercome with other vpc",
125+
Example: "ucloud vpc create-intercome --vpc-id --dstvpc-id --destregion",
126+
Run: func(cmd *cobra.Command, args []string) {
127+
resp, err := BizClient.CreateVPCIntercom(req)
128+
if err != nil {
129+
HandleError(err)
130+
return
131+
}
132+
Cxt.Printf("The intercome [%s] has been establish", resp)
133+
},
134+
}
135+
136+
cmd.Flags().SortFlags = false
137+
138+
req.VPCId = cmd.Flags().String("vpc-id", "", "Required. The source vpc you want to establish the intercome")
139+
req.DstVPCId = cmd.Flags().String("dstvpc-id", "", "Required. The target vpc you want to establish the intercome")
140+
req.DstRegion = cmd.Flags().String("dstregion", "", "Required. If the intercome established across different regions")
141+
req.Region = cmd.Flags().String("region", ConfigInstance.Region, "Optioanl. The region of source vpc which will establish the intercome")
142+
req.ProjectId = cmd.Flags().String("project-id", ConfigInstance.ProjectID, "Optional. The project id of the source vpc")
143+
cmd.MarkFlagRequired("vpc-id")
144+
cmd.MarkFlagRequired("dstvpc-id")
145+
cmd.MarkFlagRequired("dstregion")
146+
147+
return cmd
148+
}
149+
150+
//NewDescribeVPCIntercomRequest
151+
func NewCmdVpcListPeer() *cobra.Command {
152+
req := BizClient.NewDescribeVPCIntercomRequest()
153+
cmd := &cobra.Command{
154+
Use: "list-intercome",
155+
Short: "list intercome ",
156+
Long: "list intercome",
157+
Example: "ucloud vpc list-intercome --vpc-id xx",
158+
Run: func(cmd *cobra.Command, args []string) {
159+
resp, err := BizClient.DescribeVPCIntercom(req)
160+
if err != nil {
161+
HandleError(err)
162+
return
163+
}
164+
if global.json {
165+
PrintJSON(resp.DataSet)
166+
} else {
167+
list := make([]VPCIntercomRow, 0)
168+
for _, VPCIntercom := range resp.DataSet {
169+
row := VPCIntercomRow{}
170+
row.ProjectId = VPCIntercom.ProjectId
171+
row.Network = VPCIntercom.Network
172+
row.DstRegion = VPCIntercom.DstRegion
173+
row.Name = VPCIntercom.Name
174+
row.VPCId = VPCIntercom.VPCId
175+
row.Tag = VPCIntercom.Tag
176+
}
177+
PrintTable(list, []string{"ProjectId", "Network", "DstRegion", "Name", "VPCId", "Tag"})
178+
}
179+
180+
},
181+
}
182+
req.VPCId = cmd.Flags().String("vpc-id", "", "Required. The vpc id which you wnat to describe the information")
183+
req.ProjectId = cmd.Flags().String("project-id", ConfigInstance.ProjectID, "Optional. The project id of source vpc")
184+
req.Region = cmd.Flags().String("region", ConfigInstance.Region, "Optional, The region of source vpc")
185+
cmd.MarkFlagRequired("vpc-id")
186+
187+
return cmd
188+
}
189+
190+
//NewDeleteVPCIntercomRequest
191+
func NewCmdVpcDeletePeer() *cobra.Command {
192+
req := BizClient.NewDeleteVPCIntercomRequest()
193+
cmd := &cobra.Command{
194+
Use: "delete-intercome",
195+
Short: "delete the vpc intercome",
196+
Long: "delete the vpc intercome",
197+
Example: "ucloud vpc delete-intercome --vpc-id xxx --dstvpc-id xxx",
198+
Run: func(cmd *cobra.Command, args []string) {
199+
resp, err := BizClient.DeleteVPCIntercom(req)
200+
if err != nil {
201+
HandleError(err)
202+
return
203+
}
204+
Cxt.Printf("The intercome [%s] was deleted successfully", resp)
205+
},
206+
}
207+
208+
cmd.Flags().SortFlags = false
209+
210+
req.VPCId = cmd.Flags().String("vpc-id", "", "Required. The source vpc id from which you want to disconnect")
211+
req.DstVPCId = cmd.Flags().String("dstvpc-id", "", "Required. The target vpc which you want to disconnect with source vpc")
212+
req.ProjectId = cmd.Flags().String("project-id", ConfigInstance.ProjectID, "Optional. The project id of source vpc")
213+
req.Region = cmd.Flags().String("region", ConfigInstance.Region, "Optional. The region of source vpc from which you want to disconnect")
214+
cmd.MarkFlagRequired("vpc-id")
215+
cmd.MarkFlagRequired("dstvpc-id")
216+
return cmd
217+
}
218+
219+
//NewCreateSubnetRequest ucloud subnet create
220+
func NewCmdSubnetCreate() *cobra.Command {
221+
req := BizClient.NewCreateSubnetRequest()
222+
cmd := &cobra.Command{
223+
Use: "create-subnet",
224+
Short: "Create subnet of vpc network",
225+
Long: "Create subnet of vpc network",
226+
Example: "ucloud subnet create --vpc-id --segment",
227+
Run: func(cmd *cobra.Command, args []string) {
228+
resp, err := BizClient.CreateSubnet(req)
229+
if err != nil {
230+
HandleError(err)
231+
return
232+
}
233+
Cxt.Printf("Subnet : %v created successfully!\n", resp.SubnetId)
234+
},
235+
}
236+
flags := cmd.Flags()
237+
flags.SortFlags = false
238+
239+
req.VPCId = cmd.Flags().String("vpc-id", "", "Required. Assign the VPC network of the subnet")
240+
req.Subnet = cmd.Flags().String("segment", "", "Required. Same as the vpc network")
241+
req.Region = cmd.Flags().String("region", ConfigInstance.Region, "Optional. The region of the subnet")
242+
req.ProjectId = cmd.Flags().String("project-id", ConfigInstance.ProjectID, "Optional. The project id of the subnet")
243+
req.Netmask = cmd.Flags().Int("netmask", 24, "Optional. The number of the IPs, default is 24")
244+
req.SubnetName = cmd.Flags().String("name", "Subnet", "Optional. The default is Subnet")
245+
req.Tag = cmd.Flags().String("Group", "Default", "Optional. Business group")
246+
cmd.MarkFlagRequired("vpc-id")
247+
cmd.MarkFlagRequired("segment")
248+
249+
return cmd
250+
}

0 commit comments

Comments
 (0)