func main() { if len(os.Args) != 2 { fmt.Printf("Restart VPC\n") fmt.Printf("Usage: cloudstack-vpc-restart <vpcname>\n") os.Exit(1) } vpcName := os.Args[1] apiurl, apikey, secret := config.CloudstackClientConfig() client := cloudstack.NewClient(apiurl, apikey, secret, true) vpcService := cloudstack.NewVPCService(client) if vpcId, vpcName, err := cloudstackutils.FindVpcId(client, vpcName); err != nil { fmt.Printf("Failed to find id for VPC \"%s\": %s\n", vpcName, err.Error()) } else { if _, err := vpcService.RestartVPC(vpcService.NewRestartVPCParams(vpcId)); err != nil { fmt.Printf("Failed to restart VPC \"%s\": %s\n", vpcName, err.Error()) } else { fmt.Printf("Restarting VPC \"%s\"... ", vpcName) } } }
func FindVpcId(client *cloudstack.CloudStackClient, vpcName string) (string, string, error) { service := cloudstack.NewVPCService(client) params := service.NewListVPCsParams() params.SetName(vpcName) if vpcs, err := service.ListVPCs(params); err != nil { return "", "", err } else if vpcs.Count == 1 { return vpcs.VPCs[0].Id, vpcs.VPCs[0].Name, nil } else { return "", "", fmt.Errorf("VPC \"%s\" does not exist, or too many VPCs matching name/filter. Found %d matches.", vpcName, vpcs.Count) } }
func findVPNAddressRange(client *cloudstack.CloudStackClient, vpcId string) (string, error) { service := cloudstack.NewVPCService(client) params := service.NewListVPCsParams() params.SetId(vpcId) if vpcs, err := service.ListVPCs(params); err != nil { return "", err } else if vpcs.Count == 1 { cidr := vpcs.VPCs[0].Cidr fmt.Printf("CIDR range for VPC %s is %s\n", vpcId, cidr) return calculateVpnCidrRange(cidr), nil } else { return "", fmt.Errorf("VPC with id %s does not exist", vpcId) } }