func execAddSecurityGroupRule(cmd *cobra.Command, args []string, client *anchnet.Client, out io.Writer) {
	if len(args) != 2 {
		fmt.Fprintln(os.Stderr, "Rule name and security group ID required")
		os.Exit(1)
	}

	priority := getFlagInt(cmd, "priority")
	direction := getFlagInt(cmd, "direction")
	action := getFlagString(cmd, "action")
	protocol := getFlagString(cmd, "protocol")
	value1 := getFlagString(cmd, "value1")
	value2 := getFlagString(cmd, "value2")
	value3 := getFlagString(cmd, "value3")

	request := anchnet.AddSecurityGroupRulesRequest{
		SecurityGroupID: args[1],
		SecurityGroupRules: []anchnet.AddSecurityGroupRule{
			{
				SecurityGroupRuleName: args[0],
				Action:                anchnet.SecurityGroupRuleAction(action),
				Direction:             anchnet.SecurityGroupRuleDirection(direction),
				Protocol:              anchnet.SecurityGroupRuleProtocol(protocol),
				Priority:              priority,
				Value1:                value1,
				Value2:                value2,
				Value3:                value3,
			},
		},
	}
	var response anchnet.AddSecurityGroupRulesResponse
	sendResult(&response, out, "AddSecurityGroup", response.Code, client.SendRequest(request, &response))
}
func execCreateUserProject(cmd *cobra.Command, args []string, client *anchnet.Client, out io.Writer) {
	if len(args) == 0 {
		fmt.Fprintln(os.Stderr, "Login ID required, i.e. username")
		os.Exit(1)
	}

	sex := getFlagString(cmd, "sex")
	mobile := getFlagString(cmd, "mobile")
	passwd := getFlagString(cmd, "passwd")

	// use {userid}@caicloud.io as loginid which is supposed to be unique.
	loginID := args[0] + "@caicloud.io"

	request := anchnet.CreateUserProjectRequest{
		LoginID:     loginID,
		Sex:         sex,
		ProjectName: args[0],
		Email:       loginID,
		ContactName: args[0],
		Mobile:      mobile,
		LoginPasswd: passwd,
	}
	var response anchnet.CreateUserProjectResponse
	sendResult(&response, out, "CreateUserProject", response.Code, client.SendRequest(request, &response))
}
Esempio n. 3
0
func execWaitJob(cmd *cobra.Command, args []string, client *anchnet.Client, out io.Writer) {
	if len(args) == 0 {
		fmt.Fprintln(os.Stderr, "Job id required")
		os.Exit(1)
	}

	count := getFlagInt(cmd, "count")
	status := getFlagString(cmd, "status")
	interval := getFlagInt(cmd, "interval")
	exitOnFail := getFlagBool(cmd, "exit_on_fail")

	for i := 0; i < count; i++ {
		request := anchnet.DescribeJobsRequest{
			JobIDs: []string{args[0]},
		}
		var response anchnet.DescribeJobsResponse
		err := client.SendRequest(request, &response)
		if err == nil && len(response.ItemSet) == 1 {
			// Return if there is no error and status matches.
			if string(response.ItemSet[0].Status) == status {
				return
			}
			// Return if there is no error, and status is failed + user wants early return.
			if response.ItemSet[0].Status == anchnet.JobStatusFailed && exitOnFail {
				fmt.Fprintf(os.Stderr, "Job %v failed", args[0])
				os.Exit(1)
			}
		}
		time.Sleep(time.Duration(interval) * time.Second)
	}
	fmt.Fprintf(os.Stderr, "Time out waiting for job %v", args[0])
	os.Exit(1)
}
Esempio n. 4
0
func execCreateLoadBalancer(cmd *cobra.Command, args []string, client *anchnet.Client, out io.Writer) {
	if len(args) != 2 {
		fmt.Fprintln(os.Stderr, "Load balancer name and public ips required")
		os.Exit(1)
	}

	lb_type := getFlagInt(cmd, "type")

	refs := strings.Split(args[1], ",")
	ips := make([]anchnet.CreateLoadBalancerIP, len(refs))
	for i, ip := range refs {
		ips[i].RefID = ip
	}

	request := anchnet.CreateLoadBalancerRequest{
		Product: anchnet.CreateLoadBalancerProduct{
			Loadbalancer: anchnet.CreateLoadBalancerLB{
				Name: args[0],
				Type: anchnet.LoadBalancerType(lb_type),
			},
			Eips: ips,
		},
	}
	var response anchnet.CreateLoadBalancerResponse
	sendResult(&response, out, "CreateLoadBalancer", response.Code, client.SendRequest(request, &response))
}
func execDeleteSecurityGroups(cmd *cobra.Command, args []string, client *anchnet.Client, out io.Writer) {
	if len(args) != 1 {
		fmt.Fprintln(os.Stderr, "Security group IDs required")
		os.Exit(1)
	}

	request := anchnet.DeleteSecurityGroupsRequest{
		SecurityGroupIDs: strings.Split(args[0], ","),
	}
	var response anchnet.DeleteSecurityGroupsResponse
	sendResult(&response, out, "DeleteSecurityGroups", response.Code, client.SendRequest(request, &response))
}
Esempio n. 6
0
func execTerminateInstances(cmd *cobra.Command, args []string, client *anchnet.Client, out io.Writer) {
	if len(args) == 0 {
		fmt.Fprintln(os.Stderr, "Instance IDs required")
		os.Exit(1)
	}

	request := anchnet.TerminateInstancesRequest{
		InstanceIDs: strings.Split(args[0], ","),
	}
	var response anchnet.TerminateInstancesResponse
	sendResult(&response, out, "TerminateInstance", response.Code, client.SendRequest(request, &response))
}
Esempio n. 7
0
func execDescribeVxnets(cmd *cobra.Command, args []string, client *anchnet.Client, out io.Writer) {
	if len(args) == 0 {
		fmt.Fprintln(os.Stderr, "Vxnet IDs required")
		os.Exit(1)
	}

	request := anchnet.DescribeVxnetsRequest{
		VxnetIDs: strings.Split(args[0], ","),
	}
	var response anchnet.DescribeVxnetsResponse
	sendResult(&response, out, "DescribeVxnet", response.Code, client.SendRequest(request, &response))
}
Esempio n. 8
0
func execReleaseEips(cmd *cobra.Command, args []string, client *anchnet.Client, out io.Writer) {
	if len(args) == 0 {
		fmt.Fprintln(os.Stderr, "EIP IDs required")
		os.Exit(1)
	}

	request := anchnet.ReleaseEipsRequest{
		EipIDs: strings.Split(args[0], ","),
	}
	var response anchnet.ReleaseEipsResponse
	sendResult(&response, out, "ReleaseEips", response.Code, client.SendRequest(request, &response))
}
Esempio n. 9
0
func execDescribeJob(cmd *cobra.Command, args []string, client *anchnet.Client, out io.Writer) {
	if len(args) == 0 {
		fmt.Fprintln(os.Stderr, "Job id required")
		os.Exit(1)
	}

	request := anchnet.DescribeJobsRequest{
		JobIDs: []string{args[0]},
	}
	var response anchnet.DescribeJobsResponse
	sendResult(&response, out, "DescribeJob", response.Code, client.SendRequest(request, &response))
}
func execApplySecurityGroup(cmd *cobra.Command, args []string, client *anchnet.Client, out io.Writer) {
	if len(args) != 2 {
		fmt.Fprintln(os.Stderr, "Security group id and instance ids required")
		os.Exit(1)
	}

	request := anchnet.ApplySecurityGroupRequest{
		SecurityGroupID: args[0],
		InstanceIDs:     strings.Split(args[1], ","),
	}
	var response anchnet.ApplySecurityGroupResponse
	sendResult(&response, out, "ApplySecurityGroup", response.Code, client.SendRequest(request, &response))
}
Esempio n. 11
0
func execJoinVxnet(cmd *cobra.Command, args []string, client *anchnet.Client, out io.Writer) {
	if len(args) != 2 {
		fmt.Fprintln(os.Stderr, "Vxnet and instances IDs required")
		os.Exit(1)
	}

	request := anchnet.JoinVxnetRequest{
		VxnetID:     args[0],
		InstanceIDs: strings.Split(args[1], ","),
	}
	var response anchnet.JoinVxnetResponse
	sendResult(&response, out, "JobVxnet", response.Code, client.SendRequest(request, &response))
}
Esempio n. 12
0
func execRevokeImageFromUsers(cmd *cobra.Command, args []string, client *anchnet.Client, out io.Writer) {
	if len(args) != 2 {
		fmt.Fprintln(os.Stderr, "Image id and user ids required")
		os.Exit(1)
	}

	request := anchnet.RevokeImageFromUsersRequest{
		ImageIDs: []string{args[0]},
		UserIDs:  strings.Split(args[1], ","),
	}
	var response anchnet.RevokeImageFromUsersResponse
	sendResult(&response, out, "RevokeImageToUsers", response.Code, client.SendRequest(request, &response))
}
Esempio n. 13
0
func execCaptureInstance(cmd *cobra.Command, args []string, client *anchnet.Client, out io.Writer) {
	if len(args) != 2 {
		fmt.Fprintln(os.Stderr, "Image name and instance id required")
		os.Exit(1)
	}

	request := anchnet.CaptureInstanceRequest{
		ImageName: args[0],
		Instance:  args[1],
	}
	var response anchnet.CaptureInstanceResponse
	sendResult(&response, out, "CaptureInstance", response.Code, client.SendRequest(request, &response))
}
Esempio n. 14
0
func execDescribeProjects(cmd *cobra.Command, args []string, client *anchnet.Client, out io.Writer) {
	if len(args) == 0 {
		fmt.Fprintln(os.Stderr, "projectid required, e.g. pro-xxxxx")
		os.Exit(1)
	}

	request := anchnet.DescribeProjectsRequest{
		Projects: args[0],
	}

	var response anchnet.DescribeProjectsResponse
	sendResult(&response, out, "DescribeProjects", response.Code, client.SendRequest(request, &response))
}
func execSearchSecurityGroup(cmd *cobra.Command, args []string, client *anchnet.Client, out io.Writer) {
	if len(args) != 1 {
		fmt.Fprintln(os.Stderr, "Security group name required")
		os.Exit(1)
	}

	request := anchnet.DescribeSecurityGroupsRequest{
		SearchWord: args[0],
		Verbose:    1,
	}
	var response anchnet.DescribeSecurityGroupsResponse
	sendResult(&response, out, "SearchSecurityGroup", response.Code, client.SendRequest(request, &response))
}
Esempio n. 16
0
func execCreateVxnet(cmd *cobra.Command, args []string, client *anchnet.Client, out io.Writer) {
	if len(args) == 0 {
		fmt.Fprintln(os.Stderr, "Vxnet name required")
		os.Exit(1)
	}

	request := anchnet.CreateVxnetsRequest{
		VxnetName: args[0],
		VxnetType: anchnet.VxnetTypePriv,
		Count:     1,
	}
	var response anchnet.CreateVxnetsResponse
	sendResult(&response, out, "CreateVxnet", response.Code, client.SendRequest(request, &response))
}
Esempio n. 17
0
func execSearchUserProject(cmd *cobra.Command, args []string, client *anchnet.Client, out io.Writer) {
	if len(args) == 0 {
		fmt.Fprintln(os.Stderr, "Login ID required, i.e. username")
		os.Exit(1)
	}

	loginID := args[0] + "@caicloud.io"

	request := anchnet.DescribeProjectsRequest{
		SearchWord: loginID,
	}

	var response anchnet.DescribeProjectsResponse
	sendResult(&response, out, "SearchUserProject", response.Code, client.SendRequest(request, &response))
}
Esempio n. 18
0
func execDeleteLoadBalancer(cmd *cobra.Command, args []string, client *anchnet.Client, out io.Writer) {
	if len(args) != 2 {
		fmt.Fprintln(os.Stderr, "Load balancer ids and public ips required")
		os.Exit(1)
	}

	lbs := strings.Split(args[0], ",")
	ips := strings.Split(args[1], ",")

	request := anchnet.DeleteLoadBalancersRequest{
		LoadbalancerIDs: lbs,
		EipIDs:          ips,
	}
	var response anchnet.DeleteLoadBalancersResponse
	sendResult(&response, out, "DeleteLoadBalancer", response.Code, client.SendRequest(request, &response))
}
Esempio n. 19
0
func execSearchLoadBalancer(cmd *cobra.Command, args []string, client *anchnet.Client, out io.Writer) {
	if len(args) == 0 {
		fmt.Fprintln(os.Stderr, "LoadBalancer name required")
		os.Exit(1)
	}

	var lb_status []anchnet.LoadBalancerStatus
	for _, status := range strings.Split(getFlagString(cmd, "status"), ",") {
		lb_status = append(lb_status, anchnet.LoadBalancerStatus(status))
	}

	request := anchnet.DescribeLoadBalancersRequest{
		SearchWord: args[0],
		Status:     lb_status,
		Verbose:    1,
	}
	var response anchnet.DescribeLoadBalancersResponse
	sendResult(&response, out, "SearchLoadBalancer", response.Code, client.SendRequest(request, &response))
}
func execCreateSecurityGroup(cmd *cobra.Command, args []string, client *anchnet.Client, out io.Writer) {
	if len(args) != 1 {
		fmt.Fprintln(os.Stderr, "Security group name required")
		os.Exit(1)
	}

	// Assume number matches.
	rulename := strings.Split(getFlagString(cmd, "rulename"), ",")
	priority := strings.Split(getFlagString(cmd, "priority"), ",")
	direction := strings.Split(getFlagString(cmd, "direction"), ",")
	action := strings.Split(getFlagString(cmd, "action"), ",")
	protocol := strings.Split(getFlagString(cmd, "protocol"), ",")
	value1 := strings.Split(getFlagString(cmd, "value1"), ",")
	value2 := strings.Split(getFlagString(cmd, "value2"), ",")

	var rules []anchnet.CreateSecurityGroupRule
	for i := range rulename {
		d, _ := strconv.Atoi(direction[i])
		p, _ := strconv.Atoi(priority[i])
		rule := anchnet.CreateSecurityGroupRule{
			SecurityGroupRuleName: rulename[i],
			Action:                anchnet.SecurityGroupRuleAction(action[i]),
			Direction:             anchnet.SecurityGroupRuleDirection(d),
			Protocol:              anchnet.SecurityGroupRuleProtocol(protocol[i]),
			Priority:              p,
			Value1:                value1[i],
			Value2:                value2[i],
		}
		rules = append(rules, rule)
	}

	request := anchnet.CreateSecurityGroupRequest{
		SecurityGroupName:  args[0],
		SecurityGroupRules: rules,
	}
	var response anchnet.CreateSecurityGroupResponse
	sendResult(&response, out, "CreateSecurityGroup", response.Code, client.SendRequest(request, &response))
}
Esempio n. 21
0
func execRunInstance(cmd *cobra.Command, args []string, client *anchnet.Client, out io.Writer) {
	if len(args) == 0 {
		fmt.Fprintln(os.Stderr, "Instance name required")
		os.Exit(1)
	}

	cpu := getFlagInt(cmd, "cpu")
	amount := getFlagInt(cmd, "amount")
	memory := getFlagInt(cmd, "memory")
	passwd := getFlagString(cmd, "passwd")
	bandwidth := getFlagInt(cmd, "bandwidth")
	image_id := getFlagString(cmd, "image-id")
	ip_group := getFlagString(cmd, "ip-group")

	request := anchnet.RunInstancesRequest{
		Product: anchnet.RunInstancesProduct{
			Cloud: anchnet.RunInstancesCloud{
				VM: anchnet.RunInstancesVM{
					Name:      args[0],
					LoginMode: anchnet.LoginModePwd,
					Mem:       memory,
					Cpu:       cpu,
					Password:  passwd,
					ImageID:   image_id,
				},
				Net0: true, // Create public network
				IP: anchnet.RunInstancesIP{
					IPGroup:   anchnet.IPGroupType(ip_group),
					Bandwidth: bandwidth,
				},
				Amount: amount,
			},
		},
	}
	var response anchnet.RunInstancesResponse
	sendResult(&response, out, "RunInstance", response.Code, client.SendRequest(request, &response))
}
Esempio n. 22
0
func execTransfer(cmd *cobra.Command, args []string, client *anchnet.Client, out io.Writer) {
	if len(args) != 2 {
		fmt.Fprintln(os.Stderr, "userid and value required")
		os.Exit(1)
	}

	why := getFlagString(cmd, "why")

	// userID is a integer value.
	userID, err := strconv.Atoi(args[0])
	if err != nil {
		fmt.Fprintln(os.Stderr, "unable to convert userID to int: %v", err)
		os.Exit(1)
	}

	request := anchnet.TransferRequest{
		UserID: userID,
		Value:  args[1],
		Why:    why,
	}

	var response anchnet.TransferResponse
	sendResult(&response, out, "Transfer", response.Code, client.SendRequest(request, &response))
}
Esempio n. 23
0
func execGetChargeSummary(cmd *cobra.Command, args []string, client *anchnet.Client, out io.Writer) {
	request := anchnet.GetChargeSummaryRequest{}

	var response anchnet.GetChargeSummaryResponse
	sendResult(&response, out, "GetChargeSummary", response.Code, client.SendRequest(request, &response))
}