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

Subscribers

People subscribed via source and target branches