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 » versionPath 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 given path. |
| 29 func (n *Nova) endpoint(version bool, path string) string { |
| 30 ep := n.hostname |
| 31 if version { |
| 32 ep += n.versionPath + "/" |
| 33 } |
| 34 ep += n.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, versionPath, token, tenantId string) *Nova { |
| 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 » » versionPath: versionPath, |
38 token: token, | 53 token: token, |
| 54 tenantId: tenantId, |
39 } | 55 } |
40 return nova | 56 return nova |
41 } | 57 } |
42 | 58 |
43 // buildFlavorLinks populates the Links field of the passed | 59 // buildFlavorLinks populates the Links field of the passed |
44 // FlavorDetail as needed by OpenStack HTTP API. Call this | 60 // FlavorDetail as needed by OpenStack HTTP API. Call this |
45 // before addFlavor(). | 61 // before addFlavor(). |
46 func (n *Nova) buildFlavorLinks(flavor *nova.FlavorDetail) { | 62 func (n *Nova) buildFlavorLinks(flavor *nova.FlavorDetail) { |
47 » ep := n.hostname | 63 » url := "/flavors/" + flavor.Id |
48 » ver := strings.TrimLeft(n.baseURL, "/") | |
49 » url := n.token + "/flavors/" + flavor.Id | |
50 flavor.Links = []nova.Link{ | 64 flavor.Links = []nova.Link{ |
51 » » nova.Link{Href: ep + ver + url, Rel: "self"}, | 65 » » nova.Link{Href: n.endpoint(true, url), Rel: "self"}, |
52 » » nova.Link{Href: ep + url, Rel: "bookmark"}, | 66 » » nova.Link{Href: n.endpoint(false, url), Rel: "bookmark"}, |
53 } | 67 } |
54 } | 68 } |
55 | 69 |
56 // addFlavor creates a new flavor. | 70 // addFlavor creates a new flavor. |
57 func (n *Nova) addFlavor(flavor nova.FlavorDetail) error { | 71 func (n *Nova) addFlavor(flavor nova.FlavorDetail) error { |
58 if _, err := n.flavor(flavor.Id); err == nil { | 72 if _, err := n.flavor(flavor.Id); err == nil { |
59 return fmt.Errorf("a flavor with id %q already exists", flavor.I
d) | 73 return fmt.Errorf("a flavor with id %q already exists", flavor.I
d) |
60 } | 74 } |
61 n.flavors[flavor.Id] = flavor | 75 n.flavors[flavor.Id] = flavor |
62 return nil | 76 return nil |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 return err | 126 return err |
113 } | 127 } |
114 delete(n.flavors, flavorId) | 128 delete(n.flavors, flavorId) |
115 return nil | 129 return nil |
116 } | 130 } |
117 | 131 |
118 // buildServerLinks populates the Links field of the passed | 132 // buildServerLinks populates the Links field of the passed |
119 // ServerDetail as needed by OpenStack HTTP API. Call this | 133 // ServerDetail as needed by OpenStack HTTP API. Call this |
120 // before addServer(). | 134 // before addServer(). |
121 func (n *Nova) buildServerLinks(server *nova.ServerDetail) { | 135 func (n *Nova) buildServerLinks(server *nova.ServerDetail) { |
122 » ep := n.hostname | 136 » url := "/servers/" + server.Id |
123 » ver := strings.TrimLeft(n.baseURL, "/") | |
124 » url := n.token + "/servers/" + server.Id | |
125 server.Links = []nova.Link{ | 137 server.Links = []nova.Link{ |
126 » » nova.Link{Href: ep + ver + url, Rel: "self"}, | 138 » » nova.Link{Href: n.endpoint(true, url), Rel: "self"}, |
127 » » nova.Link{Href: ep + url, Rel: "bookmark"}, | 139 » » nova.Link{Href: n.endpoint(false, url), Rel: "bookmark"}, |
128 } | 140 } |
129 } | 141 } |
130 | 142 |
131 // addServer creates a new server. | 143 // addServer creates a new server. |
132 func (n *Nova) addServer(server nova.ServerDetail) error { | 144 func (n *Nova) addServer(server nova.ServerDetail) error { |
133 if _, err := n.server(server.Id); err == nil { | 145 if _, err := n.server(server.Id); err == nil { |
134 return fmt.Errorf("a server with id %q already exists", server.I
d) | 146 return fmt.Errorf("a server with id %q already exists", server.I
d) |
135 } | 147 } |
136 n.servers[server.Id] = server | 148 n.servers[server.Id] = server |
137 return nil | 149 return nil |
(...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
493 break | 505 break |
494 } | 506 } |
495 } | 507 } |
496 if idx == -1 { | 508 if idx == -1 { |
497 return fmt.Errorf("server %q does not have floating IP %d", serv
erId, ipId) | 509 return fmt.Errorf("server %q does not have floating IP %d", serv
erId, ipId) |
498 } | 510 } |
499 fips = append(fips[:idx], fips[idx+1:]...) | 511 fips = append(fips[:idx], fips[idx+1:]...) |
500 n.serverIPs[serverId] = fips | 512 n.serverIPs[serverId] = fips |
501 return nil | 513 return nil |
502 } | 514 } |
OLD | NEW |