Merge lp:~jtv/gwacl/random-test-ids into lp:gwacl

Proposed by Jeroen T. Vermeulen
Status: Merged
Approved by: Jeroen T. Vermeulen
Approved revision: 53
Merged at revision: 52
Proposed branch: lp:~jtv/gwacl/random-test-ids
Merge into: lp:gwacl
Diff against target: 110 lines (+57/-10)
1 file modified
example/live_example_managementapi.go (+57/-10)
To merge this branch: bzr merge lp:~jtv/gwacl/random-test-ids
Reviewer Review Type Date Requested Status
Gavin Panella Approve
Review via email: mp+154860@code.launchpad.net

Commit message

Randomize test ids in the live example.

Description of the change

We were having trouble running the live example because at least one of the identifiers we provide (hosted-service name) must be globally unique, within all of Azure.

In this branch I randomize those examples. I also add checking for required parameters, so that you get an immediate helpful error rather than an obscure failure later on. Also, I replaced the hard-coded storage-account name with one you provide in a command-line option.

With these changes, I was able to create a virtual machine given only a management certificate, a subscription id, and a storage account.

In a later branch we'll make the live example create the storage account for you.

Jeroen

To post a comment you must log in.
lp:~jtv/gwacl/random-test-ids updated
53. By Jeroen T. Vermeulen

Merge trunk, resolve import conflict.

Revision history for this message
Gavin Panella (allenap) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'example/live_example_managementapi.go'
--- example/live_example_managementapi.go 2013-03-22 02:26:53 +0000
+++ example/live_example_managementapi.go 2013-03-22 05:37:20 +0000
@@ -13,18 +13,34 @@
13 "flag"13 "flag"
14 "fmt"14 "fmt"
15 "launchpad.net/gwacl"15 "launchpad.net/gwacl"
16 "math/rand"
17 "os"
16 "time"18 "time"
17)19)
1820
19var certFile string21var certFile string
22var storageAccount string
20var subscriptionID string23var subscriptionID string
21var verbose bool24var verbose bool
2225
23func getParams() {26func getParams() error {
24 flag.StringVar(&certFile, "cert", "my-azure.pem", "Name of your management certificate file (in PEM format)")27 flag.StringVar(&certFile, "cert", "", "Name of your management certificate file (in PEM format).")
25 flag.StringVar(&subscriptionID, "subscriptionid", "", "Your Azure subscription ID")28 flag.StringVar(&storageAccount, "storageaccount", "", "Name of a storage account where the program can create a test disk.")
29 flag.StringVar(&subscriptionID, "subscriptionid", "", "Your Azure subscription ID.")
26 flag.BoolVar(&verbose, "verbose", false, "Print debugging output")30 flag.BoolVar(&verbose, "verbose", false, "Print debugging output")
31
27 flag.Parse()32 flag.Parse()
33
34 if certFile == "" {
35 return fmt.Errorf("No .pem certificate specified. Use the -cert option.")
36 }
37 if storageAccount == "" {
38 return fmt.Errorf("No storage account specified. Use the -storageaccount option.")
39 }
40 if subscriptionID == "" {
41 return fmt.Errorf("No subscription ID specified. Use the -subscriptionid option.")
42 }
43 return nil
28}44}
2945
30func checkError(err error) {46func checkError(err error) {
@@ -33,8 +49,33 @@
33 }49 }
34}50}
3551
52// makeRandomIdentifier creates an arbitrary identifier of the given length,
53// consisting of only ASCII digits and lower-case ASCII letters.
54// The identifier will start with the given prefix. The prefix must be no
55// longer than the specified length, or there'll be trouble.
56func makeRandomIdentifier(prefix string, length int) string {
57 // Only digits and lower-case ASCII letters are accepted.
58 const chars = "abcdefghijklmnopqrstuvwxyz0123456789"
59
60 if len(prefix) > length {
61 panic(fmt.Errorf("prefix '%s' is more than the requested %d characters long", prefix, length))
62 }
63
64 id := prefix
65 for len(id) < length {
66 id += string(chars[rand.Intn(len(chars))])
67 }
68 return id
69}
70
36func main() {71func main() {
37 getParams()72 rand.Seed(int64(time.Now().Nanosecond()))
73
74 err := getParams()
75 if err != nil {
76 fmt.Println(err)
77 os.Exit(1)
78 }
3879
39 api, err := gwacl.NewManagementAPI(subscriptionID, certFile)80 api, err := gwacl.NewManagementAPI(subscriptionID, certFile)
40 gwacl.SetVerbose(verbose)81 gwacl.SetVerbose(verbose)
@@ -49,8 +90,9 @@
49}90}
5091
51func ExerciseHostedServicesAPI(api *gwacl.ManagementAPI) {92func ExerciseHostedServicesAPI(api *gwacl.ManagementAPI) {
52 hostServiceName := "name-of-the-test-hosted-service"93 // A hosted-service name must be between 3 and 24 characters long.
53 fmt.Println("Creating a hosted service...")94 hostServiceName := makeRandomIdentifier("gwacl", 24)
95 fmt.Printf("Creating a hosted service: '%s'...\n", hostServiceName)
54 createHostedService := gwacl.NewCreateHostedServiceWithLocation(hostServiceName, "testLabel", "East US")96 createHostedService := gwacl.NewCreateHostedServiceWithLocation(hostServiceName, "testLabel", "East US")
55 err := api.AddHostedService(createHostedService)97 err := api.AddHostedService(createHostedService)
56 checkError(err)98 checkError(err)
@@ -69,13 +111,18 @@
69 fmt.Println("Done listing hosted services\n")111 fmt.Println("Done listing hosted services\n")
70112
71 fmt.Println("Adding VM deployment...")113 fmt.Println("Adding VM deployment...")
72 configurationSet := gwacl.NewLinuxProvisioningConfiguration("testHostname12345", "test", "test123#@!", false)114 hostname := makeRandomIdentifier("gwaclhost", 64)
115 password := makeRandomIdentifier("pwd-", 16)
116 vhdName := makeRandomIdentifier("gwacldisk", 16)
117 configurationSet := gwacl.NewLinuxProvisioningConfiguration(hostname, "test", password, false)
73 // TODO: create Storage account / generate mediaLink118 // TODO: create Storage account / generate mediaLink
74 mediaLink := "http://portalvhdstjqr9wshnh85p.blob.core.windows.net/vhds/testmachinesadsadsad"119 mediaLink := fmt.Sprintf("http://%s.blob.core.windows.net/vhds/%s.vhd", storageAccount, vhdName)
75 sourceImageName := "b39f27a8b8c64d52b05eac6a62ebad85__Ubuntu-12_04_1-LTS-amd64-server-20121218-en-us-30GB"120 sourceImageName := "b39f27a8b8c64d52b05eac6a62ebad85__Ubuntu-12_04_1-LTS-amd64-server-20121218-en-us-30GB"
76 OSVirtualHardDisk := gwacl.NewOSVirtualHardDisk("", "", "", mediaLink, sourceImageName)121 OSVirtualHardDisk := gwacl.NewOSVirtualHardDisk("", "", "", mediaLink, sourceImageName)
77 role := gwacl.NewRole("ExtraSmall", "test-role-123", []gwacl.LinuxProvisioningConfiguration{*configurationSet}, OSVirtualHardDisk)122 roleName := makeRandomIdentifier("gwaclrole", 16)
78 deployment := gwacl.NewDeploymentForCreateVMDeployment("test-machine-name", "Staging", "testLabel", []gwacl.Role{*role}, "")123 role := gwacl.NewRole("ExtraSmall", roleName, []gwacl.LinuxProvisioningConfiguration{*configurationSet}, OSVirtualHardDisk)
124 machineName := makeRandomIdentifier("gwaclmachine", 20)
125 deployment := gwacl.NewDeploymentForCreateVMDeployment(machineName, "Staging", machineName, []gwacl.Role{*role}, "")
79 api.AddDeployment(deployment, hostServiceName)126 api.AddDeployment(deployment, hostServiceName)
80 fmt.Println("Done adding VM deployment\n")127 fmt.Println("Done adding VM deployment\n")
81128

Subscribers

People subscribed via source and target branches