Left: | ||
Right: |
OLD | NEW |
---|---|
1 // Nova double testing service - internal direct API implementation | 1 // Nova double testing service - internal direct API implementation |
2 | 2 |
3 package novaservice | 3 package novaservice |
4 | 4 |
5 import ( | 5 import ( |
6 "fmt" | 6 "fmt" |
7 "launchpad.net/goose/nova" | 7 "launchpad.net/goose/nova" |
8 "strings" | 8 "strings" |
9 ) | 9 ) |
10 | 10 |
11 // Nova implements a OpenStack Nova testing service and | 11 // Nova implements a OpenStack Nova testing service and |
12 // contains the service double's internal state. | 12 // contains the service double's internal state. |
13 type Nova struct { | 13 type Nova struct { |
14 flavors map[string]nova.FlavorDetail | 14 flavors map[string]nova.FlavorDetail |
15 servers map[string]nova.ServerDetail | 15 servers map[string]nova.ServerDetail |
16 groups map[int]nova.SecurityGroup | 16 groups map[int]nova.SecurityGroup |
17 rules map[int]nova.SecurityGroupRule | 17 rules map[int]nova.SecurityGroupRule |
18 floatingIPs map[int]nova.FloatingIP | 18 floatingIPs map[int]nova.FloatingIP |
19 serverGroups map[string][]int | 19 serverGroups map[string][]int |
20 serverIPs map[string][]int | 20 serverIPs map[string][]int |
21 hostname string | 21 hostname string |
22 baseURL string | 22 baseURL string |
23 token string | 23 token string |
24 tenantId string | |
25 } | |
26 | |
27 // endpoint returns either a versioned or non-versioned service | |
28 // endpoint URL from the passed path. | |
rog
2012/12/12 15:02:33
s/passed/given/ ?
dimitern
2012/12/12 17:32:24
Done.
| |
29 func (n *Nova) endpoint(version bool, path string) string { | |
30 ep := n.hostname | |
31 if version { | |
32 ep += baseURL + "/" | |
fwereade
2012/12/12 15:18:44
maybe this var should be versionPath?
But... the
dimitern
2012/12/12 17:32:24
Done.
| |
33 } | |
34 ep += tenantId + "/" + strings.TrimLeft(path, "/") | |
35 return ep | |
24 } | 36 } |
25 | 37 |
26 // New creates an instance of the Nova object, given the parameters. | 38 // New creates an instance of the Nova object, given the parameters. |
27 func New(hostname, baseURL, token string) *Nova { | 39 func New(hostname, baseURL, token, tenantId string) *Nova { |
fwereade
2012/12/12 15:18:44
I'd rather Path than URL, I think.
dimitern
2012/12/12 17:32:24
Done.
| |
40 » if !strings.HasSuffix(hostname, "/") { | |
41 » » hostname += "/" | |
42 » } | |
28 nova := &Nova{ | 43 nova := &Nova{ |
29 flavors: make(map[string]nova.FlavorDetail), | 44 flavors: make(map[string]nova.FlavorDetail), |
30 servers: make(map[string]nova.ServerDetail), | 45 servers: make(map[string]nova.ServerDetail), |
31 groups: make(map[int]nova.SecurityGroup), | 46 groups: make(map[int]nova.SecurityGroup), |
32 rules: make(map[int]nova.SecurityGroupRule), | 47 rules: make(map[int]nova.SecurityGroupRule), |
33 floatingIPs: make(map[int]nova.FloatingIP), | 48 floatingIPs: make(map[int]nova.FloatingIP), |
34 serverGroups: make(map[string][]int), | 49 serverGroups: make(map[string][]int), |
35 serverIPs: make(map[string][]int), | 50 serverIPs: make(map[string][]int), |
36 hostname: hostname, | 51 hostname: hostname, |
37 baseURL: baseURL, | 52 baseURL: baseURL, |
38 token: token, | 53 token: token, |
54 tenantId: tenantId, | |
39 } | 55 } |
40 return nova | 56 return nova |
41 } | 57 } |
42 | 58 |
59 // links returns a populated list of links for a flavor or server. | |
60 func (n *Nova) links(path, id string) []nova.Link { | |
61 url := path + id | |
62 return []nova.Link{ | |
63 nova.Link{Href: n.endpoint(true, url), Rel: "self"}, | |
64 nova.Link{Href: n.endpoint(false, url), Rel: "bookmark"}, | |
65 } | |
66 } | |
67 | |
43 // buildFlavorLinks populates the Links field of the passed | 68 // buildFlavorLinks populates the Links field of the passed |
44 // FlavorDetail as needed by OpenStack HTTP API. Call this | 69 // FlavorDetail as needed by OpenStack HTTP API. Call this |
45 // before addFlavor(). | 70 // before addFlavor(). |
46 func (n *Nova) buildFlavorLinks(flavor *nova.FlavorDetail) { | 71 func (n *Nova) buildFlavorLinks(flavor *nova.FlavorDetail) { |
47 » ep := n.hostname | 72 » flavor.Links = n.links("/flavors/", flavor.Id) |
fwereade
2012/12/12 15:18:44
This method looks pretty redundant now.
dimitern
2012/12/12 17:32:24
Done.
| |
48 » ver := strings.TrimLeft(n.baseURL, "/") | |
49 » url := n.token + "/flavors/" + flavor.Id | |
50 » flavor.Links = []nova.Link{ | |
51 » » nova.Link{Href: ep + ver + url, Rel: "self"}, | |
52 » » nova.Link{Href: ep + url, Rel: "bookmark"}, | |
53 » } | |
54 } | 73 } |
55 | 74 |
56 // addFlavor creates a new flavor. | 75 // addFlavor creates a new flavor. |
57 func (n *Nova) addFlavor(flavor nova.FlavorDetail) error { | 76 func (n *Nova) addFlavor(flavor nova.FlavorDetail) error { |
58 if _, err := n.flavor(flavor.Id); err == nil { | 77 if _, err := n.flavor(flavor.Id); err == nil { |
59 return fmt.Errorf("a flavor with id %q already exists", flavor.I d) | 78 return fmt.Errorf("a flavor with id %q already exists", flavor.I d) |
60 } | 79 } |
61 n.flavors[flavor.Id] = flavor | 80 n.flavors[flavor.Id] = flavor |
62 return nil | 81 return nil |
63 } | 82 } |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
112 return err | 131 return err |
113 } | 132 } |
114 delete(n.flavors, flavorId) | 133 delete(n.flavors, flavorId) |
115 return nil | 134 return nil |
116 } | 135 } |
117 | 136 |
118 // buildServerLinks populates the Links field of the passed | 137 // buildServerLinks populates the Links field of the passed |
119 // ServerDetail as needed by OpenStack HTTP API. Call this | 138 // ServerDetail as needed by OpenStack HTTP API. Call this |
120 // before addServer(). | 139 // before addServer(). |
121 func (n *Nova) buildServerLinks(server *nova.ServerDetail) { | 140 func (n *Nova) buildServerLinks(server *nova.ServerDetail) { |
122 » ep := n.hostname | 141 » server.Links = n.links("/servers/", server.Id) |
fwereade
2012/12/12 15:18:44
Ditto.
dimitern
2012/12/12 17:32:24
Done.
| |
123 » ver := strings.TrimLeft(n.baseURL, "/") | |
124 » url := n.token + "/servers/" + server.Id | |
125 » server.Links = []nova.Link{ | |
126 » » nova.Link{Href: ep + ver + url, Rel: "self"}, | |
127 » » nova.Link{Href: ep + url, Rel: "bookmark"}, | |
128 » } | |
129 } | 142 } |
130 | 143 |
131 // addServer creates a new server. | 144 // addServer creates a new server. |
132 func (n *Nova) addServer(server nova.ServerDetail) error { | 145 func (n *Nova) addServer(server nova.ServerDetail) error { |
133 if _, err := n.server(server.Id); err == nil { | 146 if _, err := n.server(server.Id); err == nil { |
134 return fmt.Errorf("a server with id %q already exists", server.I d) | 147 return fmt.Errorf("a server with id %q already exists", server.I d) |
135 } | 148 } |
136 n.servers[server.Id] = server | 149 n.servers[server.Id] = server |
137 return nil | 150 return nil |
138 } | 151 } |
(...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
493 break | 506 break |
494 } | 507 } |
495 } | 508 } |
496 if idx == -1 { | 509 if idx == -1 { |
497 return fmt.Errorf("server %q does not have floating IP %d", serv erId, ipId) | 510 return fmt.Errorf("server %q does not have floating IP %d", serv erId, ipId) |
498 } | 511 } |
499 fips = append(fips[:idx], fips[idx+1:]...) | 512 fips = append(fips[:idx], fips[idx+1:]...) |
500 n.serverIPs[serverId] = fips | 513 n.serverIPs[serverId] = fips |
501 return nil | 514 return nil |
502 } | 515 } |
OLD | NEW |